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

import org.apache.log4j.Logger;
import org.dive4elements.river.importer.common.StoreMode;
import org.dive4elements.river.model.River;
import org.dive4elements.river.model.TimeInterval;
import org.dive4elements.river.model.Wst;
import org.dive4elements.river.model.WstColumn;
import org.hibernate.Query;
import org.hibernate.Session;


/** Unmapped column of a WST. */
public class ImportWstColumn
{
    private static Logger log = Logger.getLogger(ImportWstColumn.class);

    protected ImportWst wst;
    protected String    name;
    protected String    description;
    protected Integer   position;
    protected String    source;

    protected ImportTimeInterval timeInterval;

    protected List<ImportWstColumnQRange> columnQRanges;
    protected List<ImportWstColumnValue>  columnValues;

    protected StoreMode storeMode;

    protected WstColumn peer;

    public ImportWstColumn() {
        this.columnQRanges = new ArrayList<>();
        this.columnValues  = new ArrayList<>();
        this.storeMode = StoreMode.NONE;
    }

    public ImportWstColumn(
            final ImportWst wst,
            final String    name,
            final String    description,
            final Integer   position,
            final String    source
            ) {
        this();
        this.wst         = wst;
        this.name        = name;
        this.description = description;
        this.position    = position;
    }

    public ImportWstColumn(
            final ImportWst wst,
            final String    name,
            final String    description,
            final Integer   position
            ) {
        this(wst, name, description, position, null);
    }

    public ImportWst getWst() {
        return this.wst;
    }

    public void setWst(final ImportWst wst) {
        this.wst = wst;
    }

    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(final String description) {
        this.description = description;
    }

    public Integer getPosition() {
        return this.position;
    }

    public void setPosition(final Integer position) {
        this.position = position;
    }

    public String getSource() {
        return this.source;
    }

    public void setSource(final String source) {
        this.source = source;
    }

    public void addColumnValue(final BigDecimal position, final BigDecimal w) {
        this.columnValues.add(
                new ImportWstColumnValue(this, position, w));
    }

    public void addColumnQRange(final ImportWstQRange columnQRange) {
        this.columnQRanges.add(
                new ImportWstColumnQRange(this, columnQRange));
    }


    /** Get the Column Values stored in this column. */
    public List<ImportWstColumnValue> getColumnValues() {
        return this.columnValues;
    }


    public void storeDependencies(final River river) {
        log.info("store column '" + this.name + "'");
        getPeer(river);

        for (final ImportWstColumnQRange columnQRange: this.columnQRanges) {
            columnQRange.getPeer(river);
        }

        for (final ImportWstColumnValue columnValue: this.columnValues) {
            columnValue.getPeer(river);
        }
    }

    public ImportTimeInterval getTimeInterval() {
        return this.timeInterval;
    }

    public void setTimeInterval(final ImportTimeInterval timeInterval) {
        this.timeInterval = timeInterval;
    }

    public boolean guessWaterLevelIncreasing() {

        final int N = this.columnValues.size();

        if (N < 2) {
            return true;
        }

        final Random r = new Random();
        int up = 0;

        final int S = N < 50 ? N : (int)(0.1f * N)+1;
        for (int s = 0; s < S; ++s) {
            int i1, i2;
            do {
                i1 = r.nextInt(N-1);
                i2 = r.nextInt(N-1);
            } while (i1 == i2);
            ImportWstColumnValue b = this.columnValues.get(i1);
            ImportWstColumnValue a = this.columnValues.get(i2);
            if (b.getPosition().compareTo(a.getPosition()) < 0) {
                final ImportWstColumnValue t = a; a = b; b = t;
            }

            if (a.getW().compareTo(b.getW()) < 0) ++up;
        }

        return up > S - up;
    }

    /** Get corresponding mapped wst-column (from database). */
    public WstColumn getPeer(final River river) {
        if (this.peer == null) {
            final Wst w = this.wst.getPeer(river);
            List<WstColumn> columns;
            final Session session = ImporterSession.getInstance()
                    .getDatabaseSession();
            if (this.wst.storeMode == StoreMode.INSERT)
                columns = null;
            else {
                final Query query = session.createQuery(
                        "from WstColumn where" +
                                " wst=:wst and name=:name" +
                                " and source=:source" +
                        " and position=:position");
                query.setParameter("wst",      w);
                query.setParameter("name",     this.name);
                query.setParameter("position", this.position);
                query.setParameter("source",   this.source);
                columns = query.list();
            }

            final TimeInterval ti = (this.timeInterval != null) ? this.timeInterval.getPeer() : null;

            if ((columns == null) || columns.isEmpty()) {
                log.debug("source: " + this.source);
                this.peer = new WstColumn(
                        w, this.name, this.description, this.source, this.position, ti);
                session.save(this.peer);
                this.storeMode = StoreMode.INSERT;
            }
            else {
                this.peer = columns.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