1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.core;
35
36 import info.magnolia.cms.security.AccessDeniedException;
37 import info.magnolia.cms.security.Permission;
38
39 import java.io.InputStream;
40 import java.util.Calendar;
41
42 import javax.jcr.Node;
43 import javax.jcr.PathNotFoundException;
44 import javax.jcr.Property;
45 import javax.jcr.PropertyType;
46 import javax.jcr.RepositoryException;
47 import javax.jcr.Value;
48 import javax.jcr.ValueFormatException;
49
50 import org.apache.commons.lang.StringUtils;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54
55
56
57
58
59
60 public class DefaultNodeData extends AbstractNodeData {
61 private static final Logger log = LoggerFactory.getLogger(DefaultNodeData.class);
62
63 protected DefaultNodeData(Content parent, String name) {
64 super(parent, name);
65 }
66
67 public Value getValue() {
68 if(isExist()){
69 try {
70 return getJCRProperty().getValue();
71 }
72 catch (RepositoryException e) {
73 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
74 }
75 }
76 else{
77 return null;
78 }
79 }
80
81 public Value[] getValues() {
82 if(isExist()){
83 try {
84 if(this.isMultiValue() == MULTIVALUE_TRUE) {
85 return getJCRProperty().getValues();
86 } else {
87
88 return new Value[] { getJCRProperty().getValue() };
89 }
90 } catch (RepositoryException e) {
91 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
92 }
93 }
94 else{
95 return null;
96 }
97 }
98
99 public String getString() {
100 if(isExist()){
101 try {
102 return getJCRProperty().getString();
103 }
104
105 catch(ValueFormatException e){
106 final StringBuffer str = new StringBuffer();
107 Value[] values = getValues();
108 for (Value value : values) {
109 if(str.length()>0){
110 str.append(", ");
111 }
112 try {
113 str.append(value.getString());
114 }
115 catch (RepositoryException e1) {
116 throw new RuntimeException("Can't read multi value nodedata " + toString(), e);
117 }
118 }
119 return str.toString();
120 }
121 catch (RepositoryException e) {
122 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
123 }
124 }
125 else{
126 return StringUtils.EMPTY;
127 }
128 }
129
130 public long getLong() {
131 if(isExist()){
132 try {
133 return getJCRProperty().getLong();
134 }
135 catch (RepositoryException e) {
136 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
137 }
138 }
139 else{
140 return 0;
141 }
142 }
143
144 public double getDouble() {
145 if(isExist()){
146 try {
147 return getJCRProperty().getDouble();
148 }
149 catch (RepositoryException e) {
150 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
151 }
152 }
153 else{
154 return 0;
155 }
156 }
157
158 public Calendar getDate() {
159 if(isExist()){
160 try {
161 return getJCRProperty().getDate();
162 }
163 catch (RepositoryException e) {
164 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
165 }
166 }
167 else{
168 return null;
169 }
170 }
171
172 public boolean getBoolean() {
173 if(isExist()){
174 try {
175 return getJCRProperty().getBoolean();
176 }
177 catch (RepositoryException e) {
178 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
179 }
180 }
181 else{
182 return false;
183 }
184 }
185
186 public InputStream getStream() {
187 if (isExist()){
188 try {
189 return getJCRProperty().getStream();
190 }
191 catch (RepositoryException e) {
192 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
193 }
194 }
195 else{
196 return null;
197 }
198 }
199
200 public int getType() {
201 if (isExist()) {
202 try {
203 return getJCRProperty().getType();
204 }
205 catch (Exception e) {
206 log.warn("Unable to read property type for {}", name);
207 }
208 }
209 return PropertyType.UNDEFINED;
210 }
211
212 public long getContentLength() {
213 if(!isExist()){
214 return 0;
215 }
216
217 try {
218 return getJCRProperty().getLength();
219 }
220 catch (RepositoryException re) {
221 throw new RuntimeException(re);
222 }
223 }
224
225 public Property getJCRProperty() {
226 try {
227 return getJCRNode().getProperty(name);
228 }
229 catch (PathNotFoundException e) {
230 return null;
231 }
232 catch (RepositoryException e) {
233 throw new RuntimeException(e);
234 }
235 }
236
237 protected Node getJCRNode() {
238 return parent.getJCRNode();
239 }
240
241 protected Content getContentFromJCRReference() throws RepositoryException {
242 return getHierarchyManager().getContent(getJCRProperty().getNode().getPath());
243 }
244
245 public void setValue(String value) throws RepositoryException, AccessDeniedException {
246 Access.isGranted(getHierarchyManager().getAccessManager(), Path.getAbsolutePath(this.getHandle()), Permission.SET);
247 getJCRNode().setProperty(name, value);
248 }
249
250 public void setValue(int value) throws RepositoryException, AccessDeniedException {
251 Access.isGranted(getHierarchyManager().getAccessManager(), Path.getAbsolutePath(this.getHandle()), Permission.SET);
252 getJCRNode().setProperty(name, value);
253 }
254
255 public void setValue(long value) throws RepositoryException, AccessDeniedException {
256 Access.isGranted(getHierarchyManager().getAccessManager(), Path.getAbsolutePath(this.getHandle()), Permission.SET);
257 getJCRNode().setProperty(name, value);
258 }
259
260 public void setValue(double value) throws RepositoryException, AccessDeniedException {
261 Access.isGranted(getHierarchyManager().getAccessManager(), Path.getAbsolutePath(this.getHandle()), Permission.SET);
262 getJCRNode().setProperty(name, value);
263 }
264
265 public void setValue(boolean value) throws RepositoryException, AccessDeniedException {
266 Access.isGranted(getHierarchyManager().getAccessManager(), Path.getAbsolutePath(this.getHandle()), Permission.SET);
267 getJCRNode().setProperty(name, value);
268 }
269
270 public void setValue(Calendar value) throws RepositoryException, AccessDeniedException {
271 Access.isGranted(getHierarchyManager().getAccessManager(), Path.getAbsolutePath(this.getHandle()), Permission.SET);
272 getJCRNode().setProperty(name, value); }
273
274 public void setValue(Value value) throws RepositoryException, AccessDeniedException {
275 Access.isGranted(getHierarchyManager().getAccessManager(), Path.getAbsolutePath(this.getHandle()), Permission.SET);
276 getJCRNode().setProperty(name, value); }
277
278 public void setValue(Value[] value) throws RepositoryException, AccessDeniedException {
279 Access.isGranted(getHierarchyManager().getAccessManager(), Path.getAbsolutePath(this.getHandle()), Permission.SET);
280 getJCRNode().setProperty(name, value); }
281
282 public void setValue(Content value) throws RepositoryException, AccessDeniedException {
283 Access.isGranted(getHierarchyManager().getAccessManager(), Path.getAbsolutePath(this.getHandle()), Permission.SET);
284 getJCRNode().setProperty(name, value.getJCRNode());
285 }
286
287 public void setValue(InputStream value) throws RepositoryException, AccessDeniedException {
288 throw new UnsupportedOperationException("This operation is only supported for node datas of type BINARY");
289 }
290
291 public boolean isExist() {
292 try {
293 return getJCRNode().hasProperty(name);
294 }
295 catch (RepositoryException e) {
296 throw new RuntimeException(e);
297 }
298 }
299
300 public void save() throws RepositoryException {
301 if(isExist()){
302 getJCRProperty().save();
303 }
304 }
305
306 public void delete() throws RepositoryException {
307 Access.isGranted(getHierarchyManager().getAccessManager(), Path.getAbsolutePath(this.getHandle()), Permission.REMOVE);
308 if(isExist()){
309 getJCRProperty().remove();
310 }
311 }
312
313 public void refresh(boolean keepChanges) throws RepositoryException {
314 if(isExist()){
315 getJCRProperty().refresh(keepChanges);
316 }
317 }
318
319 }