]>
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
1 package org
.argeo
.slc
.jcr
.dao
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Calendar
;
5 import java
.util
.GregorianCalendar
;
8 import java
.util
.SortedMap
;
9 import java
.util
.TreeMap
;
10 import java
.util
.Vector
;
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
;
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
;
33 public class TreeTestResultNodeMapper
extends BeanNodeMapper
{
34 private final static Log log
= LogFactory
35 .getLog(TreeTestResultNodeMapper
.class);
38 * Transforms a TreeTestResult to the specified jcr Node in order to persist
43 * @throws RepositoryException
45 protected void beanToNode(BeanWrapper beanWrapper
, Node node
)
46 throws RepositoryException
{
48 if (log
.isTraceEnabled())
49 log
.debug("Map TreeTestResult to node " + node
.getPath());
51 // We know we are mapping a TreeTestResult so we cast it
52 TreeTestResult ttr
= (TreeTestResult
) beanWrapper
.getWrappedInstance();
54 // First we persist the class
55 node
.setProperty(getClassProperty(), ttr
.getClass().getName());
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
);
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].
71 SortedMap
<TreeSPath
, StructureElement
> elements
= ttr
.getElements();
73 for (TreeSPath key
: elements
.keySet()) {
74 String relPath
= key
.getAsUniqueString();
75 // We remove the first separator
76 relPath
= relPath
.substring(1);
78 // check if already exists.
79 if (!node
.hasNode(relPath
)) {
80 // TODO Factorize that
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
]);
87 tmpNode
= tmpNode
.addNode(pathes
[i
]);
91 childNode
= node
.getNode(relPath
);
93 childNode
.setProperty("label", elements
.get(key
).getLabel());
95 Map
<String
, String
> tags
= elements
.get(key
).getTags();
96 for (String tag
: tags
.keySet()) {
97 NodeIterator tagIt
= childNode
.getNodes("tag");
99 while (tagIt
.hasNext()) {
100 Node n
= tagIt
.nextNode();
101 if (n
.getProperty("name").getString().equals(tag
)) {
106 if (tagNode
== null) {
107 tagNode
= childNode
.addNode("tag");
108 tagNode
.setProperty("name", tag
);
111 tagNode
.setProperty("value", tags
.get(tag
));
113 // remove forbidden characters
114 // String cleanTag = JcrUtils.removeForbiddenCharacters(tag);
115 // if (!cleanTag.equals(tag))
116 // log.warn("Tag '" + tag + "' persisted as '" + cleanTag
118 // childNode.setProperty(cleanTag, tags.get(tag));
121 // We set the class in order to be able to retrieve
122 childNode
.setProperty(getClassProperty(), StructureElement
.class
126 SortedMap
<TreeSPath
, PartSubList
> resultParts
= ttr
.getResultParts();
128 for (TreeSPath key
: resultParts
.keySet()) {
129 String relPath
= key
.getAsUniqueString();
131 // we get rid of the '/' that begins every TreeSPath Unique string
132 // and add the partsublist level
133 relPath
= relPath
.substring(1) + "/partsublist";
135 // check if already exists.
136 if (!node
.hasNode(relPath
)) {
137 // TODO Factorize that
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
]);
144 tmpNode
= tmpNode
.addNode(pathes
[i
]);
147 log
.debug("Node created " + childNode
.getPath());
149 childNode
= node
.getNode(relPath
);
150 log
.debug("Node already existing " + childNode
.getPath());
153 List
<TestResultPart
> list
= resultParts
.get(key
).getParts();
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
));
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
171 List
<SimpleAttachment
> attachments
= ttr
.getAttachments();
172 if (attachments
.size() != 0) {
173 if (node
.hasNode("attachments"))
174 childNode
= node
.getNode("attachments");
176 if (getPrimaryNodeType() != null)
177 childNode
= node
.addNode("attachments",
178 getPrimaryNodeType());
180 childNode
= node
.addNode("attachments");
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
)
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
);
200 * Transforms a node into a TreeTestResult Instance
202 @SuppressWarnings("unchecked")
203 protected Object
nodeToBean(Node node
) throws RepositoryException
{
206 String clssName
= node
.getProperty(getClassProperty()).getString();
207 QueryManager qm
= node
.getSession().getWorkspace().getQueryManager();
210 if (log
.isTraceEnabled())
211 log
.debug("Map node " + node
.getPath() + " to bean " + clssName
);
213 // It's a very specific implementation,
214 // We don't need to use a bean wrapper.
215 TreeTestResult ttr
= new TreeTestResult();
217 // RESULTPART PARAMETERS
218 uuid
= node
.getProperty("uuid").getString();
221 if (node
.hasProperty("closeDate")) {
222 ttr
.setCloseDate((node
.getProperty("closeDate").getDate())
227 Map attributes
= new TreeMap
<String
, String
>();
228 PropertyIterator propIt
= node
.getProperties();
229 props
: while (propIt
.hasNext()) {
230 Property prop
= propIt
.nextProperty();
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")) {
240 // else it's an attribute, we retrieve it
241 attributes
.put(prop
.getName(), prop
.getString());
243 ttr
.setAttributes(attributes
);
247 if (node
.hasNode("attachments")) {
248 List
<SimpleAttachment
> attachments
= new ArrayList
<SimpleAttachment
>();
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()));
257 ttr
.setAttachments(attachments
);
260 // STRUCTURED ELEMENTS
262 String basePath
= node
.getPath();
263 SortedMap
<TreeSPath
, PartSubList
> resultParts
= new TreeMap
<TreeSPath
, PartSubList
>();
264 SortedMap
<TreeSPath
, StructureElement
> elements
= new TreeMap
<TreeSPath
, StructureElement
>();
266 // We have to add the uuid of the current node to be sure that we are in
268 String queryString
= "//testresult[@uuid='" + uuid
+ "']";
270 // Business part of the current query
271 queryString
= queryString
+ "//*[@" + getClassProperty() + "='"
272 + StructureElement
.class.getName() + "']";
274 query
= qm
.createQuery(queryString
, Query
.XPATH
);
275 ni
= query
.execute().getNodes();
277 while (ni
.hasNext()) {
278 Node curNode
= ni
.nextNode();
279 String curPath
= curNode
.getPath().substring(basePath
.length());
280 TreeSPath tsp
= new TreeSPath();
282 // We must add the "/" at the begining of the jcr path to have a
284 tsp
.setAsUniqueString(tsp
.getSeparator() + curPath
);
286 SimpleSElement se
= new SimpleSElement();
287 se
.setLabel(curNode
.getProperty("label").getString());
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());
297 // PropertyIterator tagIt = curNode.getProperties();
298 // tags: while (tagIt.hasNext()) {
299 // Property prop = tagIt.nextProperty();
300 // //log.debug("Handling property named : " + prop.getName());
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")) {
310 // // else it's an attribute, we retrieve it
311 // tagMap.put(prop.getName(), prop.getString());
315 elements
.put(tsp
, se
);
317 log
.debug("We added " + elements
.size() + " elements");
319 ttr
.setElements(elements
);
323 // We have to had the uuid of the current node to be sure that we are in
325 queryString
= "//testresult[@uuid='" + uuid
+ "']";
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(
336 TreeSPath tsp
= new TreeSPath();
337 // We must add the "/" at the begining of the jcr path to have a
339 tsp
.setAsUniqueString(tsp
.getSeparator() + curPath
);
341 NodeIterator ni2
= curNode
.getNodes("resultpart");
342 List
<TestResultPart
> parts
= new Vector
<TestResultPart
>();
343 while (ni2
.hasNext()) {
344 parts
.add((TestResultPart
) load(ni2
.nextNode()));
346 PartSubList psl
= new PartSubList();
348 resultParts
.put(tsp
, psl
);
351 ttr
.setResultParts(resultParts
);