3 import java
.beans
.PropertyDescriptor
;
4 import java
.io
.InputStream
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Calendar
;
8 import java
.util
.GregorianCalendar
;
9 import java
.util
.HashMap
;
10 import java
.util
.List
;
12 import java
.util
.StringTokenizer
;
14 import javax
.jcr
.ItemNotFoundException
;
15 import javax
.jcr
.Node
;
16 import javax
.jcr
.NodeIterator
;
17 import javax
.jcr
.Property
;
18 import javax
.jcr
.PropertyIterator
;
19 import javax
.jcr
.PropertyType
;
20 import javax
.jcr
.RepositoryException
;
21 import javax
.jcr
.Session
;
22 import javax
.jcr
.Value
;
23 import javax
.jcr
.ValueFactory
;
25 import org
.argeo
.ArgeoException
;
26 import org
.springframework
.beans
.BeanWrapper
;
27 import org
.springframework
.beans
.BeanWrapperImpl
;
29 public class BeanNodeMapper
implements NodeMapper
{
30 // private final static Log log = LogFactory.getLog(BeanNodeMapper.class);
32 private final static String NODE_VALUE
= "value";
34 // private String keyNode = "bean:key";
35 private String uuidProperty
= "uuid";
36 private String classProperty
= "class";
38 private Boolean versioning
= false;
39 private Boolean strictUuidReference
= false;
41 // TODO define a primaryNodeType Strategy
42 private String primaryNodeType
= null;
44 private ClassLoader classLoader
= getClass().getClassLoader();
46 private NodeMapperProvider nodeMapperProvider
;
49 * exposed method to retrieve a bean from a node
51 public Object
load(Node node
) {
53 if (nodeMapperProvider
!= null) {
54 NodeMapper nodeMapper
= nodeMapperProvider
.findNodeMapper(node
);
55 if (nodeMapper
!= this) {
56 return nodeMapper
.load(node
);
59 return nodeToBean(node
);
60 } catch (RepositoryException e
) {
61 throw new ArgeoException("Cannot load object from node " + node
, e
);
65 /** Update an existing node with an object */
66 public void update(Node node
, Object obj
) {
68 if (nodeMapperProvider
!= null) {
70 NodeMapper nodeMapper
= nodeMapperProvider
.findNodeMapper(node
);
71 if (nodeMapper
!= this) {
72 nodeMapper
.update(node
, obj
);
74 beanToNode(createBeanWrapper(obj
), node
);
76 beanToNode(createBeanWrapper(obj
), node
);
77 } catch (RepositoryException e
) {
78 throw new ArgeoException("Cannot update node " + node
+ " with "
84 * if no storage path is given; we use canonical path
86 * @see this.storagePath()
88 public Node
save(Session session
, Object obj
) {
89 return save(session
, storagePath(obj
), obj
);
93 * Create a new node to store an object. If the parentNode doesn't exist, it
96 * the primaryNodeType may be initialized before
98 public Node
save(Session session
, String path
, Object obj
) {
101 String parentPath
= JcrUtils
.parentPath(path
);
102 // find or create parent node
104 if (session
.itemExists(path
))
105 parentNode
= (Node
) session
.getItem(parentPath
);
107 parentNode
= JcrUtils
.mkdirs(session
, parentPath
, null,
112 if (primaryNodeType
!= null)
113 node
= parentNode
.addNode(JcrUtils
.lastPathElement(path
),
116 node
= parentNode
.addNode(JcrUtils
.lastPathElement(path
));
118 // Check specific cases
119 if (nodeMapperProvider
!= null) {
120 NodeMapper nodeMapper
= nodeMapperProvider
.findNodeMapper(node
);
121 if (nodeMapper
!= this) {
122 nodeMapper
.update(node
, obj
);
128 } catch (ArgeoException e
) {
130 } catch (Exception e
) {
131 throw new ArgeoException("Cannot save or update " + obj
+ " under "
137 * Parse the FQN of a class to string with '/' delimiters Prefix the
138 * returned string with "/objects/"
140 public String
storagePath(Object obj
) {
141 String clss
= obj
.getClass().getName();
142 StringBuffer buf
= new StringBuffer("/objects/");
143 StringTokenizer st
= new StringTokenizer(clss
, ".");
144 while (st
.hasMoreTokens()) {
145 buf
.append(st
.nextToken()).append('/');
147 buf
.append(obj
.toString());
148 return buf
.toString();
151 @SuppressWarnings("unchecked")
153 * Transforms a node into an object of the class defined by classProperty Property
155 protected Object
nodeToBean(Node node
) throws RepositoryException
{
158 String clssName
= node
.getProperty(classProperty
).getValue()
161 BeanWrapper beanWrapper
= createBeanWrapper(loadClass(clssName
));
163 // process properties
164 PropertyIterator propIt
= node
.getProperties();
165 props
: while (propIt
.hasNext()) {
166 Property prop
= propIt
.nextProperty();
167 if (!beanWrapper
.isWritableProperty(prop
.getName()))
170 PropertyDescriptor pd
= beanWrapper
.getPropertyDescriptor(prop
172 Class propClass
= pd
.getPropertyType();
174 // Process case of List and its derived classes
176 if (propClass
!= null && List
.class.isAssignableFrom(propClass
)) {
177 List
<Object
> lst
= new ArrayList
<Object
>();
178 Class
<?
> valuesClass
= classFromProperty(prop
);
179 if (valuesClass
!= null)
180 for (Value value
: prop
.getValues()) {
181 lst
.add(asObject(value
, valuesClass
));
186 // Case of other type of property accepted by jcr
187 // Long, Double, String, Binary, Date, Boolean, Name
188 Object value
= asObject(prop
.getValue(), pd
.getPropertyType());
190 beanWrapper
.setPropertyValue(prop
.getName(), value
);
193 // process children nodes
194 NodeIterator nodeIt
= node
.getNodes();
195 nodes
: while (nodeIt
.hasNext()) {
196 Node childNode
= nodeIt
.nextNode();
197 String name
= childNode
.getName();
198 if (!beanWrapper
.isWritableProperty(name
))
201 PropertyDescriptor pd
= beanWrapper
.getPropertyDescriptor(name
);
202 Class propClass
= pd
.getPropertyType();
205 if (propClass
!= null && List
.class.isAssignableFrom(propClass
)) {
206 String lstClass
= childNode
.getProperty(classProperty
)
210 lst
= (List
<Object
>) loadClass(lstClass
).newInstance();
211 } catch (Exception e
) {
212 lst
= new ArrayList
<Object
>();
215 NodeIterator valuesIt
= childNode
.getNodes();
216 while (valuesIt
.hasNext()) {
217 Node lstValueNode
= valuesIt
.nextNode();
218 Object lstValue
= nodeToBean(lstValueNode
);
222 beanWrapper
.setPropertyValue(name
, lst
);
227 if (propClass
!= null && Map
.class.isAssignableFrom(propClass
)) {
228 String mapClass
= childNode
.getProperty(classProperty
)
230 Map
<Object
, Object
> map
;
232 map
= (Map
<Object
, Object
>) loadClass(mapClass
)
234 } catch (Exception e
) {
235 map
= new HashMap
<Object
, Object
>();
239 PropertyIterator keysPropIt
= childNode
.getProperties();
240 keyProps
: while (keysPropIt
.hasNext()) {
241 Property keyProp
= keysPropIt
.nextProperty();
242 // FIXME: use property editor
243 String key
= keyProp
.getName();
244 if (classProperty
.equals(key
))
247 Class keyPropClass
= classFromProperty(keyProp
);
248 if (keyPropClass
!= null) {
249 Object mapValue
= asObject(keyProp
.getValue(),
251 map
.put(key
, mapValue
);
256 NodeIterator keysIt
= childNode
.getNodes();
257 while (keysIt
.hasNext()) {
258 Node mapValueNode
= keysIt
.nextNode();
259 // FIXME: use property editor
260 Object key
= mapValueNode
.getName();
262 Object mapValue
= nodeToBean(mapValueNode
);
264 map
.put(key
, mapValue
);
266 beanWrapper
.setPropertyValue(name
, map
);
271 Object value
= nodeToBean(childNode
);
272 beanWrapper
.setPropertyValue(name
, value
);
275 return beanWrapper
.getWrappedInstance();
276 } catch (Exception e
) {
277 throw new ArgeoException("Cannot map node " + node
, e
);
282 * Transforms an object to the specified jcr Node in order to persist it.
286 * @throws RepositoryException
288 protected void beanToNode(BeanWrapper beanWrapper
, Node node
)
289 throws RepositoryException
{
290 properties
: for (PropertyDescriptor pd
: beanWrapper
291 .getPropertyDescriptors()) {
292 String name
= pd
.getName();
293 if (!beanWrapper
.isReadableProperty(name
))
294 continue properties
;// skip
296 Object value
= beanWrapper
.getPropertyValue(name
);
298 // remove values when updating
299 if (node
.hasProperty(name
))
300 node
.setProperty(name
, (Value
) null);
301 if (node
.hasNode(name
))
302 node
.getNode(name
).remove();
307 // if (uuidProperty != null && uuidProperty.equals(name)) {
308 // // node.addMixin(ArgeoJcrConstants.MIX_REFERENCEABLE);
309 // node.setProperty(ArgeoJcrConstants.JCR_UUID, value.toString());
310 // continue properties;
313 if ("class".equals(name
)) {
314 if (classProperty
!= null) {
315 node
.setProperty(classProperty
, ((Class
<?
>) value
)
317 // TODO: store a class hierarchy?
322 // Some bean reference other classes. We must deal with this case
323 if (value
instanceof Class
<?
>) {
324 node
.setProperty(name
, ((Class
<?
>) value
).getName());
328 Value val
= asValue(node
.getSession(), value
);
330 node
.setProperty(name
, val
);
334 if (value
instanceof List
<?
>) {
335 List
<?
> lst
= (List
<?
>) value
;
336 addList(node
, name
, lst
);
340 if (value
instanceof Map
<?
, ?
>) {
341 Map
<?
, ?
> map
= (Map
<?
, ?
>) value
;
342 addMap(node
, name
, map
);
346 BeanWrapper child
= createBeanWrapper(value
);
347 // TODO: delegate to another mapper
349 // TODO: deal with references
350 // Node childNode = findChildReference(session, child);
351 // if (childNode != null) {
352 // node.setProperty(name, childNode);
353 // continue properties;
356 // default case (recursive)
357 if (node
.hasNode(name
)) {// update
359 node
.getNode(name
).remove();
361 Node childNode
= node
.addNode(name
);
362 beanToNode(child
, childNode
);
367 * Process specific case of list
372 * @throws RepositoryException
374 protected void addList(Node node
, String name
, List
<?
> lst
)
375 throws RepositoryException
{
376 if (node
.hasNode(name
)) {// update
378 node
.getNode(name
).remove();
381 Node listNode
= node
.addNode(name
);
382 listNode
.setProperty(classProperty
, lst
.getClass().getName());
383 Value
[] values
= new Value
[lst
.size()];
384 boolean atLeastOneSet
= false;
385 for (int i
= 0; i
< lst
.size(); i
++) {
386 Object lstValue
= lst
.get(i
);
387 values
[i
] = asValue(node
.getSession(), lstValue
);
388 if (values
[i
] != null) {
389 atLeastOneSet
= true;
391 Node childNode
= findChildReference(node
.getSession(),
392 createBeanWrapper(lstValue
));
393 if (childNode
!= null) {
394 values
[i
] = node
.getSession().getValueFactory()
395 .createValue(childNode
);
396 atLeastOneSet
= true;
401 // will be either properties or nodes, not both
402 if (!atLeastOneSet
&& lst
.size() != 0) {
403 for (Object lstValue
: lst
) {
404 Node childNode
= listNode
.addNode(NODE_VALUE
);
405 beanToNode(createBeanWrapper(lstValue
), childNode
);
408 listNode
.setProperty(name
, values
);
413 * Process specific case of maps.
418 * @throws RepositoryException
420 protected void addMap(Node node
, String name
, Map
<?
, ?
> map
)
421 throws RepositoryException
{
422 if (node
.hasNode(name
)) {// update
424 node
.getNode(name
).remove();
427 Node mapNode
= node
.addNode(name
);
428 mapNode
.setProperty(classProperty
, map
.getClass().getName());
429 for (Object key
: map
.keySet()) {
430 Object mapValue
= map
.get(key
);
431 // PropertyEditor pe = beanWrapper.findCustomEditor(key.getClass(),
435 if (key
instanceof CharSequence
)
436 keyStr
= key
.toString();
438 throw new ArgeoException(
439 "Cannot find property editor for class "
443 // keyStr = pe.getAsText();
445 // TODO: check string format
447 Value mapVal
= asValue(node
.getSession(), mapValue
);
449 mapNode
.setProperty(keyStr
, mapVal
);
451 Node entryNode
= mapNode
.addNode(keyStr
);
452 beanToNode(createBeanWrapper(mapValue
), entryNode
);
459 protected BeanWrapper
createBeanWrapper(Object obj
) {
460 return new BeanWrapperImpl(obj
);
463 protected BeanWrapper
createBeanWrapper(Class
<?
> clss
) {
464 return new BeanWrapperImpl(clss
);
467 /** Returns null if value cannot be found */
468 protected Value
asValue(Session session
, Object value
)
469 throws RepositoryException
{
470 ValueFactory valueFactory
= session
.getValueFactory();
471 if (value
instanceof Integer
)
472 return valueFactory
.createValue((Integer
) value
);
473 else if (value
instanceof Long
)
474 return valueFactory
.createValue((Long
) value
);
475 else if (value
instanceof Float
)
476 return valueFactory
.createValue((Float
) value
);
477 else if (value
instanceof Double
)
478 return valueFactory
.createValue((Double
) value
);
479 else if (value
instanceof Boolean
)
480 return valueFactory
.createValue((Boolean
) value
);
481 else if (value
instanceof Calendar
)
482 return valueFactory
.createValue((Calendar
) value
);
483 else if (value
instanceof Date
) {
484 Calendar cal
= new GregorianCalendar();
485 cal
.setTime((Date
) value
);
486 return valueFactory
.createValue(cal
);
487 } else if (value
instanceof CharSequence
)
488 return valueFactory
.createValue(value
.toString());
489 else if (value
instanceof InputStream
)
490 return valueFactory
.createValue((InputStream
) value
);
495 protected Class
<?
> classFromProperty(Property property
)
496 throws RepositoryException
{
497 switch (property
.getType()) {
498 case PropertyType
.LONG
:
500 case PropertyType
.DOUBLE
:
502 case PropertyType
.STRING
:
504 case PropertyType
.BOOLEAN
:
505 return Boolean
.class;
506 case PropertyType
.DATE
:
507 return Calendar
.class;
508 case PropertyType
.NAME
:
511 throw new ArgeoException("Cannot find class for property "
512 + property
+ ", type="
513 + PropertyType
.nameFromValue(property
.getType()));
517 protected Object
asObject(Value value
, Class
<?
> propClass
)
518 throws RepositoryException
{
519 if (propClass
.equals(Integer
.class))
520 return (int) value
.getLong();
521 else if (propClass
.equals(Long
.class))
522 return value
.getLong();
523 else if (propClass
.equals(Float
.class))
524 return (float) value
.getDouble();
525 else if (propClass
.equals(Double
.class))
526 return value
.getDouble();
527 else if (propClass
.equals(Boolean
.class))
528 return value
.getBoolean();
529 else if (CharSequence
.class.isAssignableFrom(propClass
))
530 return value
.getString();
531 else if (InputStream
.class.isAssignableFrom(propClass
))
532 return value
.getStream();
533 else if (Calendar
.class.isAssignableFrom(propClass
))
534 return value
.getDate();
535 else if (Date
.class.isAssignableFrom(propClass
))
536 return value
.getDate().getTime();
541 protected Node
findChildReference(Session session
, BeanWrapper child
)
542 throws RepositoryException
{
543 if (child
.isReadableProperty(uuidProperty
)) {
544 String childUuid
= child
.getPropertyValue(uuidProperty
).toString();
546 return session
.getNodeByUUID(childUuid
);
547 } catch (ItemNotFoundException e
) {
548 if (strictUuidReference
)
549 throw new ArgeoException("No node found with uuid "
556 protected Class
<?
> loadClass(String name
) {
557 // log.debug("Class loader: " + classLoader);
559 return classLoader
.loadClass(name
);
560 } catch (ClassNotFoundException e
) {
561 throw new ArgeoException("Cannot load class " + name
, e
);
565 protected String
propertyName(String name
) {
569 public void setVersioning(Boolean versioning
) {
570 this.versioning
= versioning
;
573 public void setUuidProperty(String uuidProperty
) {
574 this.uuidProperty
= uuidProperty
;
577 public void setClassProperty(String classProperty
) {
578 this.classProperty
= classProperty
;
581 public void setStrictUuidReference(Boolean strictUuidReference
) {
582 this.strictUuidReference
= strictUuidReference
;
585 public void setPrimaryNodeType(String primaryNodeType
) {
586 this.primaryNodeType
= primaryNodeType
;
589 public void setClassLoader(ClassLoader classLoader
) {
590 this.classLoader
= classLoader
;
593 public void setNodeMapperProvider(NodeMapperProvider nodeMapperProvider
) {
594 this.nodeMapperProvider
= nodeMapperProvider
;
597 public String
getPrimaryNodeType() {
598 return this.primaryNodeType
;
601 public String
getClassProperty() {
602 return this.classProperty
;