comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/Builder.java @ 1011:aca3b46160cb

Added support for more than one db connection in datacage templating. flys-artifacts/trunk@2457 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 02 Aug 2011 22:16:16 +0000
parents b81626b10cb7
children 9a1a3080ad98
comparison
equal deleted inserted replaced
1010:d419c9904825 1011:aca3b46160cb
5 5
6 import java.util.ArrayList; 6 import java.util.ArrayList;
7 import java.util.List; 7 import java.util.List;
8 import java.util.HashMap; 8 import java.util.HashMap;
9 import java.util.Map; 9 import java.util.Map;
10 import java.util.Deque;
11 import java.util.ArrayDeque;
10 12
11 import org.w3c.dom.Document; 13 import org.w3c.dom.Document;
12 import org.w3c.dom.NodeList; 14 import org.w3c.dom.NodeList;
13 import org.w3c.dom.Node; 15 import org.w3c.dom.Node;
14 import org.w3c.dom.Element; 16 import org.w3c.dom.Element;
42 XPathFactory.newInstance(); 44 XPathFactory.newInstance();
43 45
44 protected Document template; 46 protected Document template;
45 47
46 protected Map<String, CompiledStatement> compiledStatements; 48 protected Map<String, CompiledStatement> compiledStatements;
49
50 public static class NamedConnection {
51
52 protected String name;
53 protected Connection connection;
54 protected boolean cached;
55
56 public NamedConnection() {
57 }
58
59 public NamedConnection(
60 String name,
61 Connection connection
62 ) {
63 this(name, connection, true);
64 }
65
66 public NamedConnection(
67 String name,
68 Connection connection,
69 boolean cached
70 ) {
71 this.name = name;
72 this.connection = connection;
73 this.cached = cached;
74 }
75 } // class NamedConnection
47 76
48 77
49 public class BuildHelper 78 public class BuildHelper
50 { 79 {
51 protected Node output; 80 protected Node output;
52 protected Document owner; 81 protected Document owner;
53 protected StackFrames frames; 82 protected StackFrames frames;
54 protected Connection connection; 83 protected List<NamedConnection> connections;
55 protected Map<String, CompiledStatement.Instance> statements; 84 protected Map<String, CompiledStatement.Instance> statements;
85 protected Deque<NamedConnection> connectionsStack;
56 86
57 public BuildHelper( 87 public BuildHelper(
58 Node output, 88 Node output,
59 Connection connection, 89 List<NamedConnection> connections,
60 Map<String, Object> parameters 90 Map<String, Object> parameters
61 ) { 91 ) {
62 this.output = output; 92 if (connections.isEmpty()) {
63 this.connection = connection; 93 throw new IllegalArgumentException("no connections given.");
64 frames = new StackFrames(parameters); 94 }
65 statements = new HashMap<String, CompiledStatement.Instance>(); 95
66 owner = getOwnerDocument(output); 96 this.connections = connections;
97 connectionsStack = new ArrayDeque<NamedConnection>();
98 this.output = output;
99 frames = new StackFrames(parameters);
100 owner = getOwnerDocument(output);
101 statements =
102 new HashMap<String, CompiledStatement.Instance>();
67 } 103 }
68 104
69 public void build() throws SQLException { 105 public void build() throws SQLException {
70 try { 106 try {
71 synchronized (template) { 107 synchronized (template) {
111 if (stmntNode.getLength() < 1) { 147 if (stmntNode.getLength() < 1) {
112 log.warn("dc:context: too less statements"); 148 log.warn("dc:context: too less statements");
113 return; 149 return;
114 } 150 }
115 151
152 String con = current.getAttribute("connection");
153
116 String stmntText = stmntNode.item(0).getTextContent(); 154 String stmntText = stmntNode.item(0).getTextContent();
117 155
118 CompiledStatement.Instance csi = statements.get(stmntText); 156 String key = con + "-" + stmntText;
157
158 CompiledStatement.Instance csi = statements.get(key);
119 159
120 if (csi == null) { 160 if (csi == null) {
121 CompiledStatement cs = compiledStatements.get(stmntText); 161 CompiledStatement cs = compiledStatements.get(stmntText);
122 csi = cs.new Instance(); 162 csi = cs.new Instance();
123 statements.put(stmntText, csi); 163 statements.put(key, csi);
124 } 164 }
125 165
126 ResultData rd = csi.execute(connection, frames); 166 NamedConnection connection = connectionsStack.isEmpty()
127 167 ? connections.get(0)
128 String [] columns = rd.getColumnLabels(); 168 : connectionsStack.peek();
129 169
130 170 if (con.length() > 0) {
131 for (Object [] row: rd.getRows()) { 171 for (NamedConnection nc: connections) {
132 frames.enter(); 172 if (con.equals(nc.name)) {
133 try { 173 connection = nc;
134 frames.put(columns, row); 174 break;
135 for (int i = 0; i < S; ++i) { 175 }
136 build(parent, subs.item(i)); 176 }
137 } 177 }
138 } 178
139 finally { 179 connectionsStack.push(connection);
140 frames.leave(); 180 try {
141 } 181 ResultData rd = csi.execute(
182 connection.connection,
183 frames,
184 connection.cached);
185
186 String [] columns = rd.getColumnLabels();
187
188 for (Object [] row: rd.getRows()) {
189 frames.enter();
190 try {
191 frames.put(columns, row);
192 for (int i = 0; i < S; ++i) {
193 build(parent, subs.item(i));
194 }
195 }
196 finally {
197 frames.leave();
198 }
199 }
200 }
201 finally {
202 connectionsStack.pop();
142 } 203 }
143 } 204 }
144 205
145 protected void element(Node parent, Element current) 206 protected void element(Node parent, Element current)
146 throws SQLException 207 throws SQLException
452 protected static Document getOwnerDocument(Node node) { 513 protected static Document getOwnerDocument(Node node) {
453 Document document = node.getOwnerDocument(); 514 Document document = node.getOwnerDocument();
454 return document != null ? document : (Document)node; 515 return document != null ? document : (Document)node;
455 } 516 }
456 517
518 private static final List<NamedConnection> wrap(Connection connection) {
519 List<NamedConnection> list = new ArrayList<NamedConnection>(1);
520 list.add(new NamedConnection("", connection));
521 return list;
522 }
523
457 public void build( 524 public void build(
458 Connection connection, 525 Connection connection,
459 Node output, 526 Node output,
460 Map<String, Object> parameters 527 Map<String, Object> parameters
461 ) 528 )
462 throws SQLException 529 throws SQLException
463 { 530 {
464 BuildHelper helper = new BuildHelper(output, connection, parameters); 531 build(wrap(connection), output, parameters);
532 }
533
534 public void build(
535 List<NamedConnection> connections,
536 Node output,
537 Map<String, Object> parameters
538 )
539 throws SQLException
540 {
541 BuildHelper helper = new BuildHelper(output, connections, parameters);
465 542
466 helper.build(); 543 helper.build();
467 } 544 }
468 } 545 }
469 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 546 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org