Commit 62a69bedaa13a301c4acaf278d288f3a506fa37f
1 parent
8dc665d4a1
Exists in
master
and in
1 other branch
Init/Reload Tables is now Init/Reload PayloadMetaData
Showing
1 changed file
with
4 additions
and
9 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 | // InitPayloadsMetaData loads all payloads in the payloads variable. |
51 | // Returns an error if it fails. | 51 | // Returns an error if it fails. |
52 | func InitPayloads(db *ora.Ses, project string) error { | 52 | func InitPayloadsMetaData(db *ora.Ses, project string) error { |
53 | payloads = make([]Payload, 0) | ||
54 | |||
53 | jsonbuf, err := fetchJSON(db, project) | 55 | jsonbuf, err := fetchJSON(db, project) |
54 | if err != nil { | 56 | if err != nil { |
55 | return err | 57 | return err |
56 | } | 58 | } |
57 | 59 | ||
58 | mu.Lock() | 60 | mu.Lock() |
59 | defer mu.Unlock() | 61 | defer mu.Unlock() |
60 | json.Unmarshal(jsonbuf, &payloads) | 62 | json.Unmarshal(jsonbuf, &payloads) |
61 | if len(payloads) == 0 { | 63 | if len(payloads) == 0 { |
62 | return errors.New("tables config is corrupt") | 64 | return errors.New("tables config is corrupt") |
63 | } | 65 | } |
64 | return nil | 66 | return nil |
65 | } | 67 | } |
66 | 68 | ||
67 | // ReloadTables reloads all payloads in the payloads variable. | ||
68 | // Returns an error if it fails. | ||
69 | func ReloadPayloads(db *ora.Ses, project string) error { | ||
70 | payloads = make([]Payload, 0) | ||
71 | return InitPayloads(db, project) | ||
72 | } | ||
73 | |||
74 | // DecodeJSON decodes JSON data from r to v. | 69 | // DecodeJSON decodes JSON data from r to v. |
75 | // Returns an error if it fails. | 70 | // Returns an error if it fails. |
76 | func DecodeJSON(r io.Reader, v interface{}) error { | 71 | func DecodeJSON(r io.Reader, v interface{}) error { |
77 | return json.NewDecoder(r).Decode(v) | 72 | return json.NewDecoder(r).Decode(v) |
78 | } | 73 | } |
79 | 74 | ||
80 | // NewPayload returs a payload sceleton for provided table. | 75 | // NewPayload returs a payload sceleton for provided table. |
81 | func NewPayload(r *http.Request, table string) Payload { | 76 | func NewPayload(r *http.Request, table string) Payload { |
82 | var pload Payload | 77 | var pload Payload |
83 | 78 | ||
84 | pload.Method = r.Method + " " + r.RequestURI | 79 | pload.Method = r.Method + " " + r.RequestURI |
85 | pload.Type = table | 80 | pload.Type = table |
86 | if table != "" { | 81 | if table != "" { |
87 | pload.Params = make(map[string]string, 0) | 82 | pload.Params = make(map[string]string, 0) |
88 | pload.Lang = translations(table) | 83 | pload.Lang = translations(table) |
89 | pload.Fields = fields(table) | 84 | pload.Fields = fields(table) |
90 | pload.IdField = id(table) | 85 | pload.IdField = id(table) |
91 | pload.Correlations = correlations(table) | 86 | pload.Correlations = correlations(table) |
92 | } | 87 | } |
93 | return pload | 88 | return pload |
94 | } | 89 | } |
95 | 90 | ||
96 | // DeliverPayload encodes payload to w. | 91 | // DeliverPayload encodes payload to w. |
97 | func DeliverPayload(w http.ResponseWriter, payload Payload) { | 92 | func DeliverPayload(w http.ResponseWriter, payload Payload) { |
98 | json.NewEncoder(w).Encode(payload) | 93 | json.NewEncoder(w).Encode(payload) |
99 | payload.Data = nil | 94 | payload.Data = nil |
100 | } | 95 | } |
101 | 96 | ||
102 | // translations returns a slice of translations for a payload/table of ptype type. | 97 | // translations returns a slice of translations for a payload/table of ptype type. |
103 | func translations(ptype string) []Translation { | 98 | func translations(ptype string) []Translation { |
104 | var translations []Translation | 99 | var translations []Translation |
105 | 100 | ||
106 | for _, pload := range payloads { | 101 | for _, pload := range payloads { |
107 | if pload.Type == ptype { | 102 | if pload.Type == ptype { |
108 | for _, t := range pload.Lang { | 103 | for _, t := range pload.Lang { |
109 | translations = append(translations, Translation{ | 104 | translations = append(translations, Translation{ |
110 | Language: t.Language, | 105 | Language: t.Language, |
111 | FieldsLabels: t.FieldsLabels, | 106 | FieldsLabels: t.FieldsLabels, |
112 | }) | 107 | }) |
113 | } | 108 | } |
114 | } | 109 | } |
115 | } | 110 | } |
116 | 111 | ||
117 | return translations | 112 | return translations |
118 | } | 113 | } |
119 | 114 | ||
120 | // fields returns a slice of fields for a payload/table of ptype type. | 115 | // fields returns a slice of fields for a payload/table of ptype type. |
121 | func fields(ptype string) []Field { | 116 | func fields(ptype string) []Field { |
122 | var fields []Field | 117 | var fields []Field |
123 | 118 | ||
124 | for _, pload := range payloads { | 119 | for _, pload := range payloads { |
125 | if pload.Type == ptype { | 120 | if pload.Type == ptype { |
126 | for _, f := range pload.Fields { | 121 | for _, f := range pload.Fields { |
127 | fields = append(fields, f) | 122 | fields = append(fields, f) |
128 | } | 123 | } |
129 | } | 124 | } |
130 | } | 125 | } |
131 | 126 | ||
132 | return fields | 127 | return fields |
133 | } | 128 | } |
134 | 129 | ||
135 | // id returns the name of ID field of a payload/table of ptype type. | 130 | // id returns the name of ID field of a payload/table of ptype type. |
136 | func id(ptype string) string { | 131 | func id(ptype string) string { |
137 | for _, pload := range payloads { | 132 | for _, pload := range payloads { |
138 | if pload.Type == ptype { | 133 | if pload.Type == ptype { |
139 | return pload.IdField | 134 | return pload.IdField |
140 | } | 135 | } |
141 | } | 136 | } |
142 | return "" | 137 | return "" |
143 | } | 138 | } |
144 | 139 | ||
145 | // correlations returns a slice of correlation fields for a payload/table of ptype type. | 140 | // correlations returns a slice of correlation fields for a payload/table of ptype type. |
146 | func correlations(ptype string) []CorrelationField { | 141 | func correlations(ptype string) []CorrelationField { |
147 | var corr []CorrelationField | 142 | var corr []CorrelationField |
148 | 143 | ||
149 | for _, pload := range payloads { | 144 | for _, pload := range payloads { |
150 | if pload.Type == ptype { | 145 | if pload.Type == ptype { |
151 | for _, c := range pload.Correlations { | 146 | for _, c := range pload.Correlations { |
152 | corr = append(corr, c) | 147 | corr = append(corr, c) |
153 | } | 148 | } |
154 | } | 149 | } |
155 | } | 150 | } |
156 | 151 | ||
157 | return corr | 152 | return corr |
158 | } | 153 | } |
159 | 154 | ||
160 | // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table. | 155 | // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table. |
161 | // Returns an error if it fails. | 156 | // Returns an error if it fails. |
162 | func fetchJSON(db *ora.Ses, project string) ([]byte, error) { | 157 | func fetchJSON(db *ora.Ses, project string) ([]byte, error) { |
163 | db.SetCfg(db.Cfg().SetClob(ora.S)) | 158 | 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) | 159 | stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT = `+fmt.Sprintf("'%s'", project), ora.S) |
165 | defer stmt.Close() | 160 | defer stmt.Close() |
166 | if err != nil { | 161 | if err != nil { |
167 | return nil, err | 162 | return nil, err |
168 | } | 163 | } |
169 | 164 | ||
170 | rset, err := stmt.Qry() | 165 | rset, err := stmt.Qry() |
171 | if err != nil { | 166 | if err != nil { |
172 | return nil, err | 167 | return nil, err |
173 | } | 168 | } |
174 | 169 | ||
175 | var data string | 170 | var data string |
176 | if rset.Next() { | 171 | if rset.Next() { |
177 | data = rset.Row[0].(string) | 172 | data = rset.Row[0].(string) |
178 | } | 173 | } |
179 | 174 | ||
180 | //fmt.Println(data) | 175 | //fmt.Println(data) |
181 | return []byte(data), nil | 176 | return []byte(data), nil |