sascha@3142: package de.intevation.flys.artifacts.model; sascha@3142: sascha@3142: import java.io.Serializable; sascha@3142: sascha@3142: import java.util.List; sascha@3142: sascha@3142: import org.apache.log4j.Logger; sascha@3142: sascha@3142: import org.hibernate.SQLQuery; sascha@3142: import org.hibernate.Session; sascha@3142: sascha@3142: import org.hibernate.type.StandardBasicTypes; sascha@3142: sascha@3142: public class GaugeFinder sascha@3142: implements Serializable sascha@3142: { sascha@3142: private static Logger log = Logger.getLogger(GaugeFinder.class); sascha@3142: sascha@3142: public static final String SQL_DISCHARGE_SECTORS = sascha@3142: "SELECT" + sascha@3142: " g.id AS gauge_id," + sascha@3142: " nmv.name AS name," + sascha@3142: " CAST(mv.value AS NUMERIC(38,2)) AS value " + sascha@3142: "FROM gauges g" + sascha@3142: " JOIN main_values mv ON g.id = mv.gauge_id" + sascha@3142: " JOIN named_main_values nmv ON nmv.id = mv.named_value_id" + sascha@3142: " JOIN main_value_types mvt ON nmv.type_id = mvt.id " + sascha@3142: "WHERE" + sascha@3142: " mvt.name = 'Q' AND (" + sascha@3142: " nmv.name = 'MNQ' OR" + sascha@3142: " nmv.name LIKE 'MNQ(%' OR" + sascha@3142: " nmv.name = 'MQ' OR" + sascha@3142: " nmv.name LIKE 'MQ(%' OR" + sascha@3142: " nmv.name = 'MHQ' OR" + sascha@3142: " nmv.name LIKE 'MHQ(%' OR" + sascha@3142: " nmv.name = 'HQ5' OR" + sascha@3142: " nmv.name LIKE 'HQ5(%') AND" + sascha@3142: " g.river_id = :river_id " + sascha@3142: "ORDER BY" + sascha@3142: " g.id"; sascha@3142: sascha@3142: protected List gauges; sascha@3142: protected boolean isKmUp; sascha@3142: sascha@3142: public GaugeFinder(List gauges) { sascha@3142: this(gauges, true); sascha@3142: } sascha@3142: sascha@3142: public GaugeFinder( sascha@3142: List gauges, sascha@3142: boolean isKmUp sascha@3142: ) { sascha@3142: this.gauges = gauges; sascha@3142: this.isKmUp = isKmUp; sascha@3142: } sascha@3142: sascha@3142: public boolean getIsKmUp() { sascha@3142: return isKmUp; sascha@3142: } sascha@3142: sascha@3142: public void setIsKmUp(boolean isKmUp) { sascha@3142: this.isKmUp = isKmUp; sascha@3142: } sascha@3142: sascha@3143: public GaugeRange find(double km) { sascha@3142: for (GaugeRange gauge: gauges) { sascha@3142: if (gauge.inside(km)) { sascha@3142: return gauge; sascha@3142: } sascha@3142: } sascha@3142: return null; sascha@3142: } sascha@3142: sascha@3143: public GaugeRange find(Range range) { sascha@3143: return find(isKmUp ? range.start : range.end); sascha@3143: } sascha@3143: sascha@3142: public GaugeRange find(int gaugeId) { sascha@3142: for (GaugeRange gauge: gauges) { sascha@3142: if (gauge.gaugeId == gaugeId) { sascha@3142: return gauge; sascha@3142: } sascha@3142: } sascha@3142: return null; sascha@3142: } sascha@3142: sascha@3401: public List getGauges() { sascha@3401: return gauges; sascha@3401: } sascha@3401: sascha@3142: public boolean loadDischargeSectors(Session session, int riverId) { sascha@3142: sascha@3142: SQLQuery query = session.createSQLQuery(SQL_DISCHARGE_SECTORS) sascha@3142: .addScalar("gauge_id", StandardBasicTypes.INTEGER) sascha@3142: .addScalar("name", StandardBasicTypes.STRING) sascha@3142: .addScalar("value", StandardBasicTypes.DOUBLE); sascha@3142: sascha@3142: query.setInteger("river_id", riverId); sascha@3142: sascha@3142: List list = query.list(); sascha@3142: sascha@3142: if (list.isEmpty()) { sascha@3142: log.warn("River " + riverId + " has no discharge sectors."); sascha@3142: return false; sascha@3142: } sascha@3142: sascha@3142: GaugeRange gauge = null; sascha@3142: sascha@3142: for (Object [] row: list) { sascha@3142: int gaugeId = (Integer)row[0]; sascha@3142: String label = (String) row[1]; sascha@3142: Double value = (Double) row[2]; sascha@3142: sascha@3142: if (gauge == null || gauge.gaugeId != gaugeId) { sascha@3142: if ((gauge = find(gaugeId)) == null) { sascha@3142: log.warn("Cannot find gauge for id " + gaugeId + "."); sascha@3142: continue; sascha@3142: } sascha@3142: } sascha@3142: sascha@3142: gauge.addMainValue(label, value); sascha@3142: } sascha@3142: sascha@3142: for (GaugeRange g: gauges) { sascha@3142: g.buildClasses(); sascha@3142: } sascha@3142: sascha@3142: return true; sascha@3142: } sascha@3142: } sascha@3142: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :