From 7d3deb50df481b887b4f3b40d170a5cf7e15aa91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Thu, 29 Sep 2016 14:05:16 +0200 Subject: [PATCH] modified list_config with navigation (old children) --- auth_utility.go | 11 ++++---- json_utility.go | 8 +++--- list_config.go | 80 ++++++++++++++++++++++++++++++++++++++------------------- 3 files changed, 63 insertions(+), 36 deletions(-) diff --git a/auth_utility.go b/auth_utility.go index 6d0a358..3526f80 100644 --- a/auth_utility.go +++ b/auth_utility.go @@ -10,15 +10,15 @@ import ( "github.com/dgrijalva/jwt-go" ) -const OneDay = time.Hour*24 -const OneWeek = OneDay*7 +const OneDay = time.Hour*24 +const OneWeek = OneDay*7 const saltSize = 32 const appName = "korisnicki-centar" const secret = "korisnicki-centar-api" type TokenClaims struct { Username string `json:"username"` - Role string `json:"role"` + Role string `json:"role"` jwt.StandardClaims } @@ -29,7 +29,6 @@ type CredentialsStruct struct { func GenerateSalt() (string, error) { salt := "" - rawsalt := make([]byte, saltSize) _, err := rand.Read(rawsalt) if err != nil { @@ -72,7 +71,7 @@ func HashMessage(message string, presalt string) (string, string, error) { } func IssueAPIToken(username, role string) (string, error) { - var apiToken string + var apiToken string var err error if err != nil { @@ -97,7 +96,7 @@ func IssueAPIToken(username, role string) (string, error) { } func RefreshAPIToken(tokenString string) (string, error) { - var newToken string + var newToken string tokenString = strings.TrimPrefix(tokenString, "Bearer ") token, err := parseTokenFunc(tokenString) if err != nil { diff --git a/json_utility.go b/json_utility.go index 8de8565..b5b0a7d 100644 --- a/json_utility.go +++ b/json_utility.go @@ -31,6 +31,8 @@ type Translation struct { FieldsLabels map[string]string `json:"fieldsLabels"` } +// Type is not required in payload. This is only a bridge between ORACLE CLOB and +// Payload type. type payloadBuff struct { Type string `json:"tableType"` Method string `json:"method"` @@ -131,7 +133,7 @@ func loadCorrelations(id string) []CorrelationField { } func InitTables(db *ora.Ses, project string) error { - jsonbuf, _ := fetchTablesConfig(db, EqualQuotes(project)) + jsonbuf, _ := fetchJSON(db, EqualQuotes(project)) json.Unmarshal(jsonbuf, &allPayloads) if len(allPayloads) == 0 { return errors.New("tables config is corrupt") @@ -139,7 +141,7 @@ func InitTables(db *ora.Ses, project string) error { return nil } -func fetchTablesConfig(db *ora.Ses, project string) ([]byte, error) { +func fetchJSON(db *ora.Ses, project string) ([]byte, error) { stmt, err := db.Prep(`SELECT JSON_CLOB FROM TABLES_CONFIG @@ -160,8 +162,8 @@ func fetchTablesConfig(db *ora.Ses, project string) ([]byte, error) { lob := rset.Row[0].(io.Reader) bytes, err = ioutil.ReadAll(lob) if err != nil { + // Ignore for now, it's some weird streaming read/write LOB error. // TODO: Find a fix for this. - // Ignore, it's some weird streaming read/write LOB error. //return nil, err } } diff --git a/list_config.go b/list_config.go index e105379..e45583d 100644 --- a/list_config.go +++ b/list_config.go @@ -15,6 +15,7 @@ type ListOptions struct { } type ListFilter struct { + Position uint32 `json:"-"` ObjectType string `json:"-"` FiltersField string `json:"filtersField"` DefaultValues string `json:"defaultValues"` @@ -48,10 +49,12 @@ type ListActions struct { Graph bool `json:"graph"` } -type ListChildNode struct { - ObjectType string `json:"objectType"` - LabelField string `json:"label"` - Icon string `json:"icon"` +type ListNavNode struct { + ObjectType string `json:"objectType"` + LabelField string `json:"label"` + Icon string `json:"icon"` + ParentObjectType string `json:"parentObjectType"` + ParentIdField string `json:"parentIdField"` } type ListParentNode struct { @@ -84,7 +87,7 @@ type ListConfig struct { Graphs []ListGraph `json:"graphs"` Actions ListActions `json:"actions"` Parent []ListParentNode `json:"parent"` - Children []ListChildNode `json:"children"` + Navigation []ListNavNode `json:"navigation"` Pivots []ListPivot `json:"pivots"` Details ListDetails `json:"details"` } @@ -94,7 +97,7 @@ func GetListConfig(db *ora.Ses, objType string) (ListConfig, error) { var err error err = setListParams(db, &resp, objType) - resp.Children, err = getListChildren(db, 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) @@ -161,7 +164,7 @@ func NewDefaultList(objType string) ListConfig { Graph: false, }, Parent: nil, - Children: nil, + Navigation: nil, } return list @@ -204,16 +207,18 @@ func setListParams(db *ora.Ses, list *ListConfig, objType string) error { return nil } -func getListChildren(db *ora.Ses, objType string) ([]ListChildNode, error) { - resp := make([]ListChildNode, 0) +func getListNavigation(db *ora.Ses, objType string) ([]ListNavNode, error) { + resp := make([]ListNavNode, 0) var err error var stmt *ora.Stmt query := `SELECT - OBJECT_TYPE, LABEL, ICON - FROM LIST_CONFIG_CHILD - WHERE PARENT_OBJECT_TYPE = '` + objType + `'` + a.OBJECT_TYPE, a.LABEL, a.ICON, b.LIST_OBJECT_TYPE, b.PARENT_ID_FIELD, b.RB + FROM LIST_CONFIG_CHILD a + JOIN LIST_CONFIG_NAVIGATION b ON a.PARENT_CHILD_ID = b.PARENT_CHILD_ID + WHERE a.PARENT_OBJECT_TYPE = '`+objType+`' + ORDER BY b.RB ASC` - stmt, err = db.Prep(query, ora.S, ora.S, ora.S) + stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S, ora.S) if err != nil { return resp, err } @@ -224,10 +229,12 @@ func getListChildren(db *ora.Ses, objType string) ([]ListChildNode, error) { return resp, err } for rset.Next() { - resp = append(resp, ListChildNode{ + resp = append(resp, ListNavNode{ ObjectType: rset.Row[0].(string), LabelField: rset.Row[1].(string), Icon: rset.Row[2].(string), + ParentObjectType: rset.Row[3].(string), + ParentIdField: rset.Row[4].(string), }) } if rset.Err != nil { @@ -274,14 +281,15 @@ func getListActions(db *ora.Ses, objType string) (ListActions, error) { func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) { resp := make([]ListFilter, 0) - filtersField, err := getFilterFields(db, objType) + filtersFields, err := getFilterFieldsAndPosition(db, objType) if err != nil { return nil, err } - for _, field := range filtersField { + 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 @@ -297,19 +305,21 @@ func getListFilters(db *ora.Ses, objType string) ([]ListFilter, error) { } } + sortFilters(resp) + return resp, nil } -func getFilterFields(db *ora.Ses, objType string) ([]string, error) { - filtersField := make([]string, 0) +func getFilterFieldsAndPosition(db *ora.Ses, objType string) (map[string]uint32, error) { + filtersField := make(map[string]uint32, 0) var err error var stmt *ora.Stmt query := `SELECT - FILTERS_FIELD + FILTERS_FIELD, RB FROM LIST_CONFIG_FILTERS WHERE OBJECT_TYPE = '` + objType + `'` - stmt, err = db.Prep(query, ora.S) + stmt, err = db.Prep(query, ora.S, ora.U32) if err != nil { return nil, err } @@ -320,7 +330,7 @@ func getFilterFields(db *ora.Ses, objType string) ([]string, error) { return nil, err } for rset.Next() { - filtersField = append(filtersField, rset.Row[0].(string)) + filtersField[rset.Row[0].(string)] = rset.Row[1].(uint32) } if rset.Err != nil { return nil, rset.Err @@ -328,14 +338,14 @@ func getFilterFields(db *ora.Ses, objType string) ([]string, error) { return filtersField, nil } -type TempFilter struct { +type _filter struct { DefaultValues string - Label string - Type string + Label string + Type string } -func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]TempFilter, error) { - resp := make([]TempFilter, 0) +func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]_filter, error) { + resp := make([]_filter, 0) var err error var stmt *ora.Stmt query := `SELECT @@ -354,7 +364,7 @@ func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]TempFilter, er return resp, err } for rset.Next() { - resp = append(resp, TempFilter{ + resp = append(resp, _filter{ Type: rset.Row[0].(string), Label: rset.Row[1].(string), DefaultValues: rset.Row[2].(string), @@ -397,6 +407,22 @@ func getFilterDropdownConfig(db *ora.Ses, filtersField string) (Dropdown, error) return resp, nil } +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 + } + } + } +} + func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) { resp := make([]ListGraph, 0) var err error -- 1.8.1.2