Commit 8bec68a1e83749a81074acbe3fac31ace729a0bd

Authored by Marko Tikvić
1 parent 2d79a4120f
Exists in master and in 1 other branch v2

added reload tables as a separate func

Showing 1 changed file with 7 additions and 0 deletions   Show diff stats
1 package webutility 1 package webutility
2 2
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 "errors" 5 "errors"
6 "io" 6 "io"
7 "net/http" 7 "net/http"
8 "sync" 8 "sync"
9 9
10 "gopkg.in/rana/ora.v4" 10 "gopkg.in/rana/ora.v4"
11 ) 11 )
12 12
13 var mu = &sync.Mutex{} 13 var mu = &sync.Mutex{}
14 var payloads []payloadBuff 14 var payloads []payloadBuff
15 15
16 type LangMap map[string]map[string]string 16 type LangMap map[string]map[string]string
17 17
18 type Field struct { 18 type Field struct {
19 Parameter string `json:"param"` 19 Parameter string `json:"param"`
20 Type string `json:"type"` 20 Type string `json:"type"`
21 Visible bool `json:"visible"` 21 Visible bool `json:"visible"`
22 Editable bool `json:"editable"` 22 Editable bool `json:"editable"`
23 } 23 }
24 24
25 type CorrelationField struct { 25 type CorrelationField struct {
26 Result string `json:"result"` 26 Result string `json:"result"`
27 Elements []string `json:"elements"` 27 Elements []string `json:"elements"`
28 Type string `json:"type"` 28 Type string `json:"type"`
29 } 29 }
30 30
31 type Translation struct { 31 type Translation struct {
32 Language string `json:"language"` 32 Language string `json:"language"`
33 FieldsLabels map[string]string `json:"fieldsLabels"` 33 FieldsLabels map[string]string `json:"fieldsLabels"`
34 } 34 }
35 35
36 type payloadBuff struct { 36 type payloadBuff struct {
37 Type string `json:"tableType"` 37 Type string `json:"tableType"`
38 Method string `json:"method"` 38 Method string `json:"method"`
39 Params map[string]string `json:"params"` 39 Params map[string]string `json:"params"`
40 Lang []Translation `json:"lang"` 40 Lang []Translation `json:"lang"`
41 Fields []Field `json:"fields"` 41 Fields []Field `json:"fields"`
42 Correlations []CorrelationField `json:"correlationFields"` 42 Correlations []CorrelationField `json:"correlationFields"`
43 IdField string `json:"idField"` 43 IdField string `json:"idField"`
44 44
45 // Data can only hold slices of any type. It can't be used for itteration 45 // Data can only hold slices of any type. It can't be used for itteration
46 Data interface{} `json:"data"` 46 Data interface{} `json:"data"`
47 } 47 }
48 48
49 type Payload struct { 49 type Payload struct {
50 Method string `json:"method"` 50 Method string `json:"method"`
51 Params map[string]string `json:"params"` 51 Params map[string]string `json:"params"`
52 Lang []Translation `json:"lang"` 52 Lang []Translation `json:"lang"`
53 Fields []Field `json:"fields"` 53 Fields []Field `json:"fields"`
54 Correlations []CorrelationField `json:"correlationFields"` 54 Correlations []CorrelationField `json:"correlationFields"`
55 IdField string `json:"idField"` 55 IdField string `json:"idField"`
56 56
57 // Data contains JSON payload. 57 // Data contains JSON payload.
58 // It can't be used for itteration 58 // It can't be used for itteration
59 Data interface{} `json:"data"` 59 Data interface{} `json:"data"`
60 } 60 }
61 61
62 // InitTables loads all payloads in the payloads variable. 62 // InitTables loads all payloads in the payloads variable.
63 // Returns an error if it fails. 63 // Returns an error if it fails.
64 func InitTables(db *ora.Ses, project string) error { 64 func InitTables(db *ora.Ses, project string) error {
65 jsonbuf, err := fetchJSON(db, project) 65 jsonbuf, err := fetchJSON(db, project)
66 if err != nil { 66 if err != nil {
67 return err 67 return err
68 } 68 }
69 69
70 mu.Lock() 70 mu.Lock()
71 defer mu.Unlock() 71 defer mu.Unlock()
72 json.Unmarshal(jsonbuf, &payloads) 72 json.Unmarshal(jsonbuf, &payloads)
73 if len(payloads) == 0 { 73 if len(payloads) == 0 {
74 return errors.New("tables config is corrupt") 74 return errors.New("tables config is corrupt")
75 } 75 }
76 return nil 76 return nil
77 } 77 }
78 78
79 // ReloadTables reloads all payloads in the payloads variable.
80 // Returns an error if it fails.
81 func ReloadTables(db *ora.Ses, project string) error {
82 payloads = make([]payloadBuff, 0)
83 return InitTables(db, project)
84 }
85
79 // DecodeJSON decodes JSON data from r to v. 86 // DecodeJSON decodes JSON data from r to v.
80 // Returns an error if it fails. 87 // Returns an error if it fails.
81 func DecodeJSON(r io.Reader, v interface{}) error { 88 func DecodeJSON(r io.Reader, v interface{}) error {
82 return json.NewDecoder(r).Decode(v) 89 return json.NewDecoder(r).Decode(v)
83 } 90 }
84 91
85 // NewPayload returs a payload sceleton for provided table. 92 // NewPayload returs a payload sceleton for provided table.
86 func NewPayload(r *http.Request, table string) Payload { 93 func NewPayload(r *http.Request, table string) Payload {
87 var pload Payload 94 var pload Payload
88 95
89 //pload.Method = r.Method + " " + r.URL.Path 96 //pload.Method = r.Method + " " + r.URL.Path
90 pload.Method = r.Method + " " + r.RequestURI 97 pload.Method = r.Method + " " + r.RequestURI
91 if table != "" { 98 if table != "" {
92 pload.Params = make(map[string]string, 0) 99 pload.Params = make(map[string]string, 0)
93 pload.Lang = translations(table) 100 pload.Lang = translations(table)
94 pload.Fields = fields(table) 101 pload.Fields = fields(table)
95 pload.IdField = id(table) 102 pload.IdField = id(table)
96 pload.Correlations = correlations(table) 103 pload.Correlations = correlations(table)
97 } 104 }
98 return pload 105 return pload
99 } 106 }
100 107
101 // DeliverPayload encodes payload to w. 108 // DeliverPayload encodes payload to w.
102 func DeliverPayload(w http.ResponseWriter, payload Payload) { 109 func DeliverPayload(w http.ResponseWriter, payload Payload) {
103 json.NewEncoder(w).Encode(payload) 110 json.NewEncoder(w).Encode(payload)
104 payload.Data = nil 111 payload.Data = nil
105 } 112 }
106 113
107 // translations returns a slice of translations for a payload/table of ptype type. 114 // translations returns a slice of translations for a payload/table of ptype type.
108 func translations(ptype string) []Translation { 115 func translations(ptype string) []Translation {
109 var translations []Translation 116 var translations []Translation
110 117
111 for _, pload := range payloads { 118 for _, pload := range payloads {
112 if pload.Type == ptype { 119 if pload.Type == ptype {
113 for _, t := range pload.Lang { 120 for _, t := range pload.Lang {
114 translations = append(translations, Translation{ 121 translations = append(translations, Translation{
115 Language: t.Language, 122 Language: t.Language,
116 FieldsLabels: t.FieldsLabels, 123 FieldsLabels: t.FieldsLabels,
117 }) 124 })
118 } 125 }
119 } 126 }
120 } 127 }
121 128
122 return translations 129 return translations
123 } 130 }
124 131
125 // fields returns a slice of fields for a payload/table of ptype type. 132 // fields returns a slice of fields for a payload/table of ptype type.
126 func fields(ptype string) []Field { 133 func fields(ptype string) []Field {
127 var fields []Field 134 var fields []Field
128 135
129 for _, pload := range payloads { 136 for _, pload := range payloads {
130 if pload.Type == ptype { 137 if pload.Type == ptype {
131 for _, f := range pload.Fields { 138 for _, f := range pload.Fields {
132 fields = append(fields, f) 139 fields = append(fields, f)
133 } 140 }
134 } 141 }
135 } 142 }
136 143
137 return fields 144 return fields
138 } 145 }
139 146
140 // id returns the name of ID field of a payload/table of ptype type. 147 // id returns the name of ID field of a payload/table of ptype type.
141 func id(ptype string) string { 148 func id(ptype string) string {
142 for _, pload := range payloads { 149 for _, pload := range payloads {
143 if pload.Type == ptype { 150 if pload.Type == ptype {
144 return pload.IdField 151 return pload.IdField
145 } 152 }
146 } 153 }
147 return "" 154 return ""
148 } 155 }
149 156
150 // correlations returns a slice of correlation fields for a payload/table of ptype type. 157 // correlations returns a slice of correlation fields for a payload/table of ptype type.
151 func correlations(ptype string) []CorrelationField { 158 func correlations(ptype string) []CorrelationField {
152 var corr []CorrelationField 159 var corr []CorrelationField
153 160
154 for _, pload := range payloads { 161 for _, pload := range payloads {
155 if pload.Type == ptype { 162 if pload.Type == ptype {
156 for _, c := range pload.Correlations { 163 for _, c := range pload.Correlations {
157 corr = append(corr, c) 164 corr = append(corr, c)
158 } 165 }
159 } 166 }
160 } 167 }
161 168
162 return corr 169 return corr
163 } 170 }
164 171
165 // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table. 172 // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table.
166 // Returns an error if it fails. 173 // Returns an error if it fails.
167 func fetchJSON(db *ora.Ses, project string) ([]byte, error) { 174 func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
168 db.SetCfg(db.Cfg().SetClob(ora.S)) 175 db.SetCfg(db.Cfg().SetClob(ora.S))
169 stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT`+EqualQuotes(project), ora.S) 176 stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT`+EqualQuotes(project), ora.S)
170 defer stmt.Close() 177 defer stmt.Close()
171 if err != nil { 178 if err != nil {
172 return nil, err 179 return nil, err
173 } 180 }
174 181
175 rset, err := stmt.Qry() 182 rset, err := stmt.Qry()
176 if err != nil { 183 if err != nil {
177 return nil, err 184 return nil, err
178 } 185 }
179 186
180 var data string 187 var data string
181 if rset.Next() { 188 if rset.Next() {
182 data = rset.Row[0].(string) 189 data = rset.Row[0].(string)
183 } 190 }
184 191
185 //fmt.Println(data) 192 //fmt.Println(data)
186 return []byte(data), nil 193 return []byte(data), nil
187 } 194 }
188 195