comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MetaDataService.java @ 974:30c85cb33a50

meta data service: made artifact optional and allow passing extra parameters. flys-artifacts/trunk@2400 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 25 Jul 2011 11:05:33 +0000
parents e456aca4eb7b
children a111f0984706
comparison
equal deleted inserted replaced
973:c30ada285d45 974:30c85cb33a50
40 private static Logger log = Logger.getLogger(MetaDataService.class); 40 private static Logger log = Logger.getLogger(MetaDataService.class);
41 41
42 // old service. To be removed 42 // old service. To be removed
43 public static final String XPATH_RIVER = "/art:river/text()"; 43 public static final String XPATH_RIVER = "/art:river/text()";
44 44
45 public static final String XPATH_UUID = "/art:uuid/@value"; 45 public static final String XPATH_UUID = "/art:meta/art:uuid/@value";
46 public static final String XPATH_OUTS = "/art:outs/@value"; 46 public static final String XPATH_OUTS = "/art:meta/art:outs/@value";
47 public static final String XPATH_FILTERS = "/art:filters/@value"; 47 public static final String XPATH_PARAMETERS = "/art:meta/art:parameters/@value";
48 public static final String XPATH_FILTERS = "/art:meta/art:filters/@value";
48 49
49 /** The global context key of the artifact database */ 50 /** The global context key of the artifact database */
50 public static final String ARTIFACT_DATA_BASE_KEY = 51 public static final String ARTIFACT_DATA_BASE_KEY =
51 "global.artifact.database"; 52 "global.artifact.database";
52 53
100 } 101 }
101 102
102 return result; 103 return result;
103 } 104 }
104 105
106 protected static Map<String, Object> parameters(
107 String parameters,
108 Map<String, Object> data
109 ) {
110 if (parameters != null) {
111 String [] parts = parameters.split("\\s*;\\s*");
112 for (String part: parts) {
113 String [] kv = part.split("\\s*:\\s*");
114 if (kv.length < 2 || (kv[0] = kv[0].trim()).length() == 0) {
115 continue;
116 }
117 String [] values = kv[1].split("\\s*,\\s*");
118 data.put(kv[0], values.length == 1 ? values[0] : values);
119 }
120 }
121 return data;
122 }
123
124 protected static Map<String, Object> filters(
125 String filters,
126 Map<String, Object> data
127 ) {
128 if (filters != null) {
129 for (String filter: filters.split("\\s*,\\*s")) {
130 data.put(filter, "true");
131 }
132 }
133 return data;
134 }
135
105 protected Document newService( 136 protected Document newService(
106 String uuid, 137 String uuid,
107 String outsString, 138 String outsString,
139 String parameters,
108 String filters, 140 String filters,
109 GlobalContext globalContext 141 GlobalContext globalContext
110 ) { 142 ) {
111 Document result = XMLUtils.newDocument(); 143 Document result = XMLUtils.newDocument();
112 144
113 if (!StringUtils.checkUUID(uuid)) { 145 FLYSArtifact flysArtifact;
114 log.warn("'" + uuid + "' is not a UUID"); 146
115 return result; 147 log.debug("uuid: " + uuid);
116 } 148 log.debug("outs: " + outsString);
117 149 log.debug("filters: " + filters);
118 Object dbObject = 150
119 (ArtifactDatabase)globalContext.get(ARTIFACT_DATA_BASE_KEY); 151 if (uuid != null && uuid.length() != 0) {
120 152 if (!StringUtils.checkUUID(uuid)) {
121 if (!(dbObject instanceof ArtifactDatabase)) { 153 log.warn("'" + uuid + "' is not a UUID");
122 log.error("Cannot find artifact database"); 154 return result;
123 return result; 155 }
124 } 156
125 157 Object dbObject =
126 ArtifactDatabase db = (ArtifactDatabase)dbObject; 158 (ArtifactDatabase)globalContext.get(ARTIFACT_DATA_BASE_KEY);
127 159
128 Artifact artifact; 160 if (!(dbObject instanceof ArtifactDatabase)) {
129 161 log.error("Cannot find artifact database");
130 try { 162 return result;
131 artifact = db.getRawArtifact(uuid); 163 }
132 } 164
133 catch (ArtifactDatabaseException adbe) { 165 ArtifactDatabase db = (ArtifactDatabase)dbObject;
134 log.warn("fetching artifact failed", adbe); 166
135 return result; 167 Artifact artifact;
136 } 168
137 169 try {
138 if (!(artifact instanceof FLYSArtifact)) { 170 artifact = db.getRawArtifact(uuid);
139 log.warn("artifact is not a FLYS artifact."); 171 }
140 return result; 172 catch (ArtifactDatabaseException adbe) {
141 } 173 log.warn("fetching artifact failed", adbe);
142 174 return result;
143 FLYSArtifact flysArtifact = (FLYSArtifact)artifact; 175 }
176
177 if (!(artifact instanceof FLYSArtifact)) {
178 log.warn("artifact is not a FLYS artifact.");
179 return result;
180 }
181
182 flysArtifact = (FLYSArtifact)artifact;
183 }
184 else {
185 flysArtifact = null;
186 }
187
188 Map<String, Object> data =
189 filters(filters,
190 parameters(parameters,
191 new HashMap<String, Object>()));
144 192
145 String [] outs = outsString.split("\\s*,\\s*"); 193 String [] outs = outsString.split("\\s*,\\s*");
146
147 Map<String, Object> extras;
148 194
149 if (filters != null) {
150 extras = new HashMap<String, Object>();
151 for (String filter: filters.split("\\s*,\\*s")) {
152 extras.put(filter, "true");
153 }
154 }
155 else {
156 extras = null;
157 }
158
159 DataCage dc = DataCage.getInstance(); 195 DataCage dc = DataCage.getInstance();
160 196
161 dc.recommend(flysArtifact, outs, extras, result); 197 dc.recommend(flysArtifact, outs, data, result);
162 198
163 return result; 199 return result;
164 } 200 }
165 201
166 @Override 202 @Override
175 data, XPATH_UUID, ArtifactNamespaceContext.INSTANCE); 211 data, XPATH_UUID, ArtifactNamespaceContext.INSTANCE);
176 212
177 String outs = XMLUtils.xpathString( 213 String outs = XMLUtils.xpathString(
178 data, XPATH_OUTS, ArtifactNamespaceContext.INSTANCE); 214 data, XPATH_OUTS, ArtifactNamespaceContext.INSTANCE);
179 215
216 String parameters = XMLUtils.xpathString(
217 data, XPATH_PARAMETERS, ArtifactNamespaceContext.INSTANCE);
218
180 String filters = XMLUtils.xpathString( 219 String filters = XMLUtils.xpathString(
181 data, XPATH_FILTERS, ArtifactNamespaceContext.INSTANCE); 220 data, XPATH_FILTERS, ArtifactNamespaceContext.INSTANCE);
182 221
183 return uuid != null && outs != null 222 return outs != null
184 ? newService(uuid, outs, filters, globalContext) 223 ? newService(uuid, outs, parameters, filters, globalContext)
185 : oldService(data); 224 : oldService(data);
186 } 225 }
187 } 226 }
188 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 227 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org