View Javadoc

1   /**
2    * This file Copyright (c) 2010-2014 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.lang.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   * @version $Id$
71   * @see InheritanceContentWrapper a class supporting content inheritance.
72   */
73  public class ExtendingContentWrapper extends ContentWrapper {
74  
75      protected static final String EXTENDING_NODE_DATA = "extends";
76      protected static final String EXTENDING_NODE_DATA_OVERRIDE = "override";
77  
78      private boolean extending;
79  
80      private Content extendedContent;
81  
82      private static final Logger log = LoggerFactory.getLogger(ExtendingContentWrapper.class);
83  
84      //This is just for test purposes. Use ExtendingContentWrapper(Content wrappedContent) as was here before.
85      ExtendingContentWrapper(Content wrappedContent, boolean failOnError) {
86  
87          super(wrappedContent);
88          try {
89              extending = getWrappedContent().hasNodeData(EXTENDING_NODE_DATA);
90              if (extending) {
91                  NodeData extendingNodeData = getWrappedContent().getNodeData(EXTENDING_NODE_DATA);
92  
93                  // check if override is not forced
94                  String extendedNode = extendingNodeData.getString();
95  
96                  Content parent = extendingNodeData.getParent();
97  
98                  if (StringUtils.isBlank(extendedNode)) {
99                      // there is nothing to do, extending node is not defined ... probably caught in middle of config
100                     extending = false;
101                 } else if (EXTENDING_NODE_DATA_OVERRIDE.equals(extendedNode)) {
102                     extending = false;
103                 }
104                 if (extending) {
105                     if (isExists(extendedNode, parent)) {
106                         // support multiple inheritance
107                         extendedContent = wrapIfNeeded(extendingNodeData.getReferencedContent());
108                     } else {
109                         String message = "Can't find referenced node for value: " + wrappedContent;
110                         log.error(message);
111                         extending = false;
112                         if (failOnError) {
113                             throw new RuntimeException(message);
114                         }
115                     }
116 
117                 }
118 
119             }
120         } catch (RepositoryException e) {
121             throw new RuntimeException("Can't wrap node [" + wrappedContent + "]", e);
122         }
123     }
124 
125     public ExtendingContentWrapper(Content wrappedContent) {
126         this(wrappedContent, false);
127     }
128 
129     /**
130      * Does not support the extends nodedata but chains the two nodes directly. Each node is
131      * wrapped internally to ensure that each of them support the extends nodedata for themselves.
132      */
133     protected ExtendingContentWrapper(Content wrappedContent, Content extendedContent) {
134         super(wrapIfNeeded(wrappedContent));
135         extending = true;
136         try {
137             if (getWrappedContent().hasNodeData(EXTENDING_NODE_DATA)) {
138                 NodeData extendingNodeData = getWrappedContent().getNodeData(EXTENDING_NODE_DATA);
139 
140                 // check if override is not forced
141                 extending = !EXTENDING_NODE_DATA_OVERRIDE.equals(extendingNodeData.getString());
142             }
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 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             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     @Override
182     public boolean hasContent(String name) throws RepositoryException {
183         if (getWrappedContent().hasContent(name)) {
184             return true;
185         }
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         }
198         else if (extending && extendedContent.hasContent(name)) {
199             content = extendedContent.getContent(name);
200         }
201         else {
202             // this will throw a PathNotFoundException
203             content = getWrappedContent().getContent(name);
204         }
205         return wrap(content);
206     }
207 
208     @Override
209     public Collection<Content> getChildren(ContentFilter filter, String namePattern, Comparator<Content> orderCriteria) {
210         Collection<Content> directChildren = ((AbstractContent)getWrappedContent()).getChildren(filter, namePattern, orderCriteria);
211         if (extending) {
212             Collection<Content> inheritedChildren = ((AbstractContent)extendedContent).getChildren(filter, namePattern, orderCriteria);
213             // keep order, add new elements at the end of the collection
214             LinkedHashMap<String, Content> merged = new LinkedHashMap<String, Content>();
215             for (Content content : inheritedChildren) {
216                 merged.put(content.getName(), content);
217             }
218             for (Content content : directChildren) {
219                 merged.put(content.getName(), content);
220             }
221             return wrapContentNodes(merged.values());
222         }
223         return wrapContentNodes(directChildren);
224     }
225 
226     @Override
227     public NodeData getNodeData(String name) {
228         if (EXTENDING_NODE_DATA.equals(name)) {
229             return new DefaultNodeData(extendedContent, name) {
230                 @Override
231                 public boolean isExist() {
232                     return false;
233                 }
234 
235                 @Override
236                 public void save() throws RepositoryException {
237                     // do nothing
238                 }
239             };
240         }
241         try {
242             if (getWrappedContent().hasNodeData(name)) {
243                 return wrap(getWrappedContent().getNodeData(name));
244             }
245             else if (extending && extendedContent.hasNodeData(name)) {
246                 return wrap(extendedContent.getNodeData(name));
247             }
248             else {
249                 return wrap(getWrappedContent().getNodeData(name));
250             }
251         }
252         catch (RepositoryException e) {
253             throw new RuntimeException("Can't read nodedata from extended node [" + extendedContent + "]", e);
254         }
255     }
256 
257     @Override
258     public boolean hasNodeData(String name) throws RepositoryException {
259         if (EXTENDING_NODE_DATA.equals(name)) {
260             return false;
261         }
262         return super.hasNodeData(name);
263     }
264 
265     @Override
266     public Collection<NodeData> getNodeDataCollection() {
267         final Content wrapped = getWrappedContent();
268         Collection<NodeData> directChildren = wrapped.getNodeDataCollection();
269         try {
270             if (wrapped.hasNodeData(EXTENDING_NODE_DATA)) {
271                 for (NodeData child : directChildren) {
272                     if (EXTENDING_NODE_DATA.equals(child.getName())) {
273                         directChildren.remove(child);
274                         break;
275                     }
276                 }
277             }
278         } catch (RepositoryException e) {
279             throw new RuntimeException("Can't read nodedata collection from node [" + wrapped.getHandle() + "]", e);
280         }
281         if (extending) {
282             Collection<NodeData> inheritedChildren = extendedContent.getNodeDataCollection();
283             // sort by name
284             SortedMap<String, NodeData> merged = new TreeMap<String, NodeData>();
285             for (NodeData nodeData : inheritedChildren) {
286                 merged.put(nodeData.getName(), nodeData);
287             }
288             for (NodeData nodeData : directChildren) {
289                 merged.put(nodeData.getName(), nodeData);
290             }
291             return wrapNodeDatas(merged.values());
292         }
293         return wrapNodeDatas(directChildren);
294     }
295 
296     @Override
297     protected Content wrap(Content node) {
298         // get the same subnode of the extended content
299         try {
300             if (extending && extendedContent.hasContent(node.getName())) {
301                 // FIXME we have to calculate the relative path
302                 Content extendedSubContent = extendedContent.getContent(node.getName());
303                 return new ExtendingContentWrapper(node, extendedSubContent);
304             }
305         }
306         catch (RepositoryException e) {
307             throw new RuntimeException("Can't wrap " + node, e);
308         }
309         return wrapIfNeeded(node);
310     }
311 
312 }