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

import org.apache.log4j.Logger;
import org.dive4elements.river.importer.common.StoreMode;
import org.dive4elements.river.model.Porosity;
import org.dive4elements.river.model.PorosityValue;
import org.hibernate.Query;
import org.hibernate.Session;


public class ImportPorosityValue {

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


    protected PorosityValue peer;

    protected BigDecimal station;

    protected BigDecimal shoreOffset;

    protected BigDecimal porosity;

    protected String description;


    public ImportPorosityValue(
            final BigDecimal station,
            final BigDecimal shoreOffset,
            final BigDecimal porosity,
            final String     description
            ) {
        this.station     = station;
        this.shoreOffset = shoreOffset;
        this.porosity    = porosity;
        this.description = description;
    }


    public void storeDependencies(final Porosity porosity, final StoreMode parentStoreMode) {
        // log.info("store dependencies");

        getPeer(porosity, parentStoreMode);
    }


    public PorosityValue getPeer(final Porosity porosity, final StoreMode parentStoreMode) {
        log.info("get peer");

        if (this.peer == null) {
            final Session session = ImporterSession.getInstance()
                    .getDatabaseSession();
            List<PorosityValue> values;
            if (parentStoreMode == StoreMode.INSERT)
                values = null;
            else {
                final Query query = session.createQuery(
                        "from PorosityValue "
                                + "where porosity=:porosity "
                                + "and station between :station - 0.0001f "
                                + "    and :station + 0.0001f "
                                + "and porosityValue between :poros -0.0001f "
                                + "    and :poros + 0.0001f "
                                + "and description=:description");

                query.setParameter("porosity", porosity);
                query.setParameter("station", this.station.floatValue());
                query.setParameter("poros", this.porosity.floatValue());
                query.setParameter("description", this.description);

                values = query.list();
            }
            if ((values == null) || values.isEmpty()) {
                // log.debug("Create new PorosityValue DB instance.");

                this.peer = new PorosityValue(
                        porosity,
                        this.station,
                        this.shoreOffset,
                        this.porosity,
                        this.description);

                session.save(this.peer);
            }
            else {
                this.peer = values.get(0);
            }
        }

        return this.peer;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org