Blame view
list_config.go
13.3 KB
8bc396eb9 reverted changes |
1 |
package webutility |
79071a5d4 Using database/sq... |
2 3 4 5 |
import ( "database/sql" "fmt" ) |
8bc396eb9 reverted changes |
6 7 8 9 10 11 |
type ListOptions struct { GlobalFilter bool `json:"globalFilter"` LocalFilters bool `json:"localFilters"` RemoteFilters bool `json:"remoteFilters"` Pagination bool `json:"pagination"` |
79071a5d4 Using database/sq... |
12 |
PageSize uint32 `json:"pageSize"` |
8bc396eb9 reverted changes |
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 |
Pivot bool `json:"pivot"` Detail bool `json:"detail"` Total bool `json:"total"` } type ListFilter struct { Position uint32 `json:"-"` ObjectType string `json:"-"` FiltersField string `json:"filtersField"` DefaultValues string `json:"defaultValues"` FiltersType string `json:"filtersType"` FiltersLabel string `json:"filtersLabel"` DropdownConfig Dropdown `json:"dropdownConfig"` } type Dropdown struct { ObjectType string `json:"objectType"` FiltersField string `json:"filtersField"` IDField string `json:"idField"` LabelField string `json:"labelField"` } type ListGraph struct { ObjectType string `json:"objectType"` X string `json:"xField"` Y string `json:"yField"` GroupField string `json:"groupField"` Label string `json:"label"` } type ListActions struct { Create bool `json:"create"` Update bool `json:"update"` Delete bool `json:"delete"` Export bool `json:"export"` Print bool `json:"print"` Graph bool `json:"graph"` } type ListNavNode struct { ObjectType string `json:"objectType"` LabelField string `json:"label"` Icon string `json:"icon"` ParentObjectType string `json:"parentObjectType"` ParentIDField string `json:"parentIdField"` ParentFilterField string `json:"parentFilterField"` } type ListParentNode struct { ObjectType string `json:"objectType"` LabelField string `json:"labelField"` FilterField string `json:"filterField"` } type ListPivot struct { ObjectType string `json:"objectType"` GroupField string `json:"groupField"` DistinctField string `json:"distinctField"` Value string `json:"valueField"` } type ListDetails struct { ObjectType string `json:"objectType"` ParentObjectType string `json:"parentObjectType"` ParentFilterField string `json:"parentFilterField"` SingleDetail bool `json:"singleDetail"` } type ListConfig struct { ObjectType string `json:"objectType"` Title string `json:"title"` LazyLoad bool `json:"lazyLoad"` InlineEdit bool `json:"inlineEdit"` Options ListOptions `json:"options"` Filters []ListFilter `json:"defaultFilters"` Graphs []ListGraph `json:"graphs"` Actions ListActions `json:"actions"` Parent []ListParentNode `json:"parent"` Navigation []ListNavNode `json:"navigation"` Pivots []ListPivot `json:"pivots"` Details ListDetails `json:"details"` } // GetListConfig returns list configuration for the provided object type for the front-end application // or an error if it fails. |
79071a5d4 Using database/sq... |
98 |
func GetListConfig(db *sql.DB, objType string) (ListConfig, error) { |
8bc396eb9 reverted changes |
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
resp := newDefaultList(objType) var err error err = setListParams(db, &resp, objType) resp.Navigation, err = getListNavigation(db, objType) resp.Actions, err = getListActions(db, objType) resp.Filters, err = getListFilters(db, objType) resp.Options, err = getListOptions(db, objType) resp.Parent, err = getListParent(db, objType) resp.Graphs, err = getListGraph(db, objType) resp.Pivots, err = getListPivot(db, objType) resp.Details, err = getListDetails(db, objType) if err != nil { return ListConfig{}, err } return resp, nil } // GetListConfigObjectIDField takes in database connection and an object type and it returns the // ID field name for the provided object type. |
79071a5d4 Using database/sq... |
121 |
func GetListConfigObjectIDField(db *sql.DB, otype string) string { |
8bc396eb9 reverted changes |
122 |
var resp string |
8bc396eb9 reverted changes |
123 |
|
79071a5d4 Using database/sq... |
124 |
rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
125 126 |
ID_FIELD FROM LIST_CONFIG_ID_FIELD |
79071a5d4 Using database/sq... |
127 |
WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", otype)) |
8bc396eb9 reverted changes |
128 129 130 |
if err != nil { return "" } |
79071a5d4 Using database/sq... |
131 |
defer rows.Close() |
8bc396eb9 reverted changes |
132 |
|
79071a5d4 Using database/sq... |
133 134 |
if rows.Next() { rows.Scan(&resp) |
8bc396eb9 reverted changes |
135 |
} |
79071a5d4 Using database/sq... |
136 |
if rows.Err() != nil { |
8bc396eb9 reverted changes |
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 |
return "" } return resp } // newDefaultList returns default configuration for the provided object type. func newDefaultList(objType string) ListConfig { list := ListConfig{ ObjectType: objType, Title: objType, LazyLoad: false, Options: ListOptions{ GlobalFilter: true, LocalFilters: true, RemoteFilters: false, Pagination: true, PageSize: 20, }, Filters: nil, Actions: ListActions{ Create: false, Update: false, Delete: false, Export: false, Print: false, Graph: false, }, Parent: nil, Navigation: nil, } return list } // setListParams sets the default parameters of the provided configuration list for the provided object type. |
79071a5d4 Using database/sq... |
173 174 |
func setListParams(db *sql.DB, list *ListConfig, objType string) error { rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
175 176 |
OBJECT_TYPE, TITLE, LAZY_LOAD, INLINE_EDIT FROM LIST_CONFIG |
79071a5d4 Using database/sq... |
177 |
WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
8bc396eb9 reverted changes |
178 179 180 |
if err != nil { return err } |
79071a5d4 Using database/sq... |
181 182 183 184 185 |
defer rows.Close() if rows.Next() { otype, title := "", "" lazyLoad, inlineEdit := 0, 0 rows.Scan(&otype, &title, &lazyLoad, &inlineEdit) |
8bc396eb9 reverted changes |
186 |
|
8bc396eb9 reverted changes |
187 188 189 |
if otype != "" { list.ObjectType = otype } |
8bc396eb9 reverted changes |
190 191 192 |
if title != "" { list.Title = title } |
79071a5d4 Using database/sq... |
193 194 |
list.LazyLoad = lazyLoad != 0 list.InlineEdit = inlineEdit != 0 |
8bc396eb9 reverted changes |
195 |
} |
79071a5d4 Using database/sq... |
196 197 |
if rows.Err() != nil { return rows.Err() |
8bc396eb9 reverted changes |
198 199 200 201 202 |
} return nil } // getListNavigation returns list navigation node slice for the provided objectType. |
79071a5d4 Using database/sq... |
203 |
func getListNavigation(db *sql.DB, listObjType string) ([]ListNavNode, error) { |
8bc396eb9 reverted changes |
204 |
resp := make([]ListNavNode, 0) |
79071a5d4 Using database/sq... |
205 206 |
rows, err := db.Query(`SELECT a.OBJECT_TYPE, a.PARENT_OBJECT_TYPE, a.LABEL, a.ICON, a.PARENT_FILTER_FIELD, b.PARENT_ID_FIELD |
8bc396eb9 reverted changes |
207 208 |
FROM LIST_CONFIG_NAVIGATION b JOIN LIST_CONFIG_CHILD a ON b.PARENT_CHILD_ID = a.PARENT_CHILD_ID |
79071a5d4 Using database/sq... |
209 210 |
WHERE b.LIST_OBJECT_TYPE = ` + fmt.Sprintf("'%s'", listObjType) + ` ORDER BY b.RB ASC`) |
8bc396eb9 reverted changes |
211 212 213 |
if err != nil { return resp, err } |
79071a5d4 Using database/sq... |
214 |
defer rows.Close() |
8bc396eb9 reverted changes |
215 |
|
79071a5d4 Using database/sq... |
216 217 218 219 220 |
var node ListNavNode for rows.Next() { rows.Scan(&node.ObjectType, &node.ParentObjectType, &node.LabelField, &node.Icon, &node.ParentFilterField, &node.ParentIDField) resp = append(resp, node) |
8bc396eb9 reverted changes |
221 |
} |
79071a5d4 Using database/sq... |
222 223 |
if rows.Err() != nil { return nil, rows.Err() |
8bc396eb9 reverted changes |
224 225 226 227 228 229 |
} return resp, nil } // getListActions returns list actions for the provided object type. |
79071a5d4 Using database/sq... |
230 |
func getListActions(db *sql.DB, objType string) (ListActions, error) { |
8bc396eb9 reverted changes |
231 |
var resp ListActions |
79071a5d4 Using database/sq... |
232 |
rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
233 234 235 |
ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE, ACTION_EXPORT, ACTION_PRINT, ACTION_GRAPH FROM LIST_CONFIG |
79071a5d4 Using database/sq... |
236 |
WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
8bc396eb9 reverted changes |
237 238 239 |
if err != nil { return ListActions{}, err } |
79071a5d4 Using database/sq... |
240 |
defer rows.Close() |
8bc396eb9 reverted changes |
241 |
|
79071a5d4 Using database/sq... |
242 243 244 245 246 247 248 249 250 |
var create, update, delete, export, print, graph uint32 if rows.Next() { rows.Scan(&create, &update, &delete, &export, &print, &graph) resp.Create = create != 0 resp.Update = update != 0 resp.Delete = delete != 0 resp.Export = export != 0 resp.Print = print != 0 resp.Graph = graph != 0 |
8bc396eb9 reverted changes |
251 |
} |
79071a5d4 Using database/sq... |
252 253 |
if rows.Err() != nil { return ListActions{}, rows.Err() |
8bc396eb9 reverted changes |
254 255 256 257 258 |
} return resp, nil } // getListFiters returns list filter slice for the provided object type. |
79071a5d4 Using database/sq... |
259 |
func getListFilters(db *sql.DB, objType string) ([]ListFilter, error) { |
8bc396eb9 reverted changes |
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 |
resp := make([]ListFilter, 0) filtersFields, err := getFilterFieldsAndPosition(db, objType) if err != nil { return nil, err } for field, pos := range filtersFields { filters, _ := getFiltersByFilterField(db, field) for _, filter := range filters { var f ListFilter f.Position = pos f.ObjectType = objType f.FiltersField = field f.DefaultValues = filter.DefaultValues f.FiltersLabel = filter.Label f.FiltersType = filter.Type if filter.Type == "dropdown" { f.DropdownConfig, err = getFilterDropdownConfig(db, field) if err != nil { return nil, err } } resp = append(resp, f) } } sortFilters(resp) return resp, nil } // getFilterFieldsAndPosition returns a map of filter fields and their respective position in the menu. |
79071a5d4 Using database/sq... |
291 |
func getFilterFieldsAndPosition(db *sql.DB, objType string) (map[string]uint32, error) { |
8bc396eb9 reverted changes |
292 |
filtersField := make(map[string]uint32, 0) |
79071a5d4 Using database/sq... |
293 |
rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
294 295 |
FILTERS_FIELD, RB FROM LIST_CONFIG_FILTERS |
79071a5d4 Using database/sq... |
296 |
WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
8bc396eb9 reverted changes |
297 298 299 |
if err != nil { return nil, err } |
79071a5d4 Using database/sq... |
300 |
defer rows.Close() |
8bc396eb9 reverted changes |
301 |
|
79071a5d4 Using database/sq... |
302 303 304 305 306 |
for rows.Next() { var field string var rb uint32 rows.Scan(&field, &rb) filtersField[field] = rb |
8bc396eb9 reverted changes |
307 |
} |
79071a5d4 Using database/sq... |
308 309 |
if rows.Err() != nil { return nil, rows.Err() |
8bc396eb9 reverted changes |
310 311 312 313 314 315 316 317 318 319 320 |
} return filtersField, nil } type _filter struct { DefaultValues string Label string Type string } // getFiltersByFilterField returns filter slice for the provided filter field. |
79071a5d4 Using database/sq... |
321 |
func getFiltersByFilterField(db *sql.DB, filtersField string) ([]_filter, error) { |
8bc396eb9 reverted changes |
322 |
resp := make([]_filter, 0) |
79071a5d4 Using database/sq... |
323 |
rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
324 325 |
FILTERS_TYPE, FILTERS_LABEL, DEFAULT_VALUES FROM LIST_FILTERS_FIELD |
79071a5d4 Using database/sq... |
326 |
WHERE FILTERS_FIELD = ` + fmt.Sprintf("'%s'", filtersField)) |
8bc396eb9 reverted changes |
327 328 329 |
if err != nil { return resp, err } |
79071a5d4 Using database/sq... |
330 |
defer rows.Close() |
8bc396eb9 reverted changes |
331 |
|
79071a5d4 Using database/sq... |
332 333 334 335 |
var f _filter for rows.Next() { rows.Scan(&f.Type, &f.Label, &f.DefaultValues) resp = append(resp, f) |
8bc396eb9 reverted changes |
336 |
} |
79071a5d4 Using database/sq... |
337 338 |
if rows.Err() != nil { return resp, rows.Err() |
8bc396eb9 reverted changes |
339 340 341 342 343 |
} return resp, nil } // getFilterDropdownConfig returns dropdown menu for the provided filter field. |
79071a5d4 Using database/sq... |
344 |
func getFilterDropdownConfig(db *sql.DB, filtersField string) (Dropdown, error) { |
8bc396eb9 reverted changes |
345 |
var resp Dropdown |
79071a5d4 Using database/sq... |
346 |
rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
347 348 |
FILTERS_FIELD, OBJECT_TYPE, ID_FIELD, LABEL_FIELD FROM LIST_DROPDOWN_FILTER |
79071a5d4 Using database/sq... |
349 |
WHERE FILTERS_FIELD = ` + fmt.Sprintf("'%s'", filtersField)) |
8bc396eb9 reverted changes |
350 351 352 |
if err != nil { return resp, err } |
79071a5d4 Using database/sq... |
353 354 355 |
defer rows.Close() if rows.Next() { rows.Scan(&resp.FiltersField, &resp.ObjectType, &resp.IDField, &resp.LabelField) |
8bc396eb9 reverted changes |
356 |
} |
79071a5d4 Using database/sq... |
357 358 |
if rows.Err() != nil { return resp, rows.Err() |
8bc396eb9 reverted changes |
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
} return resp, nil } // sortFilters bubble sorts provided filters slice by position field. func sortFilters(filters []ListFilter) { done := false var temp ListFilter for !done { done = true for i := 0; i < len(filters)-1; i++ { if filters[i].Position > filters[i+1].Position { done = false temp = filters[i] filters[i] = filters[i+1] filters[i+1] = temp } } } } // getListGraph return list graph slice for the provided object type. |
79071a5d4 Using database/sq... |
381 |
func getListGraph(db *sql.DB, objType string) ([]ListGraph, error) { |
8bc396eb9 reverted changes |
382 |
resp := make([]ListGraph, 0) |
79071a5d4 Using database/sq... |
383 |
rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
384 385 |
OBJECT_TYPE, X_FIELD, Y_FIELD, GROUP_FIELD, LABEL FROM LIST_GRAPHS |
79071a5d4 Using database/sq... |
386 |
WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
8bc396eb9 reverted changes |
387 388 389 |
if err != nil { return resp, err } |
79071a5d4 Using database/sq... |
390 |
defer rows.Close() |
8bc396eb9 reverted changes |
391 |
|
79071a5d4 Using database/sq... |
392 393 394 395 |
var lg ListGraph for rows.Next() { rows.Scan(&lg.ObjectType, &lg.X, &lg.Y, &lg.GroupField, &lg.Label) resp = append(resp, lg) |
8bc396eb9 reverted changes |
396 |
} |
79071a5d4 Using database/sq... |
397 398 |
if rows.Err() != nil { return resp, rows.Err() |
8bc396eb9 reverted changes |
399 400 401 402 403 |
} return resp, nil } // getListOptions returns list options for the provided object type. |
79071a5d4 Using database/sq... |
404 |
func getListOptions(db *sql.DB, objType string) (ListOptions, error) { |
8bc396eb9 reverted changes |
405 |
var resp ListOptions |
79071a5d4 Using database/sq... |
406 |
rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
407 408 409 |
GLOBAL_FILTER, LOCAL_FILTER, REMOTE_FILTER, PAGINATION, PAGE_SIZE, PIVOT, DETAIL, TOTAL FROM LIST_CONFIG |
79071a5d4 Using database/sq... |
410 |
WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
8bc396eb9 reverted changes |
411 412 413 |
if err != nil { return ListOptions{}, err } |
79071a5d4 Using database/sq... |
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
defer rows.Close() if rows.Next() { var gfilter, lfilters, rfilters, pagination, pageSize, pivot, detail, total uint32 rows.Scan(&gfilter, &lfilters, &rfilters, &pagination, &pageSize, &pivot, &detail, &total) resp.GlobalFilter = gfilter != 0 resp.LocalFilters = lfilters != 0 resp.RemoteFilters = rfilters != 0 resp.Pagination = pagination != 0 resp.PageSize = pageSize resp.Pivot = pivot != 0 resp.Detail = detail != 0 resp.Total = total != 0 } if rows.Err() != nil { return ListOptions{}, rows.Err() |
8bc396eb9 reverted changes |
429 430 431 432 433 |
} return resp, nil } // getListParent returns list parent node slice for the provided object type. |
79071a5d4 Using database/sq... |
434 |
func getListParent(db *sql.DB, objType string) ([]ListParentNode, error) { |
8bc396eb9 reverted changes |
435 |
resp := make([]ListParentNode, 0) |
79071a5d4 Using database/sq... |
436 |
rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
437 438 |
PARENT_OBJECT_TYPE, PARENT_LABEL_FIELD, PARENT_FILTER_FIELD FROM LIST_CONFIG_CHILD |
79071a5d4 Using database/sq... |
439 |
WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
8bc396eb9 reverted changes |
440 441 442 |
if err != nil { return resp, err } |
79071a5d4 Using database/sq... |
443 |
defer rows.Close() |
8bc396eb9 reverted changes |
444 |
|
79071a5d4 Using database/sq... |
445 446 447 448 |
var pnode ListParentNode for rows.Next() { rows.Scan(&pnode.ObjectType, &pnode.LabelField, &pnode.FilterField) resp = append(resp, pnode) |
8bc396eb9 reverted changes |
449 |
} |
79071a5d4 Using database/sq... |
450 451 |
if rows.Err() != nil { return nil, rows.Err() |
8bc396eb9 reverted changes |
452 453 454 455 456 457 |
} return resp, nil } // getListPivot list pivot slice for the provided object type. |
79071a5d4 Using database/sq... |
458 |
func getListPivot(db *sql.DB, objType string) ([]ListPivot, error) { |
8bc396eb9 reverted changes |
459 |
resp := make([]ListPivot, 0) |
79071a5d4 Using database/sq... |
460 |
rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
461 462 |
OBJECT_TYPE, GROUP_FIELD, DISTINCT_FIELD, VALUE_FIELD FROM LIST_PIVOTS |
79071a5d4 Using database/sq... |
463 |
WHERE OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
8bc396eb9 reverted changes |
464 465 466 |
if err != nil { return resp, err } |
79071a5d4 Using database/sq... |
467 |
defer rows.Close() |
8bc396eb9 reverted changes |
468 |
|
79071a5d4 Using database/sq... |
469 470 471 472 |
var p ListPivot for rows.Next() { rows.Scan(&p.ObjectType, &p.GroupField, &p.DistinctField, &p.Value) resp = append(resp, p) |
8bc396eb9 reverted changes |
473 |
} |
79071a5d4 Using database/sq... |
474 475 |
if rows.Err() != nil { return nil, rows.Err() |
8bc396eb9 reverted changes |
476 477 478 479 480 481 |
} return resp, nil } // getListDetails returns list details for the provided object type. |
79071a5d4 Using database/sq... |
482 |
func getListDetails(db *sql.DB, objType string) (ListDetails, error) { |
8bc396eb9 reverted changes |
483 |
var resp ListDetails |
79071a5d4 Using database/sq... |
484 |
rows, err := db.Query(`SELECT |
8bc396eb9 reverted changes |
485 486 |
OBJECT_TYPE, PARENT_OBJECT_TYPE, PARENT_FILTER_FIELD, SINGLE_DETAIL FROM LIST_CONFIG_DETAIL |
79071a5d4 Using database/sq... |
487 |
WHERE PARENT_OBJECT_TYPE = ` + fmt.Sprintf("'%s'", objType)) |
8bc396eb9 reverted changes |
488 489 490 |
if err != nil { return resp, err } |
79071a5d4 Using database/sq... |
491 492 493 494 495 |
defer rows.Close() if rows.Next() { var singleDetail uint32 rows.Scan(&resp.ObjectType, &resp.ParentObjectType, &resp.ParentFilterField, &singleDetail) resp.SingleDetail = singleDetail != 0 |
8bc396eb9 reverted changes |
496 |
} |
79071a5d4 Using database/sq... |
497 498 |
if rows.Err() != nil { return resp, rows.Err() |
8bc396eb9 reverted changes |
499 500 501 502 |
} return resp, nil } |