/
view.go
410 lines (352 loc) · 13.3 KB
/
view.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package transform
import (
"errors"
"strconv"
"strings"
"github.com/andreluzz/cas-xog/constant"
"github.com/andreluzz/cas-xog/model"
"github.com/beevik/etree"
)
func specificViewTransformations(xog, aux *etree.Document, file *model.DriverFile) error {
if len(file.Sections) > 0 && file.Code == "*" {
return errors.New("tag <section> is only available for single view")
}
removeElementFromParent(xog, "//lookups")
removeElementFromParent(xog, "//objects")
if file.TargetPartition != "" && file.SourcePartition == "" {
return errors.New("can't change partition without attribute sourcePartition defined")
} else if file.TargetPartition != "" && file.SourcePartition != "" {
changePartition(xog, file.SourcePartition, file.TargetPartition)
}
if file.Code != "*" {
validateCodeAndRemoveElementsFromParent(xog, "//views/property", file.Code)
validateCodeAndRemoveElementsFromParent(xog, "//views/filter", file.Code)
validateCodeAndRemoveElementsFromParent(xog, "//views/list", file.Code)
//auxiliary xog file
removeElementFromParent(aux, "//lookups")
removeElementFromParent(aux, "//objects")
//Only executes is file code is '*' (single view)
if strings.Contains(file.Code, "*") == false {
elementsTransform := false
if len(file.Elements) > 0 {
var err error
elementsTransform, err = processElements(xog, aux, file)
if err != nil {
return err
}
}
if len(file.Sections) > 0 {
err := updateSections(xog, aux, file)
if err != nil {
return err
}
return nil
}
if elementsTransform {
xog.SetRoot(aux.Root())
return nil
}
}
updateSourceWithTargetPropertySet(xog, aux, file)
orderViewChildElements(xog, "//property")
orderViewChildElements(xog, "//propertySet")
orderViewChildElements(xog, "//filter")
orderViewChildElements(xog, "//list")
}
return nil
}
func orderViewChildElements(xog *etree.Document, sequenceElementPath string) {
for _, e := range xog.FindElements(sequenceElementPath) {
e.Parent().AddChild(e.Copy())
e.Parent().RemoveChild(e)
}
}
func processElementTypeActionGroup(element model.Element, xog, aux *etree.Document) error {
if element.Action == constant.ActionInsert {
sourceGroup := xog.FindElement("//actions/group[@code='" + element.Code + "']")
if sourceGroup == nil {
return errors.New("invalid source view action group code")
}
targetGroup := aux.FindElement("//actions/group[@code='" + element.Code + "']")
if element.InsertBefore != constant.Undefined {
insertBeforeGroup := aux.FindElement("//actions/group[@code='" + element.InsertBefore + "']")
if insertBeforeGroup == nil {
return errors.New("invalid insertBefore target view action group code")
}
insertBeforeGroup.Parent().InsertChild(insertBeforeGroup, sourceGroup)
} else {
if targetGroup == nil {
actions := aux.FindElement("//actions")
actions.AddChild(sourceGroup)
} else {
targetGroup.Parent().InsertChild(targetGroup, sourceGroup)
}
}
if targetGroup != nil {
targetGroup.Parent().RemoveChild(targetGroup)
}
} else if element.Action == constant.ActionRemove {
targetGroup := aux.FindElement("//actions/group[@code='" + element.Code + "']")
if targetGroup == nil {
return errors.New("cannot remove target view action group - invalid code")
}
targetGroup.Parent().RemoveChild(targetGroup)
}
return nil
}
func processElementTypeAction(element model.Element, xog, aux *etree.Document) error {
if element.Action == constant.ActionInsert {
sourceAction := xog.FindElement("//actions/group/action[@code='" + element.Code + "']")
if sourceAction == nil {
return errors.New("invalid source view action code")
}
sourceGroupCode := sourceAction.Parent().SelectAttrValue("code", constant.Undefined)
targetAction := aux.FindElement("//actions/group[@code='" + sourceGroupCode + "']/action[@code='" + element.Code + "']")
if element.InsertBefore != constant.Undefined {
insertBeforeAction := aux.FindElement("//actions/group/action[@code='" + element.InsertBefore + "']")
if insertBeforeAction == nil {
return errors.New("invalid insertBefore target view action code")
}
insertBeforeAction.Parent().InsertChild(insertBeforeAction, sourceAction)
} else {
if targetAction == nil {
targetActionNLS := aux.FindElement("//actions/group[@code='" + sourceGroupCode + "']/nls[1]")
targetActionNLS.Parent().InsertChild(targetActionNLS, sourceAction)
} else {
targetAction.Parent().InsertChild(targetAction, sourceAction)
}
}
if targetAction != nil {
targetAction.Parent().RemoveChild(targetAction)
}
} else if element.Action == constant.ActionRemove {
targetAction := aux.FindElement("//actions/group/action[@code='" + element.Code + "']")
if targetAction == nil {
return errors.New("cannot remove target view action - invalid code")
}
targetAction.Parent().RemoveChild(targetAction)
}
return nil
}
func processElements(xog, aux *etree.Document, file *model.DriverFile) (bool, error) {
validateCodeAndRemoveElementsFromParent(aux, "//views/property", file.Code)
validateCodeAndRemoveElementsFromParent(aux, "//views/filter", file.Code)
validateCodeAndRemoveElementsFromParent(aux, "//views/list", file.Code)
validElements := false
for _, e := range file.Elements {
if e.XPath == constant.Undefined && (e.Type == constant.ElementTypeActionGroup || e.Type == constant.ElementTypeAction) {
validElements = true
switch e.Type {
case constant.ElementTypeAction:
err := processElementTypeAction(e, xog, aux)
if err != nil {
return false, err
}
case constant.ElementTypeActionGroup:
err := processElementTypeActionGroup(e, xog, aux)
if err != nil {
return false, err
}
}
}
}
if validElements {
for i, g := range aux.FindElements("//actions/group") {
g.CreateAttr("groupOrder", strconv.Itoa(i))
}
}
return validElements, nil
}
func updateSections(xog, aux *etree.Document, file *model.DriverFile) error {
validateCodeAndRemoveElementsFromParent(aux, "//views/property", file.Code)
validateCodeAndRemoveElementsFromParent(aux, "//views/filter", file.Code)
validateCodeAndRemoveElementsFromParent(aux, "//views/list", file.Code)
nikuDataBus := aux.FindElement("//NikuDataBus")
aux.SetRoot(nikuDataBus)
aux.IndentTabs()
targetView := aux.FindElement("//views/*[@code='" + file.Code + "']")
if targetView == nil {
return errors.New("can't process section because the view (" + file.Code + ") does not exist in target environment")
}
sourceView := xog.FindElement("//views/*[@code='" + file.Code + "']")
if sourceView == nil {
return errors.New("can't process section because the view (" + file.Code + ") does not exist in source environment")
}
err := processSections(file, sourceView, targetView, constant.ActionReplace)
if err != nil {
return err
}
err = processSections(file, sourceView, targetView, constant.ActionUpdate)
if err != nil {
return err
}
err = processSections(file, sourceView, targetView, constant.ActionRemove)
if err != nil {
return err
}
err = processSections(file, sourceView, targetView, constant.ActionInsert)
if err != nil {
return err
}
for _, section := range file.Sections {
if section.Action != constant.ActionRemove && section.Action != constant.ActionReplace && section.Action != constant.ActionInsert && section.Action != constant.ActionUpdate {
return errors.New("invalid action attribute (" + section.Action + ") on tag <section>")
}
}
xog.SetRoot(aux.Root())
for i, s := range xog.FindElements("//section") {
s.CreateAttr("sequence", strconv.Itoa(i+1))
}
return nil
}
func processSections(file *model.DriverFile, sourceView, targetView *etree.Element, action string) error {
for _, section := range file.Sections {
if section.Action == action {
err := processSectionByType(section, sourceView, targetView)
if err != nil {
return err
}
}
}
return nil
}
func validateSection(section model.Section, sourceView, targetView *etree.Element) (*etree.Element, *etree.Element, error) {
var sourceSection *etree.Element
if section.Action != constant.ActionRemove {
if section.SourcePosition == "" {
return nil, nil, errors.New("attribute sourcePosition from tag <section> is not defined")
}
sourceSection = sourceView.FindElement("//section[" + section.SourcePosition + "]")
if sourceSection == nil {
return nil, nil, errors.New("source section for position " + section.SourcePosition + " does not exist")
}
}
targetSection := targetView.FindElement("//nls[1]")
if section.TargetPosition != "" {
targetSection = targetView.FindElement("//section[" + section.TargetPosition + "]")
if targetSection == nil {
return nil, nil, errors.New("target position " + section.TargetPosition + " does not exist")
}
}
return sourceSection, targetSection, nil
}
func processViewSectionActionUpdate(section model.Section, sourceSection, targetSection *etree.Element) error {
if len(section.Fields) == 0 {
return errors.New("cannot update section because there is no tag <field> defined")
}
columnRight := targetSection.FindElement("column[@sequence='2']")
if columnRight == nil {
//Create column if it does not exists
columnRight = etree.NewElement("column")
columnRight.CreateAttr("sequence", "2")
nlsElement := targetSection.FindElement("nls[1]")
targetSection.InsertChild(nlsElement, columnRight)
}
columnLeft := targetSection.FindElement("column[@sequence='1']")
if columnLeft == nil {
//Create column if it does not exists
columnLeft = etree.NewElement("column")
columnLeft.CreateAttr("sequence", "1")
targetSection.InsertChild(columnRight, columnLeft)
}
for _, f := range section.Fields {
if f.Remove {
removeElement := targetSection.FindElement("//viewFieldDescriptor[@attributeCode='" + f.Code + "']")
if removeElement == nil {
return errors.New("cannot remove field because code does not exist in target environment section")
}
removeElement.Parent().RemoveChild(removeElement)
continue
}
attributeElement := sourceSection.FindElement("//viewFieldDescriptor[@attributeCode='" + f.Code + "']")
if attributeElement == nil {
return errors.New("field attribute code does not exist in source environment view")
}
var targetAttribute *etree.Element
if f.InsertBefore != "" {
targetAttribute = targetSection.FindElement("//viewFieldDescriptor[@attributeCode='" + f.InsertBefore + "']")
if targetAttribute == nil {
//Transform views - section - trying to insert before an attribute that does not exists in target environment
return errors.New("trying to insert before an field that does not exists in target environment")
}
}
switch f.Column {
case constant.ColumnLeft:
if f.InsertBefore == "" {
columnLeft.AddChild(attributeElement)
} else {
columnLeft.InsertChild(targetAttribute, attributeElement)
}
case constant.ColumnRight:
if f.InsertBefore == "" {
columnRight.AddChild(attributeElement)
} else {
columnRight.InsertChild(targetAttribute, attributeElement)
}
default:
return errors.New("cannot update section, column value invalid, only 'right' or 'left' are available")
}
}
return nil
}
func processSectionByType(section model.Section, sourceView, targetView *etree.Element) error {
sourceSection, targetSection, err := validateSection(section, sourceView, targetView)
if err != nil {
return err
}
switch section.Action {
case constant.ActionReplace:
if section.TargetPosition == "" {
return errors.New("cannot replace section because attribute targetPosition from tag <section> is not defined")
}
targetView.InsertChild(targetSection, sourceSection)
targetView.RemoveChild(targetSection)
case constant.ActionRemove:
if section.TargetPosition == "" {
return errors.New("cannot remove section because attribute targetPosition from tag <section> is not defined")
}
targetView.RemoveChild(targetSection)
case constant.ActionInsert:
targetView.InsertChild(targetSection, sourceSection)
case constant.ActionUpdate:
err := processViewSectionActionUpdate(section, sourceSection, targetSection)
if err != nil {
return err
}
}
return nil
}
func validateCodeAndRemoveElementsFromParent(xog *etree.Document, path, code string) {
for _, e := range xog.FindElements(path) {
elementCode := e.SelectAttrValue("code", "")
if strings.Contains(code, "*") {
if strings.Contains(elementCode, code[1:]) == false {
e.Parent().RemoveChild(e)
}
} else {
if elementCode != code {
e.Parent().RemoveChild(e)
}
}
}
}
func updateSourceWithTargetPropertySet(xog, aux *etree.Document, file *model.DriverFile) {
sourcePropertySetView := xog.FindElement("//propertySet/update/view[@code='" + file.Code + "']")
if sourcePropertySetView != nil {
auxPropertySetView := aux.FindElement("//propertySet/update/view[@code='" + file.Code + "']")
if auxPropertySetView != nil {
auxPropertySetView.Parent().InsertChild(auxPropertySetView, sourcePropertySetView)
auxPropertySetView.Parent().RemoveChild(auxPropertySetView)
} else {
propertySetUpdate := aux.FindElement("//propertySet/update")
if propertySetUpdate != nil {
auxpropertySetFirstNLS := aux.FindElement("//propertySet/update/nls[1]")
propertySetUpdate.InsertChild(auxpropertySetFirstNLS, sourcePropertySetView)
}
}
}
auxPropertySet := aux.FindElement("//propertySet")
xogPropertySet := xog.FindElement("//propertySet")
xogPropertySet.Parent().AddChild(auxPropertySet)
xogPropertySet.Parent().RemoveChild(xogPropertySet)
}