comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java @ 3814:8083f6384023

merged flys-artifacts/pre2.6-2012-01-04
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:56 +0200
parents f84ddc205c8b
children 5642a83420f2
comparison
equal deleted inserted replaced
1491:2a00f4849738 3814:8083f6384023
1 package de.intevation.flys.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 de.intevation.artifacts.Artifact;
11 import de.intevation.artifacts.CallMeta;
12 import de.intevation.artifacts.GlobalContext;
13 import de.intevation.artifacts.ArtifactDatabase;
14 import de.intevation.artifacts.ArtifactDatabaseException;
15
16 import de.intevation.artifacts.common.utils.XMLUtils;
17 import de.intevation.artifacts.common.utils.StringUtils;
18
19 import de.intevation.artifacts.common.ArtifactNamespaceContext;
20
21 import de.intevation.flys.artifacts.datacage.Recommendations;
22
23 import de.intevation.flys.artifacts.FLYSArtifact;
24
25 public class MetaDataService
26 extends FLYSService
27 {
28 private static Logger log = Logger.getLogger(MetaDataService.class);
29
30 public static final String XPATH_ARTIFACT_ID = "/art:meta/art:artifact-id/@value";
31 public static final String XPATH_USER_ID = "/art:meta/art:user-id/@value";
32 public static final String XPATH_OUTS = "/art:meta/art:outs/@value";
33 public static final String XPATH_PARAMETERS = "/art:meta/art:parameters/@value";
34
35 /** The global context key of the artifact database. */
36 public static final String ARTIFACT_DATA_BASE_KEY =
37 "global.artifact.database";
38
39 public MetaDataService() {
40 }
41
42 @Override
43 protected Document doProcess(
44 Document data,
45 GlobalContext globalContext,
46 CallMeta callMeta
47 ) {
48 log.debug("MetaDataService.process");
49
50 String artifactId = XMLUtils.xpathString(
51 data, XPATH_ARTIFACT_ID, ArtifactNamespaceContext.INSTANCE);
52
53 if (artifactId != null
54 && (artifactId = artifactId.trim()).length() == 0) {
55 artifactId = null;
56 }
57
58 String userId = XMLUtils.xpathString(
59 data, XPATH_USER_ID, ArtifactNamespaceContext.INSTANCE);
60
61 if (userId != null
62 && (userId = userId.trim()).length() == 0) {
63 userId = null;
64 }
65
66 String outs = XMLUtils.xpathString(
67 data, XPATH_OUTS, ArtifactNamespaceContext.INSTANCE);
68
69 String parameters = XMLUtils.xpathString(
70 data, XPATH_PARAMETERS, ArtifactNamespaceContext.INSTANCE);
71
72 return doService(
73 artifactId, userId, outs, parameters, globalContext);
74 }
75
76 protected static Map<String, Object> splitParameters(
77 String parameters,
78 Map<String, Object> data
79 ) {
80 if (parameters != null) {
81 String [] parts = parameters.split("\\s*;\\s*");
82 for (String part: parts) {
83 String [] kv = part.split("\\s*:\\s*");
84 if (kv.length < 2 || (kv[0] = kv[0].trim()).length() == 0) {
85 continue;
86 }
87 String [] values = kv[1].split("\\s*,\\s*");
88 data.put(kv[0], values.length == 1 ? values[0] : values);
89 }
90 }
91 return data;
92 }
93
94 protected Document doService(
95 String artifactId,
96 String userId,
97 String outsString,
98 String parameters,
99 GlobalContext globalContext
100 ) {
101 Document result = XMLUtils.newDocument();
102
103 FLYSArtifact flysArtifact;
104
105 if (log.isDebugEnabled()) {
106 log.debug("artifact : " + artifactId);
107 log.debug("user : " + userId);
108 log.debug("outs : " + outsString);
109 log.debug("parameters: " + parameters);
110 }
111
112 if (userId != null && !StringUtils.checkUUID(userId)) {
113 log.warn("'" + userId + "' is not a UUID");
114 return result;
115 }
116
117 if (artifactId != null) {
118 if (!StringUtils.checkUUID(artifactId)) {
119 log.warn("'" + artifactId + "' is not a UUID");
120 return result;
121 }
122
123 Object dbObject =
124 (ArtifactDatabase)globalContext.get(ARTIFACT_DATA_BASE_KEY);
125
126 if (!(dbObject instanceof ArtifactDatabase)) {
127 log.error("Cannot find artifact database");
128 return result;
129 }
130
131 ArtifactDatabase db = (ArtifactDatabase)dbObject;
132
133 Artifact artifact;
134
135 try {
136 artifact = db.getRawArtifact(artifactId);
137 }
138 catch (ArtifactDatabaseException adbe) {
139 log.warn("fetching artifact failed", adbe);
140 return result;
141 }
142
143 if (!(artifact instanceof FLYSArtifact)) {
144 log.warn("artifact is not a FLYS artifact.");
145 return result;
146 }
147
148 flysArtifact = (FLYSArtifact)artifact;
149 }
150 else {
151 flysArtifact = null;
152 }
153
154
155 Map<String, Object> data = splitParameters(
156 parameters, new HashMap<String, Object>());
157
158 String [] outs = outsString == null
159 ? new String [0]
160 : outsString.split("\\s*,\\s*");
161
162 Recommendations rec = Recommendations.getInstance();
163 rec.recommend(
164 flysArtifact, userId, outs, data, result);
165
166 return result;
167 }
168 }
169 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org