comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java @ 1015:9a1a3080ad98

Bring user specific meta data service to life. flys-artifacts/trunk@2461 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 03 Aug 2011 16:40:04 +0000
parents
children d42fa223be48
comparison
equal deleted inserted replaced
1014:66473e72d321 1015:9a1a3080ad98
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 javax.sql.DataSource;
18
19 import org.apache.log4j.Logger;
20
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Node;
23
24 import org.hibernate.Session;
25
26 import org.hibernate.jdbc.Work;
27
28 import de.intevation.artifacts.common.utils.Config;
29 import de.intevation.artifacts.common.utils.XMLUtils;
30
31 import de.intevation.flys.artifacts.FLYSArtifact;
32
33 import de.intevation.flys.backend.SessionHolder;
34
35 import de.intevation.artifactdatabase.data.StateData;
36
37 import de.intevation.flys.artifacts.datacage.templating.Builder;
38
39 public class Recommendations
40 {
41 private static Logger log = Logger.getLogger(Recommendations.class);
42
43 public static final String XPATH_SYSTEM_TEMPLATE =
44 "/artifact-database/metadata/system/@template";
45
46 public static final String XPATH_USER_TEMPLATE =
47 "/artifact-database/metadata/user/@template";
48
49 private static Recommendations INSTANCE;
50
51 protected Builder systemBuilder;
52 protected Builder userBuilder;
53
54 public Recommendations() {
55 }
56
57 public Recommendations(Builder systemBuilder, Builder userBuilder) {
58 this.systemBuilder = systemBuilder;
59 this.userBuilder = userBuilder;
60 }
61
62 public Builder getUserBuilder() {
63 return userBuilder;
64 }
65
66 public Builder getSystemBuilder() {
67 return systemBuilder;
68 }
69
70 protected static void artifactToParameters(
71 FLYSArtifact artifact,
72 Map<String, Object> parameters
73 ) {
74 parameters.put("current-state-id", artifact.getCurrentStateId());
75
76 for (StateData sd: artifact.getAllData()) {
77 Object value = sd.getValue();
78 if (value == null) {
79 continue;
80 }
81 String key = sd.getName().replace('.', '-');
82 parameters.put(key, value);
83 }
84 }
85
86 public void recommend(
87 FLYSArtifact artifact,
88 String userId,
89 String [] outs,
90 Map<String, Object> extraParameters,
91 Node result
92 ) {
93 Map<String, Object> parameters = new HashMap<String, Object>();
94
95 if (extraParameters != null) {
96 parameters.putAll(extraParameters);
97 }
98
99 if (userId != null) {
100 parameters.put("user-id", userId);
101 }
102
103 if (artifact != null) {
104 artifactToParameters(artifact, parameters);
105 }
106
107 parameters.put("artifact-outs", outs);
108
109 parameters.put("parameters", parameters);
110
111 recommend(parameters, userId, result);
112 }
113
114 public void recommend(
115 Map<String, Object> parameters,
116 String userId,
117 Node result
118 ) {
119 recommend(parameters, userId, result, SessionHolder.HOLDER.get());
120 }
121
122 public void recommend(
123 final Map<String, Object> parameters,
124 final String userId,
125 final Node result,
126 Session session
127 ) {
128 if (systemBuilder == null || userBuilder == null) {
129 log.error("builder not configured properly.");
130 return;
131 }
132
133 session.doWork(new Work() {
134 @Override
135 public void execute(Connection systemConnection)
136 throws SQLException
137 {
138 List<Builder.NamedConnection> connections =
139 new ArrayList<Builder.NamedConnection>(2);
140
141 if (userId != null) { // system and user templates
142 connections.add(new Builder.NamedConnection(
143 Builder.CONNECTION_USER, systemConnection));
144
145 // get connection to datacage db
146 DataSource dataSource = DBConfig
147 .getInstance()
148 .getDBConnection()
149 .getDataSource();
150
151 Connection userConnection = dataSource.getConnection();
152 try {
153 connections.add(new Builder.NamedConnection(
154 Builder.CONNECTION_SYSTEM, userConnection, false));
155
156 userBuilder.build(connections, result, parameters);
157 }
158 finally {
159 userConnection.close();
160 }
161 }
162 else { // system template only
163 connections.add(new Builder.NamedConnection(
164 Builder.CONNECTION_SYSTEM, systemConnection));
165
166 systemBuilder.build(connections, result, parameters);
167 }
168 }
169 });
170 }
171
172 public static synchronized Recommendations getInstance() {
173 if (INSTANCE == null) {
174 INSTANCE = createRecommendations();
175 }
176 return INSTANCE;
177 }
178
179 protected static Document loadTemplate(File file) throws IOException {
180 InputStream in = null;
181
182 try {
183 in = new FileInputStream(file);
184
185 Document template = XMLUtils.parseDocument(in);
186
187 if (template == null) {
188 throw new IOException("cannot load template");
189 }
190 return template;
191 }
192 finally {
193 if (in != null) {
194 try {
195 in.close();
196 }
197 catch (IOException ioe) {
198 log.error(ioe);
199 }
200 }
201 }
202 }
203
204 public static Recommendations createRecommendations(
205 File systemFile,
206 File userFile
207 ) {
208 log.debug("Recommendations.createBuilder");
209
210 if (!systemFile.isFile() || !systemFile.canRead()) {
211 log.error("Cannot open template file '" + systemFile + "'");
212 return null;
213 }
214
215 if (!userFile.isFile() || !userFile.canRead()) {
216 log.error("Cannot open template file '" + userFile + "'");
217 return null;
218 }
219
220 try {
221 Document systemTemplate = loadTemplate(systemFile);
222 Document userTemplate = loadTemplate(userFile);
223 return new Recommendations(
224 new Builder(systemTemplate),
225 new Builder(userTemplate));
226 }
227 catch (IOException ioe) {
228 log.error(ioe);
229 return null;
230 }
231 }
232
233 protected static Recommendations createRecommendations() {
234 log.debug("Recommendations.createRecommendations");
235
236 String systemPath = Config.getStringXPath(XPATH_SYSTEM_TEMPLATE);
237 String userPath = Config.getStringXPath(XPATH_USER_TEMPLATE);
238
239 if (systemPath == null || userPath == null) {
240 log.error("no path to template file given");
241 return null;
242 }
243
244 systemPath = Config.replaceConfigDir(systemPath);
245 userPath = Config.replaceConfigDir(userPath);
246
247 return createRecommendations(
248 new File(systemPath),
249 new File(userPath));
250 }
251 }
252 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org