view flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightSingle.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 a5b003595d6c
children 5376aa576604
line wrap: on
line source
package de.intevation.flys.importer;

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

import java.sql.SQLException;

import org.apache.log4j.Logger;

import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.exception.ConstraintViolationException;

import de.intevation.flys.model.BedHeightSingle;
import de.intevation.flys.model.BedHeightType;
import de.intevation.flys.model.ElevationModel;
import de.intevation.flys.model.Range;
import de.intevation.flys.model.River;


public class ImportBedHeightSingle implements ImportBedHeight
{
    private static Logger log = Logger.getLogger(ImportBedHeightSingle.class);

    protected int year;
    protected int soundingWidth;

    protected String evaluationBy;
    protected String description;

    protected ImportRange          range;
    protected ImportBedHeightType  type;
    protected ImportLocationSystem locationSystem;
    protected ImportElevationModel curElevationModel;
    protected ImportElevationModel oldElevationModel;

    protected List<ImportBedHeightSingleValue> values;

    protected BedHeightSingle peer;


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


    public String getDescription() {
        return description;
    }

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


    public void setYear(int year) {
        this.year = year;
    }

    public void setTimeInterval(ImportTimeInterval timeInterval) {
        // do nothing
    }

    public void setSoundingWidth(int soundingWidth) {
        this.soundingWidth = soundingWidth;
    }

    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 setType(ImportBedHeightType type) {
        this.type = type;
    }

    public void setLocationSystem(ImportLocationSystem locationSystem) {
        this.locationSystem = locationSystem;
    }

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

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

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

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

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

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

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

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

        BedHeightSingle peer = getPeer(river);

        if (peer != null) {
            for (ImportBedHeightSingleValue value: values) {
                value.storeDependencies(peer);
            }
        }

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

    @Override
    public BedHeightSingle getPeer(River river) {
        if (peer == null) {
            BedHeightType  theType     = type != null ? type.getPeer() : null;
            ElevationModel theCurModel = curElevationModel.getPeer();
            Range          theRange    = range != null ? range.getPeer(river) : null;

            if (theType == null || theCurModel == null || theRange == null) {
                log.warn("BHS: Skip invalid file '" + description + "'");
                return null;
            }

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

            Query query = session.createQuery(
                "from BedHeightSingle where " +
                "river=:river and year=:year and soundingWidth=:soundingWidth " +
                "and type=:type and locationSystem=:locationSystem and " +
                "curElevationModel=:curElevationModel and range=:range");

            query.setParameter("river", river);
            query.setParameter("year", year);
            query.setParameter("soundingWidth", soundingWidth);
            query.setParameter("type", theType);
            query.setParameter("locationSystem", locationSystem.getPeer());
            query.setParameter("curElevationModel", theCurModel);
            query.setParameter("range", range.getPeer(river));

            List<BedHeightSingle> bedHeights = query.list();
            if (bedHeights.isEmpty()) {
                log.info("Create new BedHeightSingle DB instance.");

                peer = new BedHeightSingle(
                    river,
                    year,
                    soundingWidth,
                    theType,
                    locationSystem.getPeer(),
                    theCurModel,
                    oldElevationModel != null ? oldElevationModel.getPeer() : null,
                    range.getPeer(river),
                    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