view backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightEpoch.java @ 6328:53d08f33d094

Backend: Moved guessing of main values and there time intervals out of the STA parser. Same come will be useful to extend the WST parser to better handle official lines.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 13 Jun 2013 17:15:34 +0200
parents 4c3ccf2b0304
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.importer;

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

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

import org.dive4elements.river.model.BedHeightEpoch;
import org.dive4elements.river.model.ElevationModel;
import org.dive4elements.river.model.Range;
import org.dive4elements.river.model.River;
import org.dive4elements.river.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) {
        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: invalid km range.");
            }

            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