View Javadoc
1   /**
2    * This file Copyright (c) 2010-2015 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  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  import java.util.Calendar;
47  import java.util.Collection;
48  
49  /**
50   * Implementing some default behavior.
51   */
52  public abstract class AbstractNodeData implements NodeData{
53      private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractNodeData.class);
54  
55      protected String name;
56      private final Content parent;
57      private int multiValue = MULTIVALUE_UNDEFINED;
58  
59      protected AbstractNodeData(Content parent, String name) {
60          this.parent = parent;
61          this.name = name;
62      }
63  
64      @Override
65      public HierarchyManager getHierarchyManager() {
66          return parent.getHierarchyManager();
67      }
68  
69      @Override
70      public String getName() {
71          return this.name;
72      }
73  
74      @Override
75      public String getHandle() {
76          return Path.getAbsolutePath(parent.getHandle(), name);
77      }
78  
79      @Override
80      public boolean isGranted(long permissions) {
81          return getHierarchyManager().isGranted(getHandle(), permissions);
82      }
83  
84      @Override
85      public String getString(String lineBreak) {
86          return getString().replaceAll("\n", lineBreak);
87      }
88  
89      @Override
90      public Content getParent() {
91          return this.parent;
92      }
93  
94      @Override
95      public Content getReferencedContent(String repositoryId) throws PathNotFoundException, RepositoryException {
96          if(getParent().getWorkspace().getName().equals(repositoryId)){
97              return getReferencedContent();
98          }
99          return getReferencedContent(MgnlContext.getHierarchyManager(repositoryId));
100     }
101 
102     @Override
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     @Override
157     public int isMultiValue() {
158         if(multiValue == MULTIVALUE_UNDEFINED) {
159             try {
160                 if (isExist()) {
161                     getJCRProperty().getValue();
162                     multiValue = MULTIVALUE_FALSE;
163                 }
164 
165             } catch (ValueFormatException e) {
166                 multiValue = MULTIVALUE_TRUE;
167 
168             } catch (Exception e) {
169                 log.debug(e.getMessage(), e);
170             }
171         }
172         return this.multiValue;
173     }
174 
175     @Override
176     public String getAttribute(String name) {
177         throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
178     }
179 
180     @Override
181     public Collection<String> getAttributeNames() throws RepositoryException {
182         throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
183     }
184 
185     @Override
186     public void setAttribute(String name, String value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
187         throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
188     }
189 
190     @Override
191     public void setAttribute(String name, Calendar value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
192         throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
193     }
194 
195     @Override
196     public String toString() {
197         String workspaceName = "";
198         try {
199             workspaceName = getParent().getWorkspace().getName();
200         } catch (Exception e) {
201             // ignore
202         }
203 
204         final StringBuilder builder = new StringBuilder();
205         builder.append(workspaceName).append(":");
206         builder.append(getHandle());
207         builder.append("[");
208         builder.append(NodeDataUtil.getTypeName(this));
209         builder.append("]");
210 
211         return builder.toString();
212     }
213 }