json_utility.go 3.44 KB
package restutility

import (
	"net/http"
	"strings"
	"encoding/json"
	"errors"
	"gopkg.in/rana/ora.v3"
	"io"
	"io/ioutil"
	"fmt"
)

var allPayloads []Payload

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"`
}

type CorrelationField struct {
	Result   string   `json:"result"`
	Elements []string `json:"elements"`
	Type     string   `json:"type"`
}

type Translation struct {
	Language string `json:"language"`
	FieldsLabels map[string]string `json:"fieldsLabels"`
}

type Payload struct {
	Type         string             `json:"tableType"`
	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"`
}

func NewPayload(r *http.Request, table string) Payload {
	var pload Payload

	pload.Method = strings.ToLower(r.Method + " " + r.URL.Path)
	pload.Params = make(map[string]string, 0)
	pload.Lang = loadTranslations(allPayloads, table)
	pload.Fields = loadFields(allPayloads, table)
	pload.IdField = loadIdField(allPayloads, table)
	pload.Correlations = loadCorrelations(allPayloads, table)

	return pload
}

func DeliverPayload(w http.ResponseWriter, payload Payload) {
	json.NewEncoder(w).Encode(payload)
	payload.Data = nil
}

func loadTranslations(payloads []Payload, id string) []Translation {
	translations := make([]Translation, 0)

	for _, pload := range payloads {
		if pload.Type == id {
			for _, t := range pload.Lang {
				//translations[t.Language] = t.FieldsLabels
				translations = append(translations, Translation{
					Language: t.Language,
					FieldsLabels: t.FieldsLabels,
				})
			}
		}
	}

	return translations
}

func loadFields(payloads []Payload, id string) []Field {
	fields := make([]Field, 0)

	for _, pload := range payloads {
		if pload.Type == id{
			for _, f := range pload.Fields {
				fields = append(fields, f)
			}
		}
	}

	return fields
}

func loadIdField(payloads []Payload, id string) string {
	for _, pload := range payloads {
		if pload.Type == id {
			return pload.IdField
		}
	}
	return ""
}

func loadCorrelations(payloads []Payload, id string) []CorrelationField {
	resp := make([]CorrelationField, 0)

	for _, pload := range payloads {
		if pload.Type == id {
			for _, f := range pload.Correlations {
				resp = append(resp, f)
			}
		}
	}

	return resp
}

func InitTables(db *ora.Ses, project string) error {
	jsonbuf, _ := fetchTablesConfig(db, EqualQuotes(project))
	json.Unmarshal(jsonbuf, &allPayloads)
	if len(allPayloads) == 0 {
		return errors.New("tables config is corrupt")
	}
	fmt.Printf("broj ucitanih tabela: %d\n", len(allPayloads))
	return nil
}

func fetchTablesConfig(db *ora.Ses, project string) ([]byte, error) {
	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 {
			fmt.Printf("mega error: %v\n", err)
		}
	}

	return bytes, nil
}