From 8dbe745c3c68225cda3bf800460592e7f6184038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Thu, 22 Sep 2016 12:00:03 +0200 Subject: [PATCH] merged tables utility into JSON utility --- http_utility.go | 8 +-- json_utility.go | 153 ++++++++++++++++++++++++++++++++++++++++++------------ tables_utility.go | 132 ---------------------------------------------- 3 files changed, 122 insertions(+), 171 deletions(-) delete mode 100644 tables_utility.go diff --git a/http_utility.go b/http_utility.go index 2368972..fd30950 100644 --- a/http_utility.go +++ b/http_utility.go @@ -58,18 +58,12 @@ func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) { }) } -func DeliverPayload(w http.ResponseWriter, payload JSONPayload) { - json.NewEncoder(w).Encode(payload) - payload.Data = nil -} - //// //// HANDLER FUNC WRAPPER //// -// wrapHandlerFunc is as wrapper function for route handlers. // Sets common headers and checks for token validity. -func HandleFuncWrap(fn http.HandlerFunc) http.HandlerFunc { +func ProcessHeaders(fn http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { // @TODO: check Content-type header (must be application/json) // ctype := w.Header.Get("Content-Type") diff --git a/json_utility.go b/json_utility.go index 8ec4385..16c8b5d 100644 --- a/json_utility.go +++ b/json_utility.go @@ -3,8 +3,16 @@ package restutility import ( "net/http" "strings" + "encoding/json" + "errors" + "gopkg.in/rana/ora.v3" + "io" + "io/ioutil" + "fmt" ) +var allPayloads []Payload + type LangMap map[string]map[string]string type Field struct { @@ -14,17 +22,22 @@ type Field struct { Editable bool `json:"editable"` } -type JSONParams struct { - Lang LangMap - Fields []Field - IdField string - Correlations []CorrelationField `json:"correlation_fields"` +type CorrelationField struct { + Result string `json:"result"` + Elements []string `json:"elements"` + Type string `json:"type"` +} + +type Translation struct { + Language string `json:"language"` + FieldsLabels map[string]string `json:"fieldsLabels"` } -type JSONPayload struct { +type Payload struct { + Type string `json:"tableType"` Method string `json:"method"` Params map[string]string `json:"params"` - Lang LangMap `json:"lang"` + Lang []Translation `json:"lang"` Fields []Field `json:"fields"` Correlations []CorrelationField `json:"correlationFields"` IdField string `json:"idField"` @@ -32,38 +45,114 @@ type JSONPayload struct { Data interface{} `json:"data"` } -func NewJSONParams(lang LangMap, - fields []Field, - id string, - correlations []CorrelationField) JSONParams { +func NewPayload(r *http.Request, table string) Payload { + var pload Payload - var jp JSONParams + pload.Method = strings.ToLower(r.Method + " " + r.URL.Path) + pload.Params = make(map[string]string, 0) + pload.Lang = loadTranslations(allPayloads, table) + pload.Fields = loadFields(allPayloads, table) + pload.IdField = loadIdField(allPayloads, table) + pload.Correlations = loadCorrelations(allPayloads, table) - jp.Lang = lang - jp.Fields = fields - jp.IdField = id - jp.Correlations = correlations + return pload +} - return jp +func DeliverPayload(w http.ResponseWriter, payload Payload) { + json.NewEncoder(w).Encode(payload) + payload.Data = nil } -func NewJSONPayload(r *http.Request, table string) JSONPayload { - var obj JSONPayload - params := loadTable(table) - obj.Method = strings.ToLower(r.Method + " " + r.URL.Path) - obj.Params = make(map[string]string, 0) - obj.Lang = make(map[string]map[string]string, 0) - obj.Fields = make([]Field, 0) - obj.IdField = params.IdField - obj.Correlations = params.Correlations - - for k, m := range params.Lang { - obj.Lang[k] = m +func loadTranslations(payloads []Payload, id string) []Translation { + translations := make([]Translation, 0) + + for _, pload := range payloads { + if pload.Type == id { + for _, t := range pload.Lang { + //translations[t.Language] = t.FieldsLabels + translations = append(translations, Translation{ + Language: t.Language, + FieldsLabels: t.FieldsLabels, + }) + } + } } - for _, f := range params.Fields { - obj.Fields = append(obj.Fields, f) + + return translations +} + +func loadFields(payloads []Payload, id string) []Field { + fields := make([]Field, 0) + + for _, pload := range payloads { + if pload.Type == id{ + for _, f := range pload.Fields { + fields = append(fields, f) + } + } + } + + return fields +} + +func loadIdField(payloads []Payload, id string) string { + for _, pload := range payloads { + if pload.Type == id { + return pload.IdField + } + } + return "" +} + +func loadCorrelations(payloads []Payload, id string) []CorrelationField { + resp := make([]CorrelationField, 0) + + for _, pload := range payloads { + if pload.Type == id { + for _, f := range pload.Correlations { + resp = append(resp, f) + } + } + } + + return resp +} + +func InitTables(db *ora.Ses, project string) error { + jsonbuf, _ := fetchTablesConfig(db, EqualQuotes(project)) + json.Unmarshal(jsonbuf, &allPayloads) + if len(allPayloads) == 0 { + return errors.New("tables config is corrupt") + } + fmt.Printf("broj ucitanih tabela: %d\n", len(allPayloads)) + 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 obj + return bytes, nil } diff --git a/tables_utility.go b/tables_utility.go deleted file mode 100644 index 3b0cd21..0000000 --- a/tables_utility.go +++ /dev/null @@ -1,132 +0,0 @@ -package restutility - -import ( - "encoding/json" - "errors" - "gopkg.in/rana/ora.v3" - "io" - "io/ioutil" - "fmt" -) - -type TableConfig struct { - Tables []Table -} - -type Table struct { - TableType string `json:"tableType"` - Translations []TableTranslation `json:"translations"` - TableFields []Field `json:"tableFields"` - Correlations []CorrelationField `json:"correlationFields"` - IdField string `json:"idField"` -} - -type CorrelationField struct { - Result string `json:"result"` - Elements []string `json:"elements"` - Type string `json:"type"` -} - -type TableTranslation struct { - Language string `json:"language"` - FieldsLabels map[string]string `json:"fieldsLabels"` -} - -func (tl TableConfig) LoadTranslations(tableType string) LangMap { - translations := make(LangMap, 0) - - for _, table := range tl.Tables { - if tableType == table.TableType { - for _, t := range table.Translations { - translations[t.Language] = t.FieldsLabels - } - } - } - - return translations -} - -func (tl TableConfig) LoadFields(tableType string) []Field { - fields := make([]Field, 0) - - for _, table := range tl.Tables { - if tableType == table.TableType { - for _, f := range table.TableFields { - fields = append(fields, f) - } - } - } - - return fields -} - -func (tl TableConfig) LoadIdField(tableType string) string { - for _, table := range tl.Tables { - if tableType == table.TableType { - return table.IdField - } - } - return "" -} - -func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField { - resp := make([]CorrelationField, 0) - - for _, table := range tl.Tables { - if tableType == table.TableType { - for _, f := range table.Correlations { - resp = append(resp, f) - } - } - } - - return resp -} - -var _tables TableConfig -var _prevProject string - -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") - } - 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), - _tables.LoadIdField(table), - _tables.LoadCorrelations(table)) -} - -- 1.8.1.2