diff pkg/imports/sr.go @ 1224:bc4b642c8d04

Started with an upload sounding result to temp upload area.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 19 Nov 2018 17:14:42 +0100
parents 58acc343b1b6
children 4d7c44f7044e
line wrap: on
line diff
--- a/pkg/imports/sr.go	Mon Nov 19 16:50:04 2018 +0100
+++ b/pkg/imports/sr.go	Mon Nov 19 17:14:42 2018 +0100
@@ -20,7 +20,6 @@
 	"crypto/sha1"
 	"database/sql"
 	"encoding/hex"
-	"encoding/json"
 	"errors"
 	"fmt"
 	"io"
@@ -34,26 +33,13 @@
 
 	shp "github.com/jonas-p/go-shp"
 
+	"gemma.intevation.de/gemma/pkg/common"
+	"gemma.intevation.de/gemma/pkg/models"
 	"gemma.intevation.de/gemma/pkg/octree"
 )
 
 type SoundingResult string
 
-const SoundingResultDateFormat = "2006-01-02"
-
-type (
-	SoundingResultDate struct{ time.Time }
-
-	SoundingResultMeta struct {
-		Date           SoundingResultDate `json:"date"`
-		Bottleneck     string             `json:"bottleneck"`
-		EPSG           uint               `json:"epsg"`
-		DepthReference string             `json:"depth-reference"`
-	}
-)
-
-const wgs84 = 4326
-
 const (
 	contourStepWidth = 0.1
 	contourTolerance = 0.1
@@ -93,12 +79,6 @@
 UPDATE waterway.sounding_results SET staging_done = true
 WHERE id = $1`
 
-	checkDepthReferenceSQL = `
-SELECT true FROM depth_references WHERE depth_reference = $1`
-
-	checkBottleneckSQL = `
-SELECT true FROM waterway.bottlenecks WHERE objnam = $1`
-
 	insertPointsSQL = `
 INSERT INTO waterway.sounding_results (
   bottleneck_id,
@@ -182,8 +162,8 @@
 		return err
 	}
 
-	if err := m.validate(conn); err != nil {
-		return err
+	if err := m.Validate(conn, ctx); err != nil {
+		return common.ToError(err)
 	}
 
 	feedback.Info("Looking for '*.xyz'")
@@ -289,18 +269,6 @@
 	return err
 }
 
-func (srd *SoundingResultDate) UnmarshalJSON(data []byte) error {
-	var s string
-	if err := json.Unmarshal(data, &s); err != nil {
-		return err
-	}
-	d, err := time.Parse(SoundingResultDateFormat, s)
-	if err == nil {
-		*srd = SoundingResultDate{d}
-	}
-	return err
-}
-
 func (sr SoundingResult) CleanUp() error {
 	return os.RemoveAll(string(sr))
 }
@@ -315,50 +283,14 @@
 	return nil
 }
 
-func loadMeta(f *zip.File) (*SoundingResultMeta, error) {
+func loadMeta(f *zip.File) (*models.SoundingResultMeta, error) {
 	r, err := f.Open()
 	if err != nil {
 		return nil, err
 	}
 	defer r.Close()
-	var m SoundingResultMeta
-	err = json.NewDecoder(r).Decode(&m)
-	if err == nil {
-		if m.EPSG == 0 {
-			m.EPSG = wgs84
-		}
-	}
-	return &m, err
-}
-
-func (m *SoundingResultMeta) validate(conn *sql.Conn) error {
-
-	var b bool
-	err := conn.QueryRowContext(context.Background(),
-		checkDepthReferenceSQL,
-		m.DepthReference).Scan(&b)
-	switch {
-	case err == sql.ErrNoRows:
-		return fmt.Errorf("Unknown depth reference '%s'\n", m.DepthReference)
-	case err != nil:
-		return err
-	case !b:
-		return errors.New("Unexpected depth reference")
-	}
-
-	err = conn.QueryRowContext(context.Background(),
-		checkBottleneckSQL,
-		m.Bottleneck).Scan(&b)
-	switch {
-	case err == sql.ErrNoRows:
-		return fmt.Errorf("Unknown bottleneck '%s'\n", m.Bottleneck)
-	case err != nil:
-		return err
-	case !b:
-		return errors.New("Unexpected bottleneck")
-	}
-
-	return nil
+	var m models.SoundingResultMeta
+	return &m, m.Decode(r)
 }
 
 func loadXYZReader(r io.Reader, feedback Feedback) (octree.MultiPointZ, error) {