Commit 437859ae9053b1209db2d79e95d994ed92fb5a70

Authored by Marko Tikvić
1 parent 68d4dbbd12
Exists in master and in 1 other branch v2

hmmm

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