view flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpoch.java @ 4198:1cdbd8a0c994

Added two new tables ClickableQDTable and ClickableWTable and made Ws and Qs clickable in historical discharge calculation. The new tables define listener interfaces (clicked lower or upper icon) to listen to user clicks. In addition to this, there is an enum ClickMode with NONE, SINGLE and RANGE options, which allows to specifiy, which icons are displayed in the tables. NONE means no icon for user clicks, SINGLE has 1 icon, RANGE 2 icons for lower and upper.
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 22 Oct 2012 13:31:25 +0200
parents 3825aad9fb41
children 4ee97d914501
line wrap: on
line source
package de.intevation.flys.importer;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.exception.ConstraintViolationException;

import de.intevation.flys.model.BedHeightEpoch;
import de.intevation.flys.model.ElevationModel;
import de.intevation.flys.model.Range;
import de.intevation.flys.model.River;
import de.intevation.flys.model.TimeInterval;


/** Import Bed Height Data, 'epoch' type from csv file. */
public class ImportBedHeightEpoch implements ImportBedHeight {

    /** Private logger. */
    private static Logger log = Logger.getLogger(ImportBedHeightEpoch.class);

    protected String evaluationBy;

    /** De facto the file name. */
    protected String description;

    protected ImportTimeInterval timeInterval;
    protected ImportRange range;
    protected ImportElevationModel curElevationModel;
    protected ImportElevationModel oldElevationModel;

    protected List<ImportBedHeightEpochValue> values;

    protected BedHeightEpoch peer;

    public ImportBedHeightEpoch(String description) {
        this.description = description;
        this.values = new ArrayList<ImportBedHeightEpochValue>();
    }

    public String getDescription() {
        return description;
    }

    public int getValueCount() {
        return values.size();
    }

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

    public void setEvaluationBy(String evaluationBy) {
        this.evaluationBy = evaluationBy;
    }

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

    public void setRange(ImportRange range) {
        this.range = range;
    }

    public void setCurElevationModel(ImportElevationModel curElevationModel) {
        this.curElevationModel = curElevationModel;
    }

    public void setOldElevationModel(ImportElevationModel oldElevationModel) {
        this.oldElevationModel = oldElevationModel;
    }

    /** Does nothing. */
    public void setYear(int year) {
        // do nothing
    }

    /** Does nothing. */
    public void setSoundingWidth(int soundingWidth) {
        // do nothing
    }


    /** Does nothing. */
    public void setLocationSystem(ImportLocationSystem locationSystem) {
        // do nothing
    }


    /** Does nothing. */
    public void setType(ImportBedHeightType type) {
        // do nothing
    }

    @Override
    public void addValue(ImportBedHeightValue value) {
        values.add((ImportBedHeightEpochValue) value);
    }

    @Override
    public void storeDependencies(River river) throws SQLException,
        ConstraintViolationException {
        log.info("Store dependencies for epoch: '" + getDescription() + "'");

        BedHeightEpoch peer = getPeer(river);

        if (curElevationModel != null) {
            curElevationModel.storeDependencies();
        }

        if (oldElevationModel != null) {
            oldElevationModel.storeDependencies();
        }

        if (peer != null) {
            log.debug("store values now...");

            for (ImportBedHeightEpochValue value : values) {
                value.storeDependencies(peer);
            }
        }

        Session session = ImporterSession.getInstance().getDatabaseSession();
        session.flush();
    }

    /**
     * Asserts all dependent entities (ElevationModel, TimeInterval, Range,
     * BedHeighEpoch) are in db and returns bound (either newly created or
     * freshly fetched) BedHeightEpoch.
     */
    @Override
    public BedHeightEpoch getPeer(River river) {
        if (peer == null) {
            ElevationModel theCurModel = null;
            if (curElevationModel != null) {
                curElevationModel.storeDependencies();
                theCurModel = curElevationModel.getPeer();
            }

            if (theCurModel == null) {
                log.warn("BHE: Skip file - invalid current elevation model.");
                return null;
            }

            TimeInterval theTime = null;
            if (timeInterval != null) {
                theTime = timeInterval.getPeer();
            }

            if (theTime == null) {
                log.warn("BHE: Skip file - invalid time range.");
                return null;
            }

            Range theRange = range != null ? range.getPeer(river) : null;

            if (theRange == null) {
                log.warn("BHE: Skip file - invalid km range.");
                return null;
            }

            Session session = ImporterSession.getInstance()
                .getDatabaseSession();

            Query query = session.createQuery("from BedHeightEpoch where "
                + "   river=:river and " + "   timeInterval=:timeInterval and "
                + "   curElevationModel=:curElevationModel and "
                + "   range=:range and " + "   evaluationBy=:evaluationBy and "
                + "   description=:description");

            query.setParameter("river", river);
            query.setParameter("timeInterval", theTime);
            query.setParameter("curElevationModel", theCurModel);
            query.setParameter("range", theRange);
            query.setParameter("evaluationBy", evaluationBy);
            query.setParameter("description", description);

            List<BedHeightEpoch> bedHeights = query.list();

            if (bedHeights.isEmpty()) {
                log.info("Create new BedHeightEpoch DB instance.");

                peer = new BedHeightEpoch(river, theTime, theRange,
                    theCurModel,
                    oldElevationModel != null ? oldElevationModel.getPeer()
                        : null, evaluationBy, description);

                session.save(peer);
            }
            else {
                peer = bedHeights.get(0);
            }
        }

        return peer;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org