Blame view

json_utility.go 3.87 KB
64041a2ea   Marko Tikvić   first commit
1
2
3
4
  package restutility
  
  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"
64041a2ea   Marko Tikvić   first commit
10
  )
4a51e54d7   Marko Tikvić   simplified
11
  var allPayloads []payloadBuff
8dbe745c3   Marko Tikvić   merged tables uti...
12

64041a2ea   Marko Tikvić   first commit
13
14
15
16
17
18
19
20
  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...
21
22
23
24
25
26
27
  type CorrelationField struct {
  	Result   string   `json:"result"`
  	Elements []string `json:"elements"`
  	Type     string   `json:"type"`
  }
  
  type Translation struct {
ecec68b18   Marko Tikvić   updated todo list
28
  	Language     string            `json:"language"`
8dbe745c3   Marko Tikvić   merged tables uti...
29
  	FieldsLabels map[string]string `json:"fieldsLabels"`
64041a2ea   Marko Tikvić   first commit
30
  }
7d3deb50d   Marko Tikvić   modified list_con...
31
32
  // Type is not required in payload. This is only a bridge between ORACLE CLOB and
  // Payload type.
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
41
42
43
  	IdField      string             `json:"idField"`
  	// Data can only hold slices of any type. It can't be used for itteration
  	Data	     interface{}        `json:"data"`
  }
4a51e54d7   Marko Tikvić   simplified
44
45
46
47
48
49
50
51
52
53
  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"`
  }
8dbe745c3   Marko Tikvić   merged tables uti...
54
55
  func NewPayload(r *http.Request, table string) Payload {
  	var pload Payload
64041a2ea   Marko Tikvić   first commit
56

4a51e54d7   Marko Tikvić   simplified
57
  	pload.Method = r.Method + " " + r.URL.Path
90f4ed079   Marko Tikvić   sped up loadTable()
58
59
60
61
62
63
64
65
  
  	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)
  	}
64041a2ea   Marko Tikvić   first commit
66

8dbe745c3   Marko Tikvić   merged tables uti...
67
68
  	return pload
  }
64041a2ea   Marko Tikvić   first commit
69

8dbe745c3   Marko Tikvić   merged tables uti...
70
71
72
  func DeliverPayload(w http.ResponseWriter, payload Payload) {
  	json.NewEncoder(w).Encode(payload)
  	payload.Data = nil
64041a2ea   Marko Tikvić   first commit
73
  }
4a51e54d7   Marko Tikvić   simplified
74
  func loadTranslations(id string) []Translation {
8dbe745c3   Marko Tikvić   merged tables uti...
75
  	translations := make([]Translation, 0)
4a51e54d7   Marko Tikvić   simplified
76
  	for _, pload := range allPayloads {
8dbe745c3   Marko Tikvić   merged tables uti...
77
78
79
80
81
82
83
84
85
  		if pload.Type == id {
  			for _, t := range pload.Lang {
  				//translations[t.Language] = t.FieldsLabels
  				translations = append(translations, Translation{
  					Language: t.Language,
  					FieldsLabels: t.FieldsLabels,
  				})
  			}
  		}
64041a2ea   Marko Tikvić   first commit
86
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
87
88
89
  
  	return translations
  }
4a51e54d7   Marko Tikvić   simplified
90
  func loadFields(id string) []Field {
8dbe745c3   Marko Tikvić   merged tables uti...
91
  	fields := make([]Field, 0)
4a51e54d7   Marko Tikvić   simplified
92
  	for _, pload := range allPayloads {
8dbe745c3   Marko Tikvić   merged tables uti...
93
94
95
96
97
98
99
100
101
  		if pload.Type == id{
  			for _, f := range pload.Fields {
  				fields = append(fields, f)
  			}
  		}
  	}
  
  	return fields
  }
4a51e54d7   Marko Tikvić   simplified
102
103
  func loadIdField(id string) string {
  	for _, pload := range allPayloads {
8dbe745c3   Marko Tikvić   merged tables uti...
104
105
106
107
108
109
  		if pload.Type == id {
  			return pload.IdField
  		}
  	}
  	return ""
  }
4a51e54d7   Marko Tikvić   simplified
110
  func loadCorrelations(id string) []CorrelationField {
8dbe745c3   Marko Tikvić   merged tables uti...
111
  	resp := make([]CorrelationField, 0)
4a51e54d7   Marko Tikvić   simplified
112
  	for _, pload := range allPayloads {
8dbe745c3   Marko Tikvić   merged tables uti...
113
114
115
116
117
118
119
120
121
122
123
  		if pload.Type == id {
  			for _, f := range pload.Correlations {
  				resp = append(resp, f)
  			}
  		}
  	}
  
  	return resp
  }
  
  func InitTables(db *ora.Ses, project string) error {
7d3deb50d   Marko Tikvić   modified list_con...
124
  	jsonbuf, _ := fetchJSON(db, EqualQuotes(project))
8dbe745c3   Marko Tikvić   merged tables uti...
125
126
127
128
  	json.Unmarshal(jsonbuf, &allPayloads)
  	if len(allPayloads) == 0 {
  		return errors.New("tables config is corrupt")
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
129
130
  	return nil
  }
7d3deb50d   Marko Tikvić   modified list_con...
131
  func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
8dbe745c3   Marko Tikvić   merged tables uti...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  	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...
152
  			// Ignore for now, it's some weird streaming read/write LOB error.
4a51e54d7   Marko Tikvić   simplified
153
  			// TODO: Find a fix for this.
4a51e54d7   Marko Tikvić   simplified
154
  			//return nil, err
8dbe745c3   Marko Tikvić   merged tables uti...
155
  		}
64041a2ea   Marko Tikvić   first commit
156
  	}
8dbe745c3   Marko Tikvić   merged tables uti...
157
  	return bytes, nil
64041a2ea   Marko Tikvić   first commit
158
  }