view pkg/models/imports.go @ 5710:37c8feeecb4d

Merged branch sr-v2 into default.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 20 Feb 2024 21:28:56 +0100
parents 1222b777f51f
children
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, 2019, 2020 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
//  * Tom Gottfried <tom.gottfried@intevation.de>

package models

import (
	"errors"
	"strings"

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

type (
	// ConfigurableURLImport is configuration with an URL.
	ConfigurableURLImport struct {
		URLType
		QueueConfigurationType
	}

	// BottleneckImport is a bottleneck import.
	BottleneckImport struct {
		ConfigurableURLImport

		// Tolerance used for axis snapping
		Tolerance float64 `json:"tolerance"`
	}

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

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

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

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

	// WFSImport is the common base for WFS imports.
	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 imports a stretch.
	StretchImport struct {
		EmailType

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

	// SectionImport imports a section.
	SectionImport struct {
		EmailType

		Name      string  `json:"name"`
		From      Isrs    `json:"from"`
		To        Isrs    `json:"to"`
		Tolerance float32 `json:"tolerance"`
		ObjNam    string  `json:"objnam"`
		NObjNam   *string `json:"nobjnam"`
		Source    string  `json:"source-organization"`
		Date      Date    `json:"date-info"`
	}

	// SectionDelete deletes a section.
	SectionDelete struct {
		ID int64 `json:"id"`
	}

	// StretchDelete deletes a stretch.
	StretchDelete struct {
		ID int64 `json:"id"`
	}

	// SoundingResultDelete deletes a sounding result.
	SoundingResultDelete struct {
		BottleneckID string `json:"bottleneck-id"`
		Date         Date   `json:"date-info"`
	}
)

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

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

// MarshalAttributes stores a configuration.
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
}

// UnmarshalAttributes restores a configuration.
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
}

// MarshalAttributes stores a configuration.
func (bn *BottleneckImport) MarshalAttributes(attrs common.Attributes) error {
	if err := bn.ConfigurableURLImport.MarshalAttributes(attrs); err != nil {
		return err
	}
	attrs.SetFloat("tolerance", bn.Tolerance)
	return nil
}

// UnmarshalAttributes restores a configuration.
func (bn *BottleneckImport) UnmarshalAttributes(attrs common.Attributes) error {
	if err := bn.ConfigurableURLImport.UnmarshalAttributes(attrs); err != nil {
		return err
	}
	tol, found := attrs.Float("tolerance")
	if !found {
		return errors.New("missing 'tolerance' attribute")
	}
	bn.Tolerance = tol
	return nil
}

// MarshalAttributes stores a configuration.
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
}

// UnmarshalAttributes restores a configuration.
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
}

// MarshalAttributes stores an configuration.
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
}

// UnmarshalAttributes restores a configuration.
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
}

// MarshalAttributes stores a configuration.
func (seci *SectionImport) MarshalAttributes(attrs common.Attributes) error {
	if err := seci.EmailType.MarshalAttributes(attrs); err != nil {
		return err
	}
	attrs.Set("name", seci.Name)
	attrs.Set("from", seci.From.String())
	attrs.Set("to", seci.To.String())
	attrs.Set("objnam", seci.ObjNam)
	if seci.NObjNam != nil {
		attrs.Set("nobjnam", *seci.NObjNam)
	}
	attrs.Set("source-organization", seci.Source)
	attrs.SetDate("date-info", seci.Date.Time)

	return nil
}

// UnmarshalAttributes restores a configuration.
func (seci *SectionImport) UnmarshalAttributes(attrs common.Attributes) error {
	if err := seci.EmailType.UnmarshalAttributes(attrs); err != nil {
		return err
	}
	name, found := attrs.Get("name")
	if !found {
		return errors.New("missing 'name' attribute")
	}
	seci.Name = name
	from, found := attrs.Get("from")
	if !found {
		return errors.New("missing 'from' attribute")
	}
	f, err := IsrsFromString(from)
	if err != nil {
		return err
	}
	seci.From = *f
	to, found := attrs.Get("to")
	if !found {
		return errors.New("missing 'to' attribute")
	}
	t, err := IsrsFromString(to)
	if err != nil {
		return err
	}
	seci.To = *t
	objnam, found := attrs.Get("objnam")
	if !found {
		return errors.New("missing 'objnam' attribute")
	}
	seci.ObjNam = objnam
	nobjnam, found := attrs.Get("nobjnam")
	if found {
		seci.NObjNam = &nobjnam
	}
	source, found := attrs.Get("source-organization")
	if !found {
		return errors.New("missing 'source' attribute")
	}
	seci.Source = source
	date, found := attrs.Date("date-info")
	if !found {
		return errors.New("missing 'date-info' attribute")
	}
	seci.Date = Date{date}
	return nil
}