view flys-backend/src/main/java/de/intevation/flys/importer/ImportBedHeightEpoch.java @ 2811:8926571e47fb

Finished importing MINFO bed heights (single and epoch). flys-backend/trunk@4225 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 13 Apr 2012 07:24:55 +0000
parents 04eeb45df27b
children 0d27d02b1208
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 implements ImportBedHeight
{
    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 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;
    }

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

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

    public void setLocationSystem(ImportLocationSystem locationSystem) {
        // do 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() + "'");

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

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


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

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

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

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

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

            if (theRange == null) {
                log.warn("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