From 39765a4304025d6c7db7ef9ce0ffbc00bf3c744d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Wed, 21 Sep 2016 16:59:07 +0200 Subject: [PATCH] added list and select configs --- list_config.go | 521 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ select_config.go | 54 ++++++ tables_utility.go | 35 +++- 3 files changed, 609 insertions(+), 1 deletion(-) create mode 100644 list_config.go create mode 100644 select_config.go diff --git a/list_config.go b/list_config.go new file mode 100644 index 0000000..b75a24f --- /dev/null +++ b/list_config.go @@ -0,0 +1,521 @@ +package restutility + +import ( + "gopkg.in/rana/ora.v3" +) + +type ListOptions struct { + GlobalFilter bool `json:"globalFilter"` + LocalFilters bool `json:"localFilters"` + RemoteFilters bool `json:"remoteFilters"` + Pagination bool `json:"pagination"` + PageSize uint64 `json:"pageSize"` + Pivot bool `json:"pivot"` + Detail bool `json:"detail"` +} + +type ListFilter struct { + ObjectType string `json:"-"` + FiltersField string `json:"filtersField"` + DefaultValues string `json:"defaultValues"` + FiltersType string `json:"filtersType"` + FiltersLabel string `json:"filtersLabel"` +} + +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 ListChildNode struct { + ObjectType string `json:"objectType"` + LabelField string `json:"label"` + Icon string `json:"icon"` +} + +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"` + Children []ListChildNode `json:"children"` + Pivots []ListPivot `json:"pivots"` + Details ListDetails `json:"details"` +} + +func GetListConfigObjectIdField(db *ora.Ses, otype string) string { + var resp string + var err error + var stmt *ora.Stmt + + stmt, err = db.Prep(`SELECT + ID_FIELD + FROM LIST_CONFIG_ID_FIELD + WHERE OBJECT_TYPE = '` + otype + `'`, + ora.S) + + defer stmt.Close() + + if err != nil { + return "" + } + + rset, err := stmt.Qry() + if rset.Next() { + resp = rset.Row[0].(string) + } + + if rset.Err != nil { + return "" + } + + return resp +} + +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, + Children: nil, + } + + return list +} + +func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) { + resp := NewDefaultList(objType) + var err error + + err = setListParams(db, &resp, objType) + resp.Children, err = getListChildren(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 +} + +func setListParams(db *ora.Ses, list *ListConfig, objType string) error { + var err error + var stmt *ora.Stmt + query := `SELECT + OBJECT_TYPE, TITLE, LAZY_LOAD, INLINE_EDIT + FROM LIST_CONFIG + WHERE OBJECT_TYPE = '` + objType + `'` + + stmt, err = db.Prep(query, ora.S, ora.S, ora.U32, ora.U32) + if err != nil { + return err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return err + } + if rset.Next() { + otype := rset.Row[0].(string) + if otype != "" { + list.ObjectType = otype + } + + title := rset.Row[1].(string) + if title != "" { + list.Title = title + } + list.LazyLoad = rset.Row[2].(uint32) != 0 + list.InlineEdit = rset.Row[3].(uint32) != 0 + } + if rset.Err != nil { + return rset.Err + } + return nil +} + +func getListChildren(db *ora.Ses, objType string) ([]ListChildNode, error) { + resp := make([]ListChildNode, 0) + var err error + var stmt *ora.Stmt + query := `SELECT + OBJECT_TYPE, LABEL, ICON + FROM LIST_CONFIG_CHILD + WHERE PARENT_OBJECT_TYPE = '` + objType + `'` + + stmt, err = db.Prep(query, ora.S, ora.S, ora.S) + if err != nil { + return resp, err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return resp, err + } + for rset.Next() { + resp = append(resp, ListChildNode{ + ObjectType: rset.Row[0].(string), + LabelField: rset.Row[1].(string), + Icon: rset.Row[2].(string), + }) + } + if rset.Err != nil { + return nil, rset.Err + } + + return resp, nil +} + +func getListActions(db *ora.Ses, objType string) (ListActions, error) { + var resp ListActions + var err error + var stmt *ora.Stmt + query := `SELECT + ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE, ACTION_EXPORT, + ACTION_PRINT, ACTION_GRAPH + FROM LIST_CONFIG + WHERE OBJECT_TYPE = '` + objType + `'` + + stmt, err = db.Prep(query, ora.U32, ora.U32, ora.U32, ora.U32, + ora.U32, ora.U32) + if err != nil { + return ListActions{}, err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return ListActions{}, err + } + if rset.Next() { + resp.Create = rset.Row[0].(uint32) != 0 + resp.Update = rset.Row[1].(uint32) != 0 + resp.Delete = rset.Row[2].(uint32) != 0 + resp.Export = rset.Row[3].(uint32) != 0 + resp.Print = rset.Row[4].(uint32) != 0 + resp.Graph = rset.Row[5].(uint32) != 0 + } + if rset.Err != nil { + return ListActions{}, rset.Err + } + return resp, nil +} + +func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) { + resp := make([]ListFilter, 0) + filtersField, err := getFilterFields(db, objType) + if err != nil { + return nil, err + } + for _, field := range filtersField { + filters, _ := getFiltersByFilterField(db, field) + for _, filter := range filters { + resp = append(resp, ListFilter{ + ObjectType: objType, + FiltersField: field, + DefaultValues: filter.DefaultValues, + FiltersLabel: filter.Label, + FiltersType: filter.Type, + }) + } + } + + return resp, nil +} + +func getFilterFields(db *ora.Ses, objType string) ([]string, error) { + filtersField := make([]string, 0) + var err error + var stmt *ora.Stmt + query := `SELECT + FILTERS_FIELD + FROM LIST_CONFIG_FILTERS + WHERE OBJECT_TYPE = '` + objType + `'` + + stmt, err = db.Prep(query, ora.S) + if err != nil { + return nil, err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return nil, err + } + for rset.Next() { + filtersField = append(filtersField, rset.Row[0].(string)) + } + if rset.Err != nil { + return nil, rset.Err + } + return filtersField, nil +} + +type TempFilter struct { + DefaultValues string + Label string + Type string +} + +func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]TempFilter, error) { + resp := make([]TempFilter, 0) + var err error + var stmt *ora.Stmt + query := `SELECT + FILTERS_TYPE, FILTERS_LABEL, DEFAULT_VALUES + FROM LIST_FILTERS_FIELD + WHERE FILTERS_FIELD = '` + filtersField + `'` + + stmt, err = db.Prep(query, ora.S, ora.S, ora.S) + if err != nil { + return resp, err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return resp, err + } + for rset.Next() { + resp = append(resp, TempFilter{ + Type: rset.Row[0].(string), + Label: rset.Row[1].(string), + DefaultValues: rset.Row[2].(string), + }) + } + if rset.Err != nil { + return resp, rset.Err + } + return resp, nil +} + +func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) { + resp := make([]ListGraph, 0) + var err error + var stmt *ora.Stmt + query := `SELECT + OBJECT_TYPE, X_FIELD, Y_FIELD, GROUP_FIELD, LABEL + FROM LIST_GRAPHS + WHERE OBJECT_TYPE = '` + objType + `'` + + stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S) + if err != nil { + return resp, err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return resp, err + } + for rset.Next() { + resp = append(resp, ListGraph{ + ObjectType: rset.Row[0].(string), + X: rset.Row[1].(string), + Y: rset.Row[2].(string), + GroupField: rset.Row[3].(string), + Label: rset.Row[4].(string), + }) + } + if rset.Err != nil { + return resp, rset.Err + } + return resp, nil +} + +func getListOptions(db *ora.Ses, objType string) (ListOptions, error) { + var resp ListOptions + var err error + var stmt *ora.Stmt + query := `SELECT + GLOBAL_FILTER, LOCAL_FILTER, REMOTE_FILTER, PAGINATION, + PAGE_SIZE, PIVOT, DETAIL + FROM LIST_CONFIG + WHERE OBJECT_TYPE = '` + objType + `'` + + stmt, err = db.Prep(query, ora.U32, ora.U32, ora.U32, ora.U32, + ora.U64, ora.U64, ora.U32) + if err != nil { + return ListOptions{}, err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return ListOptions{}, err + } + if rset.Next() { + resp.GlobalFilter = rset.Row[0].(uint32) != 0 + resp.LocalFilters = rset.Row[1].(uint32) != 0 + resp.RemoteFilters = rset.Row[2].(uint32) != 0 + resp.Pagination = rset.Row[3].(uint32) != 0 + resp.PageSize = rset.Row[4].(uint64) + resp.Pivot = rset.Row[5].(uint64) != 0 + resp.Detail = rset.Row[6].(uint32) != 0 + } + if rset.Err != nil { + return ListOptions{}, rset.Err + } + return resp, nil +} + +func getListParent(db *ora.Ses, objType string) ([]ListParentNode, error) { + resp := make([]ListParentNode, 0) + var err error + var stmt *ora.Stmt + query := `SELECT + PARENT_OBJECT_TYPE, PARENT_LABEL_FIELD, PARENT_FILTER_FIELD + FROM LIST_CONFIG_CHILD + WHERE OBJECT_TYPE = '` + objType + `'` + + stmt, err = db.Prep(query, ora.S, ora.S, ora.S) + if err != nil { + return resp, err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return resp, err + } + for rset.Next() { + resp = append(resp, ListParentNode{ + ObjectType: rset.Row[0].(string), + LabelField: rset.Row[1].(string), + FilterField: rset.Row[2].(string), + }) + } + if rset.Err != nil { + return nil, rset.Err + } + + return resp, nil +} + +func getListPivot(db *ora.Ses, objType string) ([]ListPivot, error) { + resp := make([]ListPivot, 0) + var err error + var stmt *ora.Stmt + query := `SELECT + OBJECT_TYPE, GROUP_FIELD, DISTINCT_FIELD, VALUE_FIELD + FROM LIST_PIVOTS + WHERE OBJECT_TYPE = '` + objType + `'` + + stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S) + if err != nil { + return resp, err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return resp, err + } + for rset.Next() { + resp = append(resp, ListPivot{ + ObjectType: rset.Row[0].(string), + GroupField: rset.Row[1].(string), + DistinctField: rset.Row[2].(string), + Value: rset.Row[3].(string), + }) + } + if rset.Err != nil { + return nil, rset.Err + } + + return resp, nil +} + +func getListDetails(db *ora.Ses, objType string) (ListDetails, error) { + var resp ListDetails + var err error + var stmt *ora.Stmt + query := `SELECT + OBJECT_TYPE, PARENT_OBJECT_TYPE, PARENT_FILTER_FIELD, SINGLE_DETAIL + FROM LIST_CONFIG_DETAIL + WHERE PARENT_OBJECT_TYPE = '` + objType + `'` + + stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.U32) + if err != nil { + return resp, err + } + defer stmt.Close() + + rset, err := stmt.Qry() + if err != nil { + return resp, err + } + if rset.Next() { + resp.ObjectType = rset.Row[0].(string) + resp.ParentObjectType = rset.Row[1].(string) + resp.ParentFilterField = rset.Row[2].(string) + resp.SingleDetail = rset.Row[3].(uint32) != 0 + } + if rset.Err != nil { + return resp, rset.Err + } + + return resp, nil +} diff --git a/select_config.go b/select_config.go new file mode 100644 index 0000000..c9af7d0 --- /dev/null +++ b/select_config.go @@ -0,0 +1,54 @@ +package restutility + +import ( + "gopkg.in/rana/ora.v3" +) + +type SelectConfig struct { + ListObjType string `json:"listObjectType"` + ObjType string `json:"objectType"` + Type string `json:"type"` + IdField string `json:"idField"` + LabelField string `json:"labelField"` + ValueField string `json:"valueField"` +} + +func GetSelectConfig(db *ora.Ses, otype string) ([]SelectConfig, error) { + resp := make([]SelectConfig, 0) + var err error + var stmt *ora.Stmt + query := `SELECT a.LIST_OBJECT_TYPE, a.OBJECT_TYPE, a.ID_FIELD, + a.LABEL_FIELD, a.TYPE, b.FIELD + FROM LIST_SELECT_CONFIG a, LIST_VALUE_FIELD b + WHERE a.LIST_OBJECT_TYPE` + otype + ` + AND b.LIST_TYPE = a.LIST_OBJECT_TYPE + AND b.OBJECT_TYPE = a.OBJECT_TYPE` + + stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S, ora.S, + ora.S) + defer stmt.Close() + if err != nil { + return nil, err + } + + rset, err := stmt.Qry() + if err != nil { + return nil, err + } + for rset.Next() { + resp = append(resp, SelectConfig{ + ListObjType: rset.Row[0].(string), + ObjType: rset.Row[1].(string), + IdField: rset.Row[2].(string), + LabelField: rset.Row[3].(string), + Type: rset.Row[4].(string), + ValueField: rset.Row[5].(string), + }) + } + if rset.Err != nil { + return nil, rset.Err + } + + return resp, nil + +} diff --git a/tables_utility.go b/tables_utility.go index a5f52d3..3b0cd21 100644 --- a/tables_utility.go +++ b/tables_utility.go @@ -3,6 +3,10 @@ package restutility import ( "encoding/json" "errors" + "gopkg.in/rana/ora.v3" + "io" + "io/ioutil" + "fmt" ) type TableConfig struct { @@ -82,7 +86,8 @@ func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField { var _tables TableConfig var _prevProject string -func InitTables(jsonbuf []byte) error { +func InitTables(db *ora.Ses, project string) error { + jsonbuf, _ := fetchTablesConfig(db, EqualQuotes(project)) json.Unmarshal(jsonbuf, &_tables.Tables) if len(_tables.Tables) == 0 { return errors.New("tables config is corrupt") @@ -90,6 +95,34 @@ func InitTables(jsonbuf []byte) error { return nil } +func fetchTablesConfig(db *ora.Ses, project string) ([]byte, error) { + stmt, err := db.Prep(`SELECT + JSON_CLOB + FROM TABLES_CONFIG + WHERE PROJEKAT` + project, ora.S) + defer stmt.Close() + + if err != nil { + return nil, err + } + + rset, err := stmt.Qry() + if err != nil { + return nil, err + } + + bytes := make([]byte, 0) + if rset.Next() { + lob := rset.Row[0].(io.Reader) + bytes, err = ioutil.ReadAll(lob) + if err != nil { + fmt.Printf("mega error: %v\n", err) + } + } + + return bytes, nil +} + func loadTable(table string) JSONParams { return NewJSONParams(_tables.LoadTranslations(table), _tables.LoadFields(table), -- 1.8.1.2