view backend/src/main/java/org/dive4elements/river/importer/ImportFlowVelocityModel.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.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dive4elements.river.importer.common.StoreMode;
import org.dive4elements.river.model.DischargeZone;
import org.dive4elements.river.model.FlowVelocityModel;
import org.dive4elements.river.model.River;
import org.hibernate.Query;
import org.hibernate.Session;


public class ImportFlowVelocityModel {

    private static final Logger log = Logger
            .getLogger(ImportFlowVelocityModel.class);

    private String description;

    private ImportDischargeZone dischargeZone;

    private final List<ImportFlowVelocityModelValue> values;

    protected StoreMode storeMode;

    private FlowVelocityModel peer;

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

    public ImportFlowVelocityModel(final String description) {
        this();
        this.description = description;
    }

    public ImportFlowVelocityModel(final ImportDischargeZone dischargeZone,
            final String description) {
        this();
        this.dischargeZone = dischargeZone;
        this.description = description;
    }

    public void setDischargeZone(final ImportDischargeZone dischargeZone) {
        this.dischargeZone = dischargeZone;
    }

    public void setDescription(final String description) {
        this.description = description;
    }

    public void addValue(final ImportFlowVelocityModelValue value) {
        this.values.add(value);
    }

    public void storeDependencies(final River river) {
        log.debug("store dependencies");

        if (this.dischargeZone == null) {
            log.warn("skip flow velocity model: No discharge zone specified.");
            return;
        }

        this.dischargeZone.storeDependencies(river);

        final FlowVelocityModel peer = getPeer(river);

        if (peer != null) {
            int i = 0;

            for (final ImportFlowVelocityModelValue value : this.values) {
                value.storeDependencies(peer, this.storeMode);
                i++;
            }

            log.info("stored " + i + " flow velocity model values.");
        }
    }

    public FlowVelocityModel getPeer(final River river) {
        if (this.peer == null) {
            final Session session = ImporterSession.getInstance()
                    .getDatabaseSession();

            final DischargeZone zone = this.dischargeZone.getPeer(river);

            final Query query = session.createQuery("from FlowVelocityModel where "
                    + "   dischargeZone=:dischargeZone");

            query.setParameter("dischargeZone", zone);

            final List<FlowVelocityModel> model = query.list();

            if (model.isEmpty()) {
                this.peer = new FlowVelocityModel(zone, this.description);
                session.save(this.peer);
                this.storeMode = StoreMode.INSERT;
            }
            else {
                this.peer = model.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