changeset 4988:61eb65394a13

Only use JSON path when configure feature layers with Geoserver REST API.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 05 Mar 2020 11:59:11 +0100
parents 43ada68487ca
children f879933cf671 5e6ec587014e
files pkg/geoserver/boot.go
diffstat 1 files changed, 57 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/geoserver/boot.go	Thu Mar 05 11:55:39 2020 +0100
+++ b/pkg/geoserver/boot.go	Thu Mar 05 11:59:11 2020 +0100
@@ -71,32 +71,6 @@
 	return bytes.NewReader(buf.Bytes())
 }
 
-type ftXML struct {
-	XMLName  xml.Name `xml:"featureType"`
-	Name     string   `xml:"name"`
-	Title    string   `xml:"title"`
-	SRS      *string  `xml:"srs,omitempty"`
-	Metadata ftMetadata
-}
-
-type ftMetadata struct {
-	XMLName xml.Name `xml:"metadata"`
-	Entry   ftMetadataEntry
-}
-
-type ftMetadataEntry struct {
-	XMLName   xml.Name `xml:"entry"`
-	Key       string   `xml:"key,attr"`
-	VirtTable ftVirtTable
-}
-
-type ftVirtTable struct {
-	XMLName   xml.Name `xml:"virtualTable"`
-	Name      string   `xml:"name"`
-	SQL       string   `xml:"sql"`
-	KeyColumn *string  `xml:"keyColumn,omitempty"`
-} // End code for handling with XML
-
 func asJSON(req *http.Request) {
 	req.Header.Set("Content-Type", "application/json")
 }
@@ -326,48 +300,69 @@
 		log.Printf("info: creating featuretype %s.\n", table)
 
 		var req *http.Request
+
+		ft := map[string]interface{}{
+			"name":       table,
+			"nativeName": table,
+			"title":      table,
+		}
+		if srs := tables[i].SRS; srs != nil {
+			ft["srs"] = *srs
+		}
+
+		var entries []map[string]interface{}
+
 		if models.IntSQLView(tables[i]) {
-			// XXX: Creating SQL views with JSON via GeoServer REST-API fails
-			// Begin code for handling with XML instead
-			ft := ftXML{
-				Name:  table,
-				Title: table,
-				SRS:   tables[i].SRS,
-				Metadata: ftMetadata{
-					Entry: ftMetadataEntry{
-						Key: "JDBC_VIRTUAL_TABLE",
-						VirtTable: ftVirtTable{
-							Name:      table,
-							SQL:       *tables[i].SQL,
-							KeyColumn: tables[i].KeyColumn}}}}
+			vt := map[string]interface{}{
+				"name": table,
+				"sql":  *tables[i].SQL,
+			}
+			if kc := tables[i].KeyColumn; kc != nil {
+				vt["keyColumn"] = *kc
+			}
+			entry := map[string]interface{}{
+				"@key":         "JDBC_VIRTUAL_TABLE",
+				"virtualTable": vt,
+			}
+			entries = append(entries, entry)
+		}
 
-			req, err = http.NewRequest(
-				http.MethodPost,
-				datastoreURL+"/featuretypes",
-				toXMLStream(ft))
-			if err != nil {
-				return err
+		/* XXX: Experimental
+		if table == "sounding_differences" {
+			di := map[string]interface{}{
+				"enabled":             true,
+				"attribute":           "minuend",
+				"endAttribute":        "subtrahend",
+				"presentation":        "CONTINUOUS_INTERVAL",
+				"units":               "ISO8601",
+				"nearestMatchEnabled": false,
+			}
+			entry := map[string]interface{}{
+				"@key":          "time",
+				"dimensionInfo": di,
 			}
-			asContentType(req, "text/xml")
-			// End code for handling with XML instead
-		} else {
-			ft := map[string]interface{}{
-				"featureType": map[string]interface{}{
-					"name":       table,
-					"nativeName": table,
-					"title":      table,
-				},
+			entries = append(entries, entry)
+		}
+		*/
+
+		if len(entries) > 0 {
+			ft["metadata"] = map[string]interface{}{
+				"entry": entries,
 			}
+		}
 
-			req, err = http.NewRequest(
-				http.MethodPost,
-				datastoreURL+"/featuretypes",
-				toStream(ft))
-			if err != nil {
-				return err
-			}
-			asJSON(req)
+		doc := map[string]interface{}{
+			"featureType": ft,
 		}
+
+		req, err = http.NewRequest(
+			http.MethodPost,
+			datastoreURL+"/featuretypes",
+			toStream(doc))
+		if err != nil {
+			return err
+		}
+		asJSON(req)
 		auth(req)
 
 		resp, err := http.DefaultClient.Do(req)