view backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadValue.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) 2014 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.dive4elements.river.importer.common.StoreMode;
import org.dive4elements.river.model.MeasurementStation;
import org.dive4elements.river.model.SedimentLoad;
import org.dive4elements.river.model.SedimentLoadValue;
import org.hibernate.Query;
import org.hibernate.Session;

public class ImportSedimentLoadValue {

    private SedimentLoadValue peer;

    private MeasurementStation station;
    private Double             value;

    public ImportSedimentLoadValue() {
    }

    public ImportSedimentLoadValue(
            final MeasurementStation station,
            final Double             value
            ) {
        this.station      = station;
        this.value        = value;
    }

    protected SedimentLoadValue getPeer(final SedimentLoad sedimentLoad, final StoreMode parentStoreMode) {

        if (this.peer == null) {
            final Session session = ImporterSession.getInstance()
                    .getDatabaseSession();
            List<SedimentLoadValue> values;
            if (parentStoreMode == StoreMode.INSERT)
                values = null;
            else {
                final Query query = session.createQuery(
                        "from SedimentLoadValue where " +
                                "   measurementStation = :station and " +
                                "   sedimentLoad = :sedimentLoad and " +
                        "   value = :value");

                query.setParameter("station", this.station);
                query.setParameter("sedimentLoad", sedimentLoad);
                query.setParameter("value", this.value);

                values = query.list();
            }
            if ((values == null) || values.isEmpty()) {
                this.peer = new SedimentLoadValue(sedimentLoad, this.station, this.value);
                session.save(this.peer);
            }
            else {
                this.peer = values.get(0);
            }
        }

        return this.peer;
    }

    public void storeDependencies(final SedimentLoad sedimentLoad, final StoreMode parentStoreMode) {
        getPeer(sedimentLoad, parentStoreMode);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org