Commit 8dc665d4a10c02cc03ab020c7d69c85a0839b311
1 parent
16e304c58a
Exists in
master
and in
1 other branch
Init/Reload Tables is now Init/Reload PayloadMetaData
Showing
1 changed file
with
3 additions
and
3 deletions
Show diff stats
json_utility.go
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 InitPayloads(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 ReloadPayloads(db *ora.Ses, project string) error { |
70 | payloads = make([]Payload, 0) | 70 | payloads = make([]Payload, 0) |
71 | return InitTables(db, project) | 71 | return InitPayloads(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 | // DeliverPayload encodes payload to w. | 96 | // DeliverPayload encodes payload to w. |
97 | func DeliverPayload(w http.ResponseWriter, payload Payload) { | 97 | func DeliverPayload(w http.ResponseWriter, payload Payload) { |
98 | json.NewEncoder(w).Encode(payload) | 98 | json.NewEncoder(w).Encode(payload) |
99 | payload.Data = nil | 99 | payload.Data = nil |
100 | } | 100 | } |
101 | 101 | ||
102 | // 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. |
103 | func translations(ptype string) []Translation { | 103 | func translations(ptype string) []Translation { |
104 | var translations []Translation | 104 | var translations []Translation |
105 | 105 | ||
106 | for _, pload := range payloads { | 106 | for _, pload := range payloads { |
107 | if pload.Type == ptype { | 107 | if pload.Type == ptype { |
108 | for _, t := range pload.Lang { | 108 | for _, t := range pload.Lang { |
109 | translations = append(translations, Translation{ | 109 | translations = append(translations, Translation{ |
110 | Language: t.Language, | 110 | Language: t.Language, |
111 | FieldsLabels: t.FieldsLabels, | 111 | FieldsLabels: t.FieldsLabels, |
112 | }) | 112 | }) |
113 | } | 113 | } |
114 | } | 114 | } |
115 | } | 115 | } |
116 | 116 | ||
117 | return translations | 117 | return translations |
118 | } | 118 | } |
119 | 119 | ||
120 | // 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. |
121 | func fields(ptype string) []Field { | 121 | func fields(ptype string) []Field { |
122 | var fields []Field | 122 | var fields []Field |
123 | 123 | ||
124 | for _, pload := range payloads { | 124 | for _, pload := range payloads { |
125 | if pload.Type == ptype { | 125 | if pload.Type == ptype { |
126 | for _, f := range pload.Fields { | 126 | for _, f := range pload.Fields { |
127 | fields = append(fields, f) | 127 | fields = append(fields, f) |
128 | } | 128 | } |
129 | } | 129 | } |
130 | } | 130 | } |
131 | 131 | ||
132 | return fields | 132 | return fields |
133 | } | 133 | } |
134 | 134 | ||
135 | // 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. |
136 | func id(ptype string) string { | 136 | func id(ptype string) string { |
137 | for _, pload := range payloads { | 137 | for _, pload := range payloads { |
138 | if pload.Type == ptype { | 138 | if pload.Type == ptype { |
139 | return pload.IdField | 139 | return pload.IdField |
140 | } | 140 | } |
141 | } | 141 | } |
142 | return "" | 142 | return "" |
143 | } | 143 | } |
144 | 144 | ||
145 | // 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. |
146 | func correlations(ptype string) []CorrelationField { | 146 | func correlations(ptype string) []CorrelationField { |
147 | var corr []CorrelationField | 147 | var corr []CorrelationField |
148 | 148 | ||
149 | for _, pload := range payloads { | 149 | for _, pload := range payloads { |
150 | if pload.Type == ptype { | 150 | if pload.Type == ptype { |
151 | for _, c := range pload.Correlations { | 151 | for _, c := range pload.Correlations { |
152 | corr = append(corr, c) | 152 | corr = append(corr, c) |
153 | } | 153 | } |
154 | } | 154 | } |
155 | } | 155 | } |
156 | 156 | ||
157 | return corr | 157 | return corr |
158 | } | 158 | } |
159 | 159 | ||
160 | // 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. |
161 | // Returns an error if it fails. | 161 | // Returns an error if it fails. |
162 | func fetchJSON(db *ora.Ses, project string) ([]byte, error) { | 162 | func fetchJSON(db *ora.Ses, project string) ([]byte, error) { |
163 | db.SetCfg(db.Cfg().SetClob(ora.S)) | 163 | db.SetCfg(db.Cfg().SetClob(ora.S)) |
164 | 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) |
165 | defer stmt.Close() | 165 | defer stmt.Close() |
166 | if err != nil { | 166 | if err != nil { |
167 | return nil, err | 167 | return nil, err |
168 | } | 168 | } |
169 | 169 | ||
170 | rset, err := stmt.Qry() | 170 | rset, err := stmt.Qry() |
171 | if err != nil { | 171 | if err != nil { |
172 | return nil, err | 172 | return nil, err |
173 | } | 173 | } |
174 | 174 | ||
175 | var data string | 175 | var data string |
176 | if rset.Next() { | 176 | if rset.Next() { |
177 | data = rset.Row[0].(string) | 177 | data = rset.Row[0].(string) |
178 | } | 178 | } |
179 | 179 | ||
180 | //fmt.Println(data) | 180 | //fmt.Println(data) |
181 | return []byte(data), nil | 181 | return []byte(data), nil |
182 | } | 182 | } |
183 | 183 |