-
Notifications
You must be signed in to change notification settings - Fork 2
/
actions.go
237 lines (208 loc) · 7.43 KB
/
actions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2016 NDP Systèmes. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package actions
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"encoding/xml"
"fmt"
"sync"
"github.com/beevik/etree"
"github.com/labneco/doxa/doxa/models/types"
"github.com/labneco/doxa/doxa/tools/xmlutils"
"github.com/labneco/doxa/doxa/views"
)
// An ActionType defines the type of action
type ActionType string
// Action types
const (
ActionActWindow ActionType = "ir.actions.act_window"
ActionServer ActionType = "ir.actions.server"
ActionClient ActionType = "ir.actions.client"
ActionCloseWindow ActionType = "ir.actions.act_window_close"
)
// ActionViewType defines the type of view of an action
type ActionViewType string
// Action view types
const (
ActionViewTypeForm ActionViewType = "form"
ActionViewTypeTree ActionViewType = "tree"
)
// Registry is the action collection of the application
var Registry *Collection
// MakeActionRef creates an ActionRef from an action id
func MakeActionRef(id string) ActionRef {
action := Registry.GetById(id)
if action == nil {
return ActionRef{}
}
return ActionRef{id, action.Name}
}
// ActionRef is an array of two strings representing an action:
// - The first one is the ID of the action
// - The second one is the name of the action
type ActionRef [2]string
// MarshalJSON is the JSON marshalling method of ActionRef
// It marshals empty ActionRef into null instead of ["", ""].
func (ar ActionRef) MarshalJSON() ([]byte, error) {
if ar[0] == "" {
return json.Marshal(nil)
}
return json.Marshal([2]string{ar[0], ar[1]})
}
// Value extracts ID of our ActionRef for storing in the database.
func (ar ActionRef) Value() (driver.Value, error) {
return driver.Value(ar[0]), nil
}
// Scan fetches the name of our action from the ID
// stored in the database to fill the ActionRef.
func (ar *ActionRef) Scan(src interface{}) error {
switch s := src.(type) {
case string:
*ar = MakeActionRef(s)
case []byte:
*ar = MakeActionRef(string(s))
default:
return fmt.Errorf("Invalid type for ActionRef: %T", src)
}
return nil
}
// ID returns the ID of the current action reference
func (ar ActionRef) ID() string {
return ar[0]
}
// Name returns the name of the current action reference
func (ar ActionRef) Name() string {
return ar[1]
}
// IsNull returns true if this ActionRef references no action
func (ar ActionRef) IsNull() bool {
return ar[0] == "" && ar[1] == ""
}
var _ driver.Valuer = ActionRef{}
var _ sql.Scanner = &ActionRef{}
var _ json.Marshaler = &ActionRef{}
// An Collection is a collection of actions
type Collection struct {
sync.RWMutex
actions map[string]*Action
links map[string][]*Action
}
// NewCollection returns a pointer to a new
// Collection instance
func NewCollection() *Collection {
res := Collection{
actions: make(map[string]*Action),
links: make(map[string][]*Action),
}
return &res
}
// Add adds the given action to our Collection
func (ar *Collection) Add(a *Action) {
ar.Lock()
defer ar.Unlock()
ar.actions[a.ID] = a
ar.links[a.SrcModel] = append(ar.links[a.SrcModel], a)
}
// GetById returns the Action with the given id
func (ar *Collection) GetById(id string) *Action {
return ar.actions[id]
}
// GetAll returns a list of all actions of this Collection.
// Actions are returned in an arbitrary order
func (ar *Collection) GetAll() []*Action {
res := make([]*Action, len(ar.actions))
var i int
for _, action := range ar.actions {
res[i] = action
i++
}
return res
}
// MustGetById returns the Action with the given id
// It panics if the id is not found in the action registry
func (ar *Collection) MustGetById(id string) *Action {
action, ok := ar.actions[id]
if !ok {
log.Panic("Action does not exist", "action_id", id)
}
return action
}
// GetActionLinksForModel returns the list of linked actions
// for the model with the given name
func (ar *Collection) GetActionLinksForModel(modelName string) []*Action {
return ar.links[modelName]
}
// LoadFromEtree reads the action given etree.Element, creates or updates the action
// and adds it to the given Collection if it not already.
func (ar *Collection) LoadFromEtree(element *etree.Element) {
xmlBytes := []byte(xmlutils.ElementToXML(element))
var action Action
if err := xml.Unmarshal(xmlBytes, &action); err != nil {
log.Panic("Unable to unmarshal element", "error", err, "bytes", string(xmlBytes))
}
ar.Add(&action)
}
// actionHelp is a placeholder struct to recover
// help XML from action definition
type actionHelp struct {
Content string `xml:",innerxml"`
}
// A Action is the definition of an action. Actions define the
// behavior of the system in response to user requests.
type Action struct {
ID string `json:"id" xml:"id,attr"`
Type ActionType `json:"type" xml:"type,attr"`
Name string `json:"name" xml:"name,attr"`
Model string `json:"res_model" xml:"model,attr"`
ResID int64 `json:"res_id" xml:"res_id,attr"`
Method string `json:"method" xml:"method,attr"`
Groups []string `json:"groups_id" xml:"groups,attr"`
Domain string `json:"domain" xml:"domain,attr"`
HelpXML actionHelp `json:"-" xml:"help"`
Help string `json:"help" xml:"-"`
SearchView views.ViewRef `json:"search_view_id" xml:"search_view_id,attr"`
SrcModel string `json:"src_model" xml:"src_model,attr"`
Usage string `json:"usage" xml:"usage,attr"`
Views []views.ViewTuple `json:"views" xml:"view"`
View views.ViewRef `json:"view_id" xml:"view_id,attr"`
AutoRefresh bool `json:"auto_refresh" xml:"auto_refresh,attr"`
ManualSearch bool `json:"-" xml:"-"`
ActViewType ActionViewType `json:"-" xml:"view_type"`
ViewMode string `json:"view_mode" xml:"view_mode,attr"`
Multi bool `json:"multi" xml:"multi,attr"`
Target string `json:"target" xml:"target,attr"`
AutoSearch bool `json:"auto_search" xml:"auto_search,attr"`
Filter bool `json:"filter" xml:"filter,attr"`
Limit int64 `json:"limit" xml:"limit,attr"`
Context *types.Context `json:"context" xml:"context,attr"`
Flags map[string]interface{} `json:"flags"`
Tag string `json:"tag"`
names map[string]string
}
// TranslatedName returns the translated name of this action
// in the given language
func (a Action) TranslatedName(lang string) string {
res, ok := a.names[lang]
if !ok {
res = a.Name
}
return res
}
// LoadFromEtree reads the action given etree.Element, creates or updates the action
// and adds it to the action registry if it not already.
func LoadFromEtree(element *etree.Element) {
Registry.LoadFromEtree(element)
}