changeset 2059:ae0021feaac8 unify_imports

Imports: Made stretch import marshable (even if it is not needed).
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 29 Jan 2019 08:28:50 +0100
parents 09f9ae3d0526
children f9d13ce57893
files pkg/common/attributes.go pkg/controllers/srimports.go pkg/models/common.go pkg/models/imports.go
diffstat 4 files changed, 110 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/common/attributes.go	Mon Jan 28 20:35:14 2019 +0100
+++ b/pkg/common/attributes.go	Tue Jan 29 08:28:50 2019 +0100
@@ -20,6 +20,11 @@
 	"time"
 )
 
+const (
+	TimeFormat = "2006-01-02T15:04:05"
+	DateFormat = "2006-01-02"
+)
+
 type (
 
 	// Attributes is a map of optional key/value attributes
@@ -103,7 +108,23 @@
 	return ca.Set(key, v)
 }
 
-const TimeFormat = "2006-01-02T15:04:05"
+func (ca Attributes) Date(key string) (time.Time, bool) {
+	s, found := ca.Get(key)
+	if !found {
+		return time.Time{}, false
+	}
+	d, err := time.Parse(DateFormat, s)
+	if err != nil {
+		log.Printf("error: %v\n", err)
+		return time.Time{}, false
+	}
+	return d, true
+}
+
+func (ca Attributes) SetDate(key string, date time.Time) bool {
+	s := date.Format(DateFormat)
+	return ca.Set(key, s)
+}
 
 // Time gives a time.Time for a given key.
 func (ca Attributes) Time(key string) (time.Time, bool) {
--- a/pkg/controllers/srimports.go	Mon Jan 28 20:35:14 2019 +0100
+++ b/pkg/controllers/srimports.go	Tue Jan 29 08:28:50 2019 +0100
@@ -116,7 +116,7 @@
 	}
 
 	if v := req.FormValue("date"); v != "" {
-		date, err := time.Parse(models.DateFormat, v)
+		date, err := time.Parse(common.DateFormat, v)
 		if err != nil {
 			return err
 		}
--- a/pkg/models/common.go	Mon Jan 28 20:35:14 2019 +0100
+++ b/pkg/models/common.go	Tue Jan 29 08:28:50 2019 +0100
@@ -20,6 +20,8 @@
 	"fmt"
 	"strings"
 	"time"
+
+	"gemma.intevation.de/gemma/pkg/common"
 )
 
 var (
@@ -30,8 +32,6 @@
 // WGS84 is the EPSG of the World Geodetic System 1984.
 const WGS84 = 4326
 
-const DateFormat = "2006-01-02"
-
 type (
 	Date struct{ time.Time }
 	// Country is a valid country 2 letter code.
@@ -41,7 +41,7 @@
 )
 
 func (srd Date) MarshalJSON() ([]byte, error) {
-	return json.Marshal(srd.Format(DateFormat))
+	return json.Marshal(srd.Format(common.DateFormat))
 }
 
 func (srd *Date) UnmarshalJSON(data []byte) error {
@@ -49,7 +49,7 @@
 	if err := json.Unmarshal(data, &s); err != nil {
 		return err
 	}
-	d, err := time.Parse(DateFormat, s)
+	d, err := time.Parse(common.DateFormat, s)
 	if err == nil {
 		*srd = Date{d}
 	}
--- a/pkg/models/imports.go	Mon Jan 28 20:35:14 2019 +0100
+++ b/pkg/models/imports.go	Tue Jan 29 08:28:50 2019 +0100
@@ -14,6 +14,7 @@
 
 import (
 	"errors"
+	"strings"
 
 	"gemma.intevation.de/gemma/pkg/common"
 )
@@ -179,3 +180,85 @@
 	fdi.SourceOrganization = source
 	return nil
 }
+
+func (sti *StretchImport) MarshalAttributes(attrs common.Attributes) error {
+	if err := sti.EmailType.MarshalAttributes(attrs); err != nil {
+		return err
+	}
+	attrs.Set("name", sti.Name)
+	attrs.Set("from", sti.From.String())
+	attrs.Set("to", sti.To.String())
+	attrs.Set("objnam", sti.ObjNam)
+	if sti.NObjNam != nil {
+		attrs.Set("nobjnam", *sti.NObjNam)
+	}
+	attrs.Set("source-organization", sti.Source)
+	attrs.SetDate("date-info", sti.Date.Time)
+	if len(sti.Countries) > 0 {
+		countries := make([]string, len(sti.Countries))
+		for i, c := range sti.Countries {
+			countries[i] = string(c)
+		}
+		attrs.Set("countries", strings.Join(countries, ","))
+	}
+
+	return nil
+}
+
+func (sti *StretchImport) UnmarshalAttributes(attrs common.Attributes) error {
+	if err := sti.EmailType.UnmarshalAttributes(attrs); err != nil {
+		return err
+	}
+	name, found := attrs.Get("name")
+	if !found {
+		return errors.New("missing 'name' attribute")
+	}
+	sti.Name = name
+	from, found := attrs.Get("from")
+	if !found {
+		return errors.New("missing 'from' attribute")
+	}
+	f, err := IsrsFromString(from)
+	if err != nil {
+		return err
+	}
+	sti.From = *f
+	to, found := attrs.Get("from")
+	if !found {
+		return errors.New("missing 'to' attribute")
+	}
+	t, err := IsrsFromString(to)
+	if err != nil {
+		return err
+	}
+	sti.To = *t
+	objnam, found := attrs.Get("objnam")
+	if !found {
+		return errors.New("missing 'objnam' attribute")
+	}
+	sti.ObjNam = objnam
+	nobjnam, found := attrs.Get("nobjnam")
+	if found {
+		sti.NObjNam = &nobjnam
+	}
+	source, found := attrs.Get("source-organization")
+	if !found {
+		return errors.New("missing 'source' attribute")
+	}
+	sti.Source = source
+	date, found := attrs.Date("date-info")
+	if !found {
+		return errors.New("missing 'date-info' attribute")
+	}
+	sti.Date = Date{date}
+	countries, found := attrs.Get("countries")
+	if found {
+		csp := strings.Split(countries, ",")
+		cs := make(UniqueCountries, len(csp))
+		for i, c := range csp {
+			cs[i] = Country(c)
+		}
+		sti.Countries = cs
+	}
+	return nil
+}