View Javadoc
1   /**
2    * This file Copyright (c) 2008-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.util;
35  
36  import info.magnolia.cms.core.AbstractContent;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.ItemType;
39  import info.magnolia.cms.core.NodeData;
40  
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.Collections;
44  import java.util.Comparator;
45  import java.util.List;
46  
47  import javax.jcr.PathNotFoundException;
48  import javax.jcr.RepositoryException;
49  
50  import org.apache.commons.lang.StringUtils;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  
55  /**
56   * This wrapper inherits content from the parent hierarchy. The method {@link #isAnchor()} defines
57   * the anchor to which the inheritance is performed relative to. By default the anchor is a page
58   * (mgnl:content).
59   * <p>
60   * The inheritance is then performed as follows:
61   * <ul>
62   * <li>try to get the content directly</li>
63   * <li>find next anchor</li>
64   * <li>try to get the content from the anchor</li>
65   * <li>repeat until no anchor can be found anymore (root)</li>
66   * </ul>
67   * <p>
68   * The {@link #getChildren()} methods merge the direct and inherited children by first adding the
69   * inherited children to the collection and then the direct children.
70   */
71  public class InheritanceContentWrapper extends ContentWrapper {
72  
73      private static Logger log = LoggerFactory.getLogger(InheritanceContentWrapper.class);
74  
75      /**
76       * From where the inheritance started.
77       */
78      private final Content start;
79  
80      /**
81       * Used if in the {@link #wrap(Content)} method.
82       */
83      public InheritanceContentWrapper(Content wrappedContent, Content start) {
84          super(wrappedContent);
85          this.start = start;
86      }
87  
88      /**
89       * Starts the inheritance.
90       */
91      public InheritanceContentWrapper(Content node) {
92          this(node, node);
93      }
94  
95      @Override
96      public boolean hasContent(String name) throws RepositoryException {
97          return getContentSafely(name) != null;
98      }
99  
100     @Override
101     public Content getContent(String name) throws RepositoryException {
102         Content inherited = getContentSafely(name);
103         if(inherited == null){
104             throw new PathNotFoundException("Can't inherit a node [" + name + "] on node [" + getWrappedContent().getHandle() + "]");
105         }
106         return inherited;
107     }
108 
109     @Override
110     public Collection<Content> getChildren(ContentFilter filter, String namePattern, Comparator<Content> orderCriteria){
111         List<Content> children = new ArrayList<Content>();
112 
113         // add inherited children
114         try {
115             Content inherited = getContentSafely(findNextAnchor(), resolveInnerPath());
116             if(inherited != null){
117                 children.addAll(((AbstractContent)inherited).getChildren(filter, namePattern, orderCriteria));
118             }
119         }
120         catch (RepositoryException e) {
121             throw new RuntimeException("Can't inherit children from " + getWrappedContent(), e);
122         }
123 
124         // add direct children
125         children.addAll(((AbstractContent)getWrappedContent()).getChildren(filter, namePattern, orderCriteria));
126         if(orderCriteria != null){
127             Collections.sort(children, orderCriteria);
128         }
129 
130         return wrapContentNodes(children);
131     }
132 
133     /**
134      * Returns the inner path of the this node up to the anchor.
135      */
136     protected String resolveInnerPath() throws RepositoryException {
137         final String path;
138         InheritanceContentWrapper anchor = findAnchor();
139         // if no anchor left we are relative to the root
140         if(anchor == null){
141             path = this.getHandle();
142         }
143         else{
144             path = StringUtils.substringAfter(this.getHandle(), anchor.getHandle());
145         }
146         return StringUtils.removeStart(path,"/");
147     }
148 
149     /**
150      * This method returns null if no content has been found.
151      */
152     protected Content getContentSafely(String name) throws RepositoryException {
153         if(getWrappedContent().hasContent(name)){
154             return super.getContent(name);
155         }
156 
157         String innerPath = resolveInnerPath() + "/" + name;
158         innerPath = StringUtils.removeStart(innerPath,"/");
159 
160         Content inherited = getContentSafely(findNextAnchor(), innerPath);
161         return inherited;
162     }
163 
164     /**
165      * This method returns null if no content has been found.
166      */
167     protected Content getContentSafely(InheritanceContentWrapper anchor, String path) throws RepositoryException{
168         if(anchor == null){
169             return null;
170         }
171         if(StringUtils.isEmpty(path)){
172             return anchor;
173         }
174         return anchor.getContentSafely(path);
175     }
176 
177     /**
178      * Find the anchor for this node.
179      */
180     protected InheritanceContentWrapper findAnchor() throws RepositoryException{
181         if(getLevel() ==0){
182             return null;
183         }
184         if(isAnchor()){
185             return this;
186         }
187         // until the current node is the anchor
188         return ((InheritanceContentWrapper)getParent()).findAnchor();
189     }
190 
191     /**
192      * Find next anchor.
193      */
194     protected InheritanceContentWrapper findNextAnchor() throws RepositoryException{
195         final InheritanceContentWrapper currentAnchor = findAnchor();
196         if(currentAnchor != null && getLevel() >0){
197             return ((InheritanceContentWrapper)currentAnchor.getParent()).findAnchor();
198         }
199         return null;
200     }
201 
202     /**
203      * True if this node is an anchor. By default true if this node is of type mgnl:content (page)
204      */
205     protected boolean isAnchor() {
206         return isNodeType(ItemType.CONTENT.getSystemName());
207     }
208 
209     @Override
210     public NodeData getNodeData(String name) {
211         try {
212             if (getWrappedContent().hasNodeData(name)) {
213                 return getWrappedContent().getNodeData(name);
214             }
215             Content inherited = getContentSafely(findNextAnchor(), resolveInnerPath());
216             if(inherited != null){
217                 return inherited.getNodeData(name);
218             }
219         }
220         catch (RepositoryException e) {
221             throw new RuntimeException("Can't inherit nodedata " + name + "  for " + getWrappedContent(), e);
222 
223         }
224         // creates a none existing node data in the standard manner
225         return super.getNodeData(name);
226     }
227 
228     @Override
229     public boolean hasNodeData(String name) throws RepositoryException {
230         try {
231             if (getWrappedContent().hasNodeData(name)) {
232                 return getWrappedContent().hasNodeData(name);
233             }
234             Content inherited = getContentSafely(findNextAnchor(), resolveInnerPath());
235             if (inherited != null) {
236                 return inherited.hasNodeData(name);
237             }
238         } catch (RepositoryException e) {
239             throw new RuntimeException("Can't inherit nodedata " + name + "  for " + getWrappedContent(), e);
240 
241         }
242         // creates a none existing node data in the standard manner
243         return super.hasNodeData(name);
244     }
245 
246     /**
247      * Wrap returned nodes. Sets the inherited flag
248      */
249     @Override
250     protected Content wrap(Content node) {
251         // only wrap once
252         if(node instanceof InheritanceContentWrapper){
253             return node;
254         }
255         return new InheritanceContentWrapper(node, start);
256     }
257 
258     /**
259      * True if this is not a sub node of the starting point.
260      */
261     public boolean isInherited() {
262         return !getWrappedContent().getHandle().startsWith(start.getHandle());
263     }
264 
265 }