Commit d66628295471b832e377f7a6867a083f9f771b82

Authored by Marko Tikvić
1 parent 1769d6d42e
Exists in master and in 1 other branch v2

cleaned up

... ... @@ -5,12 +5,14 @@ import (
5 5 "net/http"
6 6 )
7 7  
8   -const templateHttpErr500_EN = "An internal server error has occurred."
9   -const templateHttpErr500_RS = "Došlo je do greške na serveru."
10   -const templateHttpErr400_EN = "Bad request: invalid request body."
11   -const templateHttpErr400_RS = "Neispravan zahtev."
12   -const templateHttpErr401_EN = "Unauthorized request."
13   -const templateHttpErr401_RS = "Neautorizovan zahtev."
  8 +const (
  9 + templateHttpErr500_EN = "An internal server error has occurred."
  10 + templateHttpErr500_RS = "Došlo je do greške na serveru."
  11 + templateHttpErr400_EN = "Bad request: invalid request body."
  12 + templateHttpErr400_RS = "Neispravan zahtev."
  13 + templateHttpErr401_EN = "Unauthorized request."
  14 + templateHttpErr401_RS = "Neautorizovan zahtev."
  15 +)
14 16  
15 17 type httpError struct {
16 18 Error []HttpErrorDesc `json:"error"`
... ... @@ -65,6 +67,7 @@ func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
65 67 })
66 68 }
67 69  
  70 +// SetDefaultHeaders set's default headers for an HTTP response.
68 71 func SetDefaultHeaders(w http.ResponseWriter) {
69 72 w.Header().Set("Access-Control-Allow-Origin", "*")
70 73  
... ...
1 1 package webutility
2 2  
3 3 import (
4   - //"fmt"
5 4 "encoding/json"
6 5 "errors"
7 6 "io"
8 7 "net/http"
9   - //"io/ioutil"
10 8 "sync"
11 9  
12   - //"gopkg.in/rana/ora.v3"
13 10 "gopkg.in/rana/ora.v4"
14 11 )
15 12  
... ... @@ -57,10 +54,34 @@ type Payload struct {
57 54 Correlations []CorrelationField `json:"correlationFields"`
58 55 IdField string `json:"idField"`
59 56  
60   - // Data can only hold slices of any type. It can't be used for itteration
  57 + // Data contains JSON payload.
  58 + // It can't be used for itteration
61 59 Data interface{} `json:"data"`
62 60 }
63 61  
  62 +// InitTables loads all payloads in the payloads variable.
  63 +// Returns an error if it fails.
  64 +func InitTables(db *ora.Ses, project string) error {
  65 + jsonbuf, err := fetchJSON(db, project)
  66 + if err != nil {
  67 + return err
  68 + }
  69 +
  70 + mu.Lock()
  71 + defer mu.Unlock()
  72 + json.Unmarshal(jsonbuf, &payloads)
  73 + if len(payloads) == 0 {
  74 + return errors.New("tables config is corrupt")
  75 + }
  76 + return nil
  77 +}
  78 +
  79 +// DecodeJSON decodes JSON data from r to v.
  80 +// Returns an error if it fails.
  81 +func DecodeJSON(r io.Reader, v interface{}) error {
  82 + return json.NewDecoder(r).Decode(v)
  83 +}
  84 +
64 85 // NewPayload returs a payload sceleton for provided table.
65 86 func NewPayload(r *http.Request, table string) Payload {
66 87 var pload Payload
... ... @@ -140,23 +161,6 @@ func correlations(ptype string) []CorrelationField {
140 161 return corr
141 162 }
142 163  
143   -// InitTables loads all payloads in the payloads variable.
144   -// Returns an error if it fails.
145   -func InitTables(db *ora.Ses, project string) error {
146   - jsonbuf, err := fetchJSON(db, project)
147   - if err != nil {
148   - return err
149   - }
150   -
151   - mu.Lock()
152   - defer mu.Unlock()
153   - json.Unmarshal(jsonbuf, &payloads)
154   - if len(payloads) == 0 {
155   - return errors.New("tables config is corrupt")
156   - }
157   - return nil
158   -}
159   -
160 164 // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table.
161 165 // Returns an error if it fails.
162 166 func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
... ... @@ -180,9 +184,3 @@ func fetchJSON(db *ora.Ses, project string) ([]byte, error) {
180 184 //fmt.Println(data)
181 185 return []byte(data), nil
182 186 }
183   -
184   -// DecodeJSON decodes JSON data from r to v.
185   -// Returns an error if it fails.
186   -func DecodeJSON(r io.Reader, v interface{}) error {
187   - return json.NewDecoder(r).Decode(v)
188   -}
... ...
1 1 package webutility
2 2  
3   -//import "gopkg.in/rana/ora.v3"
4 3 import "gopkg.in/rana/ora.v4"
5 4  
6 5 type ListOptions struct {
... ... @@ -29,15 +28,14 @@ type Dropdown struct {
29 28 FiltersField string `json:"filtersField"`
30 29 IDField string `json:"idField"`
31 30 LabelField string `json:"labelField"`
32   -
33 31 }
34 32  
35 33 type ListGraph struct {
36 34 ObjectType string `json:"objectType"`
37   - X string `json:"xField"`
38   - Y string `json:"yField"`
  35 + X string `json:"xField"`
  36 + Y string `json:"yField"`
39 37 GroupField string `json:"groupField"`
40   - Label string `json:"label"`
  38 + Label string `json:"label"`
41 39 }
42 40  
43 41 type ListActions struct {
... ... @@ -126,7 +124,7 @@ func GetListConfigObjectIDField(db *ora.Ses, otype string) string {
126 124 stmt, err = db.Prep(`SELECT
127 125 ID_FIELD
128 126 FROM LIST_CONFIG_ID_FIELD
129   - WHERE OBJECT_TYPE = '` + otype + `'`,
  127 + WHERE OBJECT_TYPE = '`+otype+`'`,
130 128 ora.S)
131 129  
132 130 defer stmt.Close()
... ... @@ -151,14 +149,14 @@ func GetListConfigObjectIDField(db *ora.Ses, otype string) string {
151 149 func newDefaultList(objType string) ListConfig {
152 150 list := ListConfig{
153 151 ObjectType: objType,
154   - Title: objType,
155   - LazyLoad: false,
  152 + Title: objType,
  153 + LazyLoad: false,
156 154 Options: ListOptions{
157   - GlobalFilter: true,
158   - LocalFilters: true,
  155 + GlobalFilter: true,
  156 + LocalFilters: true,
159 157 RemoteFilters: false,
160   - Pagination: true,
161   - PageSize: 20,
  158 + Pagination: true,
  159 + PageSize: 20,
162 160 },
163 161 Filters: nil,
164 162 Actions: ListActions{
... ... @@ -166,10 +164,10 @@ func newDefaultList(objType string) ListConfig {
166 164 Update: false,
167 165 Delete: false,
168 166 Export: false,
169   - Print: false,
170   - Graph: false,
  167 + Print: false,
  168 + Graph: false,
171 169 },
172   - Parent: nil,
  170 + Parent: nil,
173 171 Navigation: nil,
174 172 }
175 173  
... ... @@ -223,7 +221,7 @@ func getListNavigation(db *ora.Ses, listObjType string) ([]ListNavNode, error) {
223 221 a.OBJECT_TYPE, a.PARENT_OBJECT_TYPE, a.LABEL, a.ICON, a.PARENT_FILTER_FIELD, b.PARENT_ID_FIELD, b.RB
224 222 FROM LIST_CONFIG_NAVIGATION b
225 223 JOIN LIST_CONFIG_CHILD a ON b.PARENT_CHILD_ID = a.PARENT_CHILD_ID
226   - WHERE b.LIST_OBJECT_TYPE = '`+listObjType+`'
  224 + WHERE b.LIST_OBJECT_TYPE = '` + listObjType + `'
227 225 ORDER BY b.RB ASC`
228 226  
229 227 stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S, ora.S)
... ... @@ -238,12 +236,12 @@ func getListNavigation(db *ora.Ses, listObjType string) ([]ListNavNode, error) {
238 236 }
239 237 for rset.Next() {
240 238 resp = append(resp, ListNavNode{
241   - ObjectType: rset.Row[0].(string),
242   - ParentObjectType: rset.Row[1].(string),
243   - LabelField: rset.Row[2].(string),
244   - Icon: rset.Row[3].(string),
  239 + ObjectType: rset.Row[0].(string),
  240 + ParentObjectType: rset.Row[1].(string),
  241 + LabelField: rset.Row[2].(string),
  242 + Icon: rset.Row[3].(string),
245 243 ParentFilterField: rset.Row[4].(string),
246   - ParentIDField: rset.Row[5].(string),
  244 + ParentIDField: rset.Row[5].(string),
247 245 // RB is ignored
248 246 })
249 247 }
... ... @@ -379,8 +377,8 @@ func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]_filter, error
379 377 }
380 378 for rset.Next() {
381 379 resp = append(resp, _filter{
382   - Type: rset.Row[0].(string),
383   - Label: rset.Row[1].(string),
  380 + Type: rset.Row[0].(string),
  381 + Label: rset.Row[1].(string),
384 382 DefaultValues: rset.Row[2].(string),
385 383 })
386 384 }
... ... @@ -428,7 +426,7 @@ func sortFilters(filters []ListFilter) {
428 426 var temp ListFilter
429 427 for !done {
430 428 done = true
431   - for i := 0; i < len(filters) - 1; i++ {
  429 + for i := 0; i < len(filters)-1; i++ {
432 430 if filters[i].Position > filters[i+1].Position {
433 431 done = false
434 432 temp = filters[i]
... ... @@ -462,10 +460,10 @@ func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) {
462 460 for rset.Next() {
463 461 resp = append(resp, ListGraph{
464 462 ObjectType: rset.Row[0].(string),
465   - X: rset.Row[1].(string),
466   - Y: rset.Row[2].(string),
  463 + X: rset.Row[1].(string),
  464 + Y: rset.Row[2].(string),
467 465 GroupField: rset.Row[3].(string),
468   - Label: rset.Row[4].(string),
  466 + Label: rset.Row[4].(string),
469 467 })
470 468 }
471 469 if rset.Err() != nil {
... ... @@ -534,8 +532,8 @@ func getListParent(db *ora.Ses, objType string) ([]ListParentNode, error) {
534 532 }
535 533 for rset.Next() {
536 534 resp = append(resp, ListParentNode{
537   - ObjectType: rset.Row[0].(string),
538   - LabelField: rset.Row[1].(string),
  535 + ObjectType: rset.Row[0].(string),
  536 + LabelField: rset.Row[1].(string),
539 537 FilterField: rset.Row[2].(string),
540 538 })
541 539 }
... ... @@ -568,10 +566,10 @@ func getListPivot(db *ora.Ses, objType string) ([]ListPivot, error) {
568 566 }
569 567 for rset.Next() {
570 568 resp = append(resp, ListPivot{
571   - ObjectType: rset.Row[0].(string),
572   - GroupField: rset.Row[1].(string),
  569 + ObjectType: rset.Row[0].(string),
  570 + GroupField: rset.Row[1].(string),
573 571 DistinctField: rset.Row[2].(string),
574   - Value: rset.Row[3].(string),
  572 + Value: rset.Row[3].(string),
575 573 })
576 574 }
577 575 if rset.Err() != nil {
... ...
1 1 package webutility
2 2  
3   -//import "gopkg.in/rana/ora.v3"
4 3 import "gopkg.in/rana/ora.v4"
5 4  
6 5 type SelectConfig struct {
... ...
1 1 package webutility
2 2  
3   -import (
4   - "strings"
5   -)
  3 +import "strings"
6 4  
7 5 var patern string = "\"';&*<>=\\`:"
8 6  
... ...