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