Blame view

json_utility.go 4.5 KB
ea858b8a7   Marko Tikvić   refactoring
1
  package webutility
64041a2ea   Marko Tikvić   first commit
2
3
4
  
  import (
  	"net/http"
8dbe745c3   Marko Tikvić   merged tables uti...
5
6
7
8
9
  	"encoding/json"
  	"errors"
  	"gopkg.in/rana/ora.v3"
  	"io"
  	"io/ioutil"
17a4d0447   Marko Tikvić   mutex lock for pa...
10
  	"sync"
64041a2ea   Marko Tikvić   first commit
11
  )
17a4d0447   Marko Tikvić   mutex lock for pa...
12
  var mu = &sync.Mutex{}
6ec91280b   Marko Tikvić   working on docume...
13
  var payloads []payloadBuff
8dbe745c3   Marko Tikvić   merged tables uti...
14

64041a2ea   Marko Tikvić   first commit
15
16
17
18
19
20
21
22
  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"`
  }
8dbe745c3   Marko Tikvić   merged tables uti...
23
24
25
26
27
28
29
  type CorrelationField struct {
  	Result   string   `json:"result"`
  	Elements []string `json:"elements"`
  	Type     string   `json:"type"`
  }
  
  type Translation struct {
ecec68b18   Marko Tikvić   updated todo list
30
  	Language     string            `json:"language"`
8dbe745c3   Marko Tikvić   merged tables uti...
31
  	FieldsLabels map[string]string `json:"fieldsLabels"`
64041a2ea   Marko Tikvić   first commit
32
  }
4a51e54d7   Marko Tikvić   simplified
33
  type payloadBuff struct {
8dbe745c3   Marko Tikvić   merged tables uti...
34
  	Type         string             `json:"tableType"`
64041a2ea   Marko Tikvić   first commit
35
36
  	Method	     string             `json:"method"`
  	Params	     map[string]string  `json:"params"`
8dbe745c3   Marko Tikvić   merged tables uti...
37
  	Lang	     []Translation      `json:"lang"`
64041a2ea   Marko Tikvić   first commit
38
  	Fields	     []Field            `json:"fields"`
c430f3af5   Marko Tikvić   fixed camel case ...
39
  	Correlations []CorrelationField `json:"correlationFields"`
64041a2ea   Marko Tikvić   first commit
40
  	IdField      string             `json:"idField"`
6ec91280b   Marko Tikvić   working on docume...
41

64041a2ea   Marko Tikvić   first commit
42
43
44
  	// Data can only hold slices of any type. It can't be used for itteration
  	Data	     interface{}        `json:"data"`
  }
4a51e54d7   Marko Tikvić   simplified
45
46
47
48
49
50
51
  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"`
e1fbb41f9   Marko Tikvić   added comments
52

4a51e54d7   Marko Tikvić   simplified
53
54
55
  	// Data can only hold slices of any type. It can't be used for itteration
  	Data	     interface{}        `json:"data"`
  }
6ec91280b   Marko Tikvić   working on docume...
56
  // NewPayload returs a payload sceleton for provided table.
8dbe745c3   Marko Tikvić   merged tables uti...
57
58
  func NewPayload(r *http.Request, table string) Payload {
  	var pload Payload
64041a2ea   Marko Tikvić   first commit
59

4a51e54d7   Marko Tikvić   simplified
60
  	pload.Method = r.Method + " " + r.URL.Path
90f4ed079   Marko Tikvić   sped up loadTable()
61
62
  	if table != "" {
  		pload.Params = make(map[string]string, 0)
6ec91280b   Marko Tikvić   working on docume...
63
64
65
66
  		pload.Lang = translations(table)
  		pload.Fields = fields(table)
  		pload.IdField = id(table)
  		pload.Correlations = correlations(table)
90f4ed079   Marko Tikvić   sped up loadTable()
67
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
68
69
  	return pload
  }
64041a2ea   Marko Tikvić   first commit
70

6ec91280b   Marko Tikvić   working on docume...
71
  // DeliverPayload encodes payload to w.
8dbe745c3   Marko Tikvić   merged tables uti...
72
73
74
  func DeliverPayload(w http.ResponseWriter, payload Payload) {
  	json.NewEncoder(w).Encode(payload)
  	payload.Data = nil
64041a2ea   Marko Tikvić   first commit
75
  }
6ec91280b   Marko Tikvić   working on docume...
76
77
78
  // translations returns a slice of translations for a payload/table of ptype type.
  func translations(ptype string) []Translation {
  	var translations []Translation
8dbe745c3   Marko Tikvić   merged tables uti...
79

6ec91280b   Marko Tikvić   working on docume...
80
81
  	for _, pload := range payloads {
  		if pload.Type == ptype {
8dbe745c3   Marko Tikvić   merged tables uti...
82
  			for _, t := range pload.Lang {
8dbe745c3   Marko Tikvić   merged tables uti...
83
84
85
86
87
88
  				translations = append(translations, Translation{
  					Language: t.Language,
  					FieldsLabels: t.FieldsLabels,
  				})
  			}
  		}
64041a2ea   Marko Tikvić   first commit
89
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
90
91
92
  
  	return translations
  }
6ec91280b   Marko Tikvić   working on docume...
93
94
95
  // fields returns a slice of fields for a payload/table of ptype type.
  func fields(ptype string) []Field {
  	var fields []Field
8dbe745c3   Marko Tikvić   merged tables uti...
96

6ec91280b   Marko Tikvić   working on docume...
97
98
  	for _, pload := range payloads {
  		if pload.Type == ptype {
8dbe745c3   Marko Tikvić   merged tables uti...
99
100
101
102
103
104
105
106
  			for _, f := range pload.Fields {
  				fields = append(fields, f)
  			}
  		}
  	}
  
  	return fields
  }
6ec91280b   Marko Tikvić   working on docume...
107
108
109
110
  // id returns the name of ID field of a payload/table of ptype type.
  func id(ptype string) string {
  	for _, pload := range payloads {
  		if pload.Type == ptype {
8dbe745c3   Marko Tikvić   merged tables uti...
111
112
113
114
115
  			return pload.IdField
  		}
  	}
  	return ""
  }
6ec91280b   Marko Tikvić   working on docume...
116
117
118
  // correlations returns a slice of correlation fields for a payload/table of ptype type.
  func correlations(ptype string) []CorrelationField {
  	var corr []CorrelationField
8dbe745c3   Marko Tikvić   merged tables uti...
119

6ec91280b   Marko Tikvić   working on docume...
120
121
122
123
  	for _, pload := range payloads {
  		if pload.Type == ptype {
  			for _, c := range pload.Correlations {
  				corr = append(corr, c)
8dbe745c3   Marko Tikvić   merged tables uti...
124
125
126
  			}
  		}
  	}
6ec91280b   Marko Tikvić   working on docume...
127
  	return corr
8dbe745c3   Marko Tikvić   merged tables uti...
128
  }
6ec91280b   Marko Tikvić   working on docume...
129
130
  // InitTables loads all payloads in the payloads variable.
  // Returns an error if it fails.
8dbe745c3   Marko Tikvić   merged tables uti...
131
  func InitTables(db *ora.Ses, project string) error {
7d3deb50d   Marko Tikvić   modified list_con...
132
  	jsonbuf, _ := fetchJSON(db, EqualQuotes(project))
17a4d0447   Marko Tikvić   mutex lock for pa...
133
134
  	mu.Lock()
  	defer mu.Unlock()
6ec91280b   Marko Tikvić   working on docume...
135
136
  	json.Unmarshal(jsonbuf, &payloads)
  	if len(payloads) == 0 {
8dbe745c3   Marko Tikvić   merged tables uti...
137
138
  		return errors.New("tables config is corrupt")
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
139
140
  	return nil
  }
6ec91280b   Marko Tikvić   working on docume...
141
142
  // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table.
  // Returns an error if it fails.
7d3deb50d   Marko Tikvić   modified list_con...
143
  func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
8dbe745c3   Marko Tikvić   merged tables uti...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  	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 {
7d3deb50d   Marko Tikvić   modified list_con...
164
  			// Ignore for now, it's some weird streaming read/write LOB error.
4a51e54d7   Marko Tikvić   simplified
165
  			// TODO: Find a fix for this.
4a51e54d7   Marko Tikvić   simplified
166
  			//return nil, err
8dbe745c3   Marko Tikvić   merged tables uti...
167
  		}
64041a2ea   Marko Tikvić   first commit
168
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
169
  	return bytes, nil
64041a2ea   Marko Tikvić   first commit
170
  }
6ec91280b   Marko Tikvić   working on docume...
171
172
  // DecodeJSON decodes JSON data from r to v.
  // Returns an error if it fails.
d5192378d   Marko Tikvić   added json decodi...
173
174
175
  func DecodeJSON(r io.Reader, v interface{}) error {
  	return json.NewDecoder(r).Decode(v)
  }