]>
git.argeo.org Git - gpl/argeo-slc.git/blob - dao/TreeTestResultNodeMapper.java
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.jcr
.dao
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Calendar
;
21 import java
.util
.GregorianCalendar
;
22 import java
.util
.List
;
24 import java
.util
.SortedMap
;
25 import java
.util
.TreeMap
;
26 import java
.util
.Vector
;
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
;
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
;
48 public class TreeTestResultNodeMapper
extends BeanNodeMapper
{
49 private final static Log log
= LogFactory
50 .getLog(TreeTestResultNodeMapper
.class);
53 * Transforms a TreeTestResult to the specified jcr Node in order to persist
58 * @throws RepositoryException
60 protected void beanToNode(BeanWrapper beanWrapper
, Node node
)
61 throws RepositoryException
{
63 if (log
.isTraceEnabled())
64 log
.debug("Map TreeTestResult to node " + node
.getPath());
66 // We know we are mapping a TreeTestResult so we cast it
67 TreeTestResult ttr
= (TreeTestResult
) beanWrapper
.getWrappedInstance();
69 // First we persist the class
70 node
.setProperty(getClassProperty(), ttr
.getClass().getName());
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
);
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].
86 SortedMap
<TreeSPath
, StructureElement
> elements
= ttr
.getElements();
88 for (TreeSPath key
: elements
.keySet()) {
89 String relPath
= key
.getAsUniqueString();
90 // We remove the first separator
91 relPath
= relPath
.substring(1);
93 // check if already exists.
94 if (!node
.hasNode(relPath
)) {
95 // TODO Factorize that
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
]);
102 tmpNode
= tmpNode
.addNode(pathes
[i
]);
106 childNode
= node
.getNode(relPath
);
108 childNode
.setProperty("label", elements
.get(key
).getLabel());
110 Map
<String
, String
> tags
= elements
.get(key
).getTags();
111 for (String tag
: tags
.keySet()) {
112 NodeIterator tagIt
= childNode
.getNodes("tag");
114 while (tagIt
.hasNext()) {
115 Node n
= tagIt
.nextNode();
116 if (n
.getProperty("name").getString().equals(tag
)) {
121 if (tagNode
== null) {
122 tagNode
= childNode
.addNode("tag");
123 tagNode
.setProperty("name", tag
);
126 tagNode
.setProperty("value", tags
.get(tag
));
128 // remove forbidden characters
129 // String cleanTag = JcrUtils.removeForbiddenCharacters(tag);
130 // if (!cleanTag.equals(tag))
131 // log.warn("Tag '" + tag + "' persisted as '" + cleanTag
133 // childNode.setProperty(cleanTag, tags.get(tag));
136 // We set the class in order to be able to retrieve
137 childNode
.setProperty(getClassProperty(), StructureElement
.class
141 SortedMap
<TreeSPath
, PartSubList
> resultParts
= ttr
.getResultParts();
143 for (TreeSPath key
: resultParts
.keySet()) {
144 String relPath
= key
.getAsUniqueString();
146 // we get rid of the '/' that begins every TreeSPath Unique string
147 // and add the partsublist level
148 relPath
= relPath
.substring(1) + "/partsublist";
150 // check if already exists.
151 if (!node
.hasNode(relPath
)) {
152 // TODO Factorize that
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
]);
159 tmpNode
= tmpNode
.addNode(pathes
[i
]);
162 //log.debug("Node created " + childNode.getPath());
164 childNode
= node
.getNode(relPath
);
165 //log.debug("Node already existing " + childNode.getPath());
168 List
<TestResultPart
> list
= resultParts
.get(key
).getParts();
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
));
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
186 List
<SimpleAttachment
> attachments
= ttr
.getAttachments();
187 if (attachments
.size() != 0) {
188 if (node
.hasNode("attachments"))
189 childNode
= node
.getNode("attachments");
191 if (getPrimaryNodeType() != null)
192 childNode
= node
.addNode("attachments",
193 getPrimaryNodeType());
195 childNode
= node
.addNode("attachments");
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
)
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
);
215 * Transforms a node into a TreeTestResult Instance
217 @SuppressWarnings("unchecked")
218 protected Object
nodeToBean(Node node
) throws RepositoryException
{
221 String clssName
= node
.getProperty(getClassProperty()).getString();
222 QueryManager qm
= node
.getSession().getWorkspace().getQueryManager();
225 if (log
.isTraceEnabled())
226 log
.debug("Map node " + node
.getPath() + " to bean " + clssName
);
228 // It's a very specific implementation,
229 // We don't need to use a bean wrapper.
230 TreeTestResult ttr
= new TreeTestResult();
232 // RESULTPART PARAMETERS
233 uuid
= node
.getProperty("uuid").getString();
236 if (node
.hasProperty("closeDate")) {
237 ttr
.setCloseDate((node
.getProperty("closeDate").getDate())
242 SortedMap attributes
= new TreeMap
<String
, String
>();
243 PropertyIterator propIt
= node
.getProperties();
244 props
: while (propIt
.hasNext()) {
245 Property prop
= propIt
.nextProperty();
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")) {
255 // else it's an attribute, we retrieve it
256 attributes
.put(prop
.getName(), prop
.getString());
258 ttr
.setAttributes(attributes
);
262 if (node
.hasNode("attachments")) {
263 List
<SimpleAttachment
> attachments
= new ArrayList
<SimpleAttachment
>();
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()));
272 ttr
.setAttachments(attachments
);
275 // STRUCTURED ELEMENTS
277 String basePath
= node
.getPath();
278 SortedMap
<TreeSPath
, PartSubList
> resultParts
= new TreeMap
<TreeSPath
, PartSubList
>();
279 SortedMap
<TreeSPath
, StructureElement
> elements
= new TreeMap
<TreeSPath
, StructureElement
>();
281 // We have to add the uuid of the current node to be sure that we are in
283 String queryString
= "//testresult[@uuid='" + uuid
+ "']";
285 // Business part of the current query
286 queryString
= queryString
+ "//*[@" + getClassProperty() + "='"
287 + StructureElement
.class.getName() + "']";
289 query
= qm
.createQuery(queryString
, Query
.XPATH
);
290 ni
= query
.execute().getNodes();
292 while (ni
.hasNext()) {
293 Node curNode
= ni
.nextNode();
294 String curPath
= curNode
.getPath().substring(basePath
.length());
295 TreeSPath tsp
= new TreeSPath();
297 // We must add the "/" at the begining of the jcr path to have a
299 tsp
.setAsUniqueString(tsp
.getSeparator() + curPath
);
301 SimpleSElement se
= new SimpleSElement();
302 se
.setLabel(curNode
.getProperty("label").getString());
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());
312 // PropertyIterator tagIt = curNode.getProperties();
313 // tags: while (tagIt.hasNext()) {
314 // Property prop = tagIt.nextProperty();
315 // //log.debug("Handling property named : " + prop.getName());
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")) {
325 // // else it's an attribute, we retrieve it
326 // tagMap.put(prop.getName(), prop.getString());
330 elements
.put(tsp
, se
);
332 //log.debug("We added " + elements.size() + " elements");
334 ttr
.setElements(elements
);
338 // We have to had the uuid of the current node to be sure that we are in
340 queryString
= "//testresult[@uuid='" + uuid
+ "']";
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(
351 TreeSPath tsp
= new TreeSPath();
352 // We must add the "/" at the begining of the jcr path to have a
354 tsp
.setAsUniqueString(tsp
.getSeparator() + curPath
);
356 NodeIterator ni2
= curNode
.getNodes("resultpart");
357 List
<TestResultPart
> parts
= new Vector
<TestResultPart
>();
358 while (ni2
.hasNext()) {
359 parts
.add((TestResultPart
) load(ni2
.nextNode()));
361 PartSubList psl
= new PartSubList();
363 resultParts
.put(tsp
, psl
);
366 ttr
.setResultParts(resultParts
);