View Javadoc

1   /**
2    * This file Copyright (c) 2010-2011 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.ArrayList;
40  import java.util.Calendar;
41  import java.util.Collection;
42  
43  import javax.jcr.Node;
44  import javax.jcr.Property;
45  import javax.jcr.PropertyIterator;
46  import javax.jcr.PropertyType;
47  import javax.jcr.RepositoryException;
48  import javax.jcr.Value;
49  
50  /**
51   * A node data hiding the fact that node datas of type BINARY are stored as nodes of type {@link ItemType#NT_RESOURCE}.
52   * @author pbaerfuss
53   * @version $Id$
54   */
55  public class BinaryNodeData extends AbstractNodeData {
56  
57      /**
58       * The node containing the binary and attributes.
59       */
60      private Node binaryNode;
61  
62      /**
63       * The property containing the binary.
64       * @see ItemType.JCR_DATA
65       */
66      private Property binaryProperty;
67  
68      protected BinaryNodeData(Content parent, String name) {
69          super(parent, name);
70      }
71  
72      @Override
73      public Property getJCRProperty() {
74          if(binaryProperty == null){
75              if(isExist()){
76                  try {
77                      binaryProperty = getBinaryNode(false).getProperty(ItemType.JCR_DATA);
78                  }
79                  catch (RepositoryException e) {
80                      throw new IllegalStateException(e);
81                  }
82              }
83          }
84          return binaryProperty;
85      }
86  
87      protected Node getBinaryNode(boolean createIfNotExisting) {
88          if(binaryNode == null){
89              Node parentJCRNode = getParent().getJCRNode();
90              try {
91                  if(parentJCRNode.hasNode(name)){
92                      binaryNode = parentJCRNode.getNode(name);
93                  }
94                  else if(createIfNotExisting){
95                      binaryNode = parentJCRNode.addNode(name, ItemType.NT_RESOURCE);
96                  }
97              }
98              catch (RepositoryException e) {
99                  throw new RuntimeException(e);
100             }
101         }
102         return binaryNode;
103     }
104 
105     @Override
106     public boolean isExist() {
107         return getBinaryNode(false) != null;
108     }
109 
110     @Override
111     public InputStream getStream() {
112         if (isExist()) {
113             try {
114                 return getJCRProperty().getStream();
115             }
116             catch (RepositoryException e) {
117                 throw new RuntimeException("Can't read value of node data " + toString(), e);
118             }
119         }
120         return null;
121     }
122 
123     @Override
124     public void setValue(InputStream value) throws RepositoryException, AccessDeniedException {
125         getBinaryNode(true).setProperty(ItemType.JCR_DATA, value);
126     }
127 
128     @Override
129     public void delete() throws RepositoryException {
130         if(isExist()){
131             getBinaryNode(false).remove();
132         }
133     }
134 
135     @Override
136     public void setAttribute(String name, String value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
137         getBinaryNode(true).setProperty(name, value);
138     }
139 
140     @Override
141     public void setAttribute(String name, Calendar value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
142         getBinaryNode(true).setProperty(name, value);
143     }
144 
145     @Override
146     public String getAttribute(String name) {
147         if(isExist()){
148             Node binaryNode = getBinaryNode(false);
149             try {
150                 if(binaryNode.hasProperty(name)){
151                     return binaryNode.getProperty(name).getString();
152                 }
153             }
154             catch (RepositoryException e) {
155                 throw new IllegalStateException("Can't read attribute", e);
156             }
157         }
158         return "";
159     }
160 
161     @Override
162     public Collection<String> getAttributeNames() throws RepositoryException {
163         Collection<String> names = new ArrayList<String>();
164         if(isExist()){
165             PropertyIterator properties = getBinaryNode(false).getProperties();
166             while (properties.hasNext()) {
167                 String name = properties.nextProperty().getName();
168                 if (!name.equalsIgnoreCase(ItemType.JCR_DATA)) {
169                     names.add(name);
170                 }
171             }
172         }
173         return names;
174     }
175 
176     @Override
177     public int getType() {
178         return PropertyType.BINARY;
179     }
180 
181     @Override
182     public Value getValue() {
183         if(isExist()){
184             try {
185                 return getJCRProperty().getValue();
186             }
187             catch (RepositoryException e) {
188                 throw new IllegalStateException(e);
189             }
190         }
191         return null;
192     }
193 
194     @Override
195     public long getContentLength() {
196         if(!isExist()){
197             return 0;
198         }
199 
200         try {
201             return getJCRProperty().getLength();
202         }
203         catch (RepositoryException re) {
204             throw new RuntimeException(re);
205         }
206     }
207 
208     @Override
209     public int isMultiValue() {
210         return MULTIVALUE_FALSE;
211     }
212 
213     @Override
214     public void refresh(boolean keepChanges) throws RepositoryException {
215         if(isExist()){
216             getBinaryNode(false).refresh(keepChanges);
217         }
218     }
219 
220     @Override
221     public void save() throws RepositoryException {
222         if(isExist()){
223             getBinaryNode(false).save();
224         }
225     }
226 
227     @Override
228     public Content getReferencedContent() throws RepositoryException {
229         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
230     }
231 
232     @Override
233     public Content getReferencedContent(String repositoryId) throws RepositoryException {
234         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
235     }
236 
237     @Override
238     protected Content getContentFromJCRReference() throws RepositoryException {
239         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
240     }
241 
242     @Override
243     public String getString() {
244         if(isExist()){
245             try {
246                 return getJCRProperty().getString();
247             }
248             catch (RepositoryException e) {
249                 throw new RuntimeException("Can't read value of node data" + toString());
250             }
251         }
252         return "";
253     }
254 
255     @Override
256     public Calendar getDate() {
257         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
258     }
259 
260     @Override
261     public boolean getBoolean() {
262         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
263     }
264 
265     @Override
266     public double getDouble() {
267         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
268     }
269 
270     @Override
271     public long getLong() {
272         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
273     }
274 
275     @Override
276     public Value[] getValues() {
277         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
278     }
279 
280     @Override
281     public void setValue(String value) throws RepositoryException {
282         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
283     }
284 
285     @Override
286     public void setValue(int value) throws RepositoryException {
287         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
288     }
289 
290     @Override
291     public void setValue(long value) throws RepositoryException {
292         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
293     }
294 
295     @Override
296     public void setValue(double value) throws RepositoryException {
297         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
298     }
299 
300     @Override
301     public void setValue(boolean value) throws RepositoryException {
302         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
303     }
304 
305     @Override
306     public void setValue(Calendar value) throws RepositoryException {
307         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
308     }
309 
310     @Override
311     public void setValue(Content value) throws RepositoryException {
312         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
313     }
314 
315     @Override
316     public void setValue(Value value) throws RepositoryException {
317         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
318     }
319 
320     @Override
321     public void setValue(Value[] value) throws RepositoryException {
322         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
323     }
324 }