view pkg/imports/ubn.go @ 5670:b75d0b303328

Various fixes and improvements of gauges import: - Allow update of erased data (and thereby set erased to false) - Fix source_organization to work with ERDMS2 - Give ISRS of new and updated gauges in summary - Fixed reference of null pointers if revlevels are missing - Fixed reference of null pointer on update errors - Added ISRS to reference_code warning
author Sascha Wilde <wilde@sha-bang.de>
date Fri, 08 Dec 2023 17:29:56 +0100
parents 59a99655f34d
children 6270951dda28
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 imports

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

	"gemma.intevation.de/gemma/pkg/soap"
	"gemma.intevation.de/gemma/pkg/soap/ifbn"
)

// UploadedBottleneck is a Job to import bottlenecks from
// an uploaded XML file into the database.
type UploadedBottleneck struct {
	Dir string `json:"dir"`
	// Tolerance used for axis snapping
	Tolerance float64 `json:"tolerance"`
}

// UBNJobKind is the import queue type identifier.
const UBNJobKind JobKind = "ubn"

type ubnJobCreator struct{}

func init() { RegisterJobCreator(UBNJobKind, ubnJobCreator{}) }

func (ubnJobCreator) Description() string { return "uploaded bottlenecks" }

func (ubnJobCreator) AutoAccept() bool { return false }

func (ubnJobCreator) Create() Job { return new(UploadedBottleneck) }

func (ubnJobCreator) Depends() [2][]string {
	// Same as normal bottleneck import.
	return bnJobCreator{}.Depends()
}

// StageDone moves the imported bottleneck out of the staging area.
func (ubnJobCreator) StageDone(
	ctx context.Context,
	tx *sql.Tx,
	id int64,
	feedback Feedback,
) error {
	// Same as normal bottleneck import.
	return bnJobCreator{}.StageDone(ctx, tx, id, feedback)
}

// CleanUp of a uploaded bottleneck import removes the temp dir.
func (ubn *UploadedBottleneck) CleanUp() error { return os.RemoveAll(ubn.Dir) }

// Do executes the actual uploaded bottleneck import.
func (ubn *UploadedBottleneck) Do(
	ctx context.Context,
	importID int64,
	conn *sql.Conn,
	feedback Feedback,
) (interface{}, error) {

	fetch := func() ([]*ifbn.BottleNeckType, error) {
		var dst ifbn.Export_bn_by_isrsResponse
		if err := soap.ValidateFile(
			filepath.Join(ubn.Dir, "data.xml"),
			"IFBN.xsd",
			&dst,
		); err != nil {
			return nil, err
		}

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

		return dst.Export_bn_by_isrsResult.BottleNeckType, nil
	}

	return storeBottlenecks(
		ctx, fetch, importID, conn, feedback, ubn.Tolerance)
}