Commit 39765a4304025d6c7db7ef9ce0ffbc00bf3c744d

Authored by Marko Tikvić
1 parent c430f3af58
Exists in master and in 1 other branch v2

added list and select configs

File was created 1 package restutility
2
3 import (
4 "gopkg.in/rana/ora.v3"
5 )
6
7 type ListOptions struct {
8 GlobalFilter bool `json:"globalFilter"`
9 LocalFilters bool `json:"localFilters"`
10 RemoteFilters bool `json:"remoteFilters"`
11 Pagination bool `json:"pagination"`
12 PageSize uint64 `json:"pageSize"`
13 Pivot bool `json:"pivot"`
14 Detail bool `json:"detail"`
15 }
16
17 type ListFilter struct {
18 ObjectType string `json:"-"`
19 FiltersField string `json:"filtersField"`
20 DefaultValues string `json:"defaultValues"`
21 FiltersType string `json:"filtersType"`
22 FiltersLabel string `json:"filtersLabel"`
23 }
24
25 type ListGraph struct {
26 ObjectType string `json:"objectType"`
27 X string `json:"xField"`
28 Y string `json:"yField"`
29 GroupField string `json:"groupField"`
30 Label string `json:"label"`
31 }
32
33 type ListActions struct {
34 Create bool `json:"create"`
35 Update bool `json:"update"`
36 Delete bool `json:"delete"`
37 Export bool `json:"export"`
38 Print bool `json:"print"`
39 Graph bool `json:"graph"`
40 }
41
42 type ListChildNode struct {
43 ObjectType string `json:"objectType"`
44 LabelField string `json:"label"`
45 Icon string `json:"icon"`
46 }
47
48 type ListParentNode struct {
49 ObjectType string `json:"objectType"`
50 LabelField string `json:"labelField"`
51 FilterField string `json:"filterField"`
52 }
53
54 type ListPivot struct {
55 ObjectType string `json:"objectType"`
56 GroupField string `json:"groupField"`
57 DistinctField string `json:"distinctField"`
58 Value string `json:"valueField"`
59 }
60
61 type ListDetails struct {
62 ObjectType string `json:"objectType"`
63 ParentObjectType string `json:"parentObjectType"`
64 ParentFilterField string `json:"parentFilterField"`
65 SingleDetail bool `json:"singleDetail"`
66 }
67
68 type ListConfig struct {
69 ObjectType string `json:"objectType"`
70 Title string `json:"title"`
71 LazyLoad bool `json:"lazyLoad"`
72 InlineEdit bool `json:"inlineEdit"`
73 Options ListOptions `json:"options"`
74 Filters []ListFilter `json:"defaultFilters"`
75 Graphs []ListGraph `json:"graphs"`
76 Actions ListActions `json:"actions"`
77 Parent []ListParentNode `json:"parent"`
78 Children []ListChildNode `json:"children"`
79 Pivots []ListPivot `json:"pivots"`
80 Details ListDetails `json:"details"`
81 }
82
83 func GetListConfigObjectIdField(db *ora.Ses, otype string) string {
84 var resp string
85 var err error
86 var stmt *ora.Stmt
87
88 stmt, err = db.Prep(`SELECT
89 ID_FIELD
90 FROM LIST_CONFIG_ID_FIELD
91 WHERE OBJECT_TYPE = '` + otype + `'`,
92 ora.S)
93
94 defer stmt.Close()
95
96 if err != nil {
97 return ""
98 }
99
100 rset, err := stmt.Qry()
101 if rset.Next() {
102 resp = rset.Row[0].(string)
103 }
104
105 if rset.Err != nil {
106 return ""
107 }
108
109 return resp
110 }
111
112 func NewDefaultList(objType string) ListConfig {
113 list := ListConfig{
114 ObjectType: objType,
115 Title: objType,
116 LazyLoad: false,
117 Options: ListOptions{
118 GlobalFilter: true,
119 LocalFilters: true,
120 RemoteFilters: false,
121 Pagination: true,
122 PageSize: 20,
123 },
124 Filters: nil,
125 Actions: ListActions{
126 Create: false,
127 Update: false,
128 Delete: false,
129 Export: false,
130 Print: false,
131 Graph: false,
132 },
133 Parent: nil,
134 Children: nil,
135 }
136
137 return list
138 }
139
140 func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) {
141 resp := NewDefaultList(objType)
142 var err error
143
144 err = setListParams(db, &resp, objType)
145 resp.Children, err = getListChildren(db, objType)
146 resp.Actions, err = getListActions(db, objType)
147 resp.Filters, err = getListFilters(db, objType)
148 resp.Options, err = getListOptions(db, objType)
149 resp.Parent, err = getListParent(db, objType)
150 resp.Graphs, err = getListGraph(db, objType)
151 resp.Pivots, err = getListPivot(db, objType)
152 resp.Details, err = getListDetails(db, objType)
153
154 if err != nil {
155 return ListConfig{}, err
156 }
157
158 return resp, nil
159 }
160
161 func setListParams(db *ora.Ses, list *ListConfig, objType string) error {
162 var err error
163 var stmt *ora.Stmt
164 query := `SELECT
165 OBJECT_TYPE, TITLE, LAZY_LOAD, INLINE_EDIT
166 FROM LIST_CONFIG
167 WHERE OBJECT_TYPE = '` + objType + `'`
168
169 stmt, err = db.Prep(query, ora.S, ora.S, ora.U32, ora.U32)
170 if err != nil {
171 return err
172 }
173 defer stmt.Close()
174
175 rset, err := stmt.Qry()
176 if err != nil {
177 return err
178 }
179 if rset.Next() {
180 otype := rset.Row[0].(string)
181 if otype != "" {
182 list.ObjectType = otype
183 }
184
185 title := rset.Row[1].(string)
186 if title != "" {
187 list.Title = title
188 }
189 list.LazyLoad = rset.Row[2].(uint32) != 0
190 list.InlineEdit = rset.Row[3].(uint32) != 0
191 }
192 if rset.Err != nil {
193 return rset.Err
194 }
195 return nil
196 }
197
198 func getListChildren(db *ora.Ses, objType string) ([]ListChildNode, error) {
199 resp := make([]ListChildNode, 0)
200 var err error
201 var stmt *ora.Stmt
202 query := `SELECT
203 OBJECT_TYPE, LABEL, ICON
204 FROM LIST_CONFIG_CHILD
205 WHERE PARENT_OBJECT_TYPE = '` + objType + `'`
206
207 stmt, err = db.Prep(query, ora.S, ora.S, ora.S)
208 if err != nil {
209 return resp, err
210 }
211 defer stmt.Close()
212
213 rset, err := stmt.Qry()
214 if err != nil {
215 return resp, err
216 }
217 for rset.Next() {
218 resp = append(resp, ListChildNode{
219 ObjectType: rset.Row[0].(string),
220 LabelField: rset.Row[1].(string),
221 Icon: rset.Row[2].(string),
222 })
223 }
224 if rset.Err != nil {
225 return nil, rset.Err
226 }
227
228 return resp, nil
229 }
230
231 func getListActions(db *ora.Ses, objType string) (ListActions, error) {
232 var resp ListActions
233 var err error
234 var stmt *ora.Stmt
235 query := `SELECT
236 ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE, ACTION_EXPORT,
237 ACTION_PRINT, ACTION_GRAPH
238 FROM LIST_CONFIG
239 WHERE OBJECT_TYPE = '` + objType + `'`
240
241 stmt, err = db.Prep(query, ora.U32, ora.U32, ora.U32, ora.U32,
242 ora.U32, ora.U32)
243 if err != nil {
244 return ListActions{}, err
245 }
246 defer stmt.Close()
247
248 rset, err := stmt.Qry()
249 if err != nil {
250 return ListActions{}, err
251 }
252 if rset.Next() {
253 resp.Create = rset.Row[0].(uint32) != 0
254 resp.Update = rset.Row[1].(uint32) != 0
255 resp.Delete = rset.Row[2].(uint32) != 0
256 resp.Export = rset.Row[3].(uint32) != 0
257 resp.Print = rset.Row[4].(uint32) != 0
258 resp.Graph = rset.Row[5].(uint32) != 0
259 }
260 if rset.Err != nil {
261 return ListActions{}, rset.Err
262 }
263 return resp, nil
264 }
265
266 func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) {
267 resp := make([]ListFilter, 0)
268 filtersField, err := getFilterFields(db, objType)
269 if err != nil {
270 return nil, err
271 }
272 for _, field := range filtersField {
273 filters, _ := getFiltersByFilterField(db, field)
274 for _, filter := range filters {
275 resp = append(resp, ListFilter{
276 ObjectType: objType,
277 FiltersField: field,
278 DefaultValues: filter.DefaultValues,
279 FiltersLabel: filter.Label,
280 FiltersType: filter.Type,
281 })
282 }
283 }
284
285 return resp, nil
286 }
287
288 func getFilterFields(db *ora.Ses, objType string) ([]string, error) {
289 filtersField := make([]string, 0)
290 var err error
291 var stmt *ora.Stmt
292 query := `SELECT
293 FILTERS_FIELD
294 FROM LIST_CONFIG_FILTERS
295 WHERE OBJECT_TYPE = '` + objType + `'`
296
297 stmt, err = db.Prep(query, ora.S)
298 if err != nil {
299 return nil, err
300 }
301 defer stmt.Close()
302
303 rset, err := stmt.Qry()
304 if err != nil {
305 return nil, err
306 }
307 for rset.Next() {
308 filtersField = append(filtersField, rset.Row[0].(string))
309 }
310 if rset.Err != nil {
311 return nil, rset.Err
312 }
313 return filtersField, nil
314 }
315
316 type TempFilter struct {
317 DefaultValues string
318 Label string
319 Type string
320 }
321
322 func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]TempFilter, error) {
323 resp := make([]TempFilter, 0)
324 var err error
325 var stmt *ora.Stmt
326 query := `SELECT
327 FILTERS_TYPE, FILTERS_LABEL, DEFAULT_VALUES
328 FROM LIST_FILTERS_FIELD
329 WHERE FILTERS_FIELD = '` + filtersField + `'`
330
331 stmt, err = db.Prep(query, ora.S, ora.S, ora.S)
332 if err != nil {
333 return resp, err
334 }
335 defer stmt.Close()
336
337 rset, err := stmt.Qry()
338 if err != nil {
339 return resp, err
340 }
341 for rset.Next() {
342 resp = append(resp, TempFilter{
343 Type: rset.Row[0].(string),
344 Label: rset.Row[1].(string),
345 DefaultValues: rset.Row[2].(string),
346 })
347 }
348 if rset.Err != nil {
349 return resp, rset.Err
350 }
351 return resp, nil
352 }
353
354 func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) {
355 resp := make([]ListGraph, 0)
356 var err error
357 var stmt *ora.Stmt
358 query := `SELECT
359 OBJECT_TYPE, X_FIELD, Y_FIELD, GROUP_FIELD, LABEL
360 FROM LIST_GRAPHS
361 WHERE OBJECT_TYPE = '` + objType + `'`
362
363 stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S)
364 if err != nil {
365 return resp, err
366 }
367 defer stmt.Close()
368
369 rset, err := stmt.Qry()
370 if err != nil {
371 return resp, err
372 }
373 for rset.Next() {
374 resp = append(resp, ListGraph{
375 ObjectType: rset.Row[0].(string),
376 X: rset.Row[1].(string),
377 Y: rset.Row[2].(string),
378 GroupField: rset.Row[3].(string),
379 Label: rset.Row[4].(string),
380 })
381 }
382 if rset.Err != nil {
383 return resp, rset.Err
384 }
385 return resp, nil
386 }
387
388 func getListOptions(db *ora.Ses, objType string) (ListOptions, error) {
389 var resp ListOptions
390 var err error
391 var stmt *ora.Stmt
392 query := `SELECT
393 GLOBAL_FILTER, LOCAL_FILTER, REMOTE_FILTER, PAGINATION,
394 PAGE_SIZE, PIVOT, DETAIL
395 FROM LIST_CONFIG
396 WHERE OBJECT_TYPE = '` + objType + `'`
397
398 stmt, err = db.Prep(query, ora.U32, ora.U32, ora.U32, ora.U32,
399 ora.U64, ora.U64, ora.U32)
400 if err != nil {
401 return ListOptions{}, err
402 }
403 defer stmt.Close()
404
405 rset, err := stmt.Qry()
406 if err != nil {
407 return ListOptions{}, err
408 }
409 if rset.Next() {
410 resp.GlobalFilter = rset.Row[0].(uint32) != 0
411 resp.LocalFilters = rset.Row[1].(uint32) != 0
412 resp.RemoteFilters = rset.Row[2].(uint32) != 0
413 resp.Pagination = rset.Row[3].(uint32) != 0
414 resp.PageSize = rset.Row[4].(uint64)
415 resp.Pivot = rset.Row[5].(uint64) != 0
416 resp.Detail = rset.Row[6].(uint32) != 0
417 }
418 if rset.Err != nil {
419 return ListOptions{}, rset.Err
420 }
421 return resp, nil
422 }
423
424 func getListParent(db *ora.Ses, objType string) ([]ListParentNode, error) {
425 resp := make([]ListParentNode, 0)
426 var err error
427 var stmt *ora.Stmt
428 query := `SELECT
429 PARENT_OBJECT_TYPE, PARENT_LABEL_FIELD, PARENT_FILTER_FIELD
430 FROM LIST_CONFIG_CHILD
431 WHERE OBJECT_TYPE = '` + objType + `'`
432
433 stmt, err = db.Prep(query, ora.S, ora.S, ora.S)
434 if err != nil {
435 return resp, err
436 }
437 defer stmt.Close()
438
439 rset, err := stmt.Qry()
440 if err != nil {
441 return resp, err
442 }
443 for rset.Next() {
444 resp = append(resp, ListParentNode{
445 ObjectType: rset.Row[0].(string),
446 LabelField: rset.Row[1].(string),
447 FilterField: rset.Row[2].(string),
448 })
449 }
450 if rset.Err != nil {
451 return nil, rset.Err
452 }
453
454 return resp, nil
455 }
456
457 func getListPivot(db *ora.Ses, objType string) ([]ListPivot, error) {
458 resp := make([]ListPivot, 0)
459 var err error
460 var stmt *ora.Stmt
461 query := `SELECT
462 OBJECT_TYPE, GROUP_FIELD, DISTINCT_FIELD, VALUE_FIELD
463 FROM LIST_PIVOTS
464 WHERE OBJECT_TYPE = '` + objType + `'`
465
466 stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S)
467 if err != nil {
468 return resp, err
469 }
470 defer stmt.Close()
471
472 rset, err := stmt.Qry()
473 if err != nil {
474 return resp, err
475 }
476 for rset.Next() {
477 resp = append(resp, ListPivot{
478 ObjectType: rset.Row[0].(string),
479 GroupField: rset.Row[1].(string),
480 DistinctField: rset.Row[2].(string),
481 Value: rset.Row[3].(string),
482 })
483 }
484 if rset.Err != nil {
485 return nil, rset.Err
486 }
487
488 return resp, nil
489 }
490
491 func getListDetails(db *ora.Ses, objType string) (ListDetails, error) {
492 var resp ListDetails
493 var err error
494 var stmt *ora.Stmt
495 query := `SELECT
496 OBJECT_TYPE, PARENT_OBJECT_TYPE, PARENT_FILTER_FIELD, SINGLE_DETAIL
497 FROM LIST_CONFIG_DETAIL
498 WHERE PARENT_OBJECT_TYPE = '` + objType + `'`
499
500 stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.U32)
501 if err != nil {
502 return resp, err
503 }
504 defer stmt.Close()
505
506 rset, err := stmt.Qry()
507 if err != nil {
508 return resp, err
509 }
510 if rset.Next() {
511 resp.ObjectType = rset.Row[0].(string)
512 resp.ParentObjectType = rset.Row[1].(string)
513 resp.ParentFilterField = rset.Row[2].(string)
514 resp.SingleDetail = rset.Row[3].(uint32) != 0
515 }
516 if rset.Err != nil {
517 return resp, rset.Err
518 }
519
520 return resp, nil
521 }
522
File was created 1 package restutility
2
3 import (
4 "gopkg.in/rana/ora.v3"
5 )
6
7 type SelectConfig struct {
8 ListObjType string `json:"listObjectType"`
9 ObjType string `json:"objectType"`
10 Type string `json:"type"`
11 IdField string `json:"idField"`
12 LabelField string `json:"labelField"`
13 ValueField string `json:"valueField"`
14 }
15
16 func GetSelectConfig(db *ora.Ses, otype string) ([]SelectConfig, error) {
17 resp := make([]SelectConfig, 0)
18 var err error
19 var stmt *ora.Stmt
20 query := `SELECT a.LIST_OBJECT_TYPE, a.OBJECT_TYPE, a.ID_FIELD,
21 a.LABEL_FIELD, a.TYPE, b.FIELD
22 FROM LIST_SELECT_CONFIG a, LIST_VALUE_FIELD b
23 WHERE a.LIST_OBJECT_TYPE` + otype + `
24 AND b.LIST_TYPE = a.LIST_OBJECT_TYPE
25 AND b.OBJECT_TYPE = a.OBJECT_TYPE`
26
27 stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S, ora.S,
28 ora.S)
29 defer stmt.Close()
30 if err != nil {
31 return nil, err
32 }
33
34 rset, err := stmt.Qry()
35 if err != nil {
36 return nil, err
37 }
38 for rset.Next() {
39 resp = append(resp, SelectConfig{
40 ListObjType: rset.Row[0].(string),
41 ObjType: rset.Row[1].(string),
42 IdField: rset.Row[2].(string),
43 LabelField: rset.Row[3].(string),
44 Type: rset.Row[4].(string),
45 ValueField: rset.Row[5].(string),
46 })
47 }
48 if rset.Err != nil {
49 return nil, rset.Err
50 }
51
52 return resp, nil
53
54 }
55
tables_utility.go
1 package restutility 1 package restutility
2 2
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 "errors" 5 "errors"
6 "gopkg.in/rana/ora.v3"
7 "io"
8 "io/ioutil"
9 "fmt"
6 ) 10 )
7 11
8 type TableConfig struct { 12 type TableConfig struct {
9 Tables []Table 13 Tables []Table
10 } 14 }
11 15
12 type Table struct { 16 type Table struct {
13 TableType string `json:"tableType"` 17 TableType string `json:"tableType"`
14 Translations []TableTranslation `json:"translations"` 18 Translations []TableTranslation `json:"translations"`
15 TableFields []Field `json:"tableFields"` 19 TableFields []Field `json:"tableFields"`
16 Correlations []CorrelationField `json:"correlationFields"` 20 Correlations []CorrelationField `json:"correlationFields"`
17 IdField string `json:"idField"` 21 IdField string `json:"idField"`
18 } 22 }
19 23
20 type CorrelationField struct { 24 type CorrelationField struct {
21 Result string `json:"result"` 25 Result string `json:"result"`
22 Elements []string `json:"elements"` 26 Elements []string `json:"elements"`
23 Type string `json:"type"` 27 Type string `json:"type"`
24 } 28 }
25 29
26 type TableTranslation struct { 30 type TableTranslation struct {
27 Language string `json:"language"` 31 Language string `json:"language"`
28 FieldsLabels map[string]string `json:"fieldsLabels"` 32 FieldsLabels map[string]string `json:"fieldsLabels"`
29 } 33 }
30 34
31 func (tl TableConfig) LoadTranslations(tableType string) LangMap { 35 func (tl TableConfig) LoadTranslations(tableType string) LangMap {
32 translations := make(LangMap, 0) 36 translations := make(LangMap, 0)
33 37
34 for _, table := range tl.Tables { 38 for _, table := range tl.Tables {
35 if tableType == table.TableType { 39 if tableType == table.TableType {
36 for _, t := range table.Translations { 40 for _, t := range table.Translations {
37 translations[t.Language] = t.FieldsLabels 41 translations[t.Language] = t.FieldsLabels
38 } 42 }
39 } 43 }
40 } 44 }
41 45
42 return translations 46 return translations
43 } 47 }
44 48
45 func (tl TableConfig) LoadFields(tableType string) []Field { 49 func (tl TableConfig) LoadFields(tableType string) []Field {
46 fields := make([]Field, 0) 50 fields := make([]Field, 0)
47 51
48 for _, table := range tl.Tables { 52 for _, table := range tl.Tables {
49 if tableType == table.TableType { 53 if tableType == table.TableType {
50 for _, f := range table.TableFields { 54 for _, f := range table.TableFields {
51 fields = append(fields, f) 55 fields = append(fields, f)
52 } 56 }
53 } 57 }
54 } 58 }
55 59
56 return fields 60 return fields
57 } 61 }
58 62
59 func (tl TableConfig) LoadIdField(tableType string) string { 63 func (tl TableConfig) LoadIdField(tableType string) string {
60 for _, table := range tl.Tables { 64 for _, table := range tl.Tables {
61 if tableType == table.TableType { 65 if tableType == table.TableType {
62 return table.IdField 66 return table.IdField
63 } 67 }
64 } 68 }
65 return "" 69 return ""
66 } 70 }
67 71
68 func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField { 72 func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField {
69 resp := make([]CorrelationField, 0) 73 resp := make([]CorrelationField, 0)
70 74
71 for _, table := range tl.Tables { 75 for _, table := range tl.Tables {
72 if tableType == table.TableType { 76 if tableType == table.TableType {
73 for _, f := range table.Correlations { 77 for _, f := range table.Correlations {
74 resp = append(resp, f) 78 resp = append(resp, f)
75 } 79 }
76 } 80 }
77 } 81 }
78 82
79 return resp 83 return resp
80 } 84 }
81 85
82 var _tables TableConfig 86 var _tables TableConfig
83 var _prevProject string 87 var _prevProject string
84 88
85 func InitTables(jsonbuf []byte) error { 89 func InitTables(db *ora.Ses, project string) error {
90 jsonbuf, _ := fetchTablesConfig(db, EqualQuotes(project))
86 json.Unmarshal(jsonbuf, &_tables.Tables) 91 json.Unmarshal(jsonbuf, &_tables.Tables)
87 if len(_tables.Tables) == 0 { 92 if len(_tables.Tables) == 0 {
88 return errors.New("tables config is corrupt") 93 return errors.New("tables config is corrupt")
89 } 94 }
90 return nil 95 return nil
91 } 96 }
92 97
98 func fetchTablesConfig(db *ora.Ses, project string) ([]byte, error) {
99 stmt, err := db.Prep(`SELECT
100 JSON_CLOB
101 FROM TABLES_CONFIG
102 WHERE PROJEKAT` + project, ora.S)
103 defer stmt.Close()
104
105 if err != nil {
106 return nil, err
107 }
108
109 rset, err := stmt.Qry()
110 if err != nil {
111 return nil, err
112 }
113
114 bytes := make([]byte, 0)
115 if rset.Next() {
116 lob := rset.Row[0].(io.Reader)
117 bytes, err = ioutil.ReadAll(lob)
118 if err != nil {
119 fmt.Printf("mega error: %v\n", err)
120 }
121 }
122
123 return bytes, nil
124 }
125
93 func loadTable(table string) JSONParams { 126 func loadTable(table string) JSONParams {
94 return NewJSONParams(_tables.LoadTranslations(table), 127 return NewJSONParams(_tables.LoadTranslations(table),
95 _tables.LoadFields(table), 128 _tables.LoadFields(table),
96 _tables.LoadIdField(table), 129 _tables.LoadIdField(table),
97 _tables.LoadCorrelations(table)) 130 _tables.LoadCorrelations(table))
98 } 131 }
99 132
100 133