comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java @ 1190:f514894ec2fd

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

http://dive4elements.wald.intevation.org