Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java @ 1046:0a5eff5511b1
Fixed flys/issue262: Unified user and system datacage template
flys-artifacts/trunk@2508 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 19 Aug 2011 09:48:13 +0000 |
parents | c586b6220f35 |
children | 3ca999f507b7 |
comparison
equal
deleted
inserted
replaced
1045:ba840385ad2e | 1046:0a5eff5511b1 |
---|---|
12 import java.io.FileInputStream; | 12 import java.io.FileInputStream; |
13 | 13 |
14 import java.sql.Connection; | 14 import java.sql.Connection; |
15 import java.sql.SQLException; | 15 import java.sql.SQLException; |
16 | 16 |
17 import javax.sql.DataSource; | |
18 | |
19 import org.apache.log4j.Logger; | 17 import org.apache.log4j.Logger; |
20 | 18 |
21 import org.w3c.dom.Document; | 19 import org.w3c.dom.Document; |
22 import org.w3c.dom.Node; | 20 import org.w3c.dom.Node; |
23 | 21 |
41 private static Logger log = Logger.getLogger(Recommendations.class); | 39 private static Logger log = Logger.getLogger(Recommendations.class); |
42 | 40 |
43 private static final boolean DEVELOPMENT_MODE = | 41 private static final boolean DEVELOPMENT_MODE = |
44 Boolean.getBoolean("flys.datacage.recommendations.development"); | 42 Boolean.getBoolean("flys.datacage.recommendations.development"); |
45 | 43 |
46 public static final String XPATH_SYSTEM_TEMPLATE = | 44 public static final String XPATH_TEMPLATE = |
47 "/artifact-database/metadata/system/@template"; | 45 "/artifact-database/metadata/template/text()"; |
48 | 46 |
49 public static final String XPATH_USER_TEMPLATE = | 47 public static final String DEFAULT_TEMPLATE_PATH = |
50 "/artifact-database/metadata/user/@template"; | 48 "${artifacts.config.dir}/meta-data.xml"; |
51 | 49 |
52 private static Recommendations INSTANCE; | 50 private static Recommendations INSTANCE; |
53 | 51 |
54 public static class BuilderProvider | 52 public static class BuilderProvider |
55 { | 53 { |
100 public BuilderProvider toStaticProvider() { | 98 public BuilderProvider toStaticProvider() { |
101 return new BuilderProvider(builder); | 99 return new BuilderProvider(builder); |
102 } | 100 } |
103 } // class BuilderProvider | 101 } // class BuilderProvider |
104 | 102 |
105 protected BuilderProvider systemBuilderProvider; | 103 protected BuilderProvider builderProvider; |
106 protected BuilderProvider userBuilderProvider; | |
107 | 104 |
108 public Recommendations() { | 105 public Recommendations() { |
109 } | 106 } |
110 | 107 |
111 public Recommendations( | 108 public Recommendations(BuilderProvider builderProvider) { |
112 BuilderProvider systemBuilderProvider, | 109 this.builderProvider = builderProvider; |
113 BuilderProvider userBuilderProvider) { | 110 } |
114 this.systemBuilderProvider = systemBuilderProvider; | 111 |
115 this.userBuilderProvider = userBuilderProvider; | 112 public Builder getBuilder() { |
116 } | 113 return builderProvider.getBuilder(); |
117 | |
118 public Builder getUserBuilder() { | |
119 return userBuilderProvider.getBuilder(); | |
120 } | |
121 | |
122 public Builder getSystemBuilder() { | |
123 return systemBuilderProvider.getBuilder(); | |
124 } | 114 } |
125 | 115 |
126 protected static void artifactToParameters( | 116 protected static void artifactToParameters( |
127 FLYSArtifact artifact, | 117 FLYSArtifact artifact, |
128 Map<String, Object> parameters | 118 Map<String, Object> parameters |
188 throws SQLException | 178 throws SQLException |
189 { | 179 { |
190 List<Builder.NamedConnection> connections = | 180 List<Builder.NamedConnection> connections = |
191 new ArrayList<Builder.NamedConnection>(2); | 181 new ArrayList<Builder.NamedConnection>(2); |
192 | 182 |
193 if (userId != null) { // Use system and user templates. | 183 Connection userConnection = userId != null |
194 // Get connection to datacage db. | 184 ? DBConfig |
195 DataSource dataSource = DBConfig | |
196 .getInstance() | 185 .getInstance() |
197 .getDBConnection() | 186 .getDBConnection() |
198 .getDataSource(); | 187 .getDataSource() |
199 | 188 .getConnection() |
200 Connection userConnection = dataSource.getConnection(); | 189 : null; |
201 try { | 190 |
191 try { | |
192 if (userConnection != null) { | |
202 connections.add(new Builder.NamedConnection( | 193 connections.add(new Builder.NamedConnection( |
203 Builder.CONNECTION_USER, userConnection, false)); | 194 Builder.CONNECTION_USER, userConnection, false)); |
204 | |
205 connections.add(new Builder.NamedConnection( | |
206 Builder.CONNECTION_SYSTEM, systemConnection, true)); | |
207 | |
208 getUserBuilder().build(connections, result, parameters); | |
209 } | 195 } |
210 finally { | 196 |
197 connections.add(new Builder.NamedConnection( | |
198 Builder.CONNECTION_SYSTEM, systemConnection, true)); | |
199 | |
200 getBuilder().build(connections, result, parameters); | |
201 } | |
202 finally { | |
203 if (userConnection != null) { | |
211 userConnection.close(); | 204 userConnection.close(); |
212 } | 205 } |
213 } | |
214 else { // Use system template only. | |
215 connections.add(new Builder.NamedConnection( | |
216 Builder.CONNECTION_SYSTEM, systemConnection, true)); | |
217 | |
218 getSystemBuilder().build(connections, result, parameters); | |
219 } | 206 } |
220 } | 207 } |
221 }); | 208 }); |
222 } | 209 } |
223 | 210 |
251 } | 238 } |
252 } | 239 } |
253 } | 240 } |
254 } | 241 } |
255 | 242 |
256 public static Recommendations createRecommendations( | 243 public static Recommendations createRecommendations(File file) { |
257 File systemFile, | |
258 File userFile | |
259 ) { | |
260 log.debug("Recommendations.createBuilder"); | 244 log.debug("Recommendations.createBuilder"); |
261 | 245 |
262 if (!systemFile.isFile() || !systemFile.canRead()) { | 246 if (!file.isFile() || !file.canRead()) { |
263 log.error("Cannot open template file '" + systemFile + "'"); | 247 log.error("Cannot open template file '" + file + "'"); |
264 return null; | 248 return null; |
265 } | 249 } |
266 | 250 |
267 if (!userFile.isFile() || !userFile.canRead()) { | 251 FileBuilderProvider fbp = new FileBuilderProvider(file); |
268 log.error("Cannot open template file '" + userFile + "'"); | 252 |
269 return null; | 253 if (fbp.getBuilder() == null) { |
270 } | |
271 | |
272 FileBuilderProvider ufbp = new FileBuilderProvider(userFile); | |
273 FileBuilderProvider sfbp = new FileBuilderProvider(systemFile); | |
274 | |
275 if (ufbp.getBuilder() == null || sfbp.getBuilder() == null) { | |
276 log.error("failed loading builder"); | 254 log.error("failed loading builder"); |
277 return null; | 255 return null; |
278 } | 256 } |
279 | 257 |
280 BuilderProvider ubp; | 258 BuilderProvider bp = DEVELOPMENT_MODE |
281 BuilderProvider sbp; | 259 ? fbp |
282 | 260 : fbp.toStaticProvider(); |
283 if (DEVELOPMENT_MODE) { | 261 |
284 ubp = ufbp; | 262 return new Recommendations(bp); |
285 sbp = sfbp; | |
286 } | |
287 else { | |
288 ubp = ufbp.toStaticProvider(); | |
289 sbp = sfbp.toStaticProvider(); | |
290 } | |
291 | |
292 return new Recommendations(sbp, ubp); | |
293 } | 263 } |
294 | 264 |
295 protected static Recommendations createRecommendations() { | 265 protected static Recommendations createRecommendations() { |
296 log.debug("Recommendations.createRecommendations"); | 266 log.debug("Recommendations.createRecommendations"); |
297 | 267 |
298 String systemPath = Config.getStringXPath(XPATH_SYSTEM_TEMPLATE); | 268 String path = Config.getStringXPath(XPATH_TEMPLATE); |
299 String userPath = Config.getStringXPath(XPATH_USER_TEMPLATE); | 269 |
300 | 270 if (path == null) { |
301 if (systemPath == null || userPath == null) { | 271 path = DEFAULT_TEMPLATE_PATH; |
302 log.error("no path to template file given"); | 272 } |
303 return null; | 273 |
304 } | 274 path = Config.replaceConfigDir(path); |
305 | 275 |
306 systemPath = Config.replaceConfigDir(systemPath); | 276 log.info("Meta data template: " + path); |
307 userPath = Config.replaceConfigDir(userPath); | 277 |
308 | 278 return createRecommendations(new File(path)); |
309 return createRecommendations( | |
310 new File(systemPath), | |
311 new File(userPath)); | |
312 } | 279 } |
313 } | 280 } |
314 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : | 281 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |