changeset 2203:8d1a945d0c3b

Uploaded fairway availabilty import: Implemented in terms of normal fairway availabilty import.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 12 Feb 2019 23:24:09 +0100
parents 0aee7d4954ae
children 25f73251a6ac
files pkg/controllers/routes.go pkg/controllers/ufaimports.go pkg/imports/fa.go pkg/imports/ufa.go
diffstat 4 files changed, 182 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/routes.go	Tue Feb 12 18:32:48 2019 +0100
+++ b/pkg/controllers/routes.go	Tue Feb 12 23:24:09 2019 +0100
@@ -208,6 +208,9 @@
 	api.Handle("/imports/ubn", waterwayAdmin(
 		http.HandlerFunc(importUploadedBottleneck))).Methods(http.MethodPost)
 
+	api.Handle("/imports/ufa", waterwayAdmin(
+		http.HandlerFunc(importUploadedFairwayAvailability))).Methods(http.MethodPost)
+
 	api.Handle("/imports/{kind:st}", sysAdmin(&JSONHandler{
 		Input:  importModel,
 		Handle: manualImport,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/controllers/ufaimports.go	Tue Feb 12 23:24:09 2019 +0100
@@ -0,0 +1,81 @@
+// 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 controllers
+
+import (
+	"log"
+	"net/http"
+	"time"
+
+	"gemma.intevation.de/gemma/pkg/auth"
+	"gemma.intevation.de/gemma/pkg/common"
+	"gemma.intevation.de/gemma/pkg/imports"
+	"gemma.intevation.de/gemma/pkg/misc"
+)
+
+const (
+	maxUploadedFairwayAvailabilitySize = 25 * 1024 * 1024
+	uploadFairwayAvailabilityName      = "ufa"
+)
+
+func importUploadedFairwayAvailability(rw http.ResponseWriter, req *http.Request) {
+
+	dir, err := misc.StoreUploadedFile(
+		req,
+		uploadFairwayAvailabilityName,
+		"data.xml",
+		maxUploadedFairwayAvailabilitySize)
+	if err != nil {
+		log.Printf("error: %v\n", err)
+		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	ufa := &imports.UploadedFairwayAvailability{Dir: dir}
+
+	serialized, err := common.ToJSONString(ufa)
+	if err != nil {
+		log.Printf("error: %v\n", err)
+		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	session, _ := auth.GetSession(req)
+
+	sendEmail := req.FormValue("email") != ""
+
+	jobID, err := imports.AddJob(
+		imports.UFAJobKind,
+		time.Time{}, // due
+		nil,         // trys
+		nil,         // wait retry
+		session.User,
+		sendEmail,
+		serialized)
+
+	if err != nil {
+		log.Printf("error: %v\n", err)
+		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
+		return
+	}
+
+	log.Printf("info: added import #%d to queue\n", jobID)
+
+	result := struct {
+		ID int64 `json:"id"`
+	}{
+		ID: jobID,
+	}
+	SendJSON(rw, http.StatusCreated, &result)
+}
--- a/pkg/imports/fa.go	Tue Feb 12 18:32:48 2019 +0100
+++ b/pkg/imports/fa.go	Tue Feb 12 23:24:09 2019 +0100
@@ -10,6 +10,7 @@
 //
 // Author(s):
 //  * Raimund Renkert <raimund.renkert@intevation.de>
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 package imports
 
@@ -473,7 +474,10 @@
 	feedback Feedback,
 ) (interface{}, error) {
 
-	fetch := func(ctx context.Context, tx *sql.Tx, bns bottlenecks) ([]*ifaf.FairwayAvailability, error) {
+	fetch := func(
+		ctx context.Context,
+		tx *sql.Tx, bns bottlenecks,
+	) ([]*ifaf.FairwayAvailability, error) {
 
 		latest, err := latestDate(ctx, tx)
 		if err != nil {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/imports/ufa.go	Tue Feb 12 23:24:09 2019 +0100
@@ -0,0 +1,93 @@
+// 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/ifaf"
+)
+
+type UploadedFairwayAvailability struct {
+	Dir string
+}
+
+const UFAJobKind JobKind = "ufa"
+
+type ufaJobCreator struct{}
+
+func init() {
+	RegisterJobCreator(UFAJobKind, ufaJobCreator{})
+}
+
+func (ufaJobCreator) Description() string {
+	return "uploaded fairway availability"
+}
+
+func (ufaJobCreator) Create() Job { return new(UploadedFairwayAvailability) }
+
+func (ufaJobCreator) Depends() []string {
+	// Same as faJobCreator
+	return faJobCreator{}.Depends()
+}
+
+func (ufaJobCreator) AutoAccept() bool { return true }
+
+func (ufaJobCreator) StageDone(context.Context, *sql.Tx, int64) error {
+	return nil
+}
+
+func (ufa *UploadedFairwayAvailability) CleanUp() error {
+	return os.RemoveAll(ufa.Dir)
+}
+
+// Do executes the actual uploaded fairway availability import.
+func (ufa *UploadedFairwayAvailability) Do(
+	ctx context.Context,
+	importID int64,
+	conn *sql.Conn,
+	feedback Feedback,
+) (interface{}, error) {
+
+	fetch := func(
+		ctx context.Context,
+		tx *sql.Tx,
+		bns bottlenecks,
+	) ([]*ifaf.FairwayAvailability, error) {
+
+		var response ifaf.Get_bottleneck_faResponse
+
+		if err := soap.ValidateFile(
+			filepath.Join(ufa.Dir, "data.xml"),
+			"IFAF.xsd",
+			&response,
+		); err != nil {
+			return nil, err
+		}
+
+		if response.Get_bottleneck_faResult == nil {
+			return nil, errors.New("No bottlenecks found")
+		}
+
+		result := response.Get_bottleneck_faResult
+		return result.FairwayAvailability, nil
+	}
+
+	return storeFairwayAvailability(ctx, conn, feedback, fetch)
+}