view backend/src/main/java/org/dive4elements/river/importer/ImportHYKFormation.java @ 9650:a2a42a6bac6b

Importer (s/u-info) extensions: outer try/catch for parse and log of line no, catching parsing exception if not enough value fields, parsing error and warning log messages with line number, detecting and rejecting duplicate data series, better differentiation between error and warning log messages
author mschaefer
date Mon, 23 Mar 2020 14:57:03 +0100
parents 392bbcd8a88b
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.importer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.dive4elements.river.importer.common.StoreMode;
import org.dive4elements.river.model.HYKEntry;
import org.dive4elements.river.model.HYKFormation;
import org.hibernate.Query;
import org.hibernate.Session;

public class ImportHYKFormation
{
    protected int            formationNum;
    protected ImportHYKEntry entry;
    protected BigDecimal     top;
    protected BigDecimal     bottom;
    protected BigDecimal     distanceVL;
    protected BigDecimal     distanceHF;
    protected BigDecimal     distanceVR;

    protected List<ImportHYKFlowZone> zones;

    protected StoreMode storeMode;

    protected HYKFormation peer;

    public ImportHYKFormation() {
        this.zones = new ArrayList<>();
        this.storeMode = StoreMode.NONE;
    }

    public ImportHYKFormation(
            final int            formationNum,
            final ImportHYKEntry entry,
            final BigDecimal     top,
            final BigDecimal     bottom,
            final BigDecimal     distanceVL,
            final BigDecimal     distanceHF,
            final BigDecimal     distanceVR
            ) {
        this();
        this.formationNum = formationNum;
        this.entry        = entry;
        this.top          = top;
        this.bottom       = bottom;
        this.distanceVL   = distanceVL;
        this.distanceHF   = distanceHF;
        this.distanceVR   = distanceVR;
    }

    public void addFlowZone(final ImportHYKFlowZone zone) {
        this.zones.add(zone);
        zone.setFormation(this);
    }

    public int getFormationNum() {
        return this.formationNum;
    }

    public void setFormationNum(final int formationNum) {
        this.formationNum = formationNum;
    }

    public ImportHYKEntry getEntry() {
        return this.entry;
    }

    public void setEntry(final ImportHYKEntry entry) {
        this.entry = entry;
    }

    public BigDecimal getTop() {
        return this.top;
    }

    public void setTop(final BigDecimal top) {
        this.top = top;
    }

    public BigDecimal getBottom() {
        return this.bottom;
    }

    public void setBottom(final BigDecimal bottom) {
        this.bottom = bottom;
    }

    public BigDecimal getDistanceVL() {
        return this.distanceVL;
    }

    public void setDistanceVL(final BigDecimal distanceVL) {
        this.distanceVL = distanceVL;
    }

    public BigDecimal getDistanceHF() {
        return this.distanceHF;
    }

    public void setDistanceHF(final BigDecimal distanceHF) {
        this.distanceHF = distanceHF;
    }

    public BigDecimal getDistanceVR() {
        return this.distanceVR;
    }

    public void setDistanceVR(final BigDecimal distanceVR) {
        this.distanceVR = distanceVR;
    }

    public void storeDependencies() {
        getPeer();
        for (final ImportHYKFlowZone zone: this.zones) {
            zone.storeDependencies();
        }
    }

    public HYKFormation getPeer() {
        if (this.peer == null) {
            final HYKEntry e = this.entry.getPeer();
            final Session session = ImporterSession.getInstance()
                    .getDatabaseSession();
            List<HYKFormation> formations;
            if (this.entry.storeMode == StoreMode.INSERT)
                formations = null;
            else {
                final Query query = session.createQuery(
                        "from HYKFormation where formationNum=:formationNum " +
                                "and entry=:entry and top=:top and bottom=:bottom " +
                                "and distanceVL=:distanceVL and distanceHF=:distanceHF " +
                        "and distanceVR=:distanceVR");
                query.setParameter("formationNum", this.formationNum);
                query.setParameter("entry", e);
                query.setParameter("top", this.top);
                query.setParameter("bottom", this.bottom);
                query.setParameter("distanceVL", this.distanceVL);
                query.setParameter("distanceHF", this.distanceHF);
                query.setParameter("distanceVR", this.distanceVR);
                formations = query.list();
            }
            if ((formations == null) || formations.isEmpty()) {
                this.peer = new HYKFormation(
                        this.formationNum, e, this.top, this.bottom,
                        this.distanceVL, this.distanceHF, this.distanceVR);
                session.save(this.peer);
                this.storeMode = StoreMode.INSERT;
            }
            else {
                this.peer = formations.get(0);
                this.storeMode = StoreMode.UPDATE;
            }
        }
        return this.peer;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org