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.util.NodeDataUtil;
38  import info.magnolia.context.MgnlContext;
39  import org.apache.commons.lang.StringUtils;
40  
41  import javax.jcr.ItemNotFoundException;
42  import javax.jcr.PathNotFoundException;
43  import javax.jcr.PropertyType;
44  import javax.jcr.RepositoryException;
45  import javax.jcr.ValueFormatException;
46  
47  import java.util.Calendar;
48  import java.util.Collection;
49  
50  /**
51   * Implementing some default behavior.
52   * @author pbaerfuss
53   * @version $Id$
54   *
55   */
56  public abstract class AbstractNodeData implements NodeData{
57      private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractNodeData.class);
58  
59      protected String name;
60      protected Content parent;
61      private int multiValue = MULTIVALUE_UNDEFINED;
62  
63      protected AbstractNodeData(Content parent, String name) {
64          this.parent = parent;
65          this.name = name;
66      }
67  
68      public HierarchyManager getHierarchyManager() {
69          return parent.getHierarchyManager();
70      }
71  
72      public String getName() {
73          return this.name;
74      }
75  
76      public String getHandle() {
77          return Path.getAbsolutePath(parent.getHandle(), name);
78      }
79  
80      public boolean isGranted(long permissions) {
81          return getHierarchyManager().getAccessManager().isGranted(getHandle(), permissions);
82      }
83  
84      public String getString(String lineBreak) {
85          return getString().replaceAll("\n", lineBreak); //$NON-NLS-1$
86      }
87  
88      public Content getParent() throws AccessDeniedException, ItemNotFoundException, javax.jcr.AccessDeniedException, RepositoryException {
89          return this.parent;
90      }
91  
92      public void setParent(Content parent) {
93          this.parent = parent;
94      }
95  
96      public Content getReferencedContent(String repositoryId) throws PathNotFoundException, RepositoryException {
97          if(this.getHierarchyManager().getName().equals(repositoryId)){
98              return getReferencedContent();
99          }
100         return getReferencedContent(MgnlContext.getHierarchyManager(repositoryId));
101     }
102 
103     public Content getReferencedContent() throws PathNotFoundException, RepositoryException {
104         return getReferencedContent(this.getHierarchyManager());
105     }
106 
107     protected Content getReferencedContent(HierarchyManager hm) throws PathNotFoundException, RepositoryException {
108         if(!isExist()){
109             return null;
110         }
111         // node containing this property
112         Content node = getParent();
113         Content refNode = null;
114 
115         int type = getType();
116 
117         if (type == PropertyType.REFERENCE) {
118             refNode = getContentFromJCRReference();
119         } else if (type != PropertyType.PATH && type != PropertyType.STRING) {
120             throw new ItemNotFoundException("can't find referenced node for value " + PropertyType.nameFromValue(type) + "[" + getString() + "]. This type is not supported.");
121         }
122 
123         final String pathOrUUID = this.getString();
124         if (StringUtils.isNotBlank(pathOrUUID)) {
125             // we support uuids as strings
126             if (!StringUtils.contains(pathOrUUID, "/")) {
127                 try {
128                     refNode = hm.getContentByUUID(pathOrUUID);
129                 } catch (ItemNotFoundException e) {
130                     // this is not an uuid
131                 }
132             }
133             // not found by uuid, check the path then
134             if (refNode == null) {
135                 // is this relative path?
136                 if (!pathOrUUID.startsWith("/") && node.hasContent(pathOrUUID)) {
137                     refNode = node.getContent(pathOrUUID);
138                 } else if (pathOrUUID.startsWith("/") && hm.isExist(pathOrUUID)){
139                     refNode = hm.getContent(pathOrUUID);
140                 }
141             }
142         }
143 
144         if(refNode==null){
145             throw new ItemNotFoundException("can't find referenced node for value [" + getString() + "]");
146         }
147 
148         return refNode;
149     }
150 
151     /**
152      * Specific implementation for retrieving the referenced node when using a property of type REFERENCE.
153      */
154     protected abstract Content getContentFromJCRReference() throws RepositoryException;
155 
156     public int isMultiValue() {
157         if(multiValue == MULTIVALUE_UNDEFINED) {
158             try {
159                 if (isExist()) {
160                     getJCRProperty().getValue();
161                     multiValue = MULTIVALUE_FALSE;
162                 }
163 
164             } catch (ValueFormatException e) {
165                 multiValue = MULTIVALUE_TRUE;
166 
167             } catch (Exception e) {
168                 log.debug(e.getMessage(), e);
169             }
170         }
171         return this.multiValue;
172     }
173 
174     public String getAttribute(String name) {
175         throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
176     }
177 
178     public Collection<String> getAttributeNames() throws RepositoryException {
179         throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
180     }
181 
182     public void setAttribute(String name, String value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
183         throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
184     }
185 
186     public void setAttribute(String name, Calendar value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
187         throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
188     }
189 
190     public String toString() {
191         final StringBuilder buffer = new StringBuilder();
192         buffer.append(getHierarchyManager().getName()).append(":");
193         buffer.append(getHandle());
194         buffer.append("[");
195         buffer.append(NodeDataUtil.getTypeName(this));
196         buffer.append("]");
197 
198         return buffer.toString();
199     }
200 }