view pkg/imports/modelconvert.go @ 4962:1b309a8e7673 fairway-marks-import

Distinguish more clearly between BCNLAT HYDRO and IENC features This allows importing them from different sources while keeping the history of data intact. Additionally, storing them in different tables also allows to have different attributes (here only dirimp via an m:n-table) and different constraints (currently not implemented) according to the IENC feature catalogue. Since both new tables inherit from a table with the same name as the old table, all entries still can be accessed via a table of the same name. Thus, no changes to GeoServer layers are necessary. ToDo: solve layout problems in the client SPA.
author Tom Gottfried <tom@intevation.de>
date Thu, 27 Feb 2020 21:05:09 +0100
parents b0dbc0f2c748
children 58dc06e91c39
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, 2020 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>

package imports

import (
	"gemma.intevation.de/gemma/pkg/models"
)

var kindToImportModel = map[JobKind]func() interface{}{
	BNJobKind:          func() interface{} { return new(models.BottleneckImport) },
	GMJobKind:          func() interface{} { return new(models.GaugeMeasurementImport) },
	FAJobKind:          func() interface{} { return new(models.FairwayAvailabilityImport) },
	WXJobKind:          func() interface{} { return new(models.WaterwayAxisImport) },
	WAJobKind:          func() interface{} { return new(models.WaterwayAreaImport) },
	WGJobKind:          func() interface{} { return new(models.WaterwayGaugeImport) },
	DMVJobKind:         func() interface{} { return new(models.DistanceMarksVirtualImport) },
	FDJobKind:          func() interface{} { return new(models.FairwayDimensionImport) },
	DMAJobKind:         func() interface{} { return new(models.DistanceMarksAshoreImport) },
	BCNLATHYDROJobKind: func() interface{} { return FindJobCreator(BCNLATHYDROJobKind).Create() },
	BCNLATIENCJobKind:  func() interface{} { return FindJobCreator(BCNLATIENCJobKind).Create() },
	BOYCARJobKind:      func() interface{} { return FindJobCreator(BOYCARJobKind).Create() },
	BOYLATJobKind:      func() interface{} { return FindJobCreator(BOYLATJobKind).Create() },
	BOYSAWJobKind:      func() interface{} { return FindJobCreator(BOYSAWJobKind).Create() },
	BOYSPPJobKind:      func() interface{} { return FindJobCreator(BOYSPPJobKind).Create() },
	DAYMARJobKind:      func() interface{} { return FindJobCreator(DAYMARJobKind).Create() },
	LIGHTSJobKind:      func() interface{} { return FindJobCreator(LIGHTSJobKind).Create() },
	RTPBCNJobKind:      func() interface{} { return FindJobCreator(RTPBCNJobKind).Create() },
	TOPMARJobKind:      func() interface{} { return FindJobCreator(TOPMARJobKind).Create() },
	NOTMRKJobKind:      func() interface{} { return FindJobCreator(NOTMRKJobKind).Create() },
	STJobKind:          func() interface{} { return new(models.StretchImport) },
	SECJobKind:         func() interface{} { return new(models.SectionImport) },
	DSECJobKind:        func() interface{} { return new(models.SectionDelete) },
	DSTJobKind:         func() interface{} { return new(models.StretchDelete) },
	DSRJobKind:         func() interface{} { return new(models.SoundingResultDelete) },
}

// ImportModelForJobKind returns the constructor function to
// de-serialize an incoming JSON REST represention of an import.
func ImportModelForJobKind(kind JobKind) func() interface{} {
	return kindToImportModel[kind]
}

var convertModel = map[JobKind]func(interface{}) interface{}{

	BNJobKind: func(input interface{}) interface{} {
		bi := input.(*models.BottleneckImport)
		return &Bottleneck{
			URL:       bi.URL,
			Tolerance: bi.Tolerance,
			Insecure:  bi.Insecure,
		}
	},

	GMJobKind: func(input interface{}) interface{} {
		gi := input.(*models.GaugeMeasurementImport)
		return &GaugeMeasurement{
			URL:      gi.URL,
			Insecure: gi.Insecure,
		}
	},

	FAJobKind: func(input interface{}) interface{} {
		fai := input.(*models.FairwayAvailabilityImport)
		return &FairwayAvailability{
			URL:      fai.URL,
			Insecure: fai.Insecure,
		}
	},

	WXJobKind: func(input interface{}) interface{} {
		wxi := input.(*models.WaterwayAxisImport)
		return &WaterwayAxis{
			URL:         wxi.URL,
			FeatureType: wxi.FeatureType,
			SortBy:      nilString(wxi.SortBy),
			User:        nilString(wxi.User),
			Password:    nilString(wxi.Password),
		}
	},

	WAJobKind: func(input interface{}) interface{} {
		wai := input.(*models.WaterwayAreaImport)
		return &WaterwayArea{
			URL:         wai.URL,
			FeatureType: wai.FeatureType,
			SortBy:      nilString(wai.SortBy),
			User:        nilString(wai.User),
			Password:    nilString(wai.Password),
		}
	},

	WGJobKind: func(input interface{}) interface{} {
		wgi := input.(*models.WaterwayGaugeImport)
		return &WaterwayGauge{
			URL:      wgi.URL,
			Username: nilString(wgi.User),
			Password: nilString(wgi.Password),
			Insecure: wgi.Insecure,
		}
	},

	DMVJobKind: func(input interface{}) interface{} {
		dmvi := input.(*models.DistanceMarksVirtualImport)
		return &DistanceMarksVirtual{
			URL:      dmvi.URL,
			Username: nilString(dmvi.User),
			Password: nilString(dmvi.Password),
			Insecure: dmvi.Insecure,
		}
	},

	FDJobKind: func(input interface{}) interface{} {
		fdi := input.(*models.FairwayDimensionImport)
		return &FairwayDimension{
			URL:                fdi.URL,
			FeatureType:        fdi.FeatureType,
			SortBy:             nilString(fdi.SortBy),
			LOS:                fdi.LOS,
			MinWidth:           fdi.MinWidth,
			MaxWidth:           fdi.MaxWidth,
			Depth:              fdi.Depth,
			SourceOrganization: fdi.SourceOrganization,
			User:               nilString(fdi.User),
			Password:           nilString(fdi.Password),
		}
	},

	DMAJobKind: func(input interface{}) interface{} {
		dmai := input.(*models.DistanceMarksAshoreImport)
		return &DistanceMarksAshore{
			URL:         dmai.URL,
			FeatureType: dmai.FeatureType,
			SortBy:      nilString(dmai.SortBy),
			User:        nilString(dmai.User),
			Password:    nilString(dmai.Password),
		}
	},

	STJobKind: func(input interface{}) interface{} {
		sti := input.(*models.StretchImport)
		return &Stretch{
			Name:      sti.Name,
			From:      sti.From,
			To:        sti.To,
			Tolerance: sti.Tolerance,
			ObjNam:    sti.ObjNam,
			NObjNam:   sti.NObjNam,
			Source:    sti.Source,
			Date:      sti.Date,
			Countries: sti.Countries,
		}
	},

	SECJobKind: func(input interface{}) interface{} {
		seci := input.(*models.SectionImport)
		return &Section{
			Name:      seci.Name,
			From:      seci.From,
			To:        seci.To,
			Tolerance: seci.Tolerance,
			ObjNam:    seci.ObjNam,
			NObjNam:   seci.NObjNam,
			Source:    seci.Source,
			Date:      seci.Date,
		}
	},

	DSECJobKind: func(input interface{}) interface{} {
		dsec := input.(*models.SectionDelete)
		return &DeleteSection{ID: dsec.ID}
	},

	DSTJobKind: func(input interface{}) interface{} {
		dst := input.(*models.StretchDelete)
		return &DeleteStretch{ID: dst.ID}
	},

	DSRJobKind: func(input interface{}) interface{} {
		dsr := input.(*models.SoundingResultDelete)
		return &DeleteSoundingResult{
			BottleneckID: dsr.BottleneckId,
			Date:         dsr.Date,
		}
	},
}

func nilString(s *string) string {
	if s != nil {
		return *s
	}
	return ""
}

// ConvertToInternal converts an external JSON REST represention
// of an import into the internal one store in the import queue.
func ConvertToInternal(kind JobKind, src interface{}) interface{} {
	fn := convertModel[kind]
	if fn == nil {
		return src
	}
	return fn(src)
}