Commit 66478e04fb0d28d69edb9df67bd3379e6da1f061

Authored by Marko Tikvić
1 parent 0c52d77175
Exists in master and in 1 other branch v2

payload now returns type

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