Blame view

json_utility.go 4.62 KB
ea858b8a7   Marko Tikvić   refactoring
1
  package webutility
64041a2ea   Marko Tikvić   first commit
2
3
  
  import (
8dbe745c3   Marko Tikvić   merged tables uti...
4
5
  	"encoding/json"
  	"errors"
66478e04f   Marko Tikvić   payload now retur...
6
  	"fmt"
8dbe745c3   Marko Tikvić   merged tables uti...
7
  	"io"
d2ddf82ef   Marko Tikvić   started on new rbac
8
  	"net/http"
17a4d0447   Marko Tikvić   mutex lock for pa...
9
  	"sync"
1d0f61553   Marko Tikvić   can't fetch clob
10

1d0f61553   Marko Tikvić   can't fetch clob
11
  	"gopkg.in/rana/ora.v4"
64041a2ea   Marko Tikvić   first commit
12
  )
17a4d0447   Marko Tikvić   mutex lock for pa...
13
  var mu = &sync.Mutex{}
66478e04f   Marko Tikvić   payload now retur...
14
  var payloads []Payload
8dbe745c3   Marko Tikvić   merged tables uti...
15

64041a2ea   Marko Tikvić   first commit
16
17
18
  type LangMap map[string]map[string]string
  
  type Field struct {
d2ddf82ef   Marko Tikvić   started on new rbac
19
20
21
22
  	Parameter string `json:"param"`
  	Type      string `json:"type"`
  	Visible   bool   `json:"visible"`
  	Editable  bool   `json:"editable"`
64041a2ea   Marko Tikvić   first commit
23
  }
8dbe745c3   Marko Tikvić   merged tables uti...
24
25
26
27
28
29
30
  type CorrelationField struct {
  	Result   string   `json:"result"`
  	Elements []string `json:"elements"`
  	Type     string   `json:"type"`
  }
  
  type Translation struct {
ecec68b18   Marko Tikvić   updated todo list
31
  	Language     string            `json:"language"`
8dbe745c3   Marko Tikvić   merged tables uti...
32
  	FieldsLabels map[string]string `json:"fieldsLabels"`
64041a2ea   Marko Tikvić   first commit
33
  }
4a51e54d7   Marko Tikvić   simplified
34
  type Payload struct {
66478e04f   Marko Tikvić   payload now retur...
35
  	Type         string             `json:"type"`
d2ddf82ef   Marko Tikvić   started on new rbac
36
37
38
39
  	Method       string             `json:"method"`
  	Params       map[string]string  `json:"params"`
  	Lang         []Translation      `json:"lang"`
  	Fields       []Field            `json:"fields"`
4a51e54d7   Marko Tikvić   simplified
40
41
  	Correlations []CorrelationField `json:"correlationFields"`
  	IdField      string             `json:"idField"`
e1fbb41f9   Marko Tikvić   added comments
42

66478e04f   Marko Tikvić   payload now retur...
43
  	// Data holds JSON payload. It can't be used for itteration.
d2ddf82ef   Marko Tikvić   started on new rbac
44
  	Data interface{} `json:"data"`
4a51e54d7   Marko Tikvić   simplified
45
  }
d66628295   Marko Tikvić   cleaned up
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  // InitTables loads all payloads in the payloads variable.
  // Returns an error if it fails.
  func InitTables(db *ora.Ses, project string) error {
  	jsonbuf, err := fetchJSON(db, project)
  	if err != nil {
  		return err
  	}
  
  	mu.Lock()
  	defer mu.Unlock()
  	json.Unmarshal(jsonbuf, &payloads)
  	if len(payloads) == 0 {
  		return errors.New("tables config is corrupt")
  	}
  	return nil
  }
8bec68a1e   Marko Tikvić   added reload tabl...
62
63
64
  // ReloadTables reloads all payloads in the payloads variable.
  // Returns an error if it fails.
  func ReloadTables(db *ora.Ses, project string) error {
66478e04f   Marko Tikvić   payload now retur...
65
  	payloads = make([]Payload, 0)
8bec68a1e   Marko Tikvić   added reload tabl...
66
67
  	return InitTables(db, project)
  }
d66628295   Marko Tikvić   cleaned up
68
69
70
71
72
  // DecodeJSON decodes JSON data from r to v.
  // Returns an error if it fails.
  func DecodeJSON(r io.Reader, v interface{}) error {
  	return json.NewDecoder(r).Decode(v)
  }
6ec91280b   Marko Tikvić   working on docume...
73
  // NewPayload returs a payload sceleton for provided table.
8dbe745c3   Marko Tikvić   merged tables uti...
74
75
  func NewPayload(r *http.Request, table string) Payload {
  	var pload Payload
64041a2ea   Marko Tikvić   first commit
76

2d79a4120   Marko Tikvić   Responses contain...
77
  	pload.Method = r.Method + " " + r.RequestURI
66478e04f   Marko Tikvić   payload now retur...
78
  	pload.Type = table
90f4ed079   Marko Tikvić   sped up loadTable()
79
80
  	if table != "" {
  		pload.Params = make(map[string]string, 0)
6ec91280b   Marko Tikvić   working on docume...
81
82
83
84
  		pload.Lang = translations(table)
  		pload.Fields = fields(table)
  		pload.IdField = id(table)
  		pload.Correlations = correlations(table)
90f4ed079   Marko Tikvić   sped up loadTable()
85
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
86
87
  	return pload
  }
64041a2ea   Marko Tikvić   first commit
88

4cb308199   Marko Tikvić   BuildPayload -> M...
89
90
  // MakePayload returs a payload for provided table with populated Data field.
  func MakePayload(r *http.Request, table string, data interface{}) Payload {
0c52d7717   Marko Tikvić   make payload with...
91
  	var pload Payload
0c52d7717   Marko Tikvić   make payload with...
92
93
94
95
96
97
98
99
100
101
102
  	pload.Method = r.Method + " " + r.RequestURI
  	if table != "" {
  		pload.Params = make(map[string]string, 0)
  		pload.Lang = translations(table)
  		pload.Fields = fields(table)
  		pload.IdField = id(table)
  		pload.Correlations = correlations(table)
  	}
  	pload.Data = data
  	return pload
  }
6ec91280b   Marko Tikvić   working on docume...
103
  // DeliverPayload encodes payload to w.
8dbe745c3   Marko Tikvić   merged tables uti...
104
105
106
  func DeliverPayload(w http.ResponseWriter, payload Payload) {
  	json.NewEncoder(w).Encode(payload)
  	payload.Data = nil
64041a2ea   Marko Tikvić   first commit
107
  }
6ec91280b   Marko Tikvić   working on docume...
108
109
110
  // 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...
111

6ec91280b   Marko Tikvić   working on docume...
112
113
  	for _, pload := range payloads {
  		if pload.Type == ptype {
8dbe745c3   Marko Tikvić   merged tables uti...
114
  			for _, t := range pload.Lang {
8dbe745c3   Marko Tikvić   merged tables uti...
115
  				translations = append(translations, Translation{
d2ddf82ef   Marko Tikvić   started on new rbac
116
  					Language:     t.Language,
8dbe745c3   Marko Tikvić   merged tables uti...
117
118
119
120
  					FieldsLabels: t.FieldsLabels,
  				})
  			}
  		}
64041a2ea   Marko Tikvić   first commit
121
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
122
123
124
  
  	return translations
  }
6ec91280b   Marko Tikvić   working on docume...
125
126
127
  // 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...
128

6ec91280b   Marko Tikvić   working on docume...
129
130
  	for _, pload := range payloads {
  		if pload.Type == ptype {
8dbe745c3   Marko Tikvić   merged tables uti...
131
132
133
134
135
136
137
138
  			for _, f := range pload.Fields {
  				fields = append(fields, f)
  			}
  		}
  	}
  
  	return fields
  }
6ec91280b   Marko Tikvić   working on docume...
139
140
141
142
  // 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...
143
144
145
146
147
  			return pload.IdField
  		}
  	}
  	return ""
  }
6ec91280b   Marko Tikvić   working on docume...
148
149
150
  // 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...
151

6ec91280b   Marko Tikvić   working on docume...
152
153
154
155
  	for _, pload := range payloads {
  		if pload.Type == ptype {
  			for _, c := range pload.Correlations {
  				corr = append(corr, c)
8dbe745c3   Marko Tikvić   merged tables uti...
156
157
158
  			}
  		}
  	}
6ec91280b   Marko Tikvić   working on docume...
159
  	return corr
8dbe745c3   Marko Tikvić   merged tables uti...
160
  }
6ec91280b   Marko Tikvić   working on docume...
161
162
  // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table.
  // Returns an error if it fails.
5316eaa97   Marko Tikvić   ora v4 bug with u...
163
  func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
b0a6d6322   Marko Tikvić   still can't read utf
164
  	db.SetCfg(db.Cfg().SetClob(ora.S))
66478e04f   Marko Tikvić   payload now retur...
165
  	stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT = `+fmt.Sprintf("'%s'", project), ora.S)
8dbe745c3   Marko Tikvić   merged tables uti...
166
  	defer stmt.Close()
8dbe745c3   Marko Tikvić   merged tables uti...
167
  	if err != nil {
5316eaa97   Marko Tikvić   ora v4 bug with u...
168
  		return nil, err
8dbe745c3   Marko Tikvić   merged tables uti...
169
170
171
172
  	}
  
  	rset, err := stmt.Qry()
  	if err != nil {
5316eaa97   Marko Tikvić   ora v4 bug with u...
173
  		return nil, err
8dbe745c3   Marko Tikvić   merged tables uti...
174
  	}
1d0f61553   Marko Tikvić   can't fetch clob
175
  	var data string
8dbe745c3   Marko Tikvić   merged tables uti...
176
  	if rset.Next() {
1d0f61553   Marko Tikvić   can't fetch clob
177
  		data = rset.Row[0].(string)
64041a2ea   Marko Tikvić   first commit
178
  	}
b0a6d6322   Marko Tikvić   still can't read utf
179
  	//fmt.Println(data)
5316eaa97   Marko Tikvić   ora v4 bug with u...
180
  	return []byte(data), nil
64041a2ea   Marko Tikvić   first commit
181
  }