view flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpoch.java @ 2810:04eeb45df27b

Implemented model classes and importer classes for bed height epochs. flys-backend/trunk@4222 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 12 Apr 2012 12:50:49 +0000
parents 33f40b23edd8
children 8926571e47fb
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.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;


public class ImportBedHeightEpoch
{
    private static Logger log = Logger.getLogger(ImportBedHeightEpoch.class);

    protected String evaluationBy;
    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 List<ImportBedHeightEpochValue> getValues() {
        return values;
    }


    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;
    }

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


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

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

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

        BedHeightEpoch peer = getPeer(river);

        log.debug("store values now...");

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


    public BedHeightEpoch getPeer(River river) {
        if (peer == null) {
            ElevationModel theCurModel = curElevationModel != null
                ? curElevationModel.getPeer()
                : null;

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

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

            if (theCurModel == null || theRange == null || theTime == null) {
                log.warn("Skip invalid file '" + description + "'");
                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
                );
            }
            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