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