annotate flys-aft/src/main/java/de/intevation/aft/River.java @ 4081:7a7b0f0cb653

Check if AFT gauges are in DIPS, too. flys-aft/trunk@3456 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 16 Dec 2011 19:25:31 +0000
parents 42094f01afa6
children d13011e53022
rev   line source
4075
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.aft;
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
4078
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
3 import java.util.Map;
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
4 import java.util.HashMap;
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
5
4076
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
6 import java.sql.ResultSet;
4075
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import java.sql.SQLException;
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import org.apache.log4j.Logger;
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
4078
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
11 import org.w3c.dom.Document;
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
12 import org.w3c.dom.NodeList;
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
13 import org.w3c.dom.Element;
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
14
4075
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 import de.intevation.db.ConnectedStatements;
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
4079
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
17 import java.util.regex.Matcher;
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
18 import java.util.regex.Pattern;
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
19
4075
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 public class River
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 extends IdPair
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 {
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 private static Logger log = Logger.getLogger(River.class);
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
4079
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
25 public static final Pattern NUMBER = Pattern.compile("0*([0-9]+)");
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
26
4075
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 protected String name;
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 public River() {
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 }
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 public River(int id1, int id2, String name) {
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 super(id1, id2);
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 this.name = name;
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 }
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 public String getName() {
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 return name;
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 }
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40
4079
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
41 public static Long numberToLong(String s) {
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
42 Matcher m = NUMBER.matcher(s);
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
43 if (!m.matches()) return null;
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
44 String ns = m.group(1);
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
45 return Long.valueOf(ns.length() == 0 ? "0": ns);
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
46 }
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
47
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
48 public static Map<Long, DIPSGauge> indexByNumber(Document document) {
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
49 Map<Long, DIPSGauge> map = new HashMap<Long, DIPSGauge>();
4078
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
50 NodeList nodes = document.getElementsByTagName("PEGELSTATION");
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
51 for (int i = nodes.getLength()-1; i >= 0; --i) {
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
52 Element element = (Element)nodes.item(i);
4079
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
53 Long number = numberToLong(element.getAttribute("NUMMER"));
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
54 if (number != null) {
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
55 map.put(number, new DIPSGauge(element));
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
56 }
4078
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
57 }
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
58 return map;
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
59 }
0944cdd2ee90 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
60
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4076
diff changeset
61 public void sync(SyncContext context) throws SQLException {
4075
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 log.info("sync river: " + this);
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4076
diff changeset
63
4081
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
64 Map<Long, DIPSGauge> dipsGauges = indexByNumber(context.getDips());
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
65
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4076
diff changeset
66 ConnectedStatements flysStatements = context.getFlysStatements();
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4076
diff changeset
67 ConnectedStatements aftStatements = context.getAftStatements();
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4076
diff changeset
68
4076
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
69 ResultSet messstellenRs = aftStatements
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
70 .getStatement("select.messstelle")
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
71 .clearParameters()
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
72 .setInt("GEWAESSER_NR", id2).executeQuery();
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
73
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
74 while (messstellenRs.next()) {
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
75 String name = messstellenRs.getString("NAME");
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
76 String num = messstellenRs.getString("MESSSTELLE_NR");
4081
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
77 Long number = numberToLong(num);
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
78 if (number == null) {
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
79 log.warn("Invalid MESSSTELLE_NR for MESSSTELLE '"+name+"'");
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
80 continue;
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
81 }
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
82 DIPSGauge dipsGauge = dipsGauges.get(number);
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
83 if (dipsGauges == null) {
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
84 log.warn("MESSSTELLE '" + name + "' not found in DIPS.");
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
85 continue;
7a7b0f0cb653 Check if AFT gauges are in DIPS, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4079
diff changeset
86 }
4076
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
87 }
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
88
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
89 messstellenRs.close();
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
90
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
91 ResultSet gaugesRs = flysStatements
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
92 .getStatement("select.gauges")
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
93 .clearParameters()
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
94 .setInt("river_id", id1).executeQuery();
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
95
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
96 while (gaugesRs.next()) {
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
97 int gaugeId = gaugesRs.getInt("id");
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
98 String name = gaugesRs.getString("name");
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
99 System.err.println(name + ": " + gaugeId);
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
100 }
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
101
039413d7e394 Load gauges from both dbs.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
102 gaugesRs.close();
4079
42094f01afa6 Added model for DIPS gauges.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4078
diff changeset
103
4075
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 }
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 public String toString() {
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 return "[River: name=" + name + ", " + super.toString() + "]";
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 }
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 }
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org