View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.ui.framework.availability;
35  
36  import info.magnolia.objectfactory.ComponentProvider;
37  import info.magnolia.ui.api.availability.AvailabilityChecker;
38  import info.magnolia.ui.api.availability.AvailabilityDefinition;
39  import info.magnolia.ui.api.availability.AvailabilityRule;
40  import info.magnolia.ui.api.availability.AvailabilityRuleDefinition;
41  import info.magnolia.ui.framework.availability.shorthandrules.AccessGrantedRule;
42  import info.magnolia.ui.framework.availability.shorthandrules.JcrNodeTypesAllowedRule;
43  import info.magnolia.ui.framework.availability.shorthandrules.JcrNodesAllowedRule;
44  import info.magnolia.ui.framework.availability.shorthandrules.JcrPropertiesAllowedRule;
45  import info.magnolia.ui.framework.availability.shorthandrules.JcrRootAllowedRule;
46  import info.magnolia.ui.framework.availability.shorthandrules.MultipleItemsAllowedRule;
47  import info.magnolia.ui.framework.availability.shorthandrules.WritePermissionRequiredRule;
48  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
49  
50  import java.util.ArrayList;
51  import java.util.Collection;
52  import java.util.Iterator;
53  import java.util.List;
54  
55  import javax.inject.Inject;
56  
57  import org.apache.commons.lang3.ObjectUtils;
58  
59  /**
60   * Implements {@link info.magnolia.ui.api.availability.AvailabilityChecker}.
61   */
62  public class AvailabilityCheckerImpl implements AvailabilityChecker {
63  
64      private final ComponentProvider componentProvider;
65  
66      private final ContentConnector contentConnector;
67  
68      private JcrNodesAllowedRule jcrNodesAllowedRule;
69      private JcrPropertiesAllowedRule jcrPropertiesAllowedRule;
70      private MultipleItemsAllowedRule multipleItemsAllowedRule;
71      private JcrRootAllowedRule jcrRootAllowedRule;
72      private JcrNodeTypesAllowedRule jcrNodeTypesAllowedRule;
73      private AccessGrantedRule accessGrantedRule;
74      private WritePermissionRequiredRule writePermissionRequiredRule;
75  
76      @Inject
77      public AvailabilityCheckerImpl(ComponentProvider componentProvider, ContentConnector contentConnector) {
78          this.componentProvider = componentProvider;
79          this.contentConnector = contentConnector;
80          this.jcrNodesAllowedRule = createJcrNodesAllowedRule();
81          this.jcrPropertiesAllowedRule = createJcrPropertiesAllowedRule();
82          this.jcrRootAllowedRule = createJcrRootAllowedRule();
83          this.jcrNodeTypesAllowedRule = createJcrNodeTypesAllowedRule();
84          this.multipleItemsAllowedRule = createMultipleItemsAllowedRule();
85          this.accessGrantedRule = createAccessGrantedRule();
86          this.writePermissionRequiredRule = createWritePermissionRequiredRule();
87      }
88  
89      @Override
90      public boolean isAvailable(AvailabilityDefinition definition, List<Object> ids) {
91          // Prepare rules
92          List<AvailabilityRule> rules = prepareRules(definition);
93  
94          List<Object> idsToCheck = new ArrayList<Object>(ids);
95          Object defaultItemId = contentConnector.getDefaultItemId();
96          // In case we have no id's (no selection) add the default itemId defined by the contentConnector.
97          if (idsToCheck.isEmpty() && defaultItemId != null) {
98              idsToCheck.add(defaultItemId);
99          }
100 
101         // Since we can't combine rules with AND/OR operands, 'root' availability should keep precedence over nodes/nodeTypes-allowed rules when the default item is selected.
102         if (definition.isRoot() && idsToCheck.size() == 1 && ObjectUtils.equals(idsToCheck.get(0), defaultItemId)) {
103             rules.remove(jcrNodesAllowedRule);
104             rules.remove(jcrNodeTypesAllowedRule);
105         }
106 
107         boolean isAvailable = true;
108         Iterator<AvailabilityRule> ruleIterator = rules.iterator();
109         while (isAvailable && ruleIterator.hasNext()) {
110             AvailabilityRule rule = ruleIterator.next();
111             boolean ruleHolds = rule.isAvailable(idsToCheck);
112             isAvailable &= ruleHolds;
113         }
114         return isAvailable;
115     }
116 
117     private List<AvailabilityRule> prepareRules(AvailabilityDefinition definition) {
118         List<AvailabilityRule> rules = new ArrayList<AvailabilityRule>();
119         rules.addAll(prepareShorthandRules(definition));
120         for (AvailabilityRuleDefinition ruleDefinition : definition.getRules()) {
121             rules.add(componentProvider.newInstance(ruleDefinition.getImplementationClass(), ruleDefinition));
122         }
123         return rules;
124     }
125 
126     private Collection<? extends AvailabilityRule> prepareShorthandRules(AvailabilityDefinition definition) {
127         jcrNodesAllowedRule.setNodesAllowed(definition.isNodes());
128         jcrPropertiesAllowedRule.setPropertiesAllowed(definition.isProperties());
129         jcrRootAllowedRule.setRootAllowed(definition.isRoot());
130         jcrNodeTypesAllowedRule.setNodeTypes(definition.getNodeTypes());
131         multipleItemsAllowedRule.setMultipleItemsAllowed(definition.isMultiple());
132         accessGrantedRule.setAccessDefinition(definition.getAccess());
133         writePermissionRequiredRule.setWritePermissionRequired(definition.isWritePermissionRequired());
134 
135         // set default itemId to check "root" availability against
136         Object defaultItemId = contentConnector.getDefaultItemId();
137         jcrRootAllowedRule.setDefaultItemId(defaultItemId);
138 
139         List<AvailabilityRule> shorthands = new ArrayList<AvailabilityRule>();
140         shorthands.add(jcrRootAllowedRule);
141         shorthands.add(jcrNodesAllowedRule);
142         shorthands.add(jcrPropertiesAllowedRule);
143         shorthands.add(jcrNodeTypesAllowedRule);
144         shorthands.add(multipleItemsAllowedRule);
145         shorthands.add(accessGrantedRule);
146         shorthands.add(writePermissionRequiredRule);
147 
148         return shorthands;
149     }
150 
151     protected JcrNodesAllowedRule createJcrNodesAllowedRule() {
152         return new JcrNodesAllowedRule();
153     }
154 
155     protected JcrPropertiesAllowedRule createJcrPropertiesAllowedRule() {
156         return new JcrPropertiesAllowedRule();
157     }
158 
159     protected MultipleItemsAllowedRule createMultipleItemsAllowedRule() {
160         return new MultipleItemsAllowedRule();
161     }
162 
163     protected JcrRootAllowedRule createJcrRootAllowedRule() {
164         return new JcrRootAllowedRule();
165     }
166 
167     protected JcrNodeTypesAllowedRule createJcrNodeTypesAllowedRule() {
168         return new JcrNodeTypesAllowedRule();
169     }
170 
171     protected AccessGrantedRule createAccessGrantedRule() {
172         return new AccessGrantedRule();
173     }
174 
175     protected WritePermissionRequiredRule createWritePermissionRequiredRule() {
176         return new WritePermissionRequiredRule();
177     }
178 }