sascha@4096: package de.intevation.aft;
sascha@4096: 
sascha@4096: import java.util.Collection;
sascha@4096: import java.util.Set;
sascha@4096: import java.util.TreeSet;
sascha@4096: import java.util.Iterator;
sascha@4096: 
sascha@4096: import java.sql.ResultSet;
sascha@4096: import java.sql.SQLException;
sascha@4096: 
sascha@4096: import de.intevation.db.ConnectedStatements;
sascha@4096: import de.intevation.db.SymbolicStatement;
sascha@4096: 
sascha@4096: public class WQDiff
sascha@4096: {
sascha@4096:     protected Set<WQ> toAdd;
sascha@4096:     protected Set<WQ> toDelete;
sascha@4096: 
sascha@4096:     public WQDiff() {
sascha@4096:     }
sascha@4096: 
sascha@4096:     public WQDiff(Collection<WQ> a, Collection<WQ> b) {
sascha@4096:         toAdd    = new TreeSet<WQ>(WQ.EPS_CMP);
sascha@4096:         toDelete = new TreeSet<WQ>(WQ.EPS_CMP);
sascha@4096:         build(a, b);
sascha@4096:     }
sascha@4096: 
sascha@4096:     public void build(Collection<WQ> a, Collection<WQ> b) {
sascha@4096:         toAdd.addAll(b);
sascha@4096:         toAdd.removeAll(a);
sascha@4096: 
sascha@4096:         toDelete.addAll(a);
sascha@4096:         toDelete.removeAll(b);
sascha@4096:     }
sascha@4096: 
sascha@4096:     public void clear() {
sascha@4096:         toAdd.clear();
sascha@4096:         toDelete.clear();
sascha@4096:     }
sascha@4096: 
sascha@4096:     public Set<WQ> getToAdd() {
sascha@4096:         return toAdd;
sascha@4096:     }
sascha@4096: 
sascha@4096:     public void setToAdd(Set<WQ> toAdd) {
sascha@4096:         this.toAdd = toAdd;
sascha@4096:     }
sascha@4096: 
sascha@4096:     public Set<WQ> getToDelete() {
sascha@4096:         return toDelete;
sascha@4096:     }
sascha@4096: 
sascha@4096:     public void setToDelete(Set<WQ> toDelete) {
sascha@4096:         this.toDelete = toDelete;
sascha@4096:     }
sascha@4096: 
sascha@4096:     public boolean hasChanges() {
sascha@4096:         return !(toAdd.isEmpty() && toDelete.isEmpty());
sascha@4096:     }
sascha@4096: 
sascha@4096:     public void writeChanges(
sascha@4096:         SyncContext context, 
sascha@4096:         int         tableId
sascha@4096:     )
sascha@4096:     throws SQLException
sascha@4096:     {
sascha@4096:         ConnectedStatements flysStatements = context.getFlysStatements();
sascha@4096: 
sascha@4096:         // Delete the old entries
sascha@4096:         if (!toDelete.isEmpty()) {
sascha@4096:             SymbolicStatement.Instance deleteDTV =
sascha@4096:                 flysStatements.getStatement("delete.discharge.table.value");
sascha@4096:             for (WQ wq: toDelete) {
sascha@4096:                 deleteDTV
sascha@4096:                     .clearParameters()
sascha@4096:                     .setInt("id", wq.getId())
sascha@4096:                     .execute();
sascha@4096:             }
sascha@4096:         }
sascha@4096: 
sascha@4096:         // Add the new entries.
sascha@4096:         if (!toAdd.isEmpty()) {
sascha@4096:             SymbolicStatement.Instance nextId =
sascha@4096:                 flysStatements.getStatement("next.discharge.table.values.id");
sascha@4096: 
sascha@4096:             SymbolicStatement.Instance insertDTV =
sascha@4096:                 flysStatements.getStatement("insert.discharge.table.value");
sascha@4096: 
sascha@4096:             // Recycle old ids as much as possible.
sascha@4096:             Iterator<WQ> oldIds = toDelete.iterator();
sascha@4096: 
sascha@4096:             // Create ids for new entries.
sascha@4096:             for (WQ wq: toAdd) {
sascha@4096:                 if (oldIds.hasNext()) {
sascha@4096:                     wq.setId(oldIds.next().getId());
sascha@4096:                 }
sascha@4096:                 else {
sascha@4096:                     ResultSet rs = nextId.executeQuery();
sascha@4096:                     rs.next();
sascha@4096:                     wq.setId(rs.getInt("discharge_table_values_id"));
sascha@4096:                     rs.close();
sascha@4096:                 }
sascha@4096:             }
sascha@4096: 
sascha@4096:             // Write the new entries.
sascha@4096:             for (WQ wq: toAdd) {
sascha@4096:                 insertDTV
sascha@4096:                     .clearParameters()
sascha@4096:                     .setInt("id", wq.getId())
sascha@4096:                     .setInt("table_id", tableId)
sascha@4096:                     .setDouble("w", wq.getW())
sascha@4096:                     .setDouble("q", wq.getQ())
sascha@4096:                     .execute();
sascha@4096:             }
sascha@4096:         }
sascha@4096:     }
sascha@4096: }
sascha@4096: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :