view backend/src/main/java/org/dive4elements/river/importer/ImportWstColumn.java @ 6328:53d08f33d094

Backend: Moved guessing of main values and there time intervals out of the STA parser. Same come will be useful to extend the WST parser to better handle official lines.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 13 Jun 2013 17:15:34 +0200
parents 4c3ccf2b0304
children 7664ab97b4c7
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 org.dive4elements.river.model.Wst;
import org.dive4elements.river.model.WstColumn;
import org.dive4elements.river.model.River;
import org.dive4elements.river.model.TimeInterval;

import org.hibernate.Session;
import org.hibernate.Query;

import java.util.List;
import java.util.ArrayList;

import java.math.BigDecimal;

import org.apache.log4j.Logger;


/** 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 ImportTimeInterval timeInterval;

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

    protected WstColumn peer;

    public ImportWstColumn() {
        columnQRanges = new ArrayList<ImportWstColumnQRange>();
        columnValues  = new ArrayList<ImportWstColumnValue>();
    }

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

    public ImportWst getWst() {
        return wst;
    }

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

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

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

    public Integer getPosition() {
        return position;
    }

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

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

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


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


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

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

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

    public ImportTimeInterval getTimeInterval() {
        return timeInterval;
    }

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

    /** Get corresponding mapped wst-column (from database). */
    public WstColumn getPeer(River river) {
        if (peer == null) {
            Wst w = wst.getPeer(river);
            Session session = ImporterSession.getInstance().getDatabaseSession();
            Query query = session.createQuery(
                "from WstColumn where " +
                "wst=:wst and name=:name " +
                " and position=:position");
            query.setParameter("wst",         w);
            query.setParameter("name",        name);
            query.setParameter("position",    position);

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

            List<WstColumn> columns = query.list();
            if (columns.isEmpty()) {
                peer = new WstColumn(w, name, description, position, ti);
                session.save(peer);
            }
            else {
                peer = columns.get(0);
            }
        }
        return peer;
    }

    public void fixRangesOrder() {
        for (ImportWstColumnQRange wcqr: columnQRanges) {
            wcqr.fixRangesOrder();
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org