Commit 16e304c58a0ce3d7d1ffeb1e1308ae292188e4cd

Authored by Marko Tikvić
1 parent 4cb3081998
Exists in master and in 1 other branch v2

removed MakePayload: not as flexible as anticipated

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