json_utility.go 6.79 KB
package webutility

import (
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"
	"os"
	"sync"
	"time"

	"gopkg.in/rana/ora.v4"
)

var (
	mu        = &sync.Mutex{}
	metadata  = make(map[string]Payload)
	updateQue = make(map[string][]byte)

	metadataDB    *ora.Ses
	activeProject string

	inited bool
)

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 {
	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 holds JSON payload. It can't be used for itteration.
	Data interface{} `json:"data"`
}

// LoadPayloadsdetaData loads all payloads' information into 'metadata' variable.
func LoadPayloadsMetadata(db *ora.Ses, project string, hotloading bool, hlPeriod int) error {
	metadataDB = db
	activeProject = project

	mu.Lock()
	defer mu.Unlock()
	err := initMetadata(project)
	if err != nil {
		return err
	}
	if hotloading {
		go hotload(hlPeriod)
	}
	inited = true

	return nil
}

func GetMetadataForAllEntities() map[string]Payload {
	return metadata
}

func GetMetadataForEntity(t string) (Payload, bool) {
	p, ok := metadata[t]
	return p, ok
}

func QueEntityModelUpdate(entityType string, v interface{}) {
	updateQue[entityType], _ = json.Marshal(v)
}

func UpdateEntityModels(forceUpdate bool) (total, upd, add int, err error) {
	if !inited {
		return 0, 0, 0, errors.New("webutil: metadata not initialized but update was tried.")
	}

	total = len(updateQue)

	forUpdate := make([]string, 0)
	forAdd := make([]string, 0)

	for k, _ := range updateQue {
		if _, exists := metadata[k]; exists {
			if forceUpdate {
				forUpdate = append(forUpdate, k)
			}
		} else {
			forAdd = append(forAdd, k)
		}
	}

	for _, k := range forUpdate {
		fmt.Printf("for update: %s\n", k)

		_, err := metadataDB.PrepAndExe(`update entities set
			entity_model = :1
			where entity_type = :2`,
			string(updateQue[k]),
			k)

		if err != nil {
			fmt.Printf("webutility: update metadata: prep and exe: %v\n", err)
			continue
		}
		upd++
	}

	blankPayload, _ := json.Marshal(Payload{})
	for _, k := range forAdd {
		fmt.Printf("for add: %s\n", k)

		_, err := metadataDB.PrepAndExe(`insert into entities
			(projekat, metadata, entity_type, entity_model)
			values(:1, :2, :3, :4)`,
			activeProject,
			string(blankPayload),
			k,
			string(updateQue[k]))

		if err != nil {
			fmt.Printf("webutility: add metadata: prep and exe: %v\n", err)
			continue
		}
		metadata[k] = Payload{}
		add++
	}

	return total, upd, add, nil
}

func ModifyMetadataForEntity(entityType string, p *Payload) error {
	md, err := json.Marshal(*p)
	if err != nil {
		return err
	}

	mu.Lock()
	defer mu.Unlock()
	_, err = metadataDB.PrepAndExe(`update entities set
		metadata = :1
		where projekat = :2
		and entity_type = :3`,
		string(md),
		activeProject,
		entityType)
	if err != nil {
		return err
	}
	return nil
}

func DeleteEntityModel(entityType string) error {
	_, err := metadataDB.PrepAndExe("delete from entities where entity_type = :1", entityType)
	if err == nil {
		mu.Lock()
		delete(metadata, entityType)
		mu.Unlock()
	}
	return err
}

// NewPayload returs a payload sceleton for entity described with etype.
func NewPayload(r *http.Request, etype string) Payload {
	pload := metadata[etype]
	pload.Method = r.Method + " " + r.RequestURI
	return pload
}

// 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)
}

func initMetadata(project string) error {
	metadataDB.SetCfg(metadataDB.Cfg().SetClob(ora.S))
	stmt, err := metadataDB.Prep(`select
		entity_type,
		metadata
		from entities
		where projekat = `+fmt.Sprintf("'%s'", project),
		ora.S,
		ora.S)

	defer stmt.Close()
	if err != nil {
		return err
	}

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

	count := 0
	success := 0
	if len(metadata) > 0 {
		metadata = nil
	}
	metadata = make(map[string]Payload)
	for rset.Next() {
		name := rset.Row[0].(string)
		load := []byte(rset.Row[1].(string))

		p := Payload{}
		err := json.Unmarshal(load, &p)
		if err != nil {
			fmt.Printf("couldn't init: '%s' metadata\n", name)
		} else {
			success++
			metadata[name] = p
		}
		count++
	}
	fmt.Printf("webutility: successfully loaded %d/%d (%.1f%%) entities\n",
		success, count, float32(success)/float32(count)*100.0)

	return nil
}

func hotload(n int) {
	entityScan := make(map[string]int64)
	firstCheck := true
	for {
		time.Sleep(time.Duration(n) * time.Second)
		stmt, err := metadataDB.Prep(`select
			ora_rowscn,
			entity_type
			from entities where projekat = `+fmt.Sprintf("'%s'", activeProject),
			ora.I64,
			ora.S)
		if err != nil {
			fmt.Fprintf(os.Stderr, "hotload failed: %v\n", err)
			time.Sleep(time.Duration(n) * time.Second)
			continue
		}

		rset, err := stmt.Qry()
		if err != nil {
			fmt.Fprintf(os.Stderr, "hotload failed: %v\n", err)
			time.Sleep(time.Duration(n) * time.Second)
			continue
		}

		var toRefresh []string
		for rset.Next() {
			scanID := rset.Row[0].(int64)
			entity := rset.Row[1].(string)
			oldID, ok := entityScan[entity]
			if !ok || oldID != scanID {
				entityScan[entity] = scanID
				toRefresh = append(toRefresh, entity)
			}
		}
		stmt.Close()

		if rset.Err() != nil {
			fmt.Fprintf(os.Stderr, "hotload rset error: %v\n", rset.Err())
			time.Sleep(time.Duration(n) * time.Second)
			continue
		}

		if len(toRefresh) > 0 && !firstCheck {
			mu.Lock()
			refreshMetadata(toRefresh)
			mu.Unlock()
		}
		if firstCheck {
			firstCheck = false
		}
	}
}

func refreshMetadata(entities []string) {
	for _, e := range entities {
		fmt.Printf("refreshing %s\n", e)
		stmt, err := metadataDB.Prep(`select
			metadata
			from entities
			where projekat = `+fmt.Sprintf("'%s'", activeProject)+
			` and entity_type = `+fmt.Sprintf("'%s'", e),
			ora.S)

		if err != nil {
			fmt.Printf("webutility: refresh: prep: %v\n", err)
			stmt.Close()
			continue
		}

		rset, err := stmt.Qry()
		if err != nil {
			fmt.Printf("webutility: refresh: query: %v\n", err)
			stmt.Close()
			continue
		}

		for rset.Next() {
			load := []byte(rset.Row[0].(string))
			p := Payload{}
			err := json.Unmarshal(load, &p)
			if err != nil {
				fmt.Printf("couldn't refresh: '%s' metadata\n", e)
			} else {
				metadata[e] = p
			}
		}
		stmt.Close()
	}
}