]> git.argeo.org Git - lgpl/argeo-commons.git/blob - src/org/argeo/cms/jcr/gcr/JcrContent.java
Prepare next development cycle
[lgpl/argeo-commons.git] / src / org / argeo / cms / jcr / gcr / JcrContent.java
1 package org.argeo.cms.jcr.gcr;
2
3 import java.util.Calendar;
4 import java.util.Iterator;
5
6 import javax.jcr.Node;
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;
13
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;
18
19 public class JcrContent extends AbstractContent {
20 private JcrContentSession contentSession;
21 private Node jcrNode;
22
23 protected JcrContent(JcrContentSession contentSession, Node node) {
24 this.contentSession = contentSession;
25 this.jcrNode = node;
26 }
27
28 @Override
29 public String getName() {
30 return Jcr.getName(jcrNode);
31 }
32
33 @Override
34 public <A> A get(String key, Class<A> clss) {
35 if (isDefaultAttrTypeRequested(clss)) {
36 return (A) get(jcrNode, key);
37 }
38 return (A) Jcr.get(jcrNode, key);
39 }
40
41 @Override
42 public Iterator<Content> iterator() {
43 try {
44 return new JcrContentIterator(contentSession, jcrNode.getNodes());
45 } catch (RepositoryException e) {
46 throw new JcrException("Cannot list children of " + jcrNode, e);
47 }
48 }
49
50 @Override
51 protected Iterable<String> keys() {
52 return new Iterable<String>() {
53
54 @Override
55 public Iterator<String> iterator() {
56 try {
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);
61 }
62 }
63 };
64 }
65
66 public Node getJcrNode() {
67 return jcrNode;
68 }
69
70 /** Cast to a standard Java object. */
71 static Object get(Node node, String property) {
72 try {
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");
88 default:
89 return value.getString();
90 }
91 } catch (RepositoryException e) {
92 throw new JcrException("Cannot cast value from " + property + " of node " + node, e);
93 }
94 }
95
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;
101
102 protected JcrContentIterator(JcrContentSession contentSession, NodeIterator nodeIterator) {
103 this.contentSession = contentSession;
104 this.nodeIterator = nodeIterator;
105 }
106
107 @Override
108 public boolean hasNext() {
109 return nodeIterator.hasNext();
110 }
111
112 @Override
113 public Content next() {
114 current = new JcrContent(contentSession, nodeIterator.nextNode());
115 return current;
116 }
117
118 @Override
119 public void remove() {
120 if (current != null) {
121 // current.getJcrNode().remove();
122 }
123 throw new UnsupportedOperationException();
124 }
125
126 }
127
128 static class JcrKeyIterator implements Iterator<String> {
129 private final JcrContentSession contentSession;
130 private final PropertyIterator propertyIterator;
131
132 protected JcrKeyIterator(JcrContentSession contentSession, PropertyIterator propertyIterator) {
133 this.contentSession = contentSession;
134 this.propertyIterator = propertyIterator;
135 }
136
137 @Override
138 public boolean hasNext() {
139 return propertyIterator.hasNext();
140 }
141
142 @Override
143 public String next() {
144 Property property = null;
145 try {
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);
151 }
152 }
153
154 }
155 }