view backend/src/main/java/org/dive4elements/river/importer/ImportFlowVelocityModelValue.java @ 9709:b74f817435fe

comment removed
author dnt_bjoernsen <d.tironi@bjoernsen.de>
date Wed, 27 Jan 2021 11:47:38 +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.dive4elements.river.importer.common.StoreMode;
import org.dive4elements.river.model.FlowVelocityModel;
import org.dive4elements.river.model.FlowVelocityModelValue;
import org.hibernate.Query;
import org.hibernate.Session;


public class ImportFlowVelocityModelValue {

    private final BigDecimal station;
    private final BigDecimal q;
    private final BigDecimal totalChannel;
    private final BigDecimal mainChannel;
    private final BigDecimal shearStress;

    private FlowVelocityModelValue peer;


    public ImportFlowVelocityModelValue(
            final BigDecimal station,
            final BigDecimal q,
            final BigDecimal totalChannel,
            final BigDecimal mainChannel,
            final BigDecimal shearStress
            ) {
        this.station      = station;
        this.q            = q;
        this.totalChannel = totalChannel;
        this.mainChannel  = mainChannel;
        this.shearStress  = shearStress;
    }


    public void storeDependencies(final FlowVelocityModel model, final StoreMode parentStoreMode) {
        getPeer(model, parentStoreMode);
    }


    public FlowVelocityModelValue getPeer(final FlowVelocityModel model, final StoreMode parentStoreMode) {
        if (this.peer == null) {
            List<FlowVelocityModelValue> values;
            final Session session = ImporterSession.getInstance().getDatabaseSession();
            if (parentStoreMode == StoreMode.INSERT)
                values = null;
            else {
                final Query query = session.createQuery(
                        "from FlowVelocityModelValue where " +
                                "   flowVelocity=:model and " +
                                "   station between :station - 0.00001 and :station + 0.00001"
                        );

                query.setParameter("model", model);
                query.setParameter("station", this.station.doubleValue());

                values = query.list();
            }
            if ((values == null) || values.isEmpty()) {
                this.peer = new FlowVelocityModelValue(
                        model, this.station, this.q, this.totalChannel, this.mainChannel, this.shearStress);

                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