annotate artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/App.java @ 9232:5030c46d8cb4

Implemented xpath function that selectes the 'mean year' of a fixation-artifact. Needs to provide the ArtifactDatabase to the builder implementation.
author gernotbelger
date Fri, 06 Jul 2018 13:09:54 +0200
parents dbb26bc81843
children
rev   line source
5863
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
2 * Software engineering by Intevation GmbH
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
3 *
5994
af13ceeba52a Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5863
diff changeset
4 * This file is Free Software under the GNU AGPL (>=v3)
5863
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
5994
af13ceeba52a Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5863
diff changeset
6 * documentation coming with Dive4Elements River for details.
5863
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
7 */
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
8
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1046
diff changeset
9 package org.dive4elements.river.artifacts.datacage.templating;
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
11 import java.io.File;
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
12 import java.io.FileOutputStream;
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 import java.io.IOException;
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import java.io.OutputStream;
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
15 import java.net.MalformedURLException;
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
16 import java.util.HashMap;
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
17 import java.util.Map;
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 import org.apache.log4j.Logger;
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
20 import org.apache.log4j.PropertyConfigurator;
9232
5030c46d8cb4 Implemented xpath function that selectes the 'mean year' of a fixation-artifact.
gernotbelger
parents: 9127
diff changeset
21 import org.dive4elements.artifacts.CallContext;
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
22 import org.dive4elements.artifacts.common.utils.Config;
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1046
diff changeset
23 import org.dive4elements.artifacts.common.utils.XMLUtils;
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 1046
diff changeset
24 import org.dive4elements.river.artifacts.datacage.Recommendations;
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
25 import org.dive4elements.river.backend.SessionFactoryProvider;
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
26 import org.hibernate.Session;
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
27 import org.slf4j.bridge.SLF4JBridgeHandler;
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
28 import org.w3c.dom.Document;
1015
9a1a3080ad98 Bring user specific meta data service to life.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 998
diff changeset
29
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
30 public class App {
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 private static Logger log = Logger.getLogger(App.class);
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
33 /**
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
34 * The logging is done via Log4j. To configure the logging
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
35 * a file 'log4j.properties' is search in the configuration directory.
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
36 */
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
37 private static final String LOG4J_PROPERTIES = "log4j.properties";
1015
9a1a3080ad98 Bring user specific meta data service to life.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 998
diff changeset
38
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
39 public static final String template = System.getProperty("meta.data.template", "meta-data.xml");
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
41 public static final String userId = System.getProperty("user.id");
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
43 public static final String PARAMETERS = System.getProperty("meta.data.parameters", "");
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
44
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
45 public static final String OUTPUT = System.getProperty("meta.data.output");
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 public static Map<String, Object> getParameters() {
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
48 final HashMap<String, Object> map = new HashMap<>();
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
49 final String[] parts = PARAMETERS.split("\\s*;\\s*");
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
50 for (final String part : parts) {
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
51 final String[] kv = part.split("\\s*:\\s*");
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 if (kv.length < 2 || (kv[0] = kv[0].trim()).length() == 0) {
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 continue;
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 }
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
55 final String[] values = kv[1].split("\\s*,\\s*");
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 map.put(kv[0], values.length == 1 ? values[0] : values);
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 }
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 return map;
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
61 /**
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
62 * Trys to load the Log4j configuration from ${config.dir}/log4j.properties.
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
63 */
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
64 private static final void configureLogging() {
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
65 final File configDir = Config.getConfigDirectory();
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
66 final File propFile = new File(configDir, LOG4J_PROPERTIES);
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
67
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
68 if (propFile.isFile() && propFile.canRead()) {
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
69 try {
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
70 PropertyConfigurator.configure(propFile.toURI().toURL());
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
71 SLF4JBridgeHandler.install();
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
72 }
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
73 catch (final MalformedURLException mue) {
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
74 mue.printStackTrace(System.err);
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
75 }
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
76 }
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
77 }
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
78
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
79 public static void main(final String[] args) {
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
81 configureLogging();
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
82
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
83 final Recommendations rec = Recommendations.createRecommendations(new File(template));
1015
9a1a3080ad98 Bring user specific meta data service to life.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 998
diff changeset
84
9a1a3080ad98 Bring user specific meta data service to life.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 998
diff changeset
85 if (rec == null) {
9a1a3080ad98 Bring user specific meta data service to life.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 998
diff changeset
86 System.err.println("No recommendations created");
9a1a3080ad98 Bring user specific meta data service to life.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 998
diff changeset
87 return;
9a1a3080ad98 Bring user specific meta data service to life.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 998
diff changeset
88 }
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 final Document result = XMLUtils.newDocument();
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 final Map<String, Object> parameters = getParameters();
8927
04ad2cfce559 ANother comment
gernotbelger
parents: 8903
diff changeset
93 // REMARK/TODO: 'ARTIFACT-OUTS' needs a String-Array, should be handled by parser
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
94 parameters.put("ARTIFACT-OUTS", new String[] { "LONGITUDINAL_SECTION" });
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
95 parameters.put("PARAMETERS", parameters);
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
96 parameters.put("USER-ID", userId);
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
98 final Session session = SessionFactoryProvider.createSessionFactory().openSession();
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 try {
9232
5030c46d8cb4 Implemented xpath function that selectes the 'mean year' of a fixation-artifact.
gernotbelger
parents: 9127
diff changeset
101 final CallContext context = null;
5030c46d8cb4 Implemented xpath function that selectes the 'mean year' of a fixation-artifact.
gernotbelger
parents: 9127
diff changeset
102 rec.recommend(parameters, userId, result, session, context);
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
103 } finally {
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 session.close();
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 }
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 OutputStream out;
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 if (OUTPUT == null) {
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 out = System.out;
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
111 } else {
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 try {
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 out = new FileOutputStream(OUTPUT);
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 }
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
115 catch (final IOException ioe) {
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 log.error(ioe);
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 return;
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 }
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 }
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 try {
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
122 out.write('\n');
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 XMLUtils.toStream(result, out);
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
124 out.write('\n');
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
125 }
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
126 catch (final IOException e) {
8903
7de3837b204c Minor changes to helper App so it works
gernotbelger
parents: 5994
diff changeset
127 log.error(e);
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
128 } finally {
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 if (OUTPUT != null) {
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 try {
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 out.close();
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 }
9127
dbb26bc81843 Testing
gernotbelger
parents: 8927
diff changeset
133 catch (final IOException ioe) {
998
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 log.error(ioe);
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135 }
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 }
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137 }
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
138 System.exit(0);
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 }
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 }
b81626b10cb7 Datacage: Moved templating in a better suited package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org