view backend/src/main/java/org/dive4elements/river/importer/ImportSQRelationValue.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 17db08570637
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.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

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

import org.dive4elements.river.model.MeasurementStation;
import org.dive4elements.river.model.SQRelation;
import org.dive4elements.river.model.SQRelationValue;


public class ImportSQRelationValue {

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


    private SQRelationValue peer;

    private String parameter;

    private Double km;
    private Double a;
    private Double b;
    private Double qMax;
    private Double rSQ;
    private Integer nTot;
    private Integer nOutlier;
    private Double cFerguson;
    private Double cDuan;


    public ImportSQRelationValue(
        String parameter,
        Double km,
        Double a,
        Double b,
        Double qMax,
        Double rSQ,
        Integer nTot,
        Integer nOutlier,
        Double cFerguson,
        Double cDuan
    ) {
        this.parameter = parameter;
        this.km        = km;
        this.a         = a;
        this.b         = b;
        this.qMax      = qMax;
        this.rSQ       = rSQ;
        this.nTot      = nTot;
        this.nOutlier  = nOutlier;
        this.cFerguson = cFerguson;
        this.cDuan     = cDuan;
    }


    public void storeDependencies(SQRelation owner)
    throws SQLException, ConstraintViolationException
    {
        getPeer(owner);
    }


    public SQRelationValue getPeer(SQRelation owner) {
        if (peer == null) {
            Session session = ImporterSession.getInstance().getDatabaseSession();

            Query query = session.createQuery(
                "from MeasurementStation " +
                "   where station between :kml and :kmh");
            query.setDouble("kml", km - 1e-4);
            query.setDouble("kmh", km + 1e-4);

            List<MeasurementStation> result = query.list();

            if (result.isEmpty()) {
                log.error("No measurement stations found at km " + km);
                return null;
            }

            Query query2 = session.createQuery(
                "from SQRelationValue " +
                "   where sqRelation=:owner " +
                "   and parameter=:parameter" +
                "   and measurementStation=:measurementStation" +
                "   and a=:a" +
                "   and b=:b" +
                "   and qMax=:qMax" +
                "   and rSQ=:rSQ" +
                "   and cFerguson=:cFerguson" +
                "   and cDuan=:cDuan");

            query2.setParameter("owner", owner);
            query2.setString("parameter", parameter);
            query2.setParameter("measurementStation", result.get(0));
            query2.setBigDecimal("a", toBigDecimal(a));
            query2.setBigDecimal("b", toBigDecimal(b));
            query2.setBigDecimal("qMax", toBigDecimal(qMax));
            query2.setBigDecimal("rSQ", toBigDecimal(rSQ));
            query2.setBigDecimal("cFerguson", toBigDecimal(cFerguson));
            query2.setBigDecimal("cDuan", toBigDecimal(cDuan));

            List<SQRelationValue> values = query2.list();

            if (values.isEmpty()) {
                peer = new SQRelationValue(
                    owner,
                    parameter,
                    result.get(0),
                    a,
                    b,
                    qMax,
                    rSQ,
                    nTot,
                    nOutlier,
                    cFerguson,
                    cDuan
                );

                session.save(peer);
            }
            else {
                peer = values.get(0);
            }
        }
        return peer;
    }

    private static final BigDecimal toBigDecimal(Double x) {
        if (x == null) return null;
        return new BigDecimal(x);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org