Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/org/dive4elements/river/collections/CollectionDescriptionHelper.java @ 5831:bd047b71ab37
Repaired internal references
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:06:39 +0200 |
parents | flys-artifacts/src/main/java/de/intevation/flys/collections/CollectionDescriptionHelper.java@a5f65e8983be |
children |
comparison
equal
deleted
inserted
replaced
5830:160f53ee0870 | 5831:bd047b71ab37 |
---|---|
1 package org.dive4elements.river.collections; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.Date; | |
5 import java.util.List; | |
6 | |
7 import javax.xml.xpath.XPathConstants; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 import org.w3c.dom.Document; | |
11 import org.w3c.dom.Element; | |
12 import org.w3c.dom.Node; | |
13 import org.w3c.dom.NodeList; | |
14 | |
15 import org.dive4elements.artifacts.ArtifactDatabase; | |
16 import org.dive4elements.artifacts.ArtifactDatabaseException; | |
17 import org.dive4elements.artifacts.ArtifactNamespaceContext; | |
18 import org.dive4elements.artifacts.CallContext; | |
19 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
20 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; | |
21 | |
22 | |
23 public class CollectionDescriptionHelper { | |
24 | |
25 private static final Logger logger = | |
26 Logger.getLogger(CollectionDescriptionHelper.class); | |
27 | |
28 | |
29 public static final String XPATH_ARTIFACT_STATE_DATA = | |
30 "/art:result/art:ui/art:static/art:state/art:data"; | |
31 | |
32 /** Constant XPath that points to the outputmodes of an artifact. */ | |
33 public static final String XPATH_ARTIFACT_OUTPUTMODES = | |
34 "/art:result/art:outputmodes"; | |
35 | |
36 | |
37 protected ElementCreator ec; | |
38 | |
39 protected CallContext context; | |
40 protected ArtifactDatabase database; | |
41 | |
42 protected String name; | |
43 protected String uuid; | |
44 protected Date creation; | |
45 protected long ttl; | |
46 | |
47 protected List<String> artifacts; | |
48 protected CollectionAttribute attribute; | |
49 | |
50 | |
51 /** | |
52 * @param name The name of the collection. | |
53 * @param uuid The uuid of the collection. | |
54 * @param creation The creation time of the collection. | |
55 * @param ttl The time to live of the collection. | |
56 */ | |
57 public CollectionDescriptionHelper( | |
58 String name, | |
59 String uuid, | |
60 Date creation, | |
61 long ttl, | |
62 CallContext callContext | |
63 ) { | |
64 this.name = name; | |
65 this.uuid = uuid; | |
66 this.creation = creation; | |
67 this.ttl = ttl; | |
68 this.context = callContext; | |
69 this.database = callContext.getDatabase(); | |
70 this.artifacts = new ArrayList<String>(); | |
71 } | |
72 | |
73 | |
74 public void addArtifact(String uuid) { | |
75 if (uuid != null && uuid.length() > 0) { | |
76 artifacts.add(uuid); | |
77 } | |
78 } | |
79 | |
80 | |
81 public void setAttribute(CollectionAttribute attribute) { | |
82 if (attribute != null) { | |
83 this.attribute = attribute; | |
84 } | |
85 } | |
86 | |
87 | |
88 public Document toXML() { | |
89 Document doc = XMLUtils.newDocument(); | |
90 | |
91 ec = new ElementCreator( | |
92 doc, | |
93 ArtifactNamespaceContext.NAMESPACE_URI, | |
94 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
95 | |
96 Element root = ec.create("artifact-collection"); | |
97 doc.appendChild(root); | |
98 | |
99 String creationTime = creation != null | |
100 ? Long.toString(creation.getTime()) | |
101 : ""; | |
102 | |
103 ec.addAttr(root, "name", name, true); | |
104 ec.addAttr(root, "uuid", uuid, true); | |
105 ec.addAttr(root, "creation", creationTime, true); | |
106 ec.addAttr(root, "ttl", String.valueOf(ttl), true); | |
107 | |
108 appendArtifacts(root); | |
109 appendAttribute(root); | |
110 | |
111 return doc; | |
112 } | |
113 | |
114 | |
115 /** | |
116 * Appends parts of the DESCRIBE document of each Artifact to <i>root</i>. | |
117 * | |
118 * @param root The root node. | |
119 */ | |
120 protected void appendArtifacts(Element root) { | |
121 Element artifactsEl = ec.create("artifacts"); | |
122 | |
123 for (String uuid: artifacts) { | |
124 try { | |
125 Element e = buildArtifactNode(uuid); | |
126 | |
127 if (e != null) { | |
128 artifactsEl.appendChild(e); | |
129 } | |
130 } | |
131 catch (ArtifactDatabaseException dbe) { | |
132 logger.warn(dbe, dbe); | |
133 } | |
134 } | |
135 | |
136 root.appendChild(artifactsEl); | |
137 } | |
138 | |
139 | |
140 /** | |
141 * Create the Artifacts Node that contains outputmode and statedata. | |
142 * | |
143 * @param uuid uuid of the artifact. | |
144 */ | |
145 protected Element buildArtifactNode(String uuid) | |
146 throws ArtifactDatabaseException | |
147 { | |
148 logger.debug("Append artifact '" + uuid + "' to collection description"); | |
149 | |
150 // TODO | |
151 String hash = "MYHASH"; | |
152 | |
153 Element ci = ec.create("artifact"); | |
154 ec.addAttr(ci, "uuid", uuid, true); | |
155 ec.addAttr(ci, "hash", hash, true); | |
156 | |
157 // XXX I am not sure if it works well every time with an empty document | |
158 // in the describe operation of an artifact. | |
159 Document description = database.describe(uuid, null, context.getMeta()); | |
160 | |
161 // Add outputmode element(s). | |
162 Node outputModes = (Node) XMLUtils.xpath( | |
163 description, | |
164 XPATH_ARTIFACT_OUTPUTMODES, | |
165 XPathConstants.NODE, | |
166 ArtifactNamespaceContext.INSTANCE); | |
167 | |
168 if (outputModes != null) { | |
169 Document doc = ci.getOwnerDocument(); | |
170 ci.appendChild(doc.importNode(outputModes, true)); | |
171 } | |
172 | |
173 // Add state-data element(s). | |
174 Node dataNode = ci.appendChild( | |
175 ci.getOwnerDocument().createElement("art:data-items")); | |
176 | |
177 NodeList dataNodes = (NodeList) XMLUtils.xpath( | |
178 description, | |
179 XPATH_ARTIFACT_STATE_DATA, | |
180 XPathConstants.NODESET, | |
181 ArtifactNamespaceContext.INSTANCE); | |
182 | |
183 if (dataNodes != null) { | |
184 Document doc = ci.getOwnerDocument(); | |
185 for (int i = 0, D = dataNodes.getLength(); i < D; i++) { | |
186 dataNode.appendChild(doc.importNode(dataNodes.item(i), true)); | |
187 } | |
188 } | |
189 | |
190 return ci; | |
191 } | |
192 | |
193 | |
194 protected void appendAttribute(Element root) { | |
195 if (attribute != null) { | |
196 Document owner = root.getOwnerDocument(); | |
197 Document attr = attribute.toXML(); | |
198 | |
199 root.appendChild(owner.importNode(attr.getFirstChild(), true)); | |
200 } | |
201 } | |
202 } | |
203 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |