json_utility.go
4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package webutility
import (
"net/http"
"encoding/json"
"errors"
"gopkg.in/rana/ora.v3"
"io"
"io/ioutil"
"sync"
)
var mu = &sync.Mutex{}
var allPayloads []payloadBuff
type LangMap map[string]map[string]string
type Field struct {
Parameter string `json:"param"`
Type string `json:"type"`
Visible bool `json:"visible"`
Editable bool `json:"editable"`
}
type CorrelationField struct {
Result string `json:"result"`
Elements []string `json:"elements"`
Type string `json:"type"`
}
type Translation struct {
Language string `json:"language"`
FieldsLabels map[string]string `json:"fieldsLabels"`
}
type payloadBuff struct {
Type string `json:"tableType"`
Method string `json:"method"`
Params map[string]string `json:"params"`
Lang []Translation `json:"lang"`
Fields []Field `json:"fields"`
Correlations []CorrelationField `json:"correlationFields"`
IdField string `json:"idField"`
// Data can only hold slices of any type. It can't be used for itteration
Data interface{} `json:"data"`
}
type Payload struct {
Method string `json:"method"`
Params map[string]string `json:"params"`
Lang []Translation `json:"lang"`
Fields []Field `json:"fields"`
Correlations []CorrelationField `json:"correlationFields"`
IdField string `json:"idField"`
// Data can only hold slices of any type. It can't be used for itteration
Data interface{} `json:"data"`
}
// NewPayload returs payload for provided table.
func NewPayload(r *http.Request, table string) Payload {
var pload Payload
pload.Method = r.Method + " " + r.URL.Path
if table != "" {
pload.Params = make(map[string]string, 0)
pload.Lang = loadTranslations(table)
pload.Fields = loadFields(table)
pload.IdField = loadIdField(table)
pload.Correlations = loadCorrelations(table)
}
return pload
}
// DeliverPayload writes payload to w.
func DeliverPayload(w http.ResponseWriter, payload Payload) {
json.NewEncoder(w).Encode(payload)
payload.Data = nil
}
// loadTranslations loads translations for a payload of the given data type.
func loadTranslations(id string) []Translation {
translations := make([]Translation, 0)
for _, pload := range allPayloads {
if pload.Type == id {
for _, t := range pload.Lang {
//translations[t.Language] = t.FieldsLabels
translations = append(translations, Translation{
Language: t.Language,
FieldsLabels: t.FieldsLabels,
})
}
}
}
return translations
}
// loadFields loads fields for a payload of the given data type.
func loadFields(id string) []Field {
fields := make([]Field, 0)
for _, pload := range allPayloads {
if pload.Type == id{
for _, f := range pload.Fields {
fields = append(fields, f)
}
}
}
return fields
}
// loadIdField loads ID field for a payload of the given data type.
func loadIdField(id string) string {
for _, pload := range allPayloads {
if pload.Type == id {
return pload.IdField
}
}
return ""
}
// loadCorrelations loads correlations field for a payload of the given data type.
func loadCorrelations(id string) []CorrelationField {
resp := make([]CorrelationField, 0)
for _, pload := range allPayloads {
if pload.Type == id {
for _, f := range pload.Correlations {
resp = append(resp, f)
}
}
}
return resp
}
// InitTables loads all payloads in the memory.
func InitTables(db *ora.Ses, project string) error {
jsonbuf, _ := fetchJSON(db, EqualQuotes(project))
mu.Lock()
defer mu.Unlock()
json.Unmarshal(jsonbuf, &allPayloads)
if len(allPayloads) == 0 {
return errors.New("tables config is corrupt")
}
return nil
}
// fetchJSON fetches JSON configuration file from TABLES_CONFIG table.
func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
stmt, err := db.Prep(`SELECT
JSON_CLOB
FROM TABLES_CONFIG
WHERE PROJEKAT` + project, ora.S)
defer stmt.Close()
if err != nil {
return nil, err
}
rset, err := stmt.Qry()
if err != nil {
return nil, err
}
bytes := make([]byte, 0)
if rset.Next() {
lob := rset.Row[0].(io.Reader)
bytes, err = ioutil.ReadAll(lob)
if err != nil {
// Ignore for now, it's some weird streaming read/write LOB error.
// TODO: Find a fix for this.
//return nil, err
}
}
return bytes, nil
}
// DecodeJSON decodes JSON data from r to v and returns any error
// that happens during decoding process.
func DecodeJSON(r io.Reader, v interface{}) error {
return json.NewDecoder(r).Decode(v)
}