annotate flys-aft/src/main/java/de/intevation/aft/Sync.java @ 4088:52cde7fe742a

Fetch discharge table infos from AFT. flys-aft/trunk@3573 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 03 Jan 2012 11:36:07 +0000
parents 44dc38ca8492
children 859b4781554a
rev   line source
4067
d5aed044ca0d Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.aft;
d5aed044ca0d Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
3 import java.io.File;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
4
4073
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
5 import java.sql.SQLException;
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
6
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
7 import org.w3c.dom.Document;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
8
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
9 import org.apache.log4j.Logger;
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
10
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
11 import javax.xml.xpath.XPathConstants;
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
12
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
13 import de.intevation.utils.XML;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
14
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
15 import de.intevation.db.ConnectionBuilder;
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
16
4067
d5aed044ca0d Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 public class Sync
d5aed044ca0d Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 {
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
19 private static Logger log = Logger.getLogger(Sync.class);
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
20
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
21 public static final String FLYS = "flys";
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
22 public static final String AFT = "aft";
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
23
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
24 public static final String XPATH_DIPS = "/sync/dips/file/text()";
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
25 public static final String XPATH_REPAIR = "/sync/dips/repair/text()";
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
26
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
27 public static final String CONFIG_FILE =
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
28 System.getProperty("config.file", "config.xml");
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
29
4067
d5aed044ca0d Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 public static void main(String [] args) {
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
31
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
32 File configFile = new File(CONFIG_FILE);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
33
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
34 if (!configFile.isFile() || !configFile.canRead()) {
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
35 log.error("cannot read config file");
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
36 System.exit(1);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
37 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
38
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
39 Document config = XML.parseDocument(configFile, Boolean.FALSE);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
40
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
41 if (config == null) {
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
42 log.error("Cannot load config file.");
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
43 System.exit(1);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
44 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
45
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
46 String dipsF = (String)XML.xpath(
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
47 config, XPATH_DIPS, XPathConstants.STRING, null, null);
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
48
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
49 if (dipsF == null || dipsF.length() == 0) {
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
50 log.error("Cannot find path to DIPS XML in config.");
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
51 System.exit(1);
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
52 }
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
53
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
54 File dipsFile = new File(dipsF);
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
55
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
56 if (!dipsFile.isFile() || !dipsFile.canRead()) {
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
57 log.error("Cannot find '" + dipsF + "'");
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
58 System.exit(1);
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
59 }
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
60
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
61 Document dips = XML.parseDocument(dipsFile, Boolean.FALSE);
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
62
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
63 if (dips == null) {
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
64 log.error("Cannot load DIPS document.");
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
65 System.exit(1);
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
66 }
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
67
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
68 String repairF = (String)XML.xpath(
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
69 config, XPATH_REPAIR, XPathConstants.STRING, null, null);
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
70
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
71 if (repairF != null && repairF.length() > 0) {
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
72 File repairFile = new File(repairF);
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
73 if (!repairFile.isFile() || !repairFile.canRead()) {
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
74 log.warn("Cannot open DIPS repair XSLT file.");
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
75 }
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
76 else {
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
77 Document fixed = XML.transform(dips, repairFile);
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
78 if (fixed == null) {
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
79 log.warn("Fixing DIPS failed");
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
80 }
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
81 else {
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
82 dips = fixed;
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
83 }
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
84 }
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
85 }
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
86
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
87
4073
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
88 ConnectionBuilder aftConnectionBuilder =
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
89 new ConnectionBuilder(AFT, config);
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
90
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
91 ConnectionBuilder flysConnectionBuilder =
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
92 new ConnectionBuilder(FLYS, config);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4067
diff changeset
93
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
94 SyncContext syncContext = null;
4073
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
95 try {
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
96 syncContext = new SyncContext(
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
97 aftConnectionBuilder.getConnectedStatements(),
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
98 flysConnectionBuilder.getConnectedStatements(),
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
99 dips);
4075
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
100 Rivers rivers = new Rivers();
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
101 rivers.sync(syncContext);
4073
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
102 }
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
103 catch (SQLException sqle) {
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4077
diff changeset
104 log.error("syncing failed", sqle);
4073
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
105 }
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
106 finally {
4077
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
107 if (syncContext != null) {
97de7a552b79 Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4075
diff changeset
108 syncContext.close();
4075
dbd0b3b1b8b8 Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
109 }
4073
2c70fae83d0c start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
110 }
4067
d5aed044ca0d Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 }
d5aed044ca0d Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 }
d5aed044ca0d Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org