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