view pkg/imports/uwg.go @ 5719:62036f25f4a8 uploadwg

Used fixed xsd to validate direct uploaded waterway gauges.
author Sascha Wilde <wilde@sha-bang.de>
date Fri, 19 Apr 2024 13:08:36 +0200
parents 3d497077f888
children 5ca33ef30916
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, 2024 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
//  * Sascha Wilde <wilde@intevation.de>

package imports

import (
	"context"
	"database/sql"
	"errors"
	"os"
	"path/filepath"

	"gemma.intevation.de/gemma/pkg/soap"
	erdms "gemma.intevation.de/gemma/pkg/soap/erdms2"
)

// UploadedWaterwayGauge is an Job to extract gauge measurement data
// from an uploaded XML file and stores it into the database.
type UploadedWaterwayGauge struct {
	Dir string `json:"dir"`
}

// UWGJobKind is the unique name of this import job type.
const UWGJobKind JobKind = "uwg"

type uwgJobCreator struct{}

func init() { RegisterJobCreator(UWGJobKind, uwgJobCreator{}) }

func (uwgJobCreator) Description() string { return "uploaded waterway gauges" }

func (uwgJobCreator) Create() Job { return new(UploadedWaterwayGauge) }

func (uwgJobCreator) Depends() [2][]string { return wgJobCreator{}.Depends() }

func (uwgJobCreator) AutoAccept() bool { return true }

// StageDone is a NOP for gauge measurements imports.
func (uwgJobCreator) StageDone(context.Context, *sql.Tx, int64, Feedback) error { return nil }

// CleanUp removes the temporary files from the filesystem.
func (uwg *UploadedWaterwayGauge) CleanUp() error { return os.RemoveAll(uwg.Dir) }

// Do executes the actual uploaded waterway gauges import.
func (uwg *UploadedWaterwayGauge) Do(
	ctx context.Context,
	importID int64,
	conn *sql.Conn,
	feedback Feedback,
) (any, error) {

	fetch := func(
		_ context.Context,
		_ *sql.Tx,
	) ([]*erdms.GetRisDataXMLResponse, error) {

		var dst []*erdms.GetRisDataXMLResponse

		if err := soap.ValidateFile(
			filepath.Join(uwg.Dir, "data.xml"),
			"ERDMS_WS_XSD_2.0-fixed.xsd",
			&dst,
		); err != nil {
			return nil, err
		}

		if dst == nil {
			return nil, errors.New("no gauges found")
		}
		return dst, nil
	}

	return storeWaterwayGauges(
		ctx,
		conn,
		feedback,
		fetch,
	)
}