1 package org
.argeo
.cms
.jcr
.gcr
;
3 import java
.util
.Calendar
;
4 import java
.util
.Iterator
;
7 import javax
.jcr
.NodeIterator
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.PropertyIterator
;
10 import javax
.jcr
.PropertyType
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Value
;
14 import org
.argeo
.api
.gcr
.Content
;
15 import org
.argeo
.api
.gcr
.spi
.AbstractContent
;
16 import org
.argeo
.jcr
.Jcr
;
17 import org
.argeo
.jcr
.JcrException
;
19 public class JcrContent
extends AbstractContent
{
20 private JcrContentSession contentSession
;
23 protected JcrContent(JcrContentSession contentSession
, Node node
) {
24 this.contentSession
= contentSession
;
29 public String
getName() {
30 return Jcr
.getName(jcrNode
);
34 public <A
> A
get(String key
, Class
<A
> clss
) {
35 if (isDefaultAttrTypeRequested(clss
)) {
36 return (A
) get(jcrNode
, key
);
38 return (A
) Jcr
.get(jcrNode
, key
);
42 public Iterator
<Content
> iterator() {
44 return new JcrContentIterator(contentSession
, jcrNode
.getNodes());
45 } catch (RepositoryException e
) {
46 throw new JcrException("Cannot list children of " + jcrNode
, e
);
51 protected Iterable
<String
> keys() {
52 return new Iterable
<String
>() {
55 public Iterator
<String
> iterator() {
57 PropertyIterator propertyIterator
= jcrNode
.getProperties();
58 return new JcrKeyIterator(contentSession
, propertyIterator
);
59 } catch (RepositoryException e
) {
60 throw new JcrException("Cannot retrive properties from " + jcrNode
, e
);
66 public Node
getJcrNode() {
70 /** Cast to a standard Java object. */
71 static Object
get(Node node
, String property
) {
73 Value value
= node
.getProperty(property
).getValue();
74 switch (value
.getType()) {
75 case PropertyType
.STRING
:
76 return value
.getString();
77 case PropertyType
.DOUBLE
:
78 return (Double
) value
.getDouble();
79 case PropertyType
.LONG
:
80 return (Long
) value
.getLong();
81 case PropertyType
.BOOLEAN
:
82 return (Boolean
) value
.getBoolean();
83 case PropertyType
.DATE
:
84 Calendar calendar
= value
.getDate();
85 return calendar
.toInstant();
86 case PropertyType
.BINARY
:
87 throw new IllegalArgumentException("Binary is not supported as an attribute");
89 return value
.getString();
91 } catch (RepositoryException e
) {
92 throw new JcrException("Cannot cast value from " + property
+ " of node " + node
, e
);
96 static class JcrContentIterator
implements Iterator
<Content
> {
97 private final JcrContentSession contentSession
;
98 private final NodeIterator nodeIterator
;
99 // we keep track in order to be able to delete it
100 private JcrContent current
= null;
102 protected JcrContentIterator(JcrContentSession contentSession
, NodeIterator nodeIterator
) {
103 this.contentSession
= contentSession
;
104 this.nodeIterator
= nodeIterator
;
108 public boolean hasNext() {
109 return nodeIterator
.hasNext();
113 public Content
next() {
114 current
= new JcrContent(contentSession
, nodeIterator
.nextNode());
119 public void remove() {
120 if (current
!= null) {
121 // current.getJcrNode().remove();
123 throw new UnsupportedOperationException();
128 static class JcrKeyIterator
implements Iterator
<String
> {
129 private final JcrContentSession contentSession
;
130 private final PropertyIterator propertyIterator
;
132 protected JcrKeyIterator(JcrContentSession contentSession
, PropertyIterator propertyIterator
) {
133 this.contentSession
= contentSession
;
134 this.propertyIterator
= propertyIterator
;
138 public boolean hasNext() {
139 return propertyIterator
.hasNext();
143 public String
next() {
144 Property property
= null;
146 property
= propertyIterator
.nextProperty();
147 // TODO map standard property names
148 return property
.getName();
149 } catch (RepositoryException e
) {
150 throw new JcrException("Cannot retrieve property " + property
, null);