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

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


public class ImportBedHeightValue {

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


    protected ImportBedHeight bedHeight;

    protected Double station;
    protected Double height;
    protected Double uncertainty;
    protected Double dataGap;
    protected Double soundingWidth;
    protected Double minHeight;
    protected Double maxHeight;
    protected Double[] sectionHeight;

    protected BedHeightValue peer;


    public ImportBedHeightValue(final ImportBedHeight bedHeight, final Double station, final Double height, final Double uncertainty, final Double dataGap,
            final Double soundingWidth, final Double minHeight, final Double maxHeight) {
        this.bedHeight     = bedHeight;
        this.station       = station;
        this.height        = height;
        this.uncertainty   = uncertainty;
        this.dataGap       = dataGap;
        this.soundingWidth = soundingWidth;
        this.minHeight = minHeight;
        this.maxHeight = maxHeight;
        this.sectionHeight = new Double[10];
    }


    public void setSectionHeight(final int index, final Double value) {
        this.sectionHeight[index - 1] = value;
    }

    public void storeDependencies(final BedHeight bedHeight) {
        getPeer(bedHeight);
    }


    /**
     * Add this value to database or return database bound Value, assuring
     * that the BedHeight exists in db already.
     */
    public BedHeightValue getPeer(final BedHeight bedHeight) {
        if (this.peer != null)
            return this.peer;

        List<BedHeightValue> values;
        final Session session = ImporterSession.getInstance().getDatabaseSession();
        if (this.bedHeight.storeMode == StoreMode.INSERT)
            values = null;
        else {
            final Query query = session.createQuery("FROM BedHeightValue WHERE (bedHeight=:bedHeight)"
                    + " AND (station BETWEEN (:station-0.0001) AND (:station+0.0001))");
            query.setParameter("bedHeight", bedHeight);
            query.setParameter("station", this.station);
            values = query.list();
        }
        if ((values == null) || values.isEmpty()) {
            this.peer = new BedHeightValue(bedHeight, this.station, this.height, this.uncertainty, this.dataGap, this.soundingWidth,
                    this.minHeight, this.maxHeight);
            for (int i = 1; i <= 10; i++)
                this.peer.setSectionHeight(i, this.sectionHeight[i - 1]);
            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