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.lang3.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  
54  /**
55   * Wrapper class for a jcr property.
56   *
57   * @deprecated since 4.5, use jcr.Property instead.
58   */
59  @Deprecated
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      @Override
68      public Value getValue() {
69          if(isExist()){
70              try {
71                  return getJCRProperty().getValue();
72              }
73              catch (RepositoryException e) {
74                  throw new RuntimeException("Can't read value of nodedata " + toString(), e);
75              }
76          }
77          return null;
78      }
79  
80      @Override
81      public Value[] getValues() {
82          if(isExist()){
83              try {
84                  if(this.isMultiValue() == MULTIVALUE_TRUE) {
85                      return getJCRProperty().getValues();
86                  }
87                  //JCR-1464 needed for export of multivalue property with only one item
88                  return new Value[] { getJCRProperty().getValue() };
89              } catch (RepositoryException e) {
90                  throw new RuntimeException("Can't read value of nodedata " + toString(), e);
91              }
92          }
93          return null;
94      }
95  
96      @Override
97      public String getString() {
98          if(isExist()){
99              try {
100                 return getJCRProperty().getString();
101             }
102             // multi value
103             catch(ValueFormatException e){
104                 final StringBuffer str = new StringBuffer();
105                 Value[] values = getValues();
106                 for (Value value : values) {
107                     if(str.length()>0){
108                         str.append(", ");
109                     }
110                     try {
111                         str.append(value.getString());
112                     }
113                     catch (RepositoryException e1) {
114                         throw new RuntimeException("Can't read multi value nodedata " + toString(), e);
115                     }
116                 }
117                 return str.toString();
118             }
119             catch (RepositoryException e) {
120                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
121             }
122         }
123         return StringUtils.EMPTY;
124     }
125 
126     @Override
127     public long getLong() {
128         if(isExist()){
129             try {
130                 return getJCRProperty().getLong();
131             }
132             catch (RepositoryException e) {
133                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
134             }
135         }
136         return 0;
137     }
138 
139     @Override
140     public double getDouble() {
141         if(isExist()){
142             try {
143                 return getJCRProperty().getDouble();
144             }
145             catch (RepositoryException e) {
146                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
147             }
148         }
149         return 0;
150     }
151 
152     @Override
153     public Calendar getDate() {
154         if(isExist()){
155             try {
156                 return getJCRProperty().getDate();
157             }
158             catch (RepositoryException e) {
159                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
160             }
161         }
162         return null;
163     }
164 
165     @Override
166     public boolean getBoolean() {
167         if(isExist()){
168             try {
169                 return getJCRProperty().getBoolean();
170             }
171             catch (RepositoryException e) {
172                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
173             }
174         }
175         return false;
176     }
177 
178     @Override
179     public InputStream getStream() {
180         if (isExist()){
181             try {
182                 return getJCRProperty().getStream();
183             }
184             catch (RepositoryException e) {
185                 throw new RuntimeException("Can't read value of nodedata " + toString(), e);
186             }
187         }
188         return null;
189     }
190 
191     @Override
192     public int getType() {
193         if (isExist()) {
194             try {
195                 return getJCRProperty().getType();
196             }
197             catch (Exception e) {
198                 log.warn("Unable to read property type for {}", name);
199             }
200         }
201         return PropertyType.UNDEFINED;
202     }
203 
204     @Override
205     public long getContentLength() {
206         if(!isExist()){
207             return 0;
208         }
209 
210         try {
211             return getJCRProperty().getLength();
212         }
213         catch (RepositoryException re) {
214             throw new RuntimeException(re);
215         }
216     }
217 
218     @Override
219     public Property getJCRProperty() {
220         try {
221             return getJCRNode().getProperty(name);
222         }
223         catch (PathNotFoundException e) {
224             return null;
225         }
226         catch (RepositoryException e) {
227             throw new RuntimeException(e);
228         }
229     }
230 
231     protected Node getJCRNode() {
232         Content parent = getParent();
233         return parent == null ? null : parent.getJCRNode();
234     }
235 
236     @Override
237     protected Content getContentFromJCRReference() throws RepositoryException {
238         return getHierarchyManager().getContent(getJCRProperty().getNode().getPath());
239     }
240 
241     @Override
242     public void setValue(String value) throws RepositoryException, AccessDeniedException {
243         getJCRNode().setProperty(name, value);
244     }
245 
246     @Override
247     public void setValue(int value) throws RepositoryException, AccessDeniedException {
248         getJCRNode().setProperty(name, value);
249     }
250 
251     @Override
252     public void setValue(long value) throws RepositoryException, AccessDeniedException {
253         getJCRNode().setProperty(name, value);
254     }
255 
256     @Override
257     public void setValue(double value) throws RepositoryException, AccessDeniedException {
258         getJCRNode().setProperty(name, value);
259     }
260 
261     @Override
262     public void setValue(boolean value) throws RepositoryException, AccessDeniedException {
263         getJCRNode().setProperty(name, value);
264     }
265 
266     @Override
267     public void setValue(Calendar value) throws RepositoryException, AccessDeniedException {
268         getJCRNode().setProperty(name, value);    }
269 
270     @Override
271     public void setValue(Value value) throws RepositoryException, AccessDeniedException {
272         getJCRNode().setProperty(name, value);    }
273 
274     @Override
275     public void setValue(Value[] value) throws RepositoryException, AccessDeniedException {
276         getJCRNode().setProperty(name, value);    }
277 
278     @Override
279     public void setValue(Content value) throws RepositoryException, AccessDeniedException {
280         getJCRNode().setProperty(name, value.getJCRNode());
281     }
282 
283     @Override
284     public void setValue(InputStream value) throws RepositoryException, AccessDeniedException {
285         throw new UnsupportedOperationException("This operation is only supported for node datas of type BINARY");
286     }
287 
288     @Override
289     public boolean isExist() {
290         try {
291             return getJCRNode().hasProperty(name);
292         }
293         catch (RepositoryException e) {
294             throw new RuntimeException(e);
295         }
296     }
297 
298     @Override
299     public void save() throws RepositoryException {
300         if(isExist()){
301             getJCRProperty().save();
302         }
303     }
304 
305     @Override
306     public void delete() throws RepositoryException {
307         if(isExist()){
308             getJCRProperty().remove();
309         }
310     }
311 
312     @Override
313     public void refresh(boolean keepChanges) throws RepositoryException {
314         if(isExist()){
315             getJCRProperty().refresh(keepChanges);
316         }
317     }
318 }