view flys-backend/src/main/java/de/intevation/flys/importer/ImportSedimentYield.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 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.Session;
import org.hibernate.Query;
import org.hibernate.exception.ConstraintViolationException;

import de.intevation.flys.model.GrainFraction;
import de.intevation.flys.model.River;
import de.intevation.flys.model.SedimentYield;
import de.intevation.flys.model.TimeInterval;
import de.intevation.flys.model.Unit;


public class ImportSedimentYield {

    private static Logger log = Logger.getLogger(ImportSedimentYield.class);

    private ImportGrainFraction grainFraction;

    private ImportUnit unit;

    private ImportTimeInterval timeInterval;

    private String description;

    private List<ImportSedimentYieldValue> values;

    private SedimentYield peer;

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

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

    public void setUnit(ImportUnit unit) {
        this.unit = unit;
    }

    public void setGrainFraction(ImportGrainFraction grainFraction) {
        this.grainFraction = grainFraction;
    }

    public void addValue(ImportSedimentYieldValue value) {
        this.values.add(value);
    }

    public void storeDependencies(River river) throws SQLException,
        ConstraintViolationException {
        log.debug("store dependencies");

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

        SedimentYield peer = getPeer(river);

        if (peer != null) {
            int i = 0;

            for (ImportSedimentYieldValue value : values) {
                value.storeDependencies(peer);
                i++;
            }

            log.info("stored " + i + " sediment yield values.");
        }
    }

    public SedimentYield getPeer(River river) {
        log.debug("get peer");

        GrainFraction gf = grainFraction != null ? grainFraction.getPeer()
            : null;

        Unit u = unit != null ? unit.getPeer() : null;

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

        if (ti == null || u == null) {
            log.warn("Skip invalid SedimentYield: time interval or unit null!");
            return null;
        }

        if (peer == null) {
            Session session = ImporterSession.getInstance()
                .getDatabaseSession();
            Query query = session.createQuery("from SedimentYield where "
                + "   river=:river and "
                + "   grainFraction=:grainFraction and " + "   unit=:unit and "
                + "   timeInterval=:timeInterval and "
                + "   description=:description");

            query.setParameter("river", river);
            query.setParameter("grainFraction", gf);
            query.setParameter("unit", u);
            query.setParameter("timeInterval", ti);
            query.setParameter("description", description);

            List<SedimentYield> yields = query.list();
            if (yields.isEmpty()) {
                log.debug("create new SedimentYield");

                peer = new SedimentYield(river, u, ti, gf, description);
                session.save(peer);
            }
            else {
                peer = yields.get(0);
            }
        }

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

http://dive4elements.wald.intevation.org