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