Mercurial > dive4elements > river
changeset 3289:ec9aab93f292
SQ: First peek into fetching relevant data from SedDB
flys-artifacts/trunk@4957 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 12 Jul 2012 16:15:30 +0000 |
parents | cf1b769190b7 |
children | 36f3b53d5cfc |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverview.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/SQOverviewFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Measurement.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/MeasurementFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQRelationCalculation.java |
diffstat | 6 files changed, 170 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- 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 <sascha.teichmann@intevation.de> + + * 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 <sascha.teichmann@intevation.de> * src/main/java/de/intevation/flys/artifacts/services/SQKMChartService.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);
--- 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";
--- /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<String, Object> data; + + public Measurement() { + } + + public Measurement(Map<String, Object> 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 :
--- /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<String, Object> map = new HashMap<String, Object>(); + 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<Measurement> 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<Measurement> 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 :
--- 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<Measurement> 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); } }