comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents 5642a83420f2
children 2b3c4abe034f
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.datacage;
2
3 import java.util.Map;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.ArrayList;
7
8 import java.io.InputStream;
9 import java.io.IOException;
10 import java.io.File;
11
12 import java.io.FileInputStream;
13
14 import java.sql.Connection;
15 import java.sql.SQLException;
16
17 import org.apache.log4j.Logger;
18
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Node;
21
22 import org.hibernate.Session;
23
24 import org.hibernate.jdbc.Work;
25
26 import de.intevation.artifacts.common.utils.Config;
27 import de.intevation.artifacts.common.utils.XMLUtils;
28 import de.intevation.artifacts.common.utils.StringUtils;
29
30 import de.intevation.flys.artifacts.FLYSArtifact;
31
32 import de.intevation.flys.backend.SessionHolder;
33
34 import de.intevation.artifactdatabase.data.StateData;
35
36 import de.intevation.flys.artifacts.datacage.templating.Builder;
37
38 public class Recommendations
39 {
40 private static Logger log = Logger.getLogger(Recommendations.class);
41
42 private static final boolean DEVELOPMENT_MODE =
43 Boolean.getBoolean("flys.datacage.recommendations.development");
44
45 public static final String XPATH_TEMPLATE =
46 "/artifact-database/metadata/template/text()";
47
48 public static final String DEFAULT_TEMPLATE_PATH =
49 "${artifacts.config.dir}/meta-data.xml";
50
51 private static Recommendations INSTANCE;
52
53 public static class BuilderProvider
54 {
55 protected Builder builder;
56
57 public BuilderProvider() {
58 }
59
60 public BuilderProvider(Builder builder) {
61 this.builder = builder;
62 }
63
64 public Builder getBuilder() {
65 return builder;
66 }
67 } // class BuilderProvider
68
69 public static class FileBuilderProvider
70 extends BuilderProvider
71 {
72 protected File file;
73 protected long lastModified;
74
75 public FileBuilderProvider() {
76 }
77
78 public FileBuilderProvider(File file) {
79 this.file = file;
80 lastModified = Long.MIN_VALUE;
81 }
82
83 @Override
84 public synchronized Builder getBuilder() {
85 long modified = file.lastModified();
86 if (modified > lastModified) {
87 lastModified = modified;
88 try {
89 Document template = loadTemplate(file);
90 builder = new Builder(template);
91 }
92 catch (IOException ioe) {
93 log.error(ioe);
94 }
95 }
96 return builder;
97 }
98
99 public BuilderProvider toStaticProvider() {
100 return new BuilderProvider(builder);
101 }
102 } // class BuilderProvider
103
104 protected BuilderProvider builderProvider;
105
106 public Recommendations() {
107 }
108
109 public Recommendations(BuilderProvider builderProvider) {
110 this.builderProvider = builderProvider;
111 }
112
113 public Builder getBuilder() {
114 return builderProvider.getBuilder();
115 }
116
117 protected static void artifactToParameters(
118 FLYSArtifact artifact,
119 Map<String, Object> parameters
120 ) {
121 parameters.put("CURRENT-STATE-ID", artifact.getCurrentStateId());
122 parameters.put("ARTIFACT-ID", artifact.identifier());
123
124 for (StateData sd: artifact.getAllData()) {
125 Object value = sd.getValue();
126 if (value == null) {
127 continue;
128 }
129 String key = sd.getName().replace('.', '-').toUpperCase();
130 parameters.put(key, value);
131 }
132 }
133
134 public static void convertKeysToUpperCase(
135 Map<String, Object> src,
136 Map<String, Object> dst
137 ) {
138 for (Map.Entry<String, Object> entry: src.entrySet()) {
139 dst.put(entry.getKey().toUpperCase(), entry.getValue());
140 }
141 }
142
143 public void recommend(
144 FLYSArtifact artifact,
145 String userId,
146 String [] outs,
147 Map<String, Object> extraParameters,
148 Node result
149 ) {
150 Map<String, Object> parameters = new HashMap<String, Object>();
151
152 if (extraParameters != null) {
153 convertKeysToUpperCase(extraParameters, parameters);
154 }
155
156 if (userId != null) {
157 parameters.put("USER-ID", userId);
158 }
159
160 if (artifact != null) {
161 artifactToParameters(artifact, parameters);
162 }
163
164 parameters.put("ARTIFACT-OUTS", StringUtils.toUpperCase(outs));
165
166 parameters.put("PARAMETERS", parameters);
167
168 recommend(parameters, userId, result);
169 }
170
171 public void recommend(
172 Map<String, Object> parameters,
173 String userId,
174 Node result
175 ) {
176 recommend(parameters, userId, result, SessionHolder.HOLDER.get());
177 }
178
179 public void recommend(
180 final Map<String, Object> parameters,
181 final String userId,
182 final Node result,
183 Session session
184 ) {
185 session.doWork(new Work() {
186 @Override
187 public void execute(Connection systemConnection)
188 throws SQLException
189 {
190 List<Builder.NamedConnection> connections =
191 new ArrayList<Builder.NamedConnection>(2);
192
193 Connection userConnection = userId != null
194 ? DBConfig
195 .getInstance()
196 .getDBConnection()
197 .getDataSource()
198 .getConnection()
199 : null;
200
201 try {
202 if (userConnection != null) {
203 connections.add(new Builder.NamedConnection(
204 Builder.CONNECTION_USER, userConnection, false));
205 }
206
207 connections.add(new Builder.NamedConnection(
208 Builder.CONNECTION_SYSTEM, systemConnection, true));
209
210 getBuilder().build(connections, result, parameters);
211 }
212 finally {
213 if (userConnection != null) {
214 userConnection.close();
215 }
216 }
217 }
218 });
219 }
220
221 public static synchronized Recommendations getInstance() {
222 if (INSTANCE == null) {
223 INSTANCE = createRecommendations();
224 }
225 return INSTANCE;
226 }
227
228 protected static Document loadTemplate(File file) throws IOException {
229 InputStream in = null;
230
231 try {
232 in = new FileInputStream(file);
233
234 Document template = XMLUtils.parseDocument(in);
235
236 if (template == null) {
237 throw new IOException("cannot load template");
238 }
239 return template;
240 }
241 finally {
242 if (in != null) {
243 try {
244 in.close();
245 }
246 catch (IOException ioe) {
247 log.error(ioe);
248 }
249 }
250 }
251 }
252
253 public static Recommendations createRecommendations(File file) {
254 log.debug("Recommendations.createBuilder");
255
256 if (!file.isFile() || !file.canRead()) {
257 log.error("Cannot open template file '" + file + "'");
258 return null;
259 }
260
261 FileBuilderProvider fbp = new FileBuilderProvider(file);
262
263 if (fbp.getBuilder() == null) {
264 log.error("failed loading builder");
265 return null;
266 }
267
268 BuilderProvider bp = DEVELOPMENT_MODE
269 ? fbp
270 : fbp.toStaticProvider();
271
272 return new Recommendations(bp);
273 }
274
275 protected static Recommendations createRecommendations() {
276 log.debug("Recommendations.createRecommendations");
277
278 String path = Config.getStringXPath(XPATH_TEMPLATE);
279
280 if (path == null) {
281 path = DEFAULT_TEMPLATE_PATH;
282 }
283
284 path = Config.replaceConfigDir(path);
285
286 log.info("Meta data template: " + path);
287
288 return createRecommendations(new File(path));
289 }
290 }
291 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org