Mercurial > dive4elements > river
annotate flys-aft/src/main/java/de/intevation/aft/Sync.java @ 4094:b20b710aa86f
Bubble up modifications up to main().
flys-aft/trunk@3605 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 05 Jan 2012 18:58:49 +0000 |
parents | d556e29592f5 |
children | da9df3641578 |
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()"; |
4094
b20b710aa86f
Bubble up modifications up to main().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4090
diff
changeset
|
26 public static final String XPATH_NOTIFICATIONS = |
b20b710aa86f
Bubble up modifications up to main().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4090
diff
changeset
|
27 "/sync/notifications/notification"; |
4077
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
28 |
4072
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
29 public static final String CONFIG_FILE = |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
30 System.getProperty("config.file", "config.xml"); |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
31 |
4067
d5aed044ca0d
Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 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
|
33 |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
34 File configFile = new File(CONFIG_FILE); |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
35 |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
36 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
|
37 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
|
38 System.exit(1); |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
39 } |
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 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
|
42 |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
43 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
|
44 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
|
45 System.exit(1); |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
46 } |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
47 |
4077
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
48 String dipsF = (String)XML.xpath( |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
49 config, XPATH_DIPS, XPathConstants.STRING, null, null); |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
50 |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
51 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
|
52 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
|
53 System.exit(1); |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
54 } |
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 File dipsFile = new File(dipsF); |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
57 |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
58 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
|
59 log.error("Cannot find '" + dipsF + "'"); |
4077
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
60 System.exit(1); |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
61 } |
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 Document dips = XML.parseDocument(dipsFile, Boolean.FALSE); |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
64 |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
65 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
|
66 log.error("Cannot load DIPS document."); |
4077
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
67 System.exit(1); |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
68 } |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
69 |
4083
44dc38ca8492
Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4077
diff
changeset
|
70 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
|
71 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
|
72 |
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 (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
|
74 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
|
75 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
|
76 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
|
77 } |
44dc38ca8492
Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4077
diff
changeset
|
78 else { |
44dc38ca8492
Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4077
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 } |
44dc38ca8492
Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4077
diff
changeset
|
83 else { |
44dc38ca8492
Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4077
diff
changeset
|
84 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
|
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 } |
4090
d556e29592f5
Create new discharge tables if needed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4089
diff
changeset
|
88 |
d556e29592f5
Create new discharge tables if needed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4089
diff
changeset
|
89 int exitCode = 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
|
90 |
4073
2c70fae83d0c
start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4072
diff
changeset
|
91 ConnectionBuilder aftConnectionBuilder = |
2c70fae83d0c
start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4072
diff
changeset
|
92 new ConnectionBuilder(AFT, config); |
2c70fae83d0c
start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4072
diff
changeset
|
93 |
4072
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
94 ConnectionBuilder flysConnectionBuilder = |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
95 new ConnectionBuilder(FLYS, config); |
88f801888d85
load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4067
diff
changeset
|
96 |
4077
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
97 SyncContext syncContext = null; |
4094
b20b710aa86f
Bubble up modifications up to main().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4090
diff
changeset
|
98 |
b20b710aa86f
Bubble up modifications up to main().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4090
diff
changeset
|
99 boolean modified = false; |
4073
2c70fae83d0c
start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4072
diff
changeset
|
100 try { |
4077
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
101 syncContext = new SyncContext( |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
102 aftConnectionBuilder.getConnectedStatements(), |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
103 flysConnectionBuilder.getConnectedStatements(), |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
104 dips); |
4089
859b4781554a
Prefetch existing time intervals from FLYS db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4083
diff
changeset
|
105 syncContext.init(); |
4075
dbd0b3b1b8b8
Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4073
diff
changeset
|
106 Rivers rivers = new Rivers(); |
4094
b20b710aa86f
Bubble up modifications up to main().
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4090
diff
changeset
|
107 modified = rivers.sync(syncContext); |
4073
2c70fae83d0c
start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4072
diff
changeset
|
108 } |
2c70fae83d0c
start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4072
diff
changeset
|
109 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
|
110 log.error("syncing failed", sqle); |
4090
d556e29592f5
Create new discharge tables if needed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4089
diff
changeset
|
111 exitCode = 1; |
4073
2c70fae83d0c
start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4072
diff
changeset
|
112 } |
2c70fae83d0c
start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4072
diff
changeset
|
113 finally { |
4077
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
114 if (syncContext != null) { |
97de7a552b79
Load DIPS XML
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
115 syncContext.close(); |
4075
dbd0b3b1b8b8
Sync each river.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4073
diff
changeset
|
116 } |
4073
2c70fae83d0c
start syncing at river level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4072
diff
changeset
|
117 } |
4090
d556e29592f5
Create new discharge tables if needed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4089
diff
changeset
|
118 |
d556e29592f5
Create new discharge tables if needed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4089
diff
changeset
|
119 if (exitCode != 0) { |
d556e29592f5
Create new discharge tables if needed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4089
diff
changeset
|
120 System.exit(1); |
d556e29592f5
Create new discharge tables if needed.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4089
diff
changeset
|
121 } |
4067
d5aed044ca0d
Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 } |
d5aed044ca0d
Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
123 } |
d5aed044ca0d
Added AFT sync module.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |