sascha@4090: package de.intevation.aft; sascha@4090: sascha@4091: import java.util.List; sascha@4097: import java.util.Date; sascha@4091: import java.util.ArrayList; sascha@4091: sascha@4091: import java.sql.SQLException; sascha@4091: import java.sql.ResultSet; sascha@4102: import java.sql.Types; sascha@4091: sascha@4091: import de.intevation.db.SymbolicStatement; sascha@4092: import de.intevation.db.ConnectedStatements; sascha@4091: sascha@4101: import org.apache.log4j.Logger; sascha@4101: sascha@4090: public class DischargeTable sascha@4090: { sascha@4101: private static Logger log = Logger.getLogger(DischargeTable.class); sascha@4101: sascha@4090: protected int id; sascha@4090: protected int gaugeId; sascha@4090: protected TimeInterval timeInterval; sascha@4090: protected String description; sascha@4091: protected List values; sascha@4090: sascha@4090: public DischargeTable() { sascha@4090: } sascha@4090: sascha@4090: public DischargeTable( sascha@4090: int gaugeId, sascha@4090: TimeInterval timeInterval, sascha@4090: String description sascha@4090: ) { sascha@4090: this.gaugeId = gaugeId; sascha@4090: this.timeInterval = timeInterval; sascha@4090: this.description = description; sascha@4091: values = new ArrayList(); sascha@4090: } sascha@4090: sascha@4090: public DischargeTable( sascha@4090: int id, sascha@4090: int gaugeId, sascha@4090: TimeInterval timeInterval, sascha@4090: String description sascha@4090: ) { sascha@4090: this(gaugeId, timeInterval, description); sascha@4090: this.id = id; sascha@4090: } sascha@4090: sascha@4090: public int getId() { sascha@4090: return id; sascha@4090: } sascha@4090: sascha@4090: public void setId(int id) { sascha@4090: this.id = id; sascha@4090: } sascha@4090: sascha@4090: public int getGaugeId() { sascha@4090: return gaugeId; sascha@4090: } sascha@4090: sascha@4090: public void setGaugeId(int gaugeId) { sascha@4090: this.gaugeId = gaugeId; sascha@4090: } sascha@4090: sascha@4090: public TimeInterval getTimeInterval() { sascha@4090: return timeInterval; sascha@4090: } sascha@4090: sascha@4090: public void setTimeInterval(TimeInterval timeInterval) { sascha@4090: this.timeInterval = timeInterval; sascha@4090: } sascha@4090: sascha@4090: public String getDescription() { sascha@4090: return description; sascha@4090: } sascha@4090: sascha@4090: public void setDescription(String description) { sascha@4090: this.description = description; sascha@4090: } sascha@4091: sascha@4093: public void clearValues() { sascha@4093: values.clear(); sascha@4093: } sascha@4093: sascha@4099: public List getValues() { sascha@4099: return values; sascha@4099: } sascha@4099: sascha@4099: public void setValues(List values) { sascha@4099: this.values = values; sascha@4099: } sascha@4099: sascha@4099: sascha@4099: sascha@4091: protected void loadValues(SymbolicStatement.Instance query) sascha@4091: throws SQLException sascha@4091: { sascha@4091: ResultSet rs = query.executeQuery(); sascha@4091: while (rs.next()) { sascha@4091: int id = rs.getInt("id"); sascha@4091: double w = rs.getDouble("w"); sascha@4091: double q = rs.getDouble("q"); sascha@4091: values.add(new WQ(id, w, q)); sascha@4091: } sascha@4091: rs.close(); sascha@4091: } sascha@4091: sascha@4091: public void loadAftValues(SyncContext context) throws SQLException { sascha@4091: loadValues(context.getAftStatements() sascha@4091: .getStatement("select.tafelwert") sascha@4091: .clearParameters() sascha@4091: .setInt("number", getId())); sascha@4091: } sascha@4091: sascha@4091: public void loadFlysValues(SyncContext context) throws SQLException { sascha@4091: loadValues(context.getFlysStatements() sascha@4091: .getStatement("select.discharge.table.values") sascha@4091: .clearParameters() sascha@4091: .setInt("table_id", getId())); sascha@4091: } sascha@4092: sascha@4092: public void storeFlysValues( sascha@4092: SyncContext context, sascha@4092: int dischargeTableId sascha@4092: ) sascha@4092: throws SQLException sascha@4092: { sascha@4092: ConnectedStatements flysStatements = context.getFlysStatements(); sascha@4092: sascha@4092: // Create the ids. sascha@4092: SymbolicStatement.Instance nextId = flysStatements sascha@4092: .getStatement("next.discharge.table.values.id"); sascha@4092: sascha@4092: int [] ids = new int[values.size()]; sascha@4092: for (int i = 0; i < ids.length; ++i) { sascha@4092: ResultSet rs = nextId.executeQuery(); sascha@4092: rs.next(); sascha@4092: ids[i] = rs.getInt("discharge_table_values_id"); sascha@4092: rs.close(); sascha@4092: } sascha@4092: sascha@4092: // Insert the values. sascha@4092: SymbolicStatement.Instance insertDTV = flysStatements sascha@4092: .getStatement("insert.discharge.table.value"); sascha@4092: sascha@4092: for (int i = 0; i < ids.length; ++i) { sascha@4092: WQ wq = values.get(i); sascha@4092: insertDTV sascha@4092: .clearParameters() sascha@4092: .setInt("id", ids[i]) sascha@4092: .setInt("table_id", dischargeTableId) sascha@4092: .setDouble("w", wq.getW()) sascha@4092: .setDouble("q", wq.getQ()) sascha@4092: .execute(); sascha@4092: } sascha@4092: } sascha@4097: sascha@4097: public static List loadFlysDischargeTables( sascha@4097: SyncContext context, sascha@4097: int gaugeId sascha@4097: ) sascha@4097: throws SQLException sascha@4097: { sascha@4097: List dts = new ArrayList(); sascha@4097: sascha@4097: ResultSet rs = context sascha@4097: .getFlysStatements() sascha@4097: .getStatement("select.gauge.discharge.tables") sascha@4097: .clearParameters() sascha@4097: .setInt("gauge_id", gaugeId) sascha@4097: .executeQuery(); sascha@4097: sascha@4097: while (rs.next()) { sascha@4097: int id = rs.getInt("id"); sascha@4097: String description = rs.getString("description"); sascha@4097: Date startTime = rs.getDate("start_time"); sascha@4097: Date stopTime = rs.getDate("stop_time"); sascha@4097: TimeInterval ti = startTime == null sascha@4097: ? null sascha@4097: : new TimeInterval(startTime, stopTime); sascha@4097: DischargeTable dt = new DischargeTable( sascha@4097: id, gaugeId, ti, description); sascha@4097: dts.add(dt); sascha@4097: } sascha@4097: rs.close(); sascha@4097: sascha@4097: return dts; sascha@4097: } sascha@4097: sascha@4097: public static List loadAftDischargeTables( sascha@4097: SyncContext context, sascha@4097: Long officialNumber sascha@4097: ) sascha@4097: throws SQLException sascha@4097: { sascha@4097: return loadAftDischargeTables(context, officialNumber, 0); sascha@4097: } sascha@4097: sascha@4097: public static List loadAftDischargeTables( sascha@4097: SyncContext context, sascha@4097: Long officialNumber, sascha@4097: int flysGaugeId sascha@4097: ) sascha@4097: throws SQLException sascha@4097: { sascha@4097: List dts = new ArrayList(); sascha@4097: sascha@4097: ResultSet rs = context sascha@4097: .getAftStatements() sascha@4097: .getStatement("select.abflusstafel") sascha@4097: .clearParameters() sascha@4097: .setString("number", "%" + officialNumber) sascha@4097: .executeQuery(); sascha@4097: sascha@4097: while (rs.next()) { sascha@4101: int dtId = rs.getInt("ABFLUSSTAFEL_NR"); sascha@4101: Date from = rs.getDate("GUELTIG_VON"); sascha@4101: Date to = rs.getDate("GUELTIG_BIS"); sascha@4101: sascha@4101: if (from != null && to != null && from.compareTo(to) > 0) { sascha@4101: log.warn("AFT: ABFLUSSTAFEL_NR = " sascha@4101: + dtId + ": " + from + " > " + to + ". -> swap"); sascha@4101: Date temp = from; sascha@4101: from = to; sascha@4101: to = temp; sascha@4101: } sascha@4101: sascha@4097: String description = rs.getString("ABFLUSSTAFEL_BEZ"); sascha@4097: if (description == null) { sascha@4097: description = String.valueOf(officialNumber); sascha@4097: } sascha@4097: sascha@4097: double datumValue = rs.getDouble("PEGELNULLPUNKT"); sascha@4097: Double datum = rs.wasNull() ? null : datumValue; sascha@4097: sascha@4097: TimeInterval timeInterval = from == null sascha@4097: ? null sascha@4097: : new TimeInterval(from, to); sascha@4097: sascha@4097: DischargeTable dt = new DischargeTable( sascha@4097: dtId, sascha@4097: flysGaugeId, sascha@4097: timeInterval, sascha@4097: description); sascha@4097: dts.add(dt); sascha@4097: } sascha@4097: rs.close(); sascha@4097: sascha@4097: return dts; sascha@4097: } sascha@4102: sascha@4102: public void persistFlysTimeInterval( sascha@4102: SyncContext context sascha@4102: ) sascha@4102: throws SQLException sascha@4102: { sascha@4102: if (timeInterval != null) { sascha@4102: timeInterval = context.fetchOrCreateFLYSTimeInterval( sascha@4102: timeInterval); sascha@4102: } sascha@4102: } sascha@4102: sascha@4102: public int persistFlysDischargeTable( sascha@4102: SyncContext context, sascha@4102: int gaugeId sascha@4102: ) sascha@4102: throws SQLException sascha@4102: { sascha@4102: ConnectedStatements flysStatements = sascha@4102: context.getFlysStatements(); sascha@4102: sascha@4102: ResultSet rs = flysStatements sascha@4102: .getStatement("next.discharge.id") sascha@4102: .executeQuery(); sascha@4102: sascha@4102: rs.next(); sascha@4102: int flysId = rs.getInt("discharge_table_id"); sascha@4102: rs.close(); sascha@4102: sascha@4102: SymbolicStatement.Instance insertDT = flysStatements sascha@4102: .getStatement("insert.dischargetable") sascha@4102: .clearParameters() sascha@4102: .setInt("id", flysId) sascha@4102: .setInt("gauge_id", gaugeId) sascha@4102: .setString("description", description); sascha@4102: sascha@4102: if (timeInterval != null) { sascha@4102: insertDT.setInt("time_interval_id", timeInterval.getId()); sascha@4102: } sascha@4102: else { sascha@4102: insertDT.setNull("time_interval_id", Types.INTEGER); sascha@4102: } sascha@4102: sascha@4102: insertDT.execute(); sascha@4102: sascha@4102: if (log.isDebugEnabled()) { sascha@4102: log.debug("FLYS: Created discharge table id: " + id); sascha@4102: } sascha@4102: sascha@4102: return flysId; sascha@4102: } sascha@4090: } sascha@4090: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :