view backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightValue.java @ 8986:392bbcd8a88b

Database inserts accelerated by suppressing unnecessary database queries for new data series
author mschaefer
date Sun, 08 Apr 2018 18:07:06 +0200
parents a0a0a7f912ab
children 8aa7d9eaaa21
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 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;
    }


    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);
            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