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