2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.jcr
.spring
;
18 import java
.beans
.PropertyDescriptor
;
19 import java
.io
.InputStream
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Calendar
;
22 import java
.util
.Date
;
23 import java
.util
.GregorianCalendar
;
24 import java
.util
.HashMap
;
25 import java
.util
.List
;
27 import java
.util
.StringTokenizer
;
29 import javax
.jcr
.Binary
;
30 import javax
.jcr
.ItemNotFoundException
;
31 import javax
.jcr
.Node
;
32 import javax
.jcr
.NodeIterator
;
33 import javax
.jcr
.Property
;
34 import javax
.jcr
.PropertyIterator
;
35 import javax
.jcr
.PropertyType
;
36 import javax
.jcr
.RepositoryException
;
37 import javax
.jcr
.Session
;
38 import javax
.jcr
.Value
;
39 import javax
.jcr
.ValueFactory
;
41 import org
.apache
.commons
.logging
.Log
;
42 import org
.apache
.commons
.logging
.LogFactory
;
43 import org
.argeo
.ArgeoException
;
44 import org
.argeo
.jcr
.JcrUtils
;
45 import org
.argeo
.jcr
.NodeMapper
;
46 import org
.argeo
.jcr
.NodeMapperProvider
;
47 import org
.springframework
.beans
.BeanWrapper
;
48 import org
.springframework
.beans
.BeanWrapperImpl
;
51 public class BeanNodeMapper
implements NodeMapper
{
52 private final static Log log
= LogFactory
.getLog(BeanNodeMapper
.class);
54 private final static String NODE_VALUE
= "value";
56 // private String keyNode = "bean:key";
57 private String uuidProperty
= "uuid";
58 private String classProperty
= "class";
60 private Boolean versioning
= false;
61 private Boolean strictUuidReference
= false;
63 // TODO define a primaryNodeType Strategy
64 private String primaryNodeType
= null;
66 private ClassLoader classLoader
= getClass().getClassLoader();
68 private NodeMapperProvider nodeMapperProvider
;
71 * exposed method to retrieve a bean from a node
73 public Object
load(Node node
) {
75 if (nodeMapperProvider
!= null) {
76 NodeMapper nodeMapper
= nodeMapperProvider
.findNodeMapper(node
);
77 if (nodeMapper
!= this) {
78 return nodeMapper
.load(node
);
81 return nodeToBean(node
);
82 } catch (RepositoryException e
) {
83 throw new ArgeoException("Cannot load object from node " + node
, e
);
87 /** Update an existing node with an object */
88 public void update(Node node
, Object obj
) {
90 if (nodeMapperProvider
!= null) {
92 NodeMapper nodeMapper
= nodeMapperProvider
.findNodeMapper(node
);
93 if (nodeMapper
!= this) {
94 nodeMapper
.update(node
, obj
);
96 beanToNode(createBeanWrapper(obj
), node
);
98 beanToNode(createBeanWrapper(obj
), node
);
99 } catch (RepositoryException e
) {
100 throw new ArgeoException("Cannot update node " + node
+ " with "
106 * if no storage path is given; we use canonical path
108 * @see this.storagePath()
110 public Node
save(Session session
, Object obj
) {
111 return save(session
, storagePath(obj
), obj
);
115 * Create a new node to store an object. If the parentNode doesn't exist, it
118 * the primaryNodeType may be initialized before
120 public Node
save(Session session
, String path
, Object obj
) {
123 String parentPath
= JcrUtils
.parentPath(path
);
124 // find or create parent node
126 if (session
.itemExists(path
))
127 parentNode
= (Node
) session
.getItem(parentPath
);
129 parentNode
= JcrUtils
.mkdirs(session
, parentPath
, null, null,
134 if (primaryNodeType
!= null)
135 node
= parentNode
.addNode(JcrUtils
.lastPathElement(path
),
138 node
= parentNode
.addNode(JcrUtils
.lastPathElement(path
));
140 // Check specific cases
141 if (nodeMapperProvider
!= null) {
142 NodeMapper nodeMapper
= nodeMapperProvider
.findNodeMapper(node
);
143 if (nodeMapper
!= this) {
144 nodeMapper
.update(node
, obj
);
150 } catch (ArgeoException e
) {
152 } catch (Exception e
) {
153 throw new ArgeoException("Cannot save or update " + obj
+ " under "
159 * Parse the FQN of a class to string with '/' delimiters Prefix the
160 * returned string with "/objects/"
162 public String
storagePath(Object obj
) {
163 String clss
= obj
.getClass().getName();
164 StringBuffer buf
= new StringBuffer("/objects/");
165 StringTokenizer st
= new StringTokenizer(clss
, ".");
166 while (st
.hasMoreTokens()) {
167 buf
.append(st
.nextToken()).append('/');
169 buf
.append(obj
.toString());
170 return buf
.toString();
173 @SuppressWarnings("unchecked")
175 * Transforms a node into an object of the class defined by classProperty Property
177 protected Object
nodeToBean(Node node
) throws RepositoryException
{
178 if (log
.isTraceEnabled())
179 log
.trace("Load " + node
);
182 String clssName
= node
.getProperty(classProperty
).getValue()
185 BeanWrapper beanWrapper
= createBeanWrapper(loadClass(clssName
));
187 // process properties
188 PropertyIterator propIt
= node
.getProperties();
189 props
: while (propIt
.hasNext()) {
190 Property prop
= propIt
.nextProperty();
191 if (!beanWrapper
.isWritableProperty(prop
.getName()))
194 PropertyDescriptor pd
= beanWrapper
.getPropertyDescriptor(prop
196 Class
<?
> propClass
= pd
.getPropertyType();
198 if (log
.isTraceEnabled())
199 log
.trace("Load " + prop
+ ", propClass=" + propClass
200 + ", property descriptor=" + pd
);
203 if (propClass
!= null && List
.class.isAssignableFrom(propClass
)) {
204 List
<Object
> lst
= new ArrayList
<Object
>();
205 Class
<?
> valuesClass
= classFromProperty(prop
);
206 if (valuesClass
!= null)
207 for (Value value
: prop
.getValues()) {
208 lst
.add(asObject(value
, valuesClass
));
213 // Case of other type of property accepted by jcr
214 // Long, Double, String, Binary, Date, Boolean, Name
215 Object value
= asObject(prop
.getValue(), pd
.getPropertyType());
217 beanWrapper
.setPropertyValue(prop
.getName(), value
);
220 // process children nodes
221 NodeIterator nodeIt
= node
.getNodes();
222 nodes
: while (nodeIt
.hasNext()) {
223 Node childNode
= nodeIt
.nextNode();
224 String name
= childNode
.getName();
225 if (!beanWrapper
.isWritableProperty(name
))
228 PropertyDescriptor pd
= beanWrapper
.getPropertyDescriptor(name
);
229 Class
<?
> propClass
= pd
.getPropertyType();
232 if (propClass
!= null && List
.class.isAssignableFrom(propClass
)) {
233 String lstClass
= childNode
.getProperty(classProperty
)
237 lst
= (List
<Object
>) loadClass(lstClass
).newInstance();
238 } catch (Exception e
) {
239 lst
= new ArrayList
<Object
>();
242 if (childNode
.hasNodes()) {
243 // Look for children nodes
244 NodeIterator valuesIt
= childNode
.getNodes();
245 while (valuesIt
.hasNext()) {
246 Node lstValueNode
= valuesIt
.nextNode();
247 Object lstValue
= nodeToBean(lstValueNode
);
251 // look for a property with the same name which will
254 Property childProp
= childNode
.getProperty(childNode
256 Class
<?
> valuesClass
= classFromProperty(childProp
);
257 if (valuesClass
!= null)
258 if (childProp
.getDefinition().isMultiple())
259 for (Value value
: childProp
.getValues()) {
260 lst
.add(asObject(value
, valuesClass
));
263 lst
.add(asObject(childProp
.getValue(),
266 beanWrapper
.setPropertyValue(name
, lst
);
271 if (propClass
!= null && Map
.class.isAssignableFrom(propClass
)) {
272 String mapClass
= childNode
.getProperty(classProperty
)
274 Map
<Object
, Object
> map
;
276 map
= (Map
<Object
, Object
>) loadClass(mapClass
)
278 } catch (Exception e
) {
279 map
= new HashMap
<Object
, Object
>();
283 PropertyIterator keysPropIt
= childNode
.getProperties();
284 keyProps
: while (keysPropIt
.hasNext()) {
285 Property keyProp
= keysPropIt
.nextProperty();
286 // FIXME: use property editor
287 String key
= keyProp
.getName();
288 if (classProperty
.equals(key
))
291 Class
<?
> keyPropClass
= classFromProperty(keyProp
);
292 if (keyPropClass
!= null) {
293 Object mapValue
= asObject(keyProp
.getValue(),
295 map
.put(key
, mapValue
);
300 NodeIterator keysIt
= childNode
.getNodes();
301 while (keysIt
.hasNext()) {
302 Node mapValueNode
= keysIt
.nextNode();
303 // FIXME: use property editor
304 Object key
= mapValueNode
.getName();
306 Object mapValue
= nodeToBean(mapValueNode
);
308 map
.put(key
, mapValue
);
310 beanWrapper
.setPropertyValue(name
, map
);
315 Object value
= nodeToBean(childNode
);
316 beanWrapper
.setPropertyValue(name
, value
);
319 return beanWrapper
.getWrappedInstance();
320 } catch (Exception e
) {
321 throw new ArgeoException("Cannot map node " + node
, e
);
326 * Transforms an object to the specified jcr Node in order to persist it.
330 * @throws RepositoryException
332 protected void beanToNode(BeanWrapper beanWrapper
, Node node
)
333 throws RepositoryException
{
334 properties
: for (PropertyDescriptor pd
: beanWrapper
335 .getPropertyDescriptors()) {
336 String name
= pd
.getName();
337 if (!beanWrapper
.isReadableProperty(name
))
338 continue properties
;// skip
340 Object value
= beanWrapper
.getPropertyValue(name
);
342 // remove values when updating
343 if (node
.hasProperty(name
))
344 node
.setProperty(name
, (Value
) null);
345 if (node
.hasNode(name
))
346 node
.getNode(name
).remove();
351 // if (uuidProperty != null && uuidProperty.equals(name)) {
352 // // node.addMixin(ArgeoJcrConstants.MIX_REFERENCEABLE);
353 // node.setProperty(ArgeoJcrConstants.JCR_UUID, value.toString());
354 // continue properties;
357 if ("class".equals(name
)) {
358 if (classProperty
!= null) {
359 node
.setProperty(classProperty
,
360 ((Class
<?
>) value
).getName());
361 // TODO: store a class hierarchy?
366 // Some bean reference other classes. We must deal with this case
367 if (value
instanceof Class
<?
>) {
368 node
.setProperty(name
, ((Class
<?
>) value
).getName());
372 Value val
= asValue(node
.getSession(), value
);
374 node
.setProperty(name
, val
);
378 if (value
instanceof List
<?
>) {
379 List
<?
> lst
= (List
<?
>) value
;
380 addList(node
, name
, lst
);
384 if (value
instanceof Map
<?
, ?
>) {
385 Map
<?
, ?
> map
= (Map
<?
, ?
>) value
;
386 addMap(node
, name
, map
);
390 BeanWrapper child
= createBeanWrapper(value
);
391 // TODO: delegate to another mapper
393 // TODO: deal with references
394 // Node childNode = findChildReference(session, child);
395 // if (childNode != null) {
396 // node.setProperty(name, childNode);
397 // continue properties;
400 // default case (recursive)
401 if (node
.hasNode(name
)) {// update
403 node
.getNode(name
).remove();
405 Node childNode
= node
.addNode(name
);
406 beanToNode(child
, childNode
);
411 * Process specific case of list
416 * @throws RepositoryException
418 protected void addList(Node node
, String name
, List
<?
> lst
)
419 throws RepositoryException
{
420 if (node
.hasNode(name
)) {// update
422 node
.getNode(name
).remove();
425 Node listNode
= node
.addNode(name
);
426 listNode
.setProperty(classProperty
, lst
.getClass().getName());
427 Value
[] values
= new Value
[lst
.size()];
428 boolean atLeastOneSet
= false;
429 for (int i
= 0; i
< lst
.size(); i
++) {
430 Object lstValue
= lst
.get(i
);
431 values
[i
] = asValue(node
.getSession(), lstValue
);
432 if (values
[i
] != null) {
433 atLeastOneSet
= true;
435 Node childNode
= findChildReference(node
.getSession(),
436 createBeanWrapper(lstValue
));
437 if (childNode
!= null) {
438 values
[i
] = node
.getSession().getValueFactory()
439 .createValue(childNode
);
440 atLeastOneSet
= true;
445 // will be either properties or nodes, not both
446 if (!atLeastOneSet
&& lst
.size() != 0) {
447 for (Object lstValue
: lst
) {
448 Node childNode
= listNode
.addNode(NODE_VALUE
);
449 beanToNode(createBeanWrapper(lstValue
), childNode
);
452 listNode
.setProperty(name
, values
);
457 * Process specific case of maps.
462 * @throws RepositoryException
464 protected void addMap(Node node
, String name
, Map
<?
, ?
> map
)
465 throws RepositoryException
{
466 if (node
.hasNode(name
)) {// update
468 node
.getNode(name
).remove();
471 Node mapNode
= node
.addNode(name
);
472 mapNode
.setProperty(classProperty
, map
.getClass().getName());
473 for (Object key
: map
.keySet()) {
474 Object mapValue
= map
.get(key
);
475 // PropertyEditor pe = beanWrapper.findCustomEditor(key.getClass(),
479 if (key
instanceof CharSequence
)
480 keyStr
= key
.toString();
482 throw new ArgeoException(
483 "Cannot find property editor for class "
487 // keyStr = pe.getAsText();
489 // TODO: check string format
491 Value mapVal
= asValue(node
.getSession(), mapValue
);
493 mapNode
.setProperty(keyStr
, mapVal
);
495 Node entryNode
= mapNode
.addNode(keyStr
);
496 beanToNode(createBeanWrapper(mapValue
), entryNode
);
503 protected BeanWrapper
createBeanWrapper(Object obj
) {
504 return new BeanWrapperImpl(obj
);
507 protected BeanWrapper
createBeanWrapper(Class
<?
> clss
) {
508 return new BeanWrapperImpl(clss
);
511 /** Returns null if value cannot be found */
512 protected Value
asValue(Session session
, Object value
)
513 throws RepositoryException
{
514 ValueFactory valueFactory
= session
.getValueFactory();
515 if (value
instanceof Integer
)
516 return valueFactory
.createValue((Integer
) value
);
517 else if (value
instanceof Long
)
518 return valueFactory
.createValue((Long
) value
);
519 else if (value
instanceof Float
)
520 return valueFactory
.createValue((Float
) value
);
521 else if (value
instanceof Double
)
522 return valueFactory
.createValue((Double
) value
);
523 else if (value
instanceof Boolean
)
524 return valueFactory
.createValue((Boolean
) value
);
525 else if (value
instanceof Calendar
)
526 return valueFactory
.createValue((Calendar
) value
);
527 else if (value
instanceof Date
) {
528 Calendar cal
= new GregorianCalendar();
529 cal
.setTime((Date
) value
);
530 return valueFactory
.createValue(cal
);
531 } else if (value
instanceof CharSequence
)
532 return valueFactory
.createValue(value
.toString());
533 else if (value
instanceof InputStream
) {
534 Binary binary
= session
.getValueFactory().createBinary(
535 (InputStream
) value
);
536 return valueFactory
.createValue(binary
);
541 protected Class
<?
> classFromProperty(Property property
)
542 throws RepositoryException
{
543 switch (property
.getType()) {
544 case PropertyType
.LONG
:
546 case PropertyType
.DOUBLE
:
548 case PropertyType
.STRING
:
550 case PropertyType
.BOOLEAN
:
551 return Boolean
.class;
552 case PropertyType
.DATE
:
553 return Calendar
.class;
554 case PropertyType
.NAME
:
557 throw new ArgeoException("Cannot find class for property "
558 + property
+ ", type="
559 + PropertyType
.nameFromValue(property
.getType()));
563 protected Object
asObject(Value value
, Class
<?
> propClass
)
564 throws RepositoryException
{
565 if (propClass
.equals(Integer
.class))
566 return (int) value
.getLong();
567 else if (propClass
.equals(Long
.class))
568 return value
.getLong();
569 else if (propClass
.equals(Float
.class))
570 return (float) value
.getDouble();
571 else if (propClass
.equals(Double
.class))
572 return value
.getDouble();
573 else if (propClass
.equals(Boolean
.class))
574 return value
.getBoolean();
575 else if (CharSequence
.class.isAssignableFrom(propClass
))
576 return value
.getString();
577 else if (InputStream
.class.isAssignableFrom(propClass
))
578 return value
.getBinary().getStream();
579 else if (Calendar
.class.isAssignableFrom(propClass
))
580 return value
.getDate();
581 else if (Date
.class.isAssignableFrom(propClass
))
582 return value
.getDate().getTime();
587 protected Node
findChildReference(Session session
, BeanWrapper child
)
588 throws RepositoryException
{
589 if (child
.isReadableProperty(uuidProperty
)) {
590 String childUuid
= child
.getPropertyValue(uuidProperty
).toString();
592 return session
.getNodeByIdentifier(childUuid
);
593 } catch (ItemNotFoundException e
) {
594 if (strictUuidReference
)
595 throw new ArgeoException("No node found with uuid "
602 protected Class
<?
> loadClass(String name
) {
603 // log.debug("Class loader: " + classLoader);
605 return classLoader
.loadClass(name
);
606 } catch (ClassNotFoundException e
) {
607 throw new ArgeoException("Cannot load class " + name
, e
);
611 protected String
propertyName(String name
) {
615 public void setVersioning(Boolean versioning
) {
616 this.versioning
= versioning
;
619 public void setUuidProperty(String uuidProperty
) {
620 this.uuidProperty
= uuidProperty
;
623 public void setClassProperty(String classProperty
) {
624 this.classProperty
= classProperty
;
627 public void setStrictUuidReference(Boolean strictUuidReference
) {
628 this.strictUuidReference
= strictUuidReference
;
631 public void setPrimaryNodeType(String primaryNodeType
) {
632 this.primaryNodeType
= primaryNodeType
;
635 public void setClassLoader(ClassLoader classLoader
) {
636 this.classLoader
= classLoader
;
639 public void setNodeMapperProvider(NodeMapperProvider nodeMapperProvider
) {
640 this.nodeMapperProvider
= nodeMapperProvider
;
643 public String
getPrimaryNodeType() {
644 return this.primaryNodeType
;
647 public String
getClassProperty() {
648 return this.classProperty
;