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.util;
35  
36  import info.magnolia.cms.core.AbstractContent;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.DefaultNodeData;
39  import info.magnolia.cms.core.NodeData;
40  
41  import java.util.Collection;
42  import java.util.Comparator;
43  import java.util.LinkedHashMap;
44  import java.util.SortedMap;
45  import java.util.TreeMap;
46  
47  import javax.jcr.RepositoryException;
48  
49  import org.apache.commons.lang3.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  
54  /**
55   * This wrapper allows extending other nodes (mainly useful to extend configurations). A content node can define
56   * a nodeData with the name 'extends'. Its value is either an absolute or relative path. The merge is then performed as follows:
57   *
58   * <ul>
59   * <li>nodeDatas are merged and values are overwritten
60   * <li>sub content nodes are merged, the original order is guaranteed, new nodes are added at the
61   * end of the list
62   * </ul>
63   *
64   * The mechanism supports multiple inheritances as such:
65   * <ul>
66   * <li>the node the current node inherits from can again extend a node
67   * <li>nodes laying deeper in the hierarchy can extend an other node
68   * </ul>
69   *
70   * @see InheritanceContentWrapper a class supporting content inheritance.
71   * @deprecated since Magnolia 5.0 - use {@link info.magnolia.jcr.wrapper.ExtendingNodeWrapper} instead.
72   */
73  @Deprecated
74  public class ExtendingContentWrapper extends ContentWrapper {
75  
76      protected static final String EXTENDING_NODE_DATA = "extends";
77      protected static final String EXTENDING_NODE_DATA_OVERRIDE = "override";
78  
79      private boolean extending;
80  
81      private Content extendedContent;
82  
83      private static final Logger log = LoggerFactory.getLogger(ExtendingContentWrapper.class);
84  
85      //This is just for test purposes. Use ExtendingContentWrapper(Content wrappedContent) as was here before.
86      ExtendingContentWrapper(Content wrappedContent, boolean failOnError) {
87  
88          super(wrappedContent);
89          try {
90              extending = getWrappedContent().hasNodeData(EXTENDING_NODE_DATA);
91              if (extending) {
92                  NodeData extendingNodeData = getWrappedContent().getNodeData(EXTENDING_NODE_DATA);
93  
94                  // check if override is not forced
95                  String extendedNode = extendingNodeData.getString();
96  
97                  Content parent = extendingNodeData.getParent();
98  
99                  if (StringUtils.isBlank(extendedNode)) {
100                     // there is nothing to do, extending node is not defined ... probably caught in middle of config
101                     extending = false;
102                 } else if (EXTENDING_NODE_DATA_OVERRIDE.equals(extendedNode)) {
103                     extending = false;
104                 }
105                 if (extending) {
106                     if (isExists(extendedNode, parent)) {
107                         // support multiple inheritance
108                         extendedContent = wrapIfNeeded(extendingNodeData.getReferencedContent());
109                     } else {
110                         String message = "Can't find referenced node for value: " + wrappedContent;
111                         log.error(message);
112                         extending = false;
113                         if (failOnError) {
114                             throw new RuntimeException(message);
115                         }
116                     }
117 
118                 }
119 
120             }
121         } catch (RepositoryException e) {
122             throw new RuntimeException("Can't wrap node [" + wrappedContent + "]", e);
123         }
124     }
125 
126     public ExtendingContentWrapper(Content wrappedContent) {
127         this(wrappedContent, false);
128     }
129 
130     /**
131      * Does not support the extends nodedata but chains the two nodes directly. Each node is
132      * wrapped internally to ensure that each of them support the extends nodedata for themselves.
133      */
134     protected ExtendingContentWrapper(Content./../info/magnolia/cms/core/Content.html#Content">Content wrappedContent, Content extendedContent) {
135         super(wrapIfNeeded(wrappedContent));
136         extending = true;
137         try {
138             if (getWrappedContent().hasNodeData(EXTENDING_NODE_DATA)) {
139                 NodeData extendingNodeData = getWrappedContent().getNodeData(EXTENDING_NODE_DATA);
140 
141                 // check if override is not forced
142                 extending = !EXTENDING_NODE_DATA_OVERRIDE.equals(extendingNodeData.getString());
143             }
144         } catch (RepositoryException e) {
145             throw new RuntimeException("Can't determine extends point for node [" + wrappedContent + "]", e);
146         }
147         // might extend further more
148         this.extendedContent = wrapIfNeeded(extendedContent);
149     }
150 
151     private boolean isExists(String extendedNode, Content parent) throws RepositoryException {
152         if (extendedNode.startsWith("/")) {
153             return getWrappedContent().getHierarchyManager().isExist(extendedNode);
154         }
155         return parent.hasContent(extendedNode);
156     }
157 
158     private static Content./../../info/magnolia/cms/core/Content.html#Content">Content wrapIfNeeded(Content content) {
159         if (content instanceof ExtendingContentWrapper) {
160             return content;
161         }
162         return new ExtendingContentWrapper(content);
163     }
164 
165     public boolean isExtending() {
166         return this.extending;
167     }
168 
169     @Override
170     public Content getWrappedContent() {
171         Content wrapped = super.getWrappedContent();
172         if (wrapped instanceof ExtendingContentWrapper) {
173             ExtendingContentWrapperolia/cms/util/ExtendingContentWrapper.html#ExtendingContentWrapper">ExtendingContentWrapper wrappedECW = (ExtendingContentWrapper) wrapped;
174             if (!wrappedECW.extending) {
175                 // wrapped but not extending ==> should not be wrapped in the first place but decision is made too late - in init<> of the ECW
176                 return ((ExtendingContentWrapper) wrapped).getWrappedContent();
177             }
178         }
179         return super.getWrappedContent();
180     }
181 
182     @Override
183     public boolean hasContent(String name) throws RepositoryException {
184         if (getWrappedContent().hasContent(name)) {
185             return true;
186         } else if (extending && extendedContent.hasContent(name)) {
187             return true;
188         }
189         return false;
190     }
191 
192     @Override
193     public Content getContent(String name) throws RepositoryException {
194         Content content;
195         if (getWrappedContent().hasContent(name)) {
196             content = getWrappedContent().getContent(name);
197         } else if (extending && extendedContent.hasContent(name)) {
198             content = extendedContent.getContent(name);
199         } else {
200             // this will throw a PathNotFoundException
201             content = getWrappedContent().getContent(name);
202         }
203         return wrap(content);
204     }
205 
206     @Override
207     public Collection<Content> getChildren(ContentFilter filter, String namePattern, Comparator<Content> orderCriteria) {
208         Collection<Content> directChildren = ((AbstractContent) getWrappedContent()).getChildren(filter, namePattern, orderCriteria);
209         if (extending) {
210             Collection<Content> inheritedChildren = ((AbstractContent) extendedContent).getChildren(filter, namePattern, orderCriteria);
211             // keep order, add new elements at the end of the collection
212             LinkedHashMap<String, Content> merged = new LinkedHashMap<String, Content>();
213             for (Content content : inheritedChildren) {
214                 merged.put(content.getName(), content);
215             }
216             for (Content content : directChildren) {
217                 merged.put(content.getName(), content);
218             }
219             return wrapContentNodes(merged.values());
220         }
221         return wrapContentNodes(directChildren);
222     }
223 
224     @Override
225     public NodeData getNodeData(String name) {
226         if (EXTENDING_NODE_DATA.equals(name)) {
227             return new DefaultNodeData(extendedContent, name) {
228                 @Override
229                 public boolean isExist() {
230                     return false;
231                 }
232 
233                 @Override
234                 public void save() throws RepositoryException {
235                     // do nothing
236                 }
237             };
238         }
239         try {
240             if (getWrappedContent().hasNodeData(name)) {
241                 return wrap(getWrappedContent().getNodeData(name));
242             } else if (extending && extendedContent.hasNodeData(name)) {
243                 return wrap(extendedContent.getNodeData(name));
244             } else {
245                 return wrap(getWrappedContent().getNodeData(name));
246             }
247         } catch (RepositoryException e) {
248             throw new RuntimeException("Can't read nodedata from extended node [" + extendedContent + "]", e);
249         }
250     }
251 
252     @Override
253     public boolean hasNodeData(String name) throws RepositoryException {
254         if (EXTENDING_NODE_DATA.equals(name)) {
255             return false;
256         }
257         return super.hasNodeData(name);
258     }
259 
260     @Override
261     public Collection<NodeData> getNodeDataCollection() {
262         final Content wrapped = getWrappedContent();
263         Collection<NodeData> directChildren = wrapped.getNodeDataCollection();
264         try {
265             if (wrapped.hasNodeData(EXTENDING_NODE_DATA)) {
266                 for (NodeData child : directChildren) {
267                     if (EXTENDING_NODE_DATA.equals(child.getName())) {
268                         directChildren.remove(child);
269                         break;
270                     }
271                 }
272             }
273         } catch (RepositoryException e) {
274             throw new RuntimeException("Can't read nodedata collection from node [" + wrapped.getHandle() + "]", e);
275         }
276         if (extending) {
277             Collection<NodeData> inheritedChildren = extendedContent.getNodeDataCollection();
278             // sort by name
279             SortedMap<String, NodeData> merged = new TreeMap<String, NodeData>();
280             for (NodeData nodeData : inheritedChildren) {
281                 merged.put(nodeData.getName(), nodeData);
282             }
283             for (NodeData nodeData : directChildren) {
284                 merged.put(nodeData.getName(), nodeData);
285             }
286             return wrapNodeDatas(merged.values());
287         }
288         return wrapNodeDatas(directChildren);
289     }
290 
291     @Override
292     protected Contentef="../../../../info/magnolia/cms/core/Content.html#Content">Content wrap(Content node) {
293         // get the same subnode of the extended content
294         try {
295             if (extending && extendedContent.hasContent(node.getName())) {
296                 // FIXME we have to calculate the relative path
297                 Content extendedSubContent = extendedContent.getContent(node.getName());
298                 return new ExtendingContentWrapper(node, extendedSubContent);
299             }
300         } catch (RepositoryException e) {
301             throw new RuntimeException("Can't wrap " + node, e);
302         }
303         return wrapIfNeeded(node);
304     }
305 
306 }