ingo@2191: package de.intevation.flys.artifacts.states;
ingo@2191: 
ingo@2191: import java.util.Calendar;
ingo@2191: import java.util.List;
ingo@2191: 
ingo@2191: import org.apache.log4j.Logger;
christian@3745: import org.hibernate.SQLQuery;
ingo@2191: import org.hibernate.Session;
ingo@2191: import org.hibernate.type.StandardBasicTypes;
ingo@2191: 
christian@3745: import de.intevation.flys.artifacts.FLYSArtifact;
ingo@2191: import de.intevation.flys.backend.SessionHolder;
christian@3745: import de.intevation.flys.model.Gauge;
ingo@2191: import de.intevation.flys.utils.FLYSUtils;
ingo@2191: 
ingo@2191: 
ingo@2191: /**
ingo@2191:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
ingo@2191:  */
ingo@2191: public class GaugeTimerangeState extends IntRangeState {
ingo@2191: 
ingo@2191:     private static final Logger logger =
christian@3745:             Logger.getLogger(GaugeTimerangeState.class);
ingo@2191: 
ingo@2191: 
ingo@2191:     protected int[] getLowerUpper(FLYSArtifact flys) {
ingo@2191:         Gauge gauge = FLYSUtils.getReferenceGauge(flys);
ingo@2191: 
ingo@2191:         if (gauge == null) {
ingo@2191:             logger.warn("No reference gauge specified!");
ingo@2191:             return new int[] { 0, 0 };
ingo@2191:         }
ingo@2191: 
ingo@2191:         Session session = SessionHolder.HOLDER.get();
ingo@2191: 
ingo@2191:         SQLQuery query = session.createSQLQuery(
christian@3745:                 "SELECT min(start_time) as min, max(stop_time) as max " +
christian@3745:                         "FROM time_intervals WHERE id in " +
christian@3745:                         "(SELECT time_interval_id FROM discharge_tables " +
christian@3745:                 "WHERE gauge_id =:gid)");
ingo@2191: 
ingo@2191:         query.addScalar("min", StandardBasicTypes.CALENDAR);
ingo@2191:         query.addScalar("max", StandardBasicTypes.CALENDAR);
ingo@2191: 
ingo@2191:         query.setInteger("gid", gauge.getId());
ingo@2191: 
christian@3745:         List<?> results = query.list();
ingo@2191: 
ingo@2191:         if (results != null) {
ingo@2191:             Object[] res = (Object[]) results.get(0);
ingo@2191: 
ingo@2191:             Calendar lo = (Calendar) res[0];
ingo@2191:             Calendar up = (Calendar) res[1];
ingo@2191: 
christian@3745:             if (lo != null && up != null) {
christian@3745:                 return new int[] { lo.get(Calendar.YEAR), up.get(Calendar.YEAR) };
christian@3745:             }
ingo@2191:         }
ingo@2191: 
ingo@2191:         logger.warn("Could not determine time range for gauge: " + gauge);
ingo@2191: 
ingo@2191:         return null;
ingo@2191:     }
ingo@2191: 
ingo@2191: 
ingo@2191:     @Override
ingo@2191:     protected Object getLower(FLYSArtifact flys) {
ingo@2191:         int[] lowerUpper = getLowerUpper(flys);
ingo@2191: 
ingo@2191:         return lowerUpper != null ? lowerUpper[0] : 0;
ingo@2191:     }
ingo@2191: 
ingo@2191: 
ingo@2191:     @Override
ingo@2191:     protected Object getUpper(FLYSArtifact flys) {
ingo@2191:         int[] lowerUpper = getLowerUpper(flys);
ingo@2191: 
ingo@2191:         return lowerUpper != null ? lowerUpper[1] : 0;
ingo@2191:     }
raimund@2276: 
raimund@2276: 
raimund@2276:     @Override
raimund@2276:     protected String getUIProvider() {
raimund@2276:         return "gaugetimerange";
raimund@2276:     }
ingo@2191: }
ingo@2191: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :