view backend/src/main/java/org/dive4elements/river/importer/ImportHYKEntry.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.ArrayList;
import java.util.Date;
import java.util.List;

import org.dive4elements.river.importer.common.StoreMode;
import org.dive4elements.river.model.HYK;
import org.dive4elements.river.model.HYKEntry;
import org.hibernate.Query;
import org.hibernate.Session;

public class ImportHYKEntry
{
    protected ImportHYK  hyk;
    protected BigDecimal km;
    protected Date       measure;

    protected List<ImportHYKFormation> formations;

    protected StoreMode storeMode;

    protected HYKEntry peer;

    public ImportHYKEntry() {
        this.formations = new ArrayList<>();
        this.storeMode = StoreMode.NONE;
    }

    public ImportHYKEntry(
            final ImportHYK  hyk,
            final BigDecimal km,
            final Date       measure
            ) {
        this();
        this.hyk     = hyk;
        this.km      = km;
        this.measure = measure;
    }

    public ImportHYK getHYK() {
        return this.hyk;
    }

    public void setHYK(final ImportHYK hyk) {
        this.hyk = hyk;
    }

    public BigDecimal getKm() {
        return this.km;
    }

    public void setKm(final BigDecimal km) {
        this.km = km;
    }

    public void addFormation(final ImportHYKFormation formation) {
        final int numFormation = this.formations.size();
        this.formations.add(formation);
        formation.setFormationNum(numFormation);
        formation.setEntry(this);
    }

    public void storeDependencies() {
        getPeer();
        for (final ImportHYKFormation formation: this.formations) {
            formation.storeDependencies();
        }
    }

    public HYKEntry getPeer() {
        if (this.peer == null) {
            final HYK h = this.hyk.getPeer();
            final Session session = ImporterSession.getInstance()
                    .getDatabaseSession();
            List<HYKEntry> entries;
            if (this.hyk.storeMode == StoreMode.INSERT)
                entries = null;
            else {
                final Query query = session.createQuery(
                        "from HYKEntry where HYK=:hyk " +
                        "and km=:km and measure=:measure");
                query.setParameter("hyk", h);
                query.setParameter("km", this.km);
                query.setParameter("measure", this.measure);
                entries = query.list();
            }
            if ((entries == null) || entries.isEmpty()) {
                this.peer = new HYKEntry(h, this.km, this.measure);
                session.save(this.peer);
                this.storeMode = StoreMode.INSERT;
            }
            else {
                this.peer = entries.get(0);
                this.storeMode = StoreMode.UPDATE;
            }
        }
        return this.peer;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org