Commit 8dbe745c3c68225cda3bf800460592e7f6184038

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

merged tables utility into JSON utility

... ... @@ -58,18 +58,12 @@ func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) {
58 58 })
59 59 }
60 60  
61   -func DeliverPayload(w http.ResponseWriter, payload JSONPayload) {
62   - json.NewEncoder(w).Encode(payload)
63   - payload.Data = nil
64   -}
65   -
66 61 ////
67 62 //// HANDLER FUNC WRAPPER
68 63 ////
69 64  
70   -// wrapHandlerFunc is as wrapper function for route handlers.
71 65 // Sets common headers and checks for token validity.
72   -func HandleFuncWrap(fn http.HandlerFunc) http.HandlerFunc {
  66 +func ProcessHeaders(fn http.HandlerFunc) http.HandlerFunc {
73 67 return func(w http.ResponseWriter, req *http.Request) {
74 68 // @TODO: check Content-type header (must be application/json)
75 69 // ctype := w.Header.Get("Content-Type")
... ...
... ... @@ -3,8 +3,16 @@ package restutility
3 3 import (
4 4 "net/http"
5 5 "strings"
  6 + "encoding/json"
  7 + "errors"
  8 + "gopkg.in/rana/ora.v3"
  9 + "io"
  10 + "io/ioutil"
  11 + "fmt"
6 12 )
7 13  
  14 +var allPayloads []Payload
  15 +
8 16 type LangMap map[string]map[string]string
9 17  
10 18 type Field struct {
... ... @@ -14,17 +22,22 @@ type Field struct {
14 22 Editable bool `json:"editable"`
15 23 }
16 24  
17   -type JSONParams struct {
18   - Lang LangMap
19   - Fields []Field
20   - IdField string
21   - Correlations []CorrelationField `json:"correlation_fields"`
  25 +type CorrelationField struct {
  26 + Result string `json:"result"`
  27 + Elements []string `json:"elements"`
  28 + Type string `json:"type"`
  29 +}
  30 +
  31 +type Translation struct {
  32 + Language string `json:"language"`
  33 + FieldsLabels map[string]string `json:"fieldsLabels"`
22 34 }
23 35  
24   -type JSONPayload struct {
  36 +type Payload struct {
  37 + Type string `json:"tableType"`
25 38 Method string `json:"method"`
26 39 Params map[string]string `json:"params"`
27   - Lang LangMap `json:"lang"`
  40 + Lang []Translation `json:"lang"`
28 41 Fields []Field `json:"fields"`
29 42 Correlations []CorrelationField `json:"correlationFields"`
30 43 IdField string `json:"idField"`
... ... @@ -32,38 +45,114 @@ type JSONPayload struct {
32 45 Data interface{} `json:"data"`
33 46 }
34 47  
35   -func NewJSONParams(lang LangMap,
36   - fields []Field,
37   - id string,
38   - correlations []CorrelationField) JSONParams {
  48 +func NewPayload(r *http.Request, table string) Payload {
  49 + var pload Payload
39 50  
40   - var jp JSONParams
  51 + pload.Method = strings.ToLower(r.Method + " " + r.URL.Path)
  52 + pload.Params = make(map[string]string, 0)
  53 + pload.Lang = loadTranslations(allPayloads, table)
  54 + pload.Fields = loadFields(allPayloads, table)
  55 + pload.IdField = loadIdField(allPayloads, table)
  56 + pload.Correlations = loadCorrelations(allPayloads, table)
41 57  
42   - jp.Lang = lang
43   - jp.Fields = fields
44   - jp.IdField = id
45   - jp.Correlations = correlations
  58 + return pload
  59 +}
46 60  
47   - return jp
  61 +func DeliverPayload(w http.ResponseWriter, payload Payload) {
  62 + json.NewEncoder(w).Encode(payload)
  63 + payload.Data = nil
48 64 }
49 65  
50   -func NewJSONPayload(r *http.Request, table string) JSONPayload {
51   - var obj JSONPayload
52   - params := loadTable(table)
53   - obj.Method = strings.ToLower(r.Method + " " + r.URL.Path)
54   - obj.Params = make(map[string]string, 0)
55   - obj.Lang = make(map[string]map[string]string, 0)
56   - obj.Fields = make([]Field, 0)
57   - obj.IdField = params.IdField
58   - obj.Correlations = params.Correlations
59   -
60   - for k, m := range params.Lang {
61   - obj.Lang[k] = m
  66 +func loadTranslations(payloads []Payload, id string) []Translation {
  67 + translations := make([]Translation, 0)
  68 +
  69 + for _, pload := range payloads {
  70 + if pload.Type == id {
  71 + for _, t := range pload.Lang {
  72 + //translations[t.Language] = t.FieldsLabels
  73 + translations = append(translations, Translation{
  74 + Language: t.Language,
  75 + FieldsLabels: t.FieldsLabels,
  76 + })
  77 + }
  78 + }
62 79 }
63   - for _, f := range params.Fields {
64   - obj.Fields = append(obj.Fields, f)
  80 +
  81 + return translations
  82 +}
  83 +
  84 +func loadFields(payloads []Payload, id string) []Field {
  85 + fields := make([]Field, 0)
  86 +
  87 + for _, pload := range payloads {
  88 + if pload.Type == id{
  89 + for _, f := range pload.Fields {
  90 + fields = append(fields, f)
  91 + }
  92 + }
  93 + }
  94 +
  95 + return fields
  96 +}
  97 +
  98 +func loadIdField(payloads []Payload, id string) string {
  99 + for _, pload := range payloads {
  100 + if pload.Type == id {
  101 + return pload.IdField
  102 + }
  103 + }
  104 + return ""
  105 +}
  106 +
  107 +func loadCorrelations(payloads []Payload, id string) []CorrelationField {
  108 + resp := make([]CorrelationField, 0)
  109 +
  110 + for _, pload := range payloads {
  111 + if pload.Type == id {
  112 + for _, f := range pload.Correlations {
  113 + resp = append(resp, f)
  114 + }
  115 + }
  116 + }
  117 +
  118 + return resp
  119 +}
  120 +
  121 +func InitTables(db *ora.Ses, project string) error {
  122 + jsonbuf, _ := fetchTablesConfig(db, EqualQuotes(project))
  123 + json.Unmarshal(jsonbuf, &allPayloads)
  124 + if len(allPayloads) == 0 {
  125 + return errors.New("tables config is corrupt")
  126 + }
  127 + fmt.Printf("broj ucitanih tabela: %d\n", len(allPayloads))
  128 + return nil
  129 +}
  130 +
  131 +func fetchTablesConfig(db *ora.Ses, project string) ([]byte, error) {
  132 + stmt, err := db.Prep(`SELECT
  133 + JSON_CLOB
  134 + FROM TABLES_CONFIG
  135 + WHERE PROJEKAT` + project, ora.S)
  136 + defer stmt.Close()
  137 +
  138 + if err != nil {
  139 + return nil, err
  140 + }
  141 +
  142 + rset, err := stmt.Qry()
  143 + if err != nil {
  144 + return nil, err
  145 + }
  146 +
  147 + bytes := make([]byte, 0)
  148 + if rset.Next() {
  149 + lob := rset.Row[0].(io.Reader)
  150 + bytes, err = ioutil.ReadAll(lob)
  151 + if err != nil {
  152 + fmt.Printf("mega error: %v\n", err)
  153 + }
65 154 }
66 155  
67   - return obj
  156 + return bytes, nil
68 157 }
69 158  
... ...
tables_utility.go
... ... @@ -1,132 +0,0 @@
1   -package restutility
2   -
3   -import (
4   - "encoding/json"
5   - "errors"
6   - "gopkg.in/rana/ora.v3"
7   - "io"
8   - "io/ioutil"
9   - "fmt"
10   -)
11   -
12   -type TableConfig struct {
13   - Tables []Table
14   -}
15   -
16   -type Table struct {
17   - TableType string `json:"tableType"`
18   - Translations []TableTranslation `json:"translations"`
19   - TableFields []Field `json:"tableFields"`
20   - Correlations []CorrelationField `json:"correlationFields"`
21   - IdField string `json:"idField"`
22   -}
23   -
24   -type CorrelationField struct {
25   - Result string `json:"result"`
26   - Elements []string `json:"elements"`
27   - Type string `json:"type"`
28   -}
29   -
30   -type TableTranslation struct {
31   - Language string `json:"language"`
32   - FieldsLabels map[string]string `json:"fieldsLabels"`
33   -}
34   -
35   -func (tl TableConfig) LoadTranslations(tableType string) LangMap {
36   - translations := make(LangMap, 0)
37   -
38   - for _, table := range tl.Tables {
39   - if tableType == table.TableType {
40   - for _, t := range table.Translations {
41   - translations[t.Language] = t.FieldsLabels
42   - }
43   - }
44   - }
45   -
46   - return translations
47   -}
48   -
49   -func (tl TableConfig) LoadFields(tableType string) []Field {
50   - fields := make([]Field, 0)
51   -
52   - for _, table := range tl.Tables {
53   - if tableType == table.TableType {
54   - for _, f := range table.TableFields {
55   - fields = append(fields, f)
56   - }
57   - }
58   - }
59   -
60   - return fields
61   -}
62   -
63   -func (tl TableConfig) LoadIdField(tableType string) string {
64   - for _, table := range tl.Tables {
65   - if tableType == table.TableType {
66   - return table.IdField
67   - }
68   - }
69   - return ""
70   -}
71   -
72   -func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField {
73   - resp := make([]CorrelationField, 0)
74   -
75   - for _, table := range tl.Tables {
76   - if tableType == table.TableType {
77   - for _, f := range table.Correlations {
78   - resp = append(resp, f)
79   - }
80   - }
81   - }
82   -
83   - return resp
84   -}
85   -
86   -var _tables TableConfig
87   -var _prevProject string
88   -
89   -func InitTables(db *ora.Ses, project string) error {
90   - jsonbuf, _ := fetchTablesConfig(db, EqualQuotes(project))
91   - json.Unmarshal(jsonbuf, &_tables.Tables)
92   - if len(_tables.Tables) == 0 {
93   - return errors.New("tables config is corrupt")
94   - }
95   - return nil
96   -}
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   -
126   -func loadTable(table string) JSONParams {
127   - return NewJSONParams(_tables.LoadTranslations(table),
128   - _tables.LoadFields(table),
129   - _tables.LoadIdField(table),
130   - _tables.LoadCorrelations(table))
131   -}
132   -