tables_utility.go 2.91 KB
package restutility

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

type TableConfig struct {
	Tables []Table
}

type Table struct {
	TableType    string             `json:"tableType"`
	Translations []TableTranslation `json:"translations"`
	TableFields  []Field            `json:"tableFields"`
	Correlations []CorrelationField `json:"correlation_fields"`
	IdField      string             `json:"idField"`
}

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

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

func (tl TableConfig) LoadTranslations(tableType string) LangMap {
	translations := make(LangMap, 0)

	for _, table := range tl.Tables {
		if tableType == table.TableType {
			for _, t := range table.Translations {
				translations[t.Language] = t.FieldsLabels
			}
		}
	}

	return translations
}

func (tl TableConfig) LoadFields(tableType string) []Field {
	fields := make([]Field, 0)

	for _, table := range tl.Tables {
		if tableType == table.TableType {
			for _, f := range table.TableFields {
				fields = append(fields, f)
			}
		}
	}

	return fields
}

func (tl TableConfig) LoadIdField(tableType string) string {
	for _, table := range tl.Tables {
		if tableType == table.TableType {
			return table.IdField
		}
	}
	return ""
}

func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField {
	resp := make([]CorrelationField, 0)

	for _, table := range tl.Tables {
		if tableType == table.TableType {
			for _, f := range table.Correlations {
				resp = append(resp, f)
			}
		}
	}

	return resp
}

var _tables TableConfig
var _prevProject string

func getTablesConfigJSON(project string) error {
	_prevProject = project
	stmt, err := Oracle.Ses.Prep(`SELECT
		JSON_CLOB
		FROM TABLES_CONFIG
		WHERE PROJEKAT` + project, ora.S)
	defer stmt.Close()

	if err != nil {
		return err
	}

	rset, err := stmt.Qry()
	if err != nil {
		return err
	}

	if rset.Next() {
		lob := rset.Row[0].(io.Reader)
		bytes, err := ioutil.ReadAll(lob)
		if err != nil {
			fmt.Printf("mega error: %v\n", err)
		}
		json.Unmarshal(bytes, &_tables.Tables)
	}

	return nil
}

func loadTablesConfig(project string) error {
	err := getTablesConfigJSON(putQuotes(project))
	//file, err := ioutil.ReadFile("./config/tables-config.json")
	if err != nil {
		fmt.Printf("%v\n", err);
		return errors.New("unable to load tables config")
	}

	//json.Unmarshal(file, &_TABLES_CONFIG.Tables)

	if len(_TABLES_CONFIG.Tables) == 0 {
		return errors.New("tables config is corrupt")
	}

	return nil
}

func loadTable(table string) JSONParams {
	return NewJSONParams(_tables.LoadTranslations(table),
			_tables.LoadFields(table),
			_tables.LoadIdField(table),
			_tables.LoadCorrelations(table))
}

func refreshTables() error {
	return getTablesConfigJSON(_prevProject)
}