view backend/src/main/java/org/dive4elements/river/importer/ImportSQRelationValue.java @ 5844:4dd33b86dc61

Added header to river backend.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 26 Apr 2013 08:25:41 +0200
parents 5aa05a7a34b7
children 4c3ccf2b0304
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