]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultNodeMapper.java
JCR UI can run processes
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / dao / TreeTestResultNodeMapper.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.jcr.dao;
18
19 import java.util.ArrayList;
20 import java.util.Calendar;
21 import java.util.GregorianCalendar;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.SortedMap;
25 import java.util.TreeMap;
26 import java.util.Vector;
27
28 import javax.jcr.Node;
29 import javax.jcr.NodeIterator;
30 import javax.jcr.Property;
31 import javax.jcr.PropertyIterator;
32 import javax.jcr.RepositoryException;
33 import javax.jcr.query.Query;
34 import javax.jcr.query.QueryManager;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.argeo.jcr.spring.BeanNodeMapper;
39 import org.argeo.slc.core.attachment.SimpleAttachment;
40 import org.argeo.slc.core.structure.SimpleSElement;
41 import org.argeo.slc.core.structure.tree.TreeSPath;
42 import org.argeo.slc.core.test.tree.PartSubList;
43 import org.argeo.slc.core.test.tree.TreeTestResult;
44 import org.argeo.slc.structure.StructureElement;
45 import org.argeo.slc.test.TestResultPart;
46 import org.springframework.beans.BeanWrapper;
47
48 public class TreeTestResultNodeMapper extends BeanNodeMapper {
49 private final static Log log = LogFactory
50 .getLog(TreeTestResultNodeMapper.class);
51
52 /**
53 * Transforms a TreeTestResult to the specified jcr Node in order to persist
54 * it.
55 *
56 * @param beanWrapper
57 * @param node
58 * @throws RepositoryException
59 */
60 protected void beanToNode(BeanWrapper beanWrapper, Node node)
61 throws RepositoryException {
62
63 if (log.isTraceEnabled())
64 log.debug("Map TreeTestResult to node " + node.getPath());
65
66 // We know we are mapping a TreeTestResult so we cast it
67 TreeTestResult ttr = (TreeTestResult) beanWrapper.getWrappedInstance();
68
69 // First we persist the class
70 node.setProperty(getClassProperty(), ttr.getClass().getName());
71
72 // Then we persist String uuid, Date closeDate
73 node.setProperty("uuid", ttr.getUuid());
74 if (ttr.getCloseDate() != null) {
75 Calendar cal = new GregorianCalendar();
76 cal.setTime(ttr.getCloseDate());
77 node.setProperty("closeDate", cal);
78 }
79
80 Node childNode;
81
82 // Elements & resultParts are merged, we use treeSPath to build the tree
83 // Element label is stored as a property of the vertice
84 // ResultParts are stored as childNode named resultpart[xx].
85
86 SortedMap<TreeSPath, StructureElement> elements = ttr.getElements();
87
88 for (TreeSPath key : elements.keySet()) {
89 String relPath = key.getAsUniqueString();
90 // We remove the first separator
91 relPath = relPath.substring(1);
92
93 // check if already exists.
94 if (!node.hasNode(relPath)) {
95 // TODO Factorize that
96 Node tmpNode = node;
97 String[] pathes = relPath.split("/");
98 for (int i = 0; i < pathes.length; i++) {
99 if (tmpNode.hasNode(pathes[i]))
100 tmpNode = tmpNode.getNode(pathes[i]);
101 else
102 tmpNode = tmpNode.addNode(pathes[i]);
103 }
104 childNode = tmpNode;
105 } else
106 childNode = node.getNode(relPath);
107
108 childNode.setProperty("label", elements.get(key).getLabel());
109 // We add the tags
110 Map<String, String> tags = elements.get(key).getTags();
111 for (String tag : tags.keySet()) {
112 NodeIterator tagIt = childNode.getNodes("tag");
113 Node tagNode = null;
114 while (tagIt.hasNext()) {
115 Node n = tagIt.nextNode();
116 if (n.getProperty("name").getString().equals(tag)) {
117 tagNode = n;
118 }
119 }
120
121 if (tagNode == null) {
122 tagNode = childNode.addNode("tag");
123 tagNode.setProperty("name", tag);
124 }
125
126 tagNode.setProperty("value", tags.get(tag));
127
128 // remove forbidden characters
129 // String cleanTag = JcrUtils.removeForbiddenCharacters(tag);
130 // if (!cleanTag.equals(tag))
131 // log.warn("Tag '" + tag + "' persisted as '" + cleanTag
132 // + "'");
133 // childNode.setProperty(cleanTag, tags.get(tag));
134 }
135
136 // We set the class in order to be able to retrieve
137 childNode.setProperty(getClassProperty(), StructureElement.class
138 .getName());
139 }
140
141 SortedMap<TreeSPath, PartSubList> resultParts = ttr.getResultParts();
142
143 for (TreeSPath key : resultParts.keySet()) {
144 String relPath = key.getAsUniqueString();
145
146 // we get rid of the '/' that begins every TreeSPath Unique string
147 // and add the partsublist level
148 relPath = relPath.substring(1) + "/partsublist";
149
150 // check if already exists.
151 if (!node.hasNode(relPath)) {
152 // TODO Factorize that
153 Node tmpNode = node;
154 String[] pathes = relPath.split("/");
155 for (int i = 0; i < pathes.length; i++) {
156 if (tmpNode.hasNode(pathes[i]))
157 tmpNode = tmpNode.getNode(pathes[i]);
158 else
159 tmpNode = tmpNode.addNode(pathes[i]);
160 }
161 childNode = tmpNode;
162 //log.debug("Node created " + childNode.getPath());
163 } else {
164 childNode = node.getNode(relPath);
165 //log.debug("Node already existing " + childNode.getPath());
166 }
167
168 List<TestResultPart> list = resultParts.get(key).getParts();
169
170 Node listNode;
171 int i;
172 for (i = 0; i < list.size(); i++) {
173 // TestResultPart trp = list.get(i);
174 // FIXME : ResultParts are systematicaly added.
175 // There no check to see if already exists.
176 listNode = childNode.addNode("resultpart");
177 update(listNode, list.get(i));
178 }
179 }
180
181 // TODO : store files in the graph
182 // As for now, we only store on a vertice called after the name value of
183 // the SimpleAttachment Object
184 // and uuid & contentType as property
185
186 List<SimpleAttachment> attachments = ttr.getAttachments();
187 if (attachments.size() != 0) {
188 if (node.hasNode("attachments"))
189 childNode = node.getNode("attachments");
190 else {
191 if (getPrimaryNodeType() != null)
192 childNode = node.addNode("attachments",
193 getPrimaryNodeType());
194 else
195 childNode = node.addNode("attachments");
196 }
197 Node attachNode;
198 for (int i = 0; i < attachments.size(); i++) {
199 attachNode = childNode.addNode(attachments.get(i).getName());
200 attachNode.setProperty("uuid", attachments.get(i).getUuid());
201 attachNode.setProperty("contentType", attachments.get(i)
202 .getContentType());
203 }
204 }
205
206 // attributes are stored as properties of the testResult node
207 for (String key : ttr.getAttributes().keySet()) {
208 String mapValue = ttr.getAttributes().get(key);
209 node.setProperty(key, mapValue);
210 }
211
212 }
213
214 /**
215 * Transforms a node into a TreeTestResult Instance
216 */
217 @SuppressWarnings("unchecked")
218 protected Object nodeToBean(Node node) throws RepositoryException {
219 // method variables
220 String uuid;
221 String clssName = node.getProperty(getClassProperty()).getString();
222 QueryManager qm = node.getSession().getWorkspace().getQueryManager();
223 Query query;
224
225 if (log.isTraceEnabled())
226 log.debug("Map node " + node.getPath() + " to bean " + clssName);
227
228 // It's a very specific implementation,
229 // We don't need to use a bean wrapper.
230 TreeTestResult ttr = new TreeTestResult();
231
232 // RESULTPART PARAMETERS
233 uuid = node.getProperty("uuid").getString();
234 ttr.setUuid(uuid);
235
236 if (node.hasProperty("closeDate")) {
237 ttr.setCloseDate((node.getProperty("closeDate").getDate())
238 .getTime());
239 }
240
241 // ATTRIBUTES
242 SortedMap attributes = new TreeMap<String, String>();
243 PropertyIterator propIt = node.getProperties();
244 props: while (propIt.hasNext()) {
245 Property prop = propIt.nextProperty();
246
247 // TODO Define a rule to generalize it (Namespace ??)
248 // Get rid of specific case. mainly uuid
249 if ("uuid".equals(prop.getName())
250 || prop.getName().equals(getClassProperty())
251 || prop.getName().startsWith("jcr")) {
252 continue props;
253 }
254
255 // else it's an attribute, we retrieve it
256 attributes.put(prop.getName(), prop.getString());
257 }
258 ttr.setAttributes(attributes);
259
260 // ATTACHMENTS
261 NodeIterator ni;
262 if (node.hasNode("attachments")) {
263 List<SimpleAttachment> attachments = new ArrayList<SimpleAttachment>();
264
265 ni = node.getNode("attachments").getNodes();
266 while (ni.hasNext()) {
267 Node curNode = ni.nextNode();
268 attachments.add(new SimpleAttachment(curNode
269 .getProperty("uuid").getString(), curNode.getName(),
270 curNode.getProperty("contentType").getString()));
271 }
272 ttr.setAttachments(attachments);
273 }
274
275 // STRUCTURED ELEMENTS
276
277 String basePath = node.getPath();
278 SortedMap<TreeSPath, PartSubList> resultParts = new TreeMap<TreeSPath, PartSubList>();
279 SortedMap<TreeSPath, StructureElement> elements = new TreeMap<TreeSPath, StructureElement>();
280
281 // We have to add the uuid of the current node to be sure that we are in
282 // its sub tree
283 String queryString = "//testresult[@uuid='" + uuid + "']";
284
285 // Business part of the current query
286 queryString = queryString + "//*[@" + getClassProperty() + "='"
287 + StructureElement.class.getName() + "']";
288
289 query = qm.createQuery(queryString, Query.XPATH);
290 ni = query.execute().getNodes();
291
292 while (ni.hasNext()) {
293 Node curNode = ni.nextNode();
294 String curPath = curNode.getPath().substring(basePath.length());
295 TreeSPath tsp = new TreeSPath();
296
297 // We must add the "/" at the begining of the jcr path to have a
298 // TreeSPath string
299 tsp.setAsUniqueString(tsp.getSeparator() + curPath);
300
301 SimpleSElement se = new SimpleSElement();
302 se.setLabel(curNode.getProperty("label").getString());
303
304 Map<String, String> tagMap = new TreeMap<String, String>();
305 NodeIterator tagIt = node.getNodes("tag");
306 while (tagIt.hasNext()) {
307 Node tagNode = tagIt.nextNode();
308 tagMap.put(tagNode.getProperty("name").getString(), tagNode
309 .getProperty("value").getString());
310
311 }
312 // PropertyIterator tagIt = curNode.getProperties();
313 // tags: while (tagIt.hasNext()) {
314 // Property prop = tagIt.nextProperty();
315 // //log.debug("Handling property named : " + prop.getName());
316 //
317 // // TODO Define a rule to generalize it
318 // // Specific case. mainly uuid
319 // if ("uuid".equals(prop.getName())
320 // || prop.getName().equals(getClassProperty())
321 // || prop.getName().startsWith("jcr")) {
322 // continue tags;
323 // }
324 //
325 // // else it's an attribute, we retrieve it
326 // tagMap.put(prop.getName(), prop.getString());
327 // }
328
329 se.setTags(tagMap);
330 elements.put(tsp, se);
331 }
332 //log.debug("We added " + elements.size() + " elements");
333
334 ttr.setElements(elements);
335
336 // RESULTPARTS
337
338 // We have to had the uuid of the current node to be sure that we are in
339 // its sub tree
340 queryString = "//testresult[@uuid='" + uuid + "']";
341
342 // Business part of the current query
343 queryString = queryString + "//partsublist";
344 query = qm.createQuery(queryString, Query.XPATH);
345 ni = query.execute().getNodes();
346 while (ni.hasNext()) {
347 Node curNode = ni.nextNode();
348 String curPath = curNode.getParent().getPath().substring(
349 basePath.length());
350
351 TreeSPath tsp = new TreeSPath();
352 // We must add the "/" at the begining of the jcr path to have a
353 // TreeSPath string
354 tsp.setAsUniqueString(tsp.getSeparator() + curPath);
355
356 NodeIterator ni2 = curNode.getNodes("resultpart");
357 List<TestResultPart> parts = new Vector<TestResultPart>();
358 while (ni2.hasNext()) {
359 parts.add((TestResultPart) load(ni2.nextNode()));
360 }
361 PartSubList psl = new PartSubList();
362 psl.setParts(parts);
363 resultParts.put(tsp, psl);
364 }
365
366 ttr.setResultParts(resultParts);
367
368 return ttr;
369 }
370 }