view pkg/imports/wg.go @ 1818:518cea19f6b4

translations.json added to .hgignore
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 16 Jan 2019 10:20:11 +0100
parents 4910bcfab319
children b4b9089c2d79
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"
	"log"
	"strings"

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

type WaterwayGauge struct {
	// URL is the URL of the SOAP service.
	URL string `json:"url"`
	// Username is the username used to authenticate.
	Username string `json:"username"`
	// Passwort is the password to authenticate.
	Password string `json:"password"`
	// Insecure indicates if HTTPS traffic
	// should validate certificates or not.
	Insecure bool `json:"insecure"`
}

const WGJobKind JobKind = "wg"

type wgJobCreator struct{}

func init() {
	RegisterJobCreator(WGJobKind, wgJobCreator{})
}

func (wgJobCreator) Description() string { return "waterway gauges" }

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

func (wgJobCreator) Create(_ JobKind, data string) (Job, error) {
	wg := new(WaterwayGauge)
	if err := common.FromJSONString(data, wg); err != nil {
		return nil, err
	}
	return wg, nil
}

func (wgJobCreator) Depends() []string {
	return []string{
		"gauges",
	}
}

func (wgJobCreator) StageDone(
	ctx context.Context,
	tx *sql.Tx,
	id int64,
) error {
	// TODO: Implement me!
	return nil
}

func (*WaterwayGauge) CleanUp() error { return nil }

const (
	selectCurrentUserCountrySQL = `SELECT users.current_user_country()`
)

func (wg *WaterwayGauge) Do(
	ctx context.Context,
	importID int64,
	conn *sql.Conn,
	feedback Feedback,
) (interface{}, error) {

	tx, err := conn.BeginTx(ctx, nil)
	if err != nil {
		return nil, err
	}
	defer tx.Rollback()

	var country string
	err = tx.QueryRowContext(ctx, selectCurrentUserCountrySQL).Scan(&country)
	switch {
	case err == sql.ErrNoRows:
		return nil, errors.New("Cannot figure out user country")
	case err != nil:
		return nil, err
	}

	country = strings.ToUpper(country)
	feedback.Info("Using country '%s'.", country)

	var auth *soap.BasicAuth
	if wg.Username != "" {
		auth = &soap.BasicAuth{
			Login:    wg.Username,
			Password: wg.Password,
		}
	}

	client := erdms.NewRefService(wg.URL, wg.Insecure, auth)

	request := &erdms.GetRisDataXML{
		GetRisDataXMLType: &erdms.GetRisDataXMLType{
			Subcode: erdms.NoNS{Text: country + "%"},
			Funcode: erdms.NoNS{Text: "wtwgag"},
		},
	}

	data, err := client.GetRisDataXML(request)

	if err != nil {
		log.Printf("error: %v\n", err)
		return nil, err
	}

	for _, dr := range data.RisdataReturn {
		if dr.RisidxCode == nil {
			log.Printf("warn: RisidxCode == nil")
			continue
		}
		if dr.Objname.Loc == nil {
			log.Printf("warn: Objname == nil")
			continue
		}
		log.Printf("RisidxCode: %s\n", *dr.RisidxCode)
		log.Printf("\tObjname: %s\n", *dr.Objname.Loc)
	}

	// TODO: Implement me!
	return nil, errors.New("Not implemented, yet!")
}