view pkg/models/intservices.go @ 951:6eb45b3c73f6

Fixed style loading for GeoServer.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 15 Oct 2018 15:24:02 +0200
parents 2ebf677fc2e1
children a244b18cb916
line wrap: on
line source

package models

import (
	"context"
	"database/sql"
	"log"
	"net/http"
	"sync"

	"gemma.intevation.de/gemma/pkg/auth"
	"gemma.intevation.de/gemma/pkg/config"
)

type IntEntry struct {
	Name  string `json:"name"`
	Style bool   `json:"style"`
	WMS   bool   `json:"wms"`
	WFS   bool   `json:"wfs"`
}

type IntServices struct {
	entries []IntEntry
	mu      sync.Mutex
}

const (
	selectServicesSQL = `
SELECT relname, style IS NOT NULL, as_wms, as_wfs
FROM sys_admin.published_services
JOIN pg_class ON name = oid ORDER by relname`

	selectStyleSQL = `
SELECT style
FROM sys_admin.published_services
JOIN pg_class ON name = oid
WHERE relname = $1`

	updateStyleSQL = `
UPDATE sys_admin.published_services
SET style = $1::bytea
WHERE name IN (SELECT oid FROM pg_class WHERE relname = $2)`
)

var InternalServices = &IntServices{}

func (e *IntEntry) LoadStyle() (string, error) {
	var style string
	err := auth.RunAs("sys_admin", context.Background(),
		func(conn *sql.Conn) error {
			return conn.QueryRowContext(
				context.Background(),
				selectStyleSQL,
				e.Name).Scan(&style)
		})
	return style, err
}

func UpdateInternalStyle(req *http.Request, name, style string) error {
	return auth.RunAsSessionUser(req, func(conn *sql.Conn) error {
		_, err := conn.ExecContext(
			req.Context(), updateStyleSQL,
			style, name)
		if err == nil {
			InternalServices.Invalidate()
		}
		return err
	})
}

func (ps *IntServices) Find(name string) (string, bool) {
	ps.mu.Lock()
	defer ps.mu.Unlock()

	if ps.entries == nil {
		if err := ps.load(); err != nil {
			log.Printf("error: %v\n", err)
			return "", false
		}
	}

	if ps.has(name) {
		return config.GeoServerURL() + "/" + name, true
	}
	return "", false
}

func (ps *IntServices) has(service string) bool {
	var check func(*IntEntry) bool
	switch service {
	case "wms":
		check = func(e *IntEntry) bool { return e.WMS }
	case "wfs":
		check = func(e *IntEntry) bool { return e.WFS }
	default:
		return false
	}
	for i := range ps.entries {
		if check(&ps.entries[i]) {
			return true
		}
	}
	return false
}

func (ps *IntServices) load() error {
	// make empty slice to prevent retry if slice is empty.
	ps.entries = []IntEntry{}
	return auth.RunAs("sys_admin", context.Background(),
		func(conn *sql.Conn) error {
			rows, err := conn.QueryContext(
				context.Background(), selectServicesSQL)
			if err != nil {
				return err
			}
			defer rows.Close()
			for rows.Next() {
				var entry IntEntry
				if err := rows.Scan(
					&entry.Name, &entry.Style,
					&entry.WMS, &entry.WFS,
				); err != nil {
					return err
				}
				ps.entries = append(ps.entries, entry)
			}
			return rows.Err()
		})
}

func (ps *IntServices) Invalidate() {
	ps.mu.Lock()
	ps.entries = nil
	ps.mu.Unlock()
}

func InternalAll(IntEntry) bool        { return true }
func IntWMS(entry IntEntry) bool       { return entry.WMS }
func IntWFS(entry IntEntry) bool       { return entry.WFS }
func IntWithStyle(entry IntEntry) bool { return entry.Style }

func IntByName(name string) func(IntEntry) bool {
	return func(entry IntEntry) bool { return entry.Name == name }
}

func IntAnd(accept ...func(IntEntry) bool) func(IntEntry) bool {
	return func(entry IntEntry) bool {
		for _, a := range accept {
			if !a(entry) {
				return false
			}
		}
		return true
	}
}

func (ps *IntServices) Filter(accept func(IntEntry) bool) []IntEntry {
	ps.mu.Lock()
	defer ps.mu.Unlock()
	if ps.entries == nil {
		if err := ps.load(); err != nil {
			log.Printf("error: %v\n", err)
			return nil
		}
	}
	pe := make([]IntEntry, 0, len(ps.entries))
	for _, e := range ps.entries {
		if accept(e) {
			pe = append(pe, e)
		}
	}

	return pe
}