teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5992: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5992: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@5829: package org.dive4elements.river.importer; ingo@3328: rrenkert@5429: import java.math.BigDecimal; ingo@3328: import java.sql.SQLException; ingo@3329: import java.util.List; ingo@3328: tom@9726: import org.apache.logging.log4j.Logger; tom@9726: import org.apache.logging.log4j.LogManager; ingo@3328: ingo@3329: import org.hibernate.Query; ingo@3329: import org.hibernate.Session; ingo@3328: import org.hibernate.exception.ConstraintViolationException; ingo@3328: teichmann@5829: import org.dive4elements.river.model.MeasurementStation; teichmann@5829: import org.dive4elements.river.model.SQRelation; teichmann@5829: import org.dive4elements.river.model.SQRelationValue; ingo@3328: ingo@3328: ingo@3328: public class ImportSQRelationValue { ingo@3328: tom@9726: private static Logger log = LogManager.getLogger(ImportSQRelationValue.class); ingo@3328: ingo@3328: ingo@3328: private SQRelationValue peer; ingo@3328: ingo@3328: private String parameter; tom@8412: private MeasurementStation station; rrenkert@5429: private Double a; rrenkert@5429: private Double b; rrenkert@5429: private Double qMax; rrenkert@5429: private Double rSQ; rrenkert@5429: private Integer nTot; rrenkert@5429: private Integer nOutlier; rrenkert@5429: private Double cFerguson; rrenkert@5429: private Double cDuan; ingo@3328: ingo@3328: ingo@3328: public ImportSQRelationValue( ingo@3328: String parameter, tom@8412: MeasurementStation station, rrenkert@5429: Double a, rrenkert@5429: Double b, rrenkert@5429: Double qMax, rrenkert@5429: Double rSQ, rrenkert@5429: Integer nTot, rrenkert@5429: Integer nOutlier, rrenkert@5429: Double cFerguson, rrenkert@5429: Double cDuan ingo@3328: ) { ingo@3328: this.parameter = parameter; tom@8412: this.station = station; ingo@3328: this.a = a; ingo@3328: this.b = b; rrenkert@5429: this.qMax = qMax; rrenkert@5429: this.rSQ = rSQ; rrenkert@5429: this.nTot = nTot; rrenkert@5429: this.nOutlier = nOutlier; rrenkert@5429: this.cFerguson = cFerguson; rrenkert@5429: this.cDuan = cDuan; ingo@3328: } ingo@3328: ingo@3328: ingo@3328: public void storeDependencies(SQRelation owner) ingo@3328: throws SQLException, ConstraintViolationException ingo@3328: { ingo@3329: getPeer(owner); ingo@3328: } ingo@3328: ingo@3328: ingo@3329: public SQRelationValue getPeer(SQRelation owner) { ingo@3329: if (peer == null) { tom@8856: Session session = ImporterSession.getInstance() tom@8856: .getDatabaseSession(); tom@8691: Query query = session.createQuery( ingo@3329: "from SQRelationValue " + ingo@3329: " where sqRelation=:owner " + rrenkert@5429: " and measurementStation=:measurementStation" + tom@8691: " and parameter=:parameter"); ingo@3329: tom@8691: query.setParameter("owner", owner); tom@8691: query.setParameter("measurementStation", station); tom@8691: query.setString("parameter", parameter); ingo@3329: tom@8691: List values = query.list(); ingo@3329: ingo@3329: if (values.isEmpty()) { ingo@3329: peer = new SQRelationValue( ingo@3329: owner, ingo@3329: parameter, tom@8412: station, ingo@3329: a, rrenkert@5429: b, rrenkert@5429: qMax, rrenkert@5429: rSQ, rrenkert@5429: nTot, rrenkert@5429: nOutlier, rrenkert@5429: cFerguson, rrenkert@5429: cDuan ingo@3329: ); ingo@3329: ingo@3329: session.save(peer); ingo@3329: } ingo@3329: else { ingo@3329: peer = values.get(0); ingo@3329: } ingo@3328: } rrenkert@5429: return peer; rrenkert@5429: } ingo@3328: rrenkert@5429: private static final BigDecimal toBigDecimal(Double x) { rrenkert@5429: if (x == null) return null; rrenkert@5429: return new BigDecimal(x); ingo@3328: } ingo@3328: } ingo@3328: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :