View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.core;
35  
36  import info.magnolia.cms.security.AccessDeniedException;
37  
38  import java.io.InputStream;
39  import java.util.Calendar;
40  
41  import javax.jcr.Node;
42  import javax.jcr.PathNotFoundException;
43  import javax.jcr.Property;
44  import javax.jcr.PropertyType;
45  import javax.jcr.RepositoryException;
46  import javax.jcr.Value;
47  import javax.jcr.ValueFormatException;
48  
49  import org.apache.commons.lang.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  
54  /**
55   * Wrapper class for a jcr property.
56   *
57   * @version $Id$
58   * @deprecated since 4.5, use jcr.Property instead.
59   */
60  @Deprecated
61  public class DefaultNodeData extends AbstractNodeData {
62      private static final Logger log = LoggerFactory.getLogger(DefaultNodeData.class);
63  
64      protected DefaultNodeData(Content parent, String name) {
65          super(parent, name);
66      }
67  
68      @Override
69      public Value getValue() {
70          if(isExist()){
71              try {
72                  return getJCRProperty().getValue();
73              }
74              catch (RepositoryException e) {
75                  throw new RuntimeException("Can't read value of nodedata " + toString(), e);
76              }
77          }
78          return null;
79      }
80  
81      @Override
82      public Value[] getValues() {
83          if(isExist()){
84              try {
85                  if(this.isMultiValue() == MULTIVALUE_TRUE) {
86                      return getJCRProperty().getValues();
87                  }
88                  //JCR-1464 needed for export of multivalue property with only one item
89                  return new Value[] { getJCRProperty().getValue() };
90              } catch (RepositoryException e) {
91                  throw new RuntimeException("Can't read value of nodedata " + toString(), e);
92              }
93          }
94          return null;
95      }
96  
97      @Override
98      public String getString() {
99          if(isExist()){
100             try {
101                 return getJCRProperty().getString();
102             }
103             // multi value
104             catch(ValueFormatException e){
105                 final StringBuffer str = new StringBuffer();
106                 Value[] values = getValues();
107                 for (Value value : values) {
108                     if(str.length()>0){
109                         str.append(", ");
110                     }
111                     try {
112                         str.append(value.getString());
113                     }
114                     catch (RepositoryException e1) {
115                         throw new RuntimeException("Can't read multi value nodedata " + toString(), e);
116                     }
117                 }
118                 return str.toString();
119             }
120             catch (RepositoryException e) {
121                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
122             }
123         }
124         return StringUtils.EMPTY;
125     }
126 
127     @Override
128     public long getLong() {
129         if(isExist()){
130             try {
131                 return getJCRProperty().getLong();
132             }
133             catch (RepositoryException e) {
134                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
135             }
136         }
137         return 0;
138     }
139 
140     @Override
141     public double getDouble() {
142         if(isExist()){
143             try {
144                 return getJCRProperty().getDouble();
145             }
146             catch (RepositoryException e) {
147                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
148             }
149         }
150         return 0;
151     }
152 
153     @Override
154     public Calendar getDate() {
155         if(isExist()){
156             try {
157                 return getJCRProperty().getDate();
158             }
159             catch (RepositoryException e) {
160                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
161             }
162         }
163         return null;
164     }
165 
166     @Override
167     public boolean getBoolean() {
168         if(isExist()){
169             try {
170                 return getJCRProperty().getBoolean();
171             }
172             catch (RepositoryException e) {
173                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
174             }
175         }
176         return false;
177     }
178 
179     @Override
180     public InputStream getStream() {
181         if (isExist()){
182             try {
183                 return getJCRProperty().getStream();
184             }
185             catch (RepositoryException e) {
186                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
187             }
188         }
189         return null;
190     }
191 
192     @Override
193     public int getType() {
194         if (isExist()) {
195             try {
196                 return getJCRProperty().getType();
197             }
198             catch (Exception e) {
199                 log.warn("Unable to read property type for {}", name);
200             }
201         }
202         return PropertyType.UNDEFINED;
203     }
204 
205     @Override
206     public long getContentLength() {
207         if(!isExist()){
208             return 0;
209         }
210 
211         try {
212             return getJCRProperty().getLength();
213         }
214         catch (RepositoryException re) {
215             throw new RuntimeException(re);
216         }
217     }
218 
219     @Override
220     public Property getJCRProperty() {
221         try {
222             return getJCRNode().getProperty(name);
223         }
224         catch (PathNotFoundException e) {
225             return null;
226         }
227         catch (RepositoryException e) {
228             throw new RuntimeException(e);
229         }
230     }
231 
232     protected Node getJCRNode() {
233         Content parent = getParent();
234         return parent == null ? null : parent.getJCRNode();
235     }
236 
237     @Override
238     protected Content getContentFromJCRReference() throws RepositoryException {
239         return getHierarchyManager().getContent(getJCRProperty().getNode().getPath());
240     }
241 
242     @Override
243     public void setValue(String value) throws RepositoryException, AccessDeniedException {
244         getJCRNode().setProperty(name, value);
245     }
246 
247     @Override
248     public void setValue(int value) throws RepositoryException, AccessDeniedException {
249         getJCRNode().setProperty(name, value);
250     }
251 
252     @Override
253     public void setValue(long value) throws RepositoryException, AccessDeniedException {
254         getJCRNode().setProperty(name, value);
255     }
256 
257     @Override
258     public void setValue(double value) throws RepositoryException, AccessDeniedException {
259         getJCRNode().setProperty(name, value);
260     }
261 
262     @Override
263     public void setValue(boolean value) throws RepositoryException, AccessDeniedException {
264         getJCRNode().setProperty(name, value);
265     }
266 
267     @Override
268     public void setValue(Calendar value) throws RepositoryException, AccessDeniedException {
269         getJCRNode().setProperty(name, value);    }
270 
271     @Override
272     public void setValue(Value value) throws RepositoryException, AccessDeniedException {
273         getJCRNode().setProperty(name, value);    }
274 
275     @Override
276     public void setValue(Value[] value) throws RepositoryException, AccessDeniedException {
277         getJCRNode().setProperty(name, value);    }
278 
279     @Override
280     public void setValue(Content value) throws RepositoryException, AccessDeniedException {
281         getJCRNode().setProperty(name, value.getJCRNode());
282     }
283 
284     @Override
285     public void setValue(InputStream value) throws RepositoryException, AccessDeniedException {
286         throw new UnsupportedOperationException("This operation is only supported for node datas of type BINARY");
287     }
288 
289     @Override
290     public boolean isExist() {
291         try {
292             return getJCRNode().hasProperty(name);
293         }
294         catch (RepositoryException e) {
295             throw new RuntimeException(e);
296         }
297     }
298 
299     @Override
300     public void save() throws RepositoryException {
301         if(isExist()){
302             getJCRProperty().save();
303         }
304     }
305 
306     @Override
307     public void delete() throws RepositoryException {
308         if(isExist()){
309             getJCRProperty().remove();
310         }
311     }
312 
313     @Override
314     public void refresh(boolean keepChanges) throws RepositoryException {
315         if(isExist()){
316             getJCRProperty().refresh(keepChanges);
317         }
318     }
319 }