view backend/src/main/java/org/dive4elements/river/importer/ImportPorosity.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.Porosity;
import org.dive4elements.river.model.River;
import org.hibernate.Query;
import org.hibernate.Session;


public class ImportPorosity {

    private static Logger log = Logger.getLogger(ImportPorosity.class);

    protected Porosity peer;

    protected ImportDepth depth;

    protected String description;

    protected ImportTimeInterval timeInterval;

    protected List<ImportPorosityValue> values;

    protected StoreMode storeMode;

    public ImportPorosity(final String description) {
        this.description = description;
        this.values = new ArrayList<>();
        this.storeMode = StoreMode.NONE;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDepth(final ImportDepth depth) {
        this.depth = depth;
    }

    public void setTimeInterval(final ImportTimeInterval importTimeInterval) {
        this.timeInterval = importTimeInterval;
    }

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

    public void storeDependencies(final River river) {
        log.info("store dependencies for '" + getDescription() + "'");

        if (this.depth != null) {
            this.depth.storeDependencies();
        }

        final Porosity peer = getPeer(river);

        if (peer != null) {
            // log.info("store porosity values.");
            for (final ImportPorosityValue value : this.values) {
                value.storeDependencies(peer, this.storeMode);
            }
            log.info("Porosity values processed: " + this.values.size());
        }
    }

    public Porosity getPeer(final River river) {
        // log.info("get peer");

        if (this.depth == null) {
            log.warn("cannot store porosity '" + this.description
                    + "': no depth");
            return null;
        }

        if (this.peer == null) {
            final Session session = ImporterSession.getInstance()
                    .getDatabaseSession();

            final Query query = session.createQuery("from Porosity where "
                    + "   river=:river and "
                    + "   depth=:depth and "
                    + "   description=:description");

            query.setParameter("river", river);
            query.setParameter("depth", this.depth.getPeer());
            query.setParameter("description", this.description);

            final List<Porosity> porosity = query.list();

            if (porosity.isEmpty()) {
                log.debug("Create new Porosity DB instance.");

                this.peer = new Porosity(river, this.depth.getPeer(),
                        this.description, this.timeInterval.getPeer());

                session.save(this.peer);
                this.storeMode = StoreMode.INSERT;
            }
            else {
                this.peer = porosity.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