Commit 0c52d7717588af263146a6fb62038f1e834a5e98

Authored by Marko Tikvić
1 parent 8bec68a1e8
Exists in master and in 1 other branch v2

make payload with provided data

Showing 1 changed file with 17 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. 79 // ReloadTables reloads all payloads in the payloads variable.
80 // Returns an error if it fails. 80 // Returns an error if it fails.
81 func ReloadTables(db *ora.Ses, project string) error { 81 func ReloadTables(db *ora.Ses, project string) error {
82 payloads = make([]payloadBuff, 0) 82 payloads = make([]payloadBuff, 0)
83 return InitTables(db, project) 83 return InitTables(db, project)
84 } 84 }
85 85
86 // DecodeJSON decodes JSON data from r to v. 86 // DecodeJSON decodes JSON data from r to v.
87 // Returns an error if it fails. 87 // Returns an error if it fails.
88 func DecodeJSON(r io.Reader, v interface{}) error { 88 func DecodeJSON(r io.Reader, v interface{}) error {
89 return json.NewDecoder(r).Decode(v) 89 return json.NewDecoder(r).Decode(v)
90 } 90 }
91 91
92 // NewPayload returs a payload sceleton for provided table. 92 // NewPayload returs a payload sceleton for provided table.
93 func NewPayload(r *http.Request, table string) Payload { 93 func NewPayload(r *http.Request, table string) Payload {
94 var pload Payload 94 var pload Payload
95 95
96 //pload.Method = r.Method + " " + r.URL.Path 96 //pload.Method = r.Method + " " + r.URL.Path
97 pload.Method = r.Method + " " + r.RequestURI 97 pload.Method = r.Method + " " + r.RequestURI
98 if table != "" { 98 if table != "" {
99 pload.Params = make(map[string]string, 0) 99 pload.Params = make(map[string]string, 0)
100 pload.Lang = translations(table) 100 pload.Lang = translations(table)
101 pload.Fields = fields(table) 101 pload.Fields = fields(table)
102 pload.IdField = id(table) 102 pload.IdField = id(table)
103 pload.Correlations = correlations(table) 103 pload.Correlations = correlations(table)
104 } 104 }
105 return pload 105 return pload
106 } 106 }
107 107
108 // MakePayload returs a payload for provided table with populated Data field.
109 func MakePayload(r *http.Request, table string, data interface{}) Payload {
110 var pload Payload
111
112 //pload.Method = r.Method + " " + r.URL.Path
113 pload.Method = r.Method + " " + r.RequestURI
114 if table != "" {
115 pload.Params = make(map[string]string, 0)
116 pload.Lang = translations(table)
117 pload.Fields = fields(table)
118 pload.IdField = id(table)
119 pload.Correlations = correlations(table)
120 }
121 pload.Data = data
122 return pload
123 }
124
108 // DeliverPayload encodes payload to w. 125 // DeliverPayload encodes payload to w.
109 func DeliverPayload(w http.ResponseWriter, payload Payload) { 126 func DeliverPayload(w http.ResponseWriter, payload Payload) {
110 json.NewEncoder(w).Encode(payload) 127 json.NewEncoder(w).Encode(payload)
111 payload.Data = nil 128 payload.Data = nil
112 } 129 }
113 130
114 // translations returns a slice of translations for a payload/table of ptype type. 131 // translations returns a slice of translations for a payload/table of ptype type.
115 func translations(ptype string) []Translation { 132 func translations(ptype string) []Translation {
116 var translations []Translation 133 var translations []Translation
117 134
118 for _, pload := range payloads { 135 for _, pload := range payloads {
119 if pload.Type == ptype { 136 if pload.Type == ptype {
120 for _, t := range pload.Lang { 137 for _, t := range pload.Lang {
121 translations = append(translations, Translation{ 138 translations = append(translations, Translation{
122 Language: t.Language, 139 Language: t.Language,
123 FieldsLabels: t.FieldsLabels, 140 FieldsLabels: t.FieldsLabels,
124 }) 141 })
125 } 142 }
126 } 143 }
127 } 144 }
128 145
129 return translations 146 return translations
130 } 147 }
131 148
132 // fields returns a slice of fields for a payload/table of ptype type. 149 // fields returns a slice of fields for a payload/table of ptype type.
133 func fields(ptype string) []Field { 150 func fields(ptype string) []Field {
134 var fields []Field 151 var fields []Field
135 152
136 for _, pload := range payloads { 153 for _, pload := range payloads {
137 if pload.Type == ptype { 154 if pload.Type == ptype {
138 for _, f := range pload.Fields { 155 for _, f := range pload.Fields {
139 fields = append(fields, f) 156 fields = append(fields, f)
140 } 157 }
141 } 158 }
142 } 159 }
143 160
144 return fields 161 return fields
145 } 162 }
146 163
147 // id returns the name of ID field of a payload/table of ptype type. 164 // id returns the name of ID field of a payload/table of ptype type.
148 func id(ptype string) string { 165 func id(ptype string) string {
149 for _, pload := range payloads { 166 for _, pload := range payloads {
150 if pload.Type == ptype { 167 if pload.Type == ptype {
151 return pload.IdField 168 return pload.IdField
152 } 169 }
153 } 170 }
154 return "" 171 return ""
155 } 172 }
156 173
157 // correlations returns a slice of correlation fields for a payload/table of ptype type. 174 // correlations returns a slice of correlation fields for a payload/table of ptype type.
158 func correlations(ptype string) []CorrelationField { 175 func correlations(ptype string) []CorrelationField {
159 var corr []CorrelationField 176 var corr []CorrelationField
160 177
161 for _, pload := range payloads { 178 for _, pload := range payloads {
162 if pload.Type == ptype { 179 if pload.Type == ptype {
163 for _, c := range pload.Correlations { 180 for _, c := range pload.Correlations {
164 corr = append(corr, c) 181 corr = append(corr, c)
165 } 182 }
166 } 183 }
167 } 184 }
168 185
169 return corr 186 return corr
170 } 187 }
171 188
172 // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table. 189 // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table.
173 // Returns an error if it fails. 190 // Returns an error if it fails.
174 func fetchJSON(db *ora.Ses, project string) ([]byte, error) { 191 func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
175 db.SetCfg(db.Cfg().SetClob(ora.S)) 192 db.SetCfg(db.Cfg().SetClob(ora.S))
176 stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT`+EqualQuotes(project), ora.S) 193 stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT`+EqualQuotes(project), ora.S)
177 defer stmt.Close() 194 defer stmt.Close()
178 if err != nil { 195 if err != nil {
179 return nil, err 196 return nil, err
180 } 197 }
181 198
182 rset, err := stmt.Qry() 199 rset, err := stmt.Qry()
183 if err != nil { 200 if err != nil {
184 return nil, err 201 return nil, err
185 } 202 }
186 203
187 var data string 204 var data string
188 if rset.Next() { 205 if rset.Next() {
189 data = rset.Row[0].(string) 206 data = rset.Row[0].(string)
190 } 207 }
191 208
192 //fmt.Println(data) 209 //fmt.Println(data)
193 return []byte(data), nil 210 return []byte(data), nil
194 } 211 }
195 212