changeset 913:876d1f5433be geo-style

Started with direct applying style after modification via controller. WIP.
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 02 Oct 2018 23:29:55 +0200
parents 85b2648066d7
children 4bf3a3a20ce1
files pkg/controllers/geostyling.go pkg/geoserver/boot.go pkg/models/intservices.go
diffstat 3 files changed, 77 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/geostyling.go	Tue Oct 02 22:25:44 2018 +0200
+++ b/pkg/controllers/geostyling.go	Tue Oct 02 23:29:55 2018 +0200
@@ -7,6 +7,7 @@
 	"log"
 	"net/http"
 
+	"gemma.intevation.de/gemma/pkg/geoserver"
 	"gemma.intevation.de/gemma/pkg/models"
 	"github.com/gorilla/mux"
 )
@@ -64,5 +65,5 @@
 		return
 	}
 
-	// TODO: Configure GeoServer
+	geoserver.ReconfigureStyle(feature)
 }
--- a/pkg/geoserver/boot.go	Tue Oct 02 22:25:44 2018 +0200
+++ b/pkg/geoserver/boot.go	Tue Oct 02 23:29:55 2018 +0200
@@ -318,9 +318,24 @@
 	return err
 }
 
-func ensureStyles() error {
-	log.Println("info: creating styles")
+type styles struct {
+	Styles struct {
+		Style []struct {
+			Name string `json:"name"`
+		} `json:"style"`
+	} `json:"styles"`
+}
 
+func (s *styles) hasStyle(name string) bool {
+	for i := range s.Styles.Style {
+		if s.Styles.Style[i].Name == name {
+			return true
+		}
+	}
+	return false
+}
+
+func (s *styles) load() error {
 	var (
 		geoURL   = config.GeoServerURL()
 		user     = config.GeoServerUser()
@@ -328,25 +343,6 @@
 		auth     = basicAuth(user, password)
 	)
 
-	type styles struct {
-		Styles struct {
-			Style []struct {
-				Name string `json:"name"`
-			} `json:"style"`
-		} `json:"styles"`
-	}
-
-	var stls styles
-
-	hasStyle := func(name string) bool {
-		for i := range stls.Styles.Style {
-			if stls.Styles.Style[i].Name == name {
-				return true
-			}
-		}
-		return false
-	}
-
 	req, err := http.NewRequest(
 		http.MethodGet,
 		geoURL+"/rest/workspaces/"+workspaceName+"/styles.json",
@@ -359,18 +355,36 @@
 	if err != nil {
 		return err
 	}
+	defer resp.Body.Close()
 
 	// Fetch all styles
-	if err := json.NewDecoder(resp.Body).Decode(&stls); err != nil {
+	if err := json.NewDecoder(resp.Body).Decode(s); err != nil {
 		// XXX: Same quirk as with featuretypes.
 	}
-	resp.Body.Close()
+	return nil
+}
+
+func ensureStyles() error {
+	log.Println("info: creating styles")
+
+	var (
+		geoURL   = config.GeoServerURL()
+		user     = config.GeoServerUser()
+		password = config.GeoServerPassword()
+		auth     = basicAuth(user, password)
+	)
+
+	var stls styles
+
+	if err := stls.load(); err != nil {
+		return err
+	}
 
 	entries := models.InternalServices.Filter(
 		models.IntAnd(models.IntWMS, models.IntWithStyle))
 
 	for i := range entries {
-		if hasStyle(entries[i].Name) {
+		if stls.hasStyle(entries[i].Name) {
 			log.Printf("already has style for %s\n", entries[i].Name)
 			continue
 		}
@@ -515,3 +529,31 @@
 
 	return nil
 }
+
+func ReconfigureStyle(name string) {
+	Reconfigure(func() error {
+		var stls styles
+		if err := stls.load(); err != nil {
+			return err
+		}
+
+		entries := models.InternalServices.Filter(
+			models.IntAnd(
+				models.IntWMS,
+				models.IntWithStyle,
+				models.IntByName(name)))
+
+		for i := range entries {
+			entry := &entries[i]
+			// TODO: Implement me!
+
+			if stls.hasStyle(entry.Name) {
+				// Update
+			} else {
+				// Create new style
+			}
+		}
+
+		return nil
+	})
+}
--- a/pkg/models/intservices.go	Tue Oct 02 22:25:44 2018 +0200
+++ b/pkg/models/intservices.go	Tue Oct 02 23:29:55 2018 +0200
@@ -121,8 +121,15 @@
 	return func(entry IntEntry) bool { return entry.Name == name }
 }
 
-func IntAnd(a, b func(IntEntry) bool) func(IntEntry) bool {
-	return func(entry IntEntry) bool { return a(entry) && b(entry) }
+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 IntWithStyle(entry IntEntry) bool {