# HG changeset patch # User Sascha L. Teichmann # Date 1342109730 0 # Node ID ec9aab93f292e4d46dc3efb777bbd83ea0cf79ac # Parent cf1b769190b7ce9161a0e3f548610f4c64022aa6 SQ: First peek into fetching relevant data from SedDB flys-artifacts/trunk@4957 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r cf1b769190b7 -r ec9aab93f292 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Thu Jul 12 14:03:15 2012 +0000 +++ b/flys-artifacts/ChangeLog Thu Jul 12 16:15:30 2012 +0000 @@ -1,3 +1,20 @@ +2012-07-12 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java: + New. Access to the data from the SedDB. + + * src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java: + Load measurements from SedDB. + + * src/main/java/de/intevation/flys/artifacts/model/SQOverview.java: + Force NOT NULL for discharge values, too. + + * src/main/java/de/intevation/flys/artifacts/model/SQOverviewFactory.java: + Indentation fix. + + * src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java: + Iterate over all given periods. + 2012-07-12 Sascha L. Teichmann * src/main/java/de/intevation/flys/artifacts/services/SQKMChartService.java: diff -r cf1b769190b7 -r ec9aab93f292 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverview.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverview.java Thu Jul 12 14:03:15 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverview.java Thu Jul 12 16:15:30 2012 +0000 @@ -96,6 +96,7 @@ " ON s.gewaesserid = g.gewaesserid " + "WHERE" + " m.gsiebsatzid IS NOT NULL AND" + + " m.q IS NOT NULL AND" + " g.name = :name " + "ORDER by" + " s.km, m.datum"; @@ -119,8 +120,8 @@ protected void loadData(Session session) { SQLQuery query = session.createSQLQuery(SQL_SQ) - .addScalar("km", StandardBasicTypes.DOUBLE) - .addScalar("datum", StandardBasicTypes.DATE); + .addScalar("km", StandardBasicTypes.DOUBLE) + .addScalar("datum", StandardBasicTypes.DATE); query.setString("name", riverName); diff -r cf1b769190b7 -r ec9aab93f292 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverviewFactory.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverviewFactory.java Thu Jul 12 14:03:15 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverviewFactory.java Thu Jul 12 16:15:30 2012 +0000 @@ -12,7 +12,8 @@ import org.hibernate.Session; public class SQOverviewFactory { - private static Logger log = Logger.getLogger(SQOverviewFactory.class); + + private static Logger log = Logger.getLogger(SQOverviewFactory.class); public static final String CACHE_NAME = "sq-overviews"; diff -r cf1b769190b7 -r ec9aab93f292 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java Thu Jul 12 16:15:30 2012 +0000 @@ -0,0 +1,23 @@ +package de.intevation.flys.artifacts.model.sq; + +import java.util.Map; + +public class Measurement +{ + protected Map data; + + public Measurement() { + } + + public Measurement(Map data) { + this.data = data; + } + + /** TODO: Extract data. */ + + @Override + public String toString() { + return "Measurement: " + data; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : diff -r cf1b769190b7 -r ec9aab93f292 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java Thu Jul 12 16:15:30 2012 +0000 @@ -0,0 +1,96 @@ +package de.intevation.flys.artifacts.model.sq; + +import de.intevation.flys.artifacts.model.DateRange; + +import de.intevation.flys.backend.SedDBSessionHolder; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; + +import org.hibernate.SQLQuery; +import org.hibernate.Session; + +import org.hibernate.transform.BasicTransformerAdapter; + +import org.hibernate.type.StandardBasicTypes; + +public class MeasurementFactory +{ + private static final Logger log = + Logger.getLogger(MeasurementFactory.class); + + public static final String SQL_MEASSURE = + "SELECT " + + "m.TSAND AS TSAND," + + "m.TSCHWEB AS TSCHWEB," + + "m.CSCHWEB AS CSCHWEB " + + /* TODO: Select more */ + "FROM messung m " + + "JOIN station s ON m.stationid = s.stationid " + + "JOIN gewaesser g ON s.gewaesserid = g.gewaesserid " + + "WHERE " + + "g.name = :river_name AND " + + "s.km BETWEEN :location - 0.001 AND :location + 0.001 AND " + + "m.datum BETWEEN :from AND :to"; + + public static final class MeasurementResultTransformer + extends BasicTransformerAdapter + { + public static MeasurementResultTransformer INSTANCE = + new MeasurementResultTransformer(); + + public MeasurementResultTransformer() { + } + + @Override + public Object transformTuple(Object [] tuple, String [] aliases) { + Map map = new HashMap(); + for (int i = 0; i < tuple.length; ++i) { + map.put(aliases[i], tuple[i]); + } + return new Measurement(map); + } + } // class BasicTransformerAdapter + + private MeasurementFactory() { + } + + protected static List load( + Session session, + String river, + double location, + DateRange dateRange + ) { + SQLQuery query = session.createSQLQuery(SQL_MEASSURE) + .addScalar("TSAND", StandardBasicTypes.DOUBLE) + .addScalar("TSCHWEB", StandardBasicTypes.DOUBLE) + .addScalar("CSCHWEB", StandardBasicTypes.DOUBLE); + + query.setString("river_name", river); + query.setDouble("location", location); + query.setDate("from", dateRange.getFrom()); + query.setDate("to", dateRange.getTo()); + + query.setResultTransformer(MeasurementResultTransformer.INSTANCE); + + return query.list(); + } + + public static List getMeasurements( + String river, + double location, + DateRange dateRange + ) { + Session session = SedDBSessionHolder.HOLDER.get(); + try { + return load(session, river, location, dateRange); + } + finally { + session.close(); + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : diff -r cf1b769190b7 -r ec9aab93f292 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java Thu Jul 12 14:03:15 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java Thu Jul 12 16:15:30 2012 +0000 @@ -6,6 +6,10 @@ import de.intevation.flys.artifacts.model.CalculationResult; import de.intevation.flys.artifacts.model.DateRange; +import de.intevation.flys.backend.SedDBSessionHolder; + +import java.util.List; + import org.apache.log4j.Logger; public class SQRelationCalculation extends Calculation { @@ -64,10 +68,34 @@ return new CalculationResult(this); } - SQResult [] result = null; + SedDBSessionHolder.acquire(); + try { + return internalCalculate(); + } + finally { + SedDBSessionHolder.release(); + } + } + + protected CalculationResult internalCalculate() { + + boolean debug = log.isDebugEnabled(); + + for (DateRange period: periods) { + List measurements = + MeasurementFactory.getMeasurements(river, location, period); + + if (debug) { + for (Measurement measument: measurements) { + log.debug(measument.toString()); + } + } + } // TODO: Implement me! + SQResult [] result = null; + return new CalculationResult(result, this); } }