View Javadoc

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