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