view pkg/models/imports.go @ 2549:9bf6b767a56a

client: refactored and improved splitscreen for diagrams To make different diagrams possible, the splitscreen view needed to be decoupled from the cross profiles. Also the style has changed to make it more consistent with the rest of the app. The standard box header is now used and there are collapse and expand animations.
author Markus Kottlaender <markus@intevation.de>
date Fri, 08 Mar 2019 08:50:47 +0100
parents 9c4ef2345db4
children dc4fae4bdb8f
line wrap: on
line source

// This is Free Software under GNU Affero General Public License v >= 3.0
// without warranty, see README.md and license for details.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// License-Filename: LICENSES/AGPL-3.0.txt
//
// Copyright (C) 2018 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
package models

import (
	"errors"
	"strings"

	"gemma.intevation.de/gemma/pkg/common"
)

type (
	ConfigurableURLImport struct {
		URLType
		QueueConfigurationType
	}

	BottleneckImport struct {
		ConfigurableURLImport
	}

	// GaugeMeasurementImport contains data used to define the endpoint
	GaugeMeasurementImport struct {
		ConfigurableURLImport
	}

	// FairwayAvailabilityImport contains data used to define the endpoint
	FairwayAvailabilityImport struct {
		ConfigurableURLImport
	}

	// WaterwayAxisImport specifies an import of waterway gauges.
	WaterwayGaugeImport struct {
		ConfigurableURLImport
	}

	// DistanceMarksVirtualImport specifies an import of distance marks virtual.
	DistanceMarksVirtualImport struct {
		ConfigurableURLImport
	}

	WFSImport struct {
		ConfigurableURLImport

		// FeatureType is the layer to use.
		FeatureType string `json:"feature-type"`
		// SortBy sorts the feature by this key.
		SortBy *string `json:"sort-by"`
	}

	// WaterwayAxisImport specifies an import of the waterway axis.
	WaterwayAxisImport struct {
		WFSImport
	}

	// WaterwayAreaImport specifies an import of the waterway area.
	WaterwayAreaImport struct {
		WFSImport
	}

	// DistanceMarksAshoreImport specifies an import of the distance marks.
	DistanceMarksAshoreImport struct {
		WFSImport
	}

	// FairwayDimensionImport specifies an import of the waterway axis.
	FairwayDimensionImport struct {
		WFSImport

		// LOS is the level of service provided by the wfs
		LOS int `json:"los"`
		// MinWidth is the minimum width of the fairway for the specified LOS
		MinWidth int `json:"min-width"`
		// MaxWidth is the maximum width of the fairway for the specified LOS
		MaxWidth int `json:"max-width"`
		// Depth is the minimum depth of the fairway for the specified LOS
		Depth int `json:"depth"`
		// SourceOrganization specifies the source of the entry
		SourceOrganization string `json:"source-organization"`
	}

	StretchImport struct {
		EmailType

		Name      string          `json:"name"`
		From      Isrs            `json:"from"`
		To        Isrs            `json:"to"`
		ObjNam    string          `json:"objnam"`
		NObjNam   *string         `json:"nobjnam"`
		Source    string          `json:"source-organization"`
		Date      Date            `json:"date-info"`
		Countries UniqueCountries `json:"countries"`
	}
)

func (cui *ConfigurableURLImport) MarshalAttributes(attrs common.Attributes) error {
	if err := cui.URLType.MarshalAttributes(attrs); err != nil {
		return err
	}
	return cui.QueueConfigurationType.MarshalAttributes(attrs)
}

func (cui *ConfigurableURLImport) UnmarshalAttributes(attrs common.Attributes) error {
	if err := cui.URLType.UnmarshalAttributes(attrs); err != nil {
		return err
	}
	return cui.QueueConfigurationType.UnmarshalAttributes(attrs)
}

func (wi *WFSImport) MarshalAttributes(attrs common.Attributes) error {
	if err := wi.ConfigurableURLImport.MarshalAttributes(attrs); err != nil {
		return err
	}
	attrs.Set("feature-type", wi.FeatureType)
	if wi.SortBy != nil {
		attrs.Set("sort-by", *wi.SortBy)
	}
	return nil
}

func (wi *WFSImport) UnmarshalAttributes(attrs common.Attributes) error {
	if err := wi.ConfigurableURLImport.UnmarshalAttributes(attrs); err != nil {
		return err
	}
	ft, found := attrs.Get("feature-type")
	if !found {
		return errors.New("missing 'feature-type' attribute")
	}
	wi.FeatureType = ft
	if sb, found := attrs.Get("sort-by"); found {
		wi.SortBy = &sb
	}
	return nil
}

func (fdi *FairwayDimensionImport) MarshalAttributes(attrs common.Attributes) error {
	if err := fdi.WFSImport.MarshalAttributes(attrs); err != nil {
		return err
	}
	attrs.SetInt("los", fdi.LOS)
	attrs.SetInt("min-width", fdi.MinWidth)
	attrs.SetInt("max-width", fdi.MaxWidth)
	attrs.SetInt("depth", fdi.Depth)
	attrs.Set("source-organization", fdi.SourceOrganization)
	return nil
}

func (fdi *FairwayDimensionImport) UnmarshalAttributes(attrs common.Attributes) error {
	if err := fdi.WFSImport.UnmarshalAttributes(attrs); err != nil {
		return err
	}
	los, found := attrs.Int("los")
	if !found {
		return errors.New("missing 'los' attribute")
	}
	fdi.LOS = los
	minWidth, found := attrs.Int("min-width")
	if !found {
		return errors.New("missing 'min-width' attribute")
	}
	fdi.MinWidth = minWidth
	maxWidth, found := attrs.Int("max-width")
	if !found {
		return errors.New("missing 'max-width' attribute")
	}
	fdi.MaxWidth = maxWidth
	depth, found := attrs.Int("depth")
	if !found {
		return errors.New("missing 'depth' attribute")
	}
	fdi.Depth = depth
	source, found := attrs.Get("source-organization")
	if !found {
		return errors.New("missing 'source-organization' attribute")
	}
	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("to")
	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
}