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