# HG changeset patch # User Sascha L. Teichmann # Date 1538515795 -7200 # Node ID 876d1f5433be014d8748a8aa0e9f195a01efb773 # Parent 85b2648066d7033106dfc5d7ff0713067b660a51 Started with direct applying style after modification via controller. WIP. diff -r 85b2648066d7 -r 876d1f5433be pkg/controllers/geostyling.go --- 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) } diff -r 85b2648066d7 -r 876d1f5433be pkg/geoserver/boot.go --- 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 + }) +} diff -r 85b2648066d7 -r 876d1f5433be pkg/models/intservices.go --- 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 {