# HG changeset patch # User Raimund Renkert # Date 1371821744 -7200 # Node ID 9a0f8d532797609f6fe0e53d7802b2f720a37abf # Parent ec537afacac2638147e972cc7ead6637f91c6aa2 Load sedimentload fraction with sedimentload factory and add new facets to artifact. diff -r ec537afacac2 -r 9a0f8d532797 artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoad.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoad.java Fri Jun 21 15:33:06 2013 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoad.java Fri Jun 21 15:35:44 2013 +0200 @@ -24,6 +24,7 @@ protected Date start; protected Date end; protected boolean isEpoch; + protected String unit; protected HashMap kms; @@ -35,13 +36,15 @@ String description, Date start, Date end, - boolean isEpoch + boolean isEpoch, + String unit ) { this(); this.description = description; this.start = start; this.end = end; this.isEpoch = isEpoch; + this.unit = unit; } public String getDescription() { @@ -192,6 +195,27 @@ } } + public void setUnknown(double km, double unknown, Range range) { + if (kms.containsKey(km)) { + kms.get(km).setUnknown(unknown); + kms.get(km).setUnknownRange(range); + } + else { + SedimentLoadFraction f = new SedimentLoadFraction(); + f.setUnknown(unknown); + f.setUnknownRange(range); + kms.put(km, f); + } + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + public boolean hasCoarse() { for (SedimentLoadFraction slf : kms.values()) { if (slf.getCoarse() > 0d) { diff -r ec537afacac2 -r 9a0f8d532797 artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java Fri Jun 21 15:33:06 2013 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadFactory.java Fri Jun 21 15:35:44 2013 +0200 @@ -13,25 +13,21 @@ import java.util.Calendar; import java.util.Date; import java.util.List; -import java.util.Map; import java.util.TreeMap; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; import org.apache.log4j.Logger; -import org.hibernate.SQLQuery; -import org.hibernate.Session; -import org.hibernate.type.StandardBasicTypes; - -import org.dive4elements.river.model.MeasurementStation; - import org.dive4elements.river.artifacts.cache.CacheFactory; - import org.dive4elements.river.artifacts.model.Range; import org.dive4elements.river.artifacts.model.RiverFactory; import org.dive4elements.river.artifacts.model.StaticSedimentLoadCacheKey; import org.dive4elements.river.backend.SessionHolder; +import org.dive4elements.river.model.MeasurementStation; +import org.hibernate.SQLQuery; +import org.hibernate.Session; +import org.hibernate.type.StandardBasicTypes; /** Pull Sediment Loads out of db. */ public class SedimentLoadFactory @@ -60,11 +56,13 @@ "SELECT DISTINCT " + " sy.description AS description, " + " ti.start_time AS start, " + - " ti.stop_time AS end " + + " ti.stop_time AS end, " + + " u.name AS unit" + " FROM sediment_yield sy " + " JOIN rivers r ON sy.river_id = r.id " + " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " + " JOIN time_intervals ti ON sy.time_interval_id = ti.id " + + " JOIN units u ON u.id = sy.unit_id " + " WHERE r.name = :name " + " AND ti.stop_time IS NOT NULL " + " AND syv.station BETWEEN :startKm AND :endKm"; @@ -74,17 +72,51 @@ " sy.description AS description, " + " ti.start_time AS year, " + " syv.value AS load, " + - " syv.station AS km " + + " syv.station AS km, " + + " u.name AS unit " + " FROM sediment_yield sy " + " JOIN rivers r ON sy.river_id = r.id " + " JOIN time_intervals ti ON sy.time_interval_id = ti.id " + " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " + " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " + + " JOIN units u ON u.id = sy.unit_id" + " WHERE r.name = :name " + " AND ti.start_time BETWEEN :begin AND :end " + " AND ti.stop_time IS NULL " + " AND gf.name = :grain " + - " AND syv.station BETWEEN :startKm AND :endKm"; + " AND syv.station BETWEEN :startKm AND :endKm " + + " ORDER BY syv.station"; + + public static final String SQL_SELECT_UNKNOWN_DATA = + "SELECT" + + " sy.description AS description, " + + " ti.start_time AS start, " + + " ti.stop_time AS end, " + + " syv.value AS load, " + + " syv.station AS km, " + + " u.name AS unit " + + " FROM sediment_yield sy " + + " JOIN rivers r ON sy.river_id = r.id " + + " JOIN time_intervals ti ON sy.time_interval_id = ti.id " + + " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " + + " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " + + " JOIN units u ON u.id = sy.unit_id" + + " WHERE r.name = :name " + + " AND sy.description = :descr " + + " AND gf.name = 'unknown' " + + " ORDER BY syv.station"; + + public static final String SQL_SELECT_UNKNOWN = + "SELECT DISTINCT " + + " sy.description AS description, " + + " ti.start_time AS start, " + + " ti.stop_time AS end " + + "FROM sediment_yield sy " + + " JOIN rivers r ON sy.river_id = r.id " + + " JOIN time_intervals ti ON sy.time_interval_id = ti.id " + + " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " + + "WHERE r.name = :river" + + " AND gf.name = 'unknown'"; public static final String SQL_SELECT_EPOCHS_DATA = "SELECT" + @@ -92,17 +124,20 @@ " ti.start_time AS startYear, " + " syv.value AS load, " + " syv.station AS km," + - " ti.stop_time AS endYear " + + " ti.stop_time AS endYear, " + + " u.name AS unit" + " FROM sediment_yield sy " + " JOIN rivers r ON sy.river_id = r.id " + " JOIN time_intervals ti ON sy.time_interval_id = ti.id " + " JOIN sediment_yield_values syv ON sy.id = syv.sediment_yield_id " + " JOIN grain_fraction gf ON sy.grain_fraction_id = gf.id " + + " JOIN units u ON u.id = sy.unit_id " + " WHERE r.name = :name " + " AND ti.start_time BETWEEN :sbegin AND :send " + " AND ti.stop_time BETWEEN :ebegin AND :eend " + " AND gf.name = :grain " + - " AND syv.station BETWEEN :startKm AND :endKm"; + " AND syv.station BETWEEN :startKm AND :endKm " + + " ORDER BY syv.station"; private SedimentLoadFactory() { } @@ -225,7 +260,8 @@ (String) row[0], (Date) row[1], null, - false); + false, + ""); } return loads; } @@ -246,7 +282,8 @@ (String) row[0], (Date) row[1], (Date) row[2], - true); + true, + ""); } return loads; } @@ -297,7 +334,8 @@ .addScalar("description", StandardBasicTypes.STRING) .addScalar("year", StandardBasicTypes.DATE) .addScalar("load", StandardBasicTypes.DOUBLE) - .addScalar("km", StandardBasicTypes.DOUBLE); + .addScalar("km", StandardBasicTypes.DOUBLE) + .addScalar("unit", StandardBasicTypes.STRING); sqlQuery.setString("name", river); sqlQuery.setDouble("startKm", startKm); sqlQuery.setDouble("endKm", endKm); @@ -311,7 +349,8 @@ (String) row[0], (Date) row[1], null, - false); + false, + (String) row[4]); getValues("coarse", sqlQuery, load, floatStations); getValues("fine_middle", sqlQuery, load, floatStations); getValues("sand", sqlQuery, load, floatStations); @@ -331,7 +370,8 @@ .addScalar("startYear", StandardBasicTypes.DATE) .addScalar("load", StandardBasicTypes.DOUBLE) .addScalar("km", StandardBasicTypes.DOUBLE) - .addScalar("endYear", StandardBasicTypes.DATE); + .addScalar("endYear", StandardBasicTypes.DATE) + .addScalar("unit", StandardBasicTypes.STRING); sqlQuery.setString("name", river); sqlQuery.setDouble("startKm", startKm); sqlQuery.setDouble("endKm", endKm); @@ -350,7 +390,8 @@ (String) row[0], (Date) row[1], (Date) row[4], - true); + true, + (String)row[5]); TDoubleArrayList kms = new TDoubleArrayList(); for (int i = 0; i < results.size(); i++) { row = results.get(i); @@ -430,5 +471,100 @@ } } } + + public static SedimentLoad getLoadUnknown( + String river, + String description + ) { + log.debug("SedimentLoadFactory.getLoadWithData"); + Cache cache = CacheFactory.getCache(LOAD_DATA_CACHE_NAME); + + if (cache == null) { + log.debug("Cache not configured."); + return getSedimentLoadUnknownUncached(river, description); + } + + StaticSedimentLoadCacheKey key = + new StaticSedimentLoadCacheKey(river, 0d, 0d, 0, 0); + + Element element = cache.get(key); + + if (element != null) { + log.debug("SedimentLoad found in cache"); + return (SedimentLoad)element.getValue(); + } + + SedimentLoad values = getSedimentLoadUnknownUncached(river, description); + + if (values != null && key != null) { + log.debug("Store static bed height values in cache."); + element = new Element(key, values); + cache.put(element); + } + return values; + } + + /** + * Get sediment loads with fraction 'unknown' from db. + * @param river the river + * @param type the sediment load type (year or epoch) + * @return according sediment loads. + */ + public static SedimentLoad getSedimentLoadUnknownUncached( + String river, + String description + ) { + log.debug("SedimentLoadFactory.getSedimentLoadWithDataUncached"); + Session session = SessionHolder.HOLDER.get(); + SQLQuery sqlQuery = null; + + sqlQuery = session.createSQLQuery(SQL_SELECT_UNKNOWN_DATA) + .addScalar("description", StandardBasicTypes.STRING) + .addScalar("start", StandardBasicTypes.DATE) + .addScalar("end", StandardBasicTypes.DATE) + .addScalar("load", StandardBasicTypes.DOUBLE) + .addScalar("km", StandardBasicTypes.DOUBLE) + .addScalar("unit", StandardBasicTypes.STRING); + sqlQuery.setString("name", river); + sqlQuery.setString("descr", description); + List results = sqlQuery.list(); + SedimentLoad load = new SedimentLoad(); + if (results.isEmpty()) { + return new SedimentLoad(); + } + Object[] row = results.get(0); + load = new SedimentLoad( + (String) row[0], + (Date) row[1], + (Date) row[2], + false, + (String)row[5]); + + for (int i = 0; i < results.size(); i++) { + row = results.get(i); + SedimentLoadFraction fraction = new SedimentLoadFraction(); + fraction.setUnknown((Double)row[3]); + load.addKm((Double)row[4], fraction); + } + return load; + } + + public static SedimentLoad[] getSedimentLoadUnknown(String river) { + Session session = SessionHolder.HOLDER.get(); + SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_UNKNOWN) + .addScalar("description", StandardBasicTypes.STRING) + .addScalar("start", StandardBasicTypes.DATE) + .addScalar("end", StandardBasicTypes.DATE); + sqlQuery.setString("river", river); + List results = sqlQuery.list(); + SedimentLoad[] loads = new SedimentLoad[results.size()]; + int counter = 0; + for (Object[] row: results) { + loads[counter] = new SedimentLoad( + (String)row[0], (Date)row[1], (Date)row[2], false, ""); + counter++; + } + return loads; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : diff -r ec537afacac2 -r 9a0f8d532797 artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/SedimentLoadCalculate.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/SedimentLoadCalculate.java Fri Jun 21 15:33:06 2013 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/SedimentLoadCalculate.java Fri Jun 21 15:35:44 2013 +0200 @@ -9,10 +9,10 @@ package org.dive4elements.river.artifacts.states.minfo; import java.util.ArrayList; +import java.util.Date; import java.util.List; import org.apache.log4j.Logger; - import org.dive4elements.artifactdatabase.state.Facet; import org.dive4elements.artifactdatabase.state.FacetActivity; import org.dive4elements.artifacts.Artifact; @@ -21,14 +21,17 @@ import org.dive4elements.river.artifacts.D4EArtifact; import org.dive4elements.river.artifacts.access.SedimentLoadAccess; import org.dive4elements.river.artifacts.model.CalculationResult; -import org.dive4elements.river.artifacts.model.DataFacet; import org.dive4elements.river.artifacts.model.FacetTypes; import org.dive4elements.river.artifacts.model.ReportFacet; +import org.dive4elements.river.artifacts.model.minfo.SedimentLoad; import org.dive4elements.river.artifacts.model.minfo.SedimentLoadCalculation; import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFacet; +import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFactory; import org.dive4elements.river.artifacts.model.minfo.SedimentLoadResult; +import org.dive4elements.river.artifacts.model.minfo.SedimentLoadUnknownFacet; import org.dive4elements.river.artifacts.resources.Resources; import org.dive4elements.river.artifacts.states.DefaultState; +import org.dive4elements.river.utils.DateGuesser; public class SedimentLoadCalculate @@ -70,6 +73,42 @@ name.equals(SEDIMENT_LOAD_SUSP_SAND_BED)){ return Boolean.FALSE; } + else if (name.equals(SEDIMENT_LOAD_UNKOWN)) { + D4EArtifact d4e = (D4EArtifact)artifact; + SedimentLoadUnknownFacet f = + (SedimentLoadUnknownFacet) + d4e.getNativeFacet(facet, null); + SedimentLoad load = + (SedimentLoad)f.getData(artifact, null); + SedimentLoadAccess access = + new SedimentLoadAccess(d4e); + List dates = new ArrayList(); + if (access.getYearEpoch().equals("year")) { + dates.add(access.getPeriod()); + } + else { + int[][] epochs = access.getEpochs(); + for (int i = 0; i < epochs.length; i++) { + dates.add(epochs[i]); + } + } + for (int[] date: dates) { + try { + Date s = + DateGuesser.guessDate(String.valueOf(date[0])); + Date e = + DateGuesser.guessDate(String.valueOf(date[1])); + if (!(s.after(load.getEnd()) || + e.before(load.getStart()))) { + return Boolean.TRUE; + } + } + catch (IllegalArgumentException iae) { + return Boolean.FALSE; + } + } + return Boolean.FALSE; + } else { return null; } @@ -100,6 +139,10 @@ return res; } + String river = access.getRiver(); + SedimentLoad[] unknown = + SedimentLoadFactory.getSedimentLoadUnknown(river); + String type = access.getYearEpoch(); if (type.equals("year")) { generateYearFacets(context, newFacets, results, getID(), hash); @@ -114,6 +157,16 @@ if (res.getReport().hasProblems()) { newFacets.add(new ReportFacet(ComputeType.ADVANCE, hash, id)); } + + for (int i = 0; i < unknown.length; i++) { + newFacets.add(new SedimentLoadUnknownFacet( + i, + SEDIMENT_LOAD_UNKOWN, + unknown[i].getDescription(), + ComputeType.ADVANCE, + getID(), + hash)); + } facets.addAll(newFacets); return res;