comparison flys-aft/src/main/java/de/intevation/aft/WQDiff.java @ 4096:82f5266f881b

Add code to build the difference of the W/Q values of two discharge tables. flys-aft/trunk@3617 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 06 Jan 2012 17:16:46 +0000
parents
children b195fede1c3b
comparison
equal deleted inserted replaced
4095:da9df3641578 4096:82f5266f881b
1 package de.intevation.aft;
2
3 import java.util.Collection;
4 import java.util.Set;
5 import java.util.TreeSet;
6 import java.util.Iterator;
7
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10
11 import de.intevation.db.ConnectedStatements;
12 import de.intevation.db.SymbolicStatement;
13
14 public class WQDiff
15 {
16 protected Set<WQ> toAdd;
17 protected Set<WQ> toDelete;
18
19 public WQDiff() {
20 }
21
22 public WQDiff(Collection<WQ> a, Collection<WQ> b) {
23 toAdd = new TreeSet<WQ>(WQ.EPS_CMP);
24 toDelete = new TreeSet<WQ>(WQ.EPS_CMP);
25 build(a, b);
26 }
27
28 public void build(Collection<WQ> a, Collection<WQ> b) {
29 toAdd.addAll(b);
30 toAdd.removeAll(a);
31
32 toDelete.addAll(a);
33 toDelete.removeAll(b);
34 }
35
36 public void clear() {
37 toAdd.clear();
38 toDelete.clear();
39 }
40
41 public Set<WQ> getToAdd() {
42 return toAdd;
43 }
44
45 public void setToAdd(Set<WQ> toAdd) {
46 this.toAdd = toAdd;
47 }
48
49 public Set<WQ> getToDelete() {
50 return toDelete;
51 }
52
53 public void setToDelete(Set<WQ> toDelete) {
54 this.toDelete = toDelete;
55 }
56
57 public boolean hasChanges() {
58 return !(toAdd.isEmpty() && toDelete.isEmpty());
59 }
60
61 public void writeChanges(
62 SyncContext context,
63 int tableId
64 )
65 throws SQLException
66 {
67 ConnectedStatements flysStatements = context.getFlysStatements();
68
69 // Delete the old entries
70 if (!toDelete.isEmpty()) {
71 SymbolicStatement.Instance deleteDTV =
72 flysStatements.getStatement("delete.discharge.table.value");
73 for (WQ wq: toDelete) {
74 deleteDTV
75 .clearParameters()
76 .setInt("id", wq.getId())
77 .execute();
78 }
79 }
80
81 // Add the new entries.
82 if (!toAdd.isEmpty()) {
83 SymbolicStatement.Instance nextId =
84 flysStatements.getStatement("next.discharge.table.values.id");
85
86 SymbolicStatement.Instance insertDTV =
87 flysStatements.getStatement("insert.discharge.table.value");
88
89 // Recycle old ids as much as possible.
90 Iterator<WQ> oldIds = toDelete.iterator();
91
92 // Create ids for new entries.
93 for (WQ wq: toAdd) {
94 if (oldIds.hasNext()) {
95 wq.setId(oldIds.next().getId());
96 }
97 else {
98 ResultSet rs = nextId.executeQuery();
99 rs.next();
100 wq.setId(rs.getInt("discharge_table_values_id"));
101 rs.close();
102 }
103 }
104
105 // Write the new entries.
106 for (WQ wq: toAdd) {
107 insertDTV
108 .clearParameters()
109 .setInt("id", wq.getId())
110 .setInt("table_id", tableId)
111 .setDouble("w", wq.getW())
112 .setDouble("q", wq.getQ())
113 .execute();
114 }
115 }
116 }
117 }
118 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org