comparison artifacts/src/main/java/org/dive4elements/river/artifacts/services/MetaDataService.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/services/MetaDataService.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.services;
2
3 import org.w3c.dom.Document;
4
5 import org.apache.log4j.Logger;
6
7 import java.util.Map;
8 import java.util.HashMap;
9
10 import org.dive4elements.artifacts.Artifact;
11 import org.dive4elements.artifacts.CallMeta;
12 import org.dive4elements.artifacts.GlobalContext;
13 import org.dive4elements.artifacts.ArtifactDatabase;
14 import org.dive4elements.artifacts.ArtifactDatabaseException;
15
16 import org.dive4elements.artifacts.common.utils.XMLUtils;
17 import org.dive4elements.artifacts.common.utils.StringUtils;
18
19 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
20
21 import org.dive4elements.river.artifacts.datacage.Recommendations;
22
23 import org.dive4elements.river.artifacts.FLYSArtifact;
24
25
26 /**
27 * Following XPaths are evaluated on the incoming document.
28 *
29 * "/art:meta/art:artifact-id/@value" The UUID of the artifact. Optional.
30 * Used to fill the template enviroment.
31 * "/art:meta/art:user-id/@value" The UUID of the user. Optional.
32 * If given the user specific template is filled.
33 * "/art:meta/art:outs/@value" The list of outs used to recommend for the
34 * various outputs.
35 * "/art:meta/art:parameters/@value" A list of key/value pairs to inject more
36 * filters to the templating, as "key:value;key2:value2"
37 */
38 public class MetaDataService
39 extends FLYSService
40 {
41 private static Logger log = Logger.getLogger(MetaDataService.class);
42
43 public static final String XPATH_ARTIFACT_ID = "/art:meta/art:artifact-id/@value";
44 public static final String XPATH_USER_ID = "/art:meta/art:user-id/@value";
45 public static final String XPATH_OUTS = "/art:meta/art:outs/@value";
46 public static final String XPATH_PARAMETERS = "/art:meta/art:parameters/@value";
47
48 /** The global context key of the artifact database. */
49 public static final String ARTIFACT_DATA_BASE_KEY =
50 "global.artifact.database";
51
52 public MetaDataService() {
53 }
54
55 @Override
56 protected Document doProcess(
57 Document data,
58 GlobalContext globalContext,
59 CallMeta callMeta
60 ) {
61 log.debug("MetaDataService.process");
62
63 String artifactId = XMLUtils.xpathString(
64 data, XPATH_ARTIFACT_ID, ArtifactNamespaceContext.INSTANCE);
65
66 if (artifactId != null
67 && (artifactId = artifactId.trim()).length() == 0) {
68 artifactId = null;
69 }
70
71 String userId = XMLUtils.xpathString(
72 data, XPATH_USER_ID, ArtifactNamespaceContext.INSTANCE);
73
74 if (userId != null
75 && (userId = userId.trim()).length() == 0) {
76 userId = null;
77 }
78
79 String outs = XMLUtils.xpathString(
80 data, XPATH_OUTS, ArtifactNamespaceContext.INSTANCE);
81
82 String parameters = XMLUtils.xpathString(
83 data, XPATH_PARAMETERS, ArtifactNamespaceContext.INSTANCE);
84
85 return doService(
86 artifactId, userId, outs, parameters, globalContext);
87 }
88
89
90 /**
91 * Split parameterstring in the form of key1:value1;key2:value2
92 * into hash (key1->value1, key2->value2).
93 * @param parameters "key1:value1;key2:value2"
94 * @param data Map into wich to put parameter hash and return.
95 * @return parameter data
96 */
97 protected static Map<String, Object> splitParameters(
98 String parameters,
99 Map<String, Object> data
100 ) {
101 if (parameters != null) {
102 String [] parts = parameters.split("\\s*;\\s*");
103 for (String part: parts) {
104 String [] kv = part.split("\\s*:\\s*");
105 if (kv.length < 2 || (kv[0] = kv[0].trim()).length() == 0) {
106 continue;
107 }
108 String [] values = kv[1].split("\\s*,\\s*");
109 data.put(kv[0], values.length == 1 ? values[0] : values);
110 }
111 }
112 return data;
113 }
114
115 /** Return the document containing matched stuff from meta-data.xml. */
116 protected Document doService(
117 String artifactId,
118 String userId,
119 String outsString,
120 String parameters,
121 GlobalContext globalContext
122 ) {
123 Document result = XMLUtils.newDocument();
124
125 FLYSArtifact flysArtifact;
126
127 if (log.isDebugEnabled()) {
128 log.debug("artifact : " + artifactId);
129 log.debug("user : " + userId);
130 log.debug("outs : " + outsString);
131 log.debug("parameters: " + parameters);
132 }
133
134 if (userId != null && !StringUtils.checkUUID(userId)) {
135 log.warn("'" + userId + "' is not a UUID");
136 return result;
137 }
138
139 if (artifactId != null) {
140 if (!StringUtils.checkUUID(artifactId)) {
141 log.warn("'" + artifactId + "' is not a UUID");
142 return result;
143 }
144
145 Object dbObject =
146 (ArtifactDatabase)globalContext.get(ARTIFACT_DATA_BASE_KEY);
147
148 if (!(dbObject instanceof ArtifactDatabase)) {
149 log.error("Cannot find artifact database");
150 return result;
151 }
152
153 ArtifactDatabase db = (ArtifactDatabase)dbObject;
154
155 Artifact artifact;
156
157 try {
158 artifact = db.getRawArtifact(artifactId);
159 }
160 catch (ArtifactDatabaseException adbe) {
161 log.warn("fetching artifact failed", adbe);
162 return result;
163 }
164
165 if (!(artifact instanceof FLYSArtifact)) {
166 log.warn("artifact is not a FLYS artifact.");
167 return result;
168 }
169
170 flysArtifact = (FLYSArtifact)artifact;
171 }
172 else {
173 flysArtifact = null;
174 }
175
176
177 Map<String, Object> data = splitParameters(
178 parameters, new HashMap<String, Object>());
179
180 String [] outs = outsString == null
181 ? new String [0]
182 : outsString.split("\\s*,\\s*");
183
184 Recommendations rec = Recommendations.getInstance();
185 rec.recommend(
186 flysArtifact, userId, outs, data, result);
187
188 return result;
189 }
190 }
191 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org