Blame view

json_utility.go 4.37 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"
8dbe745c3   Marko Tikvić   merged tables uti...
6
  	"io"
d2ddf82ef   Marko Tikvić   started on new rbac
7
  	"net/http"
17a4d0447   Marko Tikvić   mutex lock for pa...
8
  	"sync"
1d0f61553   Marko Tikvić   can't fetch clob
9

1d0f61553   Marko Tikvić   can't fetch clob
10
  	"gopkg.in/rana/ora.v4"
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
  type LangMap map[string]map[string]string
  
  type Field struct {
d2ddf82ef   Marko Tikvić   started on new rbac
18
19
20
21
  	Parameter string `json:"param"`
  	Type      string `json:"type"`
  	Visible   bool   `json:"visible"`
  	Editable  bool   `json:"editable"`
64041a2ea   Marko Tikvić   first commit
22
  }
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"`
d2ddf82ef   Marko Tikvić   started on new rbac
35
36
37
38
  	Method       string             `json:"method"`
  	Params       map[string]string  `json:"params"`
  	Lang         []Translation      `json:"lang"`
  	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
  	// Data can only hold slices of any type. It can't be used for itteration
d2ddf82ef   Marko Tikvić   started on new rbac
43
  	Data interface{} `json:"data"`
64041a2ea   Marko Tikvić   first commit
44
  }
4a51e54d7   Marko Tikvić   simplified
45
  type Payload struct {
d2ddf82ef   Marko Tikvić   started on new rbac
46
47
48
49
  	Method       string             `json:"method"`
  	Params       map[string]string  `json:"params"`
  	Lang         []Translation      `json:"lang"`
  	Fields       []Field            `json:"fields"`
4a51e54d7   Marko Tikvić   simplified
50
51
  	Correlations []CorrelationField `json:"correlationFields"`
  	IdField      string             `json:"idField"`
e1fbb41f9   Marko Tikvić   added comments
52

d66628295   Marko Tikvić   cleaned up
53
54
  	// Data contains JSON payload.
  	// It can't be used for itteration
d2ddf82ef   Marko Tikvić   started on new rbac
55
  	Data interface{} `json:"data"`
4a51e54d7   Marko Tikvić   simplified
56
  }
d66628295   Marko Tikvić   cleaned up
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  // 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
  }
  
  // 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...
79
  // NewPayload returs a payload sceleton for provided table.
8dbe745c3   Marko Tikvić   merged tables uti...
80
81
  func NewPayload(r *http.Request, table string) Payload {
  	var pload Payload
64041a2ea   Marko Tikvić   first commit
82

4a51e54d7   Marko Tikvić   simplified
83
  	pload.Method = r.Method + " " + r.URL.Path
90f4ed079   Marko Tikvić   sped up loadTable()
84
85
  	if table != "" {
  		pload.Params = make(map[string]string, 0)
6ec91280b   Marko Tikvić   working on docume...
86
87
88
89
  		pload.Lang = translations(table)
  		pload.Fields = fields(table)
  		pload.IdField = id(table)
  		pload.Correlations = correlations(table)
90f4ed079   Marko Tikvić   sped up loadTable()
90
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
91
92
  	return pload
  }
64041a2ea   Marko Tikvić   first commit
93

6ec91280b   Marko Tikvić   working on docume...
94
  // DeliverPayload encodes payload to w.
8dbe745c3   Marko Tikvić   merged tables uti...
95
96
97
  func DeliverPayload(w http.ResponseWriter, payload Payload) {
  	json.NewEncoder(w).Encode(payload)
  	payload.Data = nil
64041a2ea   Marko Tikvić   first commit
98
  }
6ec91280b   Marko Tikvić   working on docume...
99
100
101
  // 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...
102

6ec91280b   Marko Tikvić   working on docume...
103
104
  	for _, pload := range payloads {
  		if pload.Type == ptype {
8dbe745c3   Marko Tikvić   merged tables uti...
105
  			for _, t := range pload.Lang {
8dbe745c3   Marko Tikvić   merged tables uti...
106
  				translations = append(translations, Translation{
d2ddf82ef   Marko Tikvić   started on new rbac
107
  					Language:     t.Language,
8dbe745c3   Marko Tikvić   merged tables uti...
108
109
110
111
  					FieldsLabels: t.FieldsLabels,
  				})
  			}
  		}
64041a2ea   Marko Tikvić   first commit
112
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
113
114
115
  
  	return translations
  }
6ec91280b   Marko Tikvić   working on docume...
116
117
118
  // 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...
119

6ec91280b   Marko Tikvić   working on docume...
120
121
  	for _, pload := range payloads {
  		if pload.Type == ptype {
8dbe745c3   Marko Tikvić   merged tables uti...
122
123
124
125
126
127
128
129
  			for _, f := range pload.Fields {
  				fields = append(fields, f)
  			}
  		}
  	}
  
  	return fields
  }
6ec91280b   Marko Tikvić   working on docume...
130
131
132
133
  // 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...
134
135
136
137
138
  			return pload.IdField
  		}
  	}
  	return ""
  }
6ec91280b   Marko Tikvić   working on docume...
139
140
141
  // 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...
142

6ec91280b   Marko Tikvić   working on docume...
143
144
145
146
  	for _, pload := range payloads {
  		if pload.Type == ptype {
  			for _, c := range pload.Correlations {
  				corr = append(corr, c)
8dbe745c3   Marko Tikvić   merged tables uti...
147
148
149
  			}
  		}
  	}
6ec91280b   Marko Tikvić   working on docume...
150
  	return corr
8dbe745c3   Marko Tikvić   merged tables uti...
151
  }
6ec91280b   Marko Tikvić   working on docume...
152
153
  // 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...
154
  func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
b0a6d6322   Marko Tikvić   still can't read utf
155
  	db.SetCfg(db.Cfg().SetClob(ora.S))
d2ddf82ef   Marko Tikvić   started on new rbac
156
  	stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT`+EqualQuotes(project), ora.S)
8dbe745c3   Marko Tikvić   merged tables uti...
157
  	defer stmt.Close()
8dbe745c3   Marko Tikvić   merged tables uti...
158
  	if err != nil {
5316eaa97   Marko Tikvić   ora v4 bug with u...
159
  		return nil, err
8dbe745c3   Marko Tikvić   merged tables uti...
160
161
162
163
  	}
  
  	rset, err := stmt.Qry()
  	if err != nil {
5316eaa97   Marko Tikvić   ora v4 bug with u...
164
  		return nil, err
8dbe745c3   Marko Tikvić   merged tables uti...
165
  	}
1d0f61553   Marko Tikvić   can't fetch clob
166
  	var data string
8dbe745c3   Marko Tikvić   merged tables uti...
167
  	if rset.Next() {
1d0f61553   Marko Tikvić   can't fetch clob
168
  		data = rset.Row[0].(string)
64041a2ea   Marko Tikvić   first commit
169
  	}
b0a6d6322   Marko Tikvić   still can't read utf
170
  	//fmt.Println(data)
5316eaa97   Marko Tikvić   ora v4 bug with u...
171
  	return []byte(data), nil
64041a2ea   Marko Tikvić   first commit
172
  }