diff pkg/models/intservices.go @ 873:ad9272460ef3 geo-style

Do the XSLT to adjust the layer name when updating the style column in the database.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 30 Sep 2018 14:24:37 +0200
parents f0b6852c14d1
children 8b9bd9ccdd93
line wrap: on
line diff
--- a/pkg/models/intservices.go	Sun Sep 30 13:58:49 2018 +0200
+++ b/pkg/models/intservices.go	Sun Sep 30 14:24:37 2018 +0200
@@ -3,7 +3,10 @@
 import (
 	"context"
 	"database/sql"
+	"fmt"
 	"log"
+	"net/http"
+	"strings"
 	"sync"
 
 	"gemma.intevation.de/gemma/pkg/auth"
@@ -28,22 +31,50 @@
 
 	updateStyleSQL = `
 UPDATE sys_admin.published_services
-SET style = $1
-WHERE name IN (SELECT oid FROM pg_class WHERE relname = $2)`
+SET style = xslt_process($1, $2, $3)::bytea
+WHERE name IN (SELECT oid FROM pg_class WHERE relname = $4)`
+)
+
+const (
+	replaceNameXSLT = `<?xml version="1.0"?>
+<xsl:stylesheet version="1.0"
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+  xmlns:sld="http://www.opengis.net/sld"
+  xmlns:se="http://www.opengis.net/se">
+
+  <xsl:param name="name"/>
+
+  <xsl:template
+    match="/sld:StyledLayerDescriptor/sld:NamedLayer/sld:Name/text()">
+	<xsl:value-of select="$name"/>
+  </xsl:template>
+
+  <xsl:template
+    match="/sld:StyledLayerDescriptor/sld:NamedLayer/se:Name/text()">
+	<xsl:value-of select="$name"/>
+  </xsl:template>
+
+  <xsl:template match="@*|node()">
+    <xsl:copy>
+      <xsl:apply-templates select="@*|node()"/>
+    </xsl:copy>
+  </xsl:template>
+</xsl:stylesheet>`
 )
 
 var InternalServices = &IntServices{}
 
-func UpdateInternalStyle(name, style string) error {
-	return auth.RunAs("sys_admin", context.Background(),
-		func(conn *sql.Conn) error {
-			_, err := conn.ExecContext(
-				context.Background(), updateStyleSQL, style, name)
-			if err == nil {
-				InternalServices.Invalidate()
-			}
-			return err
-		})
+func UpdateInternalStyle(req *http.Request, name, style string) error {
+	return auth.RunAsSessionUser(req, func(conn *sql.Conn) error {
+		param := fmt.Sprintf("name='%s'", strings.Replace(name, ",", "", -1))
+		_, err := conn.ExecContext(
+			req.Context(), updateStyleSQL,
+			style, replaceNameXSLT, param, name)
+		if err == nil {
+			InternalServices.Invalidate()
+		}
+		return err
+	})
 }
 
 func (ps *IntServices) Find(name string) (string, bool) {