comparison flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java @ 300:9a0e1289bab6

The FLYSArtifactCollection overrides out() and uses OutGenerators to create output for this operation. flys-artifacts/trunk@1649 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 06 Apr 2011 10:01:49 +0000
parents 8d0932c2c2ef
children 16161de47662
comparison
equal deleted inserted replaced
299:8940b0885865 300:9a0e1289bab6
1 package de.intevation.flys.collections; 1 package de.intevation.flys.collections;
2 2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.util.ArrayList;
3 import java.util.Date; 6 import java.util.Date;
7 import java.util.List;
8 import java.util.Map;
4 9
5 import javax.xml.xpath.XPathConstants; 10 import javax.xml.xpath.XPathConstants;
6 11
7 import org.apache.log4j.Logger; 12 import org.apache.log4j.Logger;
8 13
9 import org.w3c.dom.Document; 14 import org.w3c.dom.Document;
10 import org.w3c.dom.Element; 15 import org.w3c.dom.Element;
11 import org.w3c.dom.Node; 16 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList; 17 import org.w3c.dom.NodeList;
13 18
19 import de.intevation.artifacts.Artifact;
14 import de.intevation.artifacts.ArtifactDatabase; 20 import de.intevation.artifacts.ArtifactDatabase;
15 import de.intevation.artifacts.ArtifactDatabaseException; 21 import de.intevation.artifacts.ArtifactDatabaseException;
16 import de.intevation.artifacts.ArtifactNamespaceContext; 22 import de.intevation.artifacts.ArtifactNamespaceContext;
17 import de.intevation.artifacts.CallContext; 23 import de.intevation.artifacts.CallContext;
18 import de.intevation.artifacts.CallMeta; 24 import de.intevation.artifacts.CallMeta;
19 25
20 import de.intevation.artifacts.common.utils.XMLUtils; 26 import de.intevation.artifacts.common.utils.XMLUtils;
21 27
28 import de.intevation.artifactdatabase.Backend;
29 import de.intevation.artifactdatabase.Backend.PersistentArtifact;
22 import de.intevation.artifactdatabase.DefaultArtifactCollection; 30 import de.intevation.artifactdatabase.DefaultArtifactCollection;
31
32 import de.intevation.flys.artifacts.context.FLYSContext;
33 import de.intevation.flys.exports.OutGenerator;
23 34
24 35
25 /** 36 /**
26 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 37 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
27 */ 38 */
36 "/art:result/art:outputmodes"; 47 "/art:result/art:outputmodes";
37 48
38 public static final String XPATH_COLLECTION_ITEMS = 49 public static final String XPATH_COLLECTION_ITEMS =
39 "/art:result/art:artifact-collection/art:collection-item"; 50 "/art:result/art:artifact-collection/art:collection-item";
40 51
52 public static final String XPATH_OUT_NAME = "/art:action/@art:name";
53
54 public static final String XPATH_OUT_TYPE = "/art:action/@art:type";
55
56
41 57
42 @Override 58 @Override
43 public Document describe(CallContext context) { 59 public Document describe(CallContext context) {
44 log.debug("FLYSArtifactCollection.describe: " + identifier); 60 log.debug("FLYSArtifactCollection.describe: " + identifier);
45 61
69 if (attribute != null) { 85 if (attribute != null) {
70 Node child = attribute.getFirstChild(); 86 Node child = attribute.getFirstChild();
71 collection.appendChild(doc.importNode(child, true)); 87 collection.appendChild(doc.importNode(child, true));
72 } 88 }
73 89
90 ArtifactDatabase db = context.getDatabase();
91
74 try { 92 try {
75 ArtifactDatabase db = context.getDatabase(); 93 String[] artifactUUIDs = getArtifactUUIDs(context);
76 CallMeta meta = context.getMeta(); 94
77 95 for (String uuid: artifactUUIDs) {
78 Document itemList = db.listCollectionArtifacts(identifier(), meta);
79 NodeList items = (NodeList) XMLUtils.xpath(
80 itemList,
81 XPATH_COLLECTION_ITEMS,
82 XPathConstants.NODESET,
83 ArtifactNamespaceContext.INSTANCE);
84
85 if (items == null || items.getLength() == 0) {
86 log.debug("No collection items found.");
87 return doc;
88 }
89
90 int num = items.getLength();
91
92 for (int i = 0; i < num; i++) {
93 String uuid = XMLUtils.xpathString(
94 items.item(i),
95 "@art:uuid",
96 ArtifactNamespaceContext.INSTANCE);
97
98 try { 96 try {
99 artifacts.appendChild( 97 artifacts.appendChild(
100 buildArtifactNode(db, uuid, context, ec)); 98 buildArtifactNode(db, uuid, context, ec));
101 } 99 }
102 catch (ArtifactDatabaseException dbe) { 100 catch (ArtifactDatabaseException dbe) {
110 108
111 return doc; 109 return doc;
112 } 110 }
113 111
114 112
113 @Override
114 public void out(Document format, OutputStream out, CallContext context)
115 throws IOException
116 {
117 log.info("FLYSArtifactCollection.out");
118
119 String name = XMLUtils.xpathString(
120 format, XPATH_OUT_NAME, ArtifactNamespaceContext.INSTANCE);
121
122 String type = XMLUtils.xpathString(
123 format, XPATH_OUT_TYPE, ArtifactNamespaceContext.INSTANCE);
124
125 OutGenerator generator = getOutGenerator(context, name, type);
126 if (generator == null) {
127 log.error("There is no generator specified for output: " + type);
128 // TODO throw an exception.
129
130 return;
131 }
132
133 generator.init(format, out, context);
134
135 try {
136 Object[][] container = getArtifactsWithAttribute(context);
137
138 for (Object[] obj: container) {
139 generator.doOut((Artifact) obj[0], (Document) obj[1]);
140 }
141 }
142 catch (ArtifactDatabaseException ade) {
143 log.error(ade, ade);
144 }
145
146 generator.generate();
147 }
148
149
150 /**
151 * This method returns the list of artifact UUIDs that this collections
152 * contains.
153 *
154 * @param context The CallContext that is necessary to get information about
155 * the ArtifactDatabase.
156 *
157 * @return a list of uuids.
158 */
159 protected String[] getArtifactUUIDs(CallContext context)
160 throws ArtifactDatabaseException
161 {
162 log.debug("FLYSArtifactCollection.getArtifactUUIDs");
163
164 ArtifactDatabase db = context.getDatabase();
165 CallMeta meta = context.getMeta();
166
167 Document itemList = db.listCollectionArtifacts(identifier(), meta);
168 NodeList items = (NodeList) XMLUtils.xpath(
169 itemList,
170 XPATH_COLLECTION_ITEMS,
171 XPathConstants.NODESET,
172 ArtifactNamespaceContext.INSTANCE);
173
174 if (items == null || items.getLength() == 0) {
175 log.debug("No artifacts found in this collection.");
176 return null;
177 }
178
179 int num = items.getLength();
180
181 List<String> uuids = new ArrayList<String>(num);
182
183 for (int i = 0; i < num; i++) {
184 String uuid = XMLUtils.xpathString(
185 items.item(i),
186 "@art:uuid",
187 ArtifactNamespaceContext.INSTANCE);
188
189 if (uuid != null && uuid.trim().length() != 0) {
190 uuids.add(uuid);
191 }
192 }
193
194 return (String[]) uuids.toArray(new String[uuids.size()]);
195 }
196
197
198 /**
199 * Returns a concrete Artifact of this collection specified by its uuid.
200 *
201 * @param uuid The Artifact's uuid.
202 * @param context The CallContext.
203 *
204 * @return an Artifact.
205 */
206 protected Artifact getArtifact(String uuid, CallContext context)
207 throws ArtifactDatabaseException
208 {
209 log.debug("FLYSArtifactCollection.getArtifact");
210
211 Backend backend = Backend.getInstance();
212 PersistentArtifact persistent = backend.getArtifact(uuid);
213
214 return persistent != null ? persistent.getArtifact() : null;
215 }
216
217
218 /**
219 * Returns the attribute that belongs to an artifact stored in this
220 * collection.
221 *
222 * @param uuid The Artifact's uuid.
223 * @param context The CallContext.
224 *
225 * @return an attribute in form of a document.
226 */
227 protected Document getArtifactAttribute(String uuid, CallContext context)
228 throws ArtifactDatabaseException
229 {
230 log.debug("FLYSArtifactCollection.getArtifactAttribute");
231
232 // TODO FILL ME
233 return null;
234 }
235
236
237 /**
238 * Returns a list of Artifact/Attribute pairs.
239 *
240 * @param context The CallContext.
241 *
242 * @return a list of Artifact/Attribute pairs.
243 */
244 protected Object[][] getArtifactsWithAttribute(CallContext context)
245 throws ArtifactDatabaseException
246 {
247 log.debug("FLYSArtifactCollection.getArtifactWithAttribute");
248
249 ArtifactDatabase db = context.getDatabase();
250 CallMeta meta = context.getMeta();
251
252 String[] uuids = getArtifactUUIDs(context);
253 Object[][] awa = new Object[uuids.length][2];
254
255 for (int i = 0; i < uuids.length; i++) {
256 try {
257 Artifact artifact = getArtifact(uuids[i], context);
258 Document attribute = getArtifactAttribute(uuids[i], context);
259
260 if (artifact == null) {
261 log.warn("Artifact '" + uuids[i] + "' is not existing.");
262 continue;
263 }
264
265 awa[i][0] = artifact;
266 awa[i][1] = attribute;
267 }
268 catch (ArtifactDatabaseException ade) {
269 log.warn(ade, ade);
270 }
271 }
272
273 return awa;
274 }
275
276
277 /**
278 * Returns the OutGenerator for a specified <i>type</i>.
279 *
280 * @param name The name of the output type.
281 * @param type Defines the type of the desired OutGenerator.
282 *
283 * @return The OutGenerator specified by <i>type</i>.
284 */
285 protected OutGenerator getOutGenerator(
286 CallContext context,
287 String name,
288 String type)
289 {
290 FLYSContext flysContext = context instanceof FLYSContext
291 ? (FLYSContext) context
292 : (FLYSContext) context.globalContext();
293
294 Map<String, Class> generators = (Map<String, Class>)
295 flysContext.get(FLYSContext.OUTGENERATORS_KEY);
296
297 if (generators == null) {
298 log.error("No output generators found in the running application!");
299 return null;
300 }
301
302 Class clazz = generators.get(type);
303
304 try {
305 return clazz != null ? (OutGenerator) clazz.newInstance() : null;
306 }
307 catch (InstantiationException ie) {
308 log.error(ie, ie);
309 }
310 catch (IllegalAccessException iae) {
311 log.error(iae, iae);
312 }
313
314 return null;
315 }
316
317
115 protected Element buildArtifactNode( 318 protected Element buildArtifactNode(
116 ArtifactDatabase database, 319 ArtifactDatabase database,
117 String uuid, 320 String uuid,
118 CallContext context, 321 CallContext context,
119 XMLUtils.ElementCreator ec) 322 XMLUtils.ElementCreator ec)
123 326
124 // XXX I am not sure if it works well every time with an empty document 327 // XXX I am not sure if it works well every time with an empty document
125 // in the describe operation of an artifact. 328 // in the describe operation of an artifact.
126 Document description = database.describe(uuid, null, context.getMeta()); 329 Document description = database.describe(uuid, null, context.getMeta());
127 330
128 // FIXME: XMLDebug does not exists in version control.
129 // de.intevation.flys.artifacts.XMLDebug.out(description);
130
131 String hash = "MYHASH"; 331 String hash = "MYHASH";
132 // TODO 332 // TODO
133 333
134 Element ci = ec.create("artifact"); 334 Element ci = ec.create("artifact");
135 ec.addAttr(ci, "uuid", uuid, true); 335 ec.addAttr(ci, "uuid", uuid, true);

http://dive4elements.wald.intevation.org