View Javadoc

1   /**
2    * This file Copyright (c) 2011 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.rendering.template.configured;
35  
36  import info.magnolia.cms.core.MgnlNodeType;
37  import info.magnolia.jcr.RuntimeRepositoryException;
38  import info.magnolia.jcr.predicate.AbstractPredicate;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.objectfactory.Components;
41  import info.magnolia.rendering.template.InheritanceConfiguration;
42  import org.apache.commons.lang.StringUtils;
43  
44  import javax.jcr.Node;
45  import javax.jcr.RepositoryException;
46  import java.util.Comparator;
47  
48  /**
49   * Defines behavior for inheritance. Allows for enabling
50   *
51   * @version $Id$
52   */
53  public class ConfiguredInheritance implements InheritanceConfiguration {
54  
55      public static final String COMPONENTS_ALL = "all";
56      public static final String COMPONENTS_NONE = "none";
57      public static final String COMPONENTS_FILTERED = "filtered";
58  
59      public static final String PROPERTIES_ALL = "all";
60      public static final String PROPERTIES_NONE = "none";
61  
62      private boolean enabled = false;
63      private String components = COMPONENTS_FILTERED;
64      private String properties = PROPERTIES_ALL;
65      private Class<? extends AbstractPredicate<Node>> predicateClass;
66      private Class<? extends Comparator<Node>> nodeComparatorClass;
67  
68      @Override
69      public boolean isEnabled() {
70          return enabled;
71      }
72  
73      public void setEnabled(boolean enabled) {
74          this.enabled = enabled;
75      }
76  
77      public void setComponents(String components) {
78          this.components = components;
79      }
80  
81      public void setProperties(String properties) {
82          this.properties = properties;
83      }
84  
85      @Override
86      public boolean isInheritsProperties() {
87          return isEnabled() && StringUtils.equalsIgnoreCase(StringUtils.trim(properties), PROPERTIES_ALL);
88      }
89  
90      @Override
91      public boolean isInheritsComponents() {
92          return isEnabled() && (StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_ALL) || StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_FILTERED));
93      }
94  
95      @Override
96      public AbstractPredicate<Node> getComponentPredicate() {
97          if (!isEnabled()) {
98              return new InheritNothingInheritancePredicate();
99          }
100         if (predicateClass != null) {
101             return Components.newInstance(predicateClass);
102         }
103         if (StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_ALL)) {
104             return new AllComponentsInheritancePredicate();
105         }
106         if (StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_FILTERED)) {
107             return new FilteredComponentInheritancePredicate();
108         }
109         return new InheritNothingInheritancePredicate();
110     }
111 
112     public void setPredicateClass(Class<? extends AbstractPredicate<Node>> predicateClass) {
113         this.predicateClass = predicateClass;
114     }
115 
116     @Override
117     public Comparator<Node> getComponentComparator() {
118         if (nodeComparatorClass != null) {
119             return Components.newInstance(nodeComparatorClass);
120         }
121         return new NodeDepthComparator();
122     }
123 
124     public void setNodeComparatorClass(Class<? extends Comparator<Node>> nodeComparatorClass) {
125         this.nodeComparatorClass = nodeComparatorClass;
126     }
127 
128     /**
129      * Predicate for component inheritance that includes only nodes with a a property named 'inheritable' that needs to
130      * be present and set to 'true'.
131      */
132     public static class FilteredComponentInheritancePredicate extends AbstractPredicate<Node> {
133 
134         public static final String INHERITED_PROPERTY_NAME = "inheritable";
135 
136         @Override
137         public boolean evaluateTyped(Node node) {
138             try {
139                 return NodeUtil.isNodeType(node, MgnlNodeType.NT_COMPONENT) && (node.hasProperty(INHERITED_PROPERTY_NAME) && Boolean.parseBoolean(node.getProperty(INHERITED_PROPERTY_NAME).getString()));
140             } catch (RepositoryException e) {
141                 throw new RuntimeRepositoryException(e);
142             }
143         }
144     }
145 
146     /**
147      * Predicate for component inheritance that includes all components.
148      */
149     public static class AllComponentsInheritancePredicate extends AbstractPredicate<Node> {
150 
151         @Override
152         public boolean evaluateTyped(Node node) {
153             try {
154                 return NodeUtil.isNodeType(node, MgnlNodeType.NT_COMPONENT);
155             } catch (RepositoryException e) {
156                 throw new RuntimeRepositoryException(e);
157             }
158         }
159     }
160 
161     /**
162      * Predicate for component inheritance that includes no components.
163      */
164     public static class InheritNothingInheritancePredicate extends AbstractPredicate<Node> {
165 
166         @Override
167         public boolean evaluateTyped(Node node) {
168             return false;
169         }
170     }
171 
172     /**
173      * Comparator for ordering nodes by depth placing inherited nodes first.
174      */
175     public static class NodeDepthComparator implements Comparator<Node> {
176         @Override
177         public int compare(Node lhs, Node rhs) {
178             try {
179                 return rhs.getDepth() - lhs.getDepth();
180             } catch (RepositoryException e) {
181                 throw new RuntimeRepositoryException(e);
182             }
183         }
184     }
185 }