View Javadoc
1   /**
2    * This file Copyright (c) 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.contentapp.drop;
35  
36  import static org.apache.commons.lang3.StringUtils.appendIfMissing;
37  
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.ui.contentapp.browser.drop.AbstractDropConstraint;
41  
42  import javax.inject.Inject;
43  import javax.jcr.Item;
44  import javax.jcr.Node;
45  import javax.jcr.Property;
46  import javax.jcr.RepositoryException;
47  
48  import com.vaadin.shared.ui.grid.DropLocation;
49  
50  import lombok.SneakyThrows;
51  
52  /**
53   * Default drop constraint for JCR content apps.
54   *
55   * Compares node types of source and target nodes.
56   */
57  public class JcrDropConstraint extends AbstractDropConstraint<JcrDropConstraintDefinition, Item> {
58  
59      @Inject
60      public JcrDropConstraint(JcrDropConstraintDefinition definition) {
61          super(definition);
62      }
63  
64      @SneakyThrows(RepositoryException.class)
65      @Override
66      public boolean isAllowedAt(Item source, Item targetItem, DropLocation location) {
67          switch (location) {
68          case ON_TOP:
69              return allowedAsChild(source, targetItem);
70          case BELOW:
71          case ABOVE:
72              return allowedAsSibling(source, targetItem);
73          }
74          return false;
75      }
76  
77      protected boolean allowedAsChild(Item source, Item target) throws RepositoryException {
78          boolean movable = basicCheck(source, target);
79          if (movable) {
80              // Cannot move node/property to its parent
81              if (target.isSame(source.getParent())
82                      // Forbid same-name siblings
83                      || ((Node) target).hasNode(source.getName())
84                      || ((Node) target).hasProperty(source.getName())) {
85                  return false;
86              }
87              // If source is a folder and target is not a folder, return false
88              if (source.isNode()
89                      && NodeUtil.isNodeType((Node) source, NodeTypes.Folder.NAME)
90                      && !(isRoot(target) || NodeUtil.isNodeType((Node) target, NodeTypes.Folder.NAME))) {
91                      return false;
92              }
93              Node targetNode = (Node) target;
94  
95              if (source.isNode()) {
96                  Node sourceNode = (Node) source;
97                  // If both nodes are of specific primaryNodeType, return false.
98                  if (getDefinition().getPrimaryNodeType() != null && NodeUtil.isNodeType(sourceNode,
99                          getDefinition().getPrimaryNodeType()) && NodeUtil.isNodeType(targetNode,
100                         getDefinition().getPrimaryNodeType())) {
101                     return false;
102                 }
103                 return targetNode.getPrimaryNodeType().canAddChildNode(sourceNode.getName(), sourceNode.getPrimaryNodeType().getName());
104             } else {
105                 Property sourceProperty = (Property) source;
106                 if (sourceProperty.isMultiple()) {
107                     return targetNode.getPrimaryNodeType().canSetProperty(sourceProperty.getName(), sourceProperty.getValues());
108                 } else {
109                     return targetNode.getPrimaryNodeType().canSetProperty(sourceProperty.getName(), sourceProperty.getValue());
110                 }
111             }
112         }
113         return false;
114     }
115 
116     private static boolean isRoot(Item item) throws RepositoryException {
117         return item.getDepth() == 0;
118     }
119 
120     private boolean basicCheck(Item source, Item target) throws RepositoryException {
121         // Cannot move anythings to property
122         if (!target.isNode()) {
123             return false;
124         }
125         // Destination is not itself nor a descendant of source
126         return !isAncestorOrSelf(source, target);
127     }
128 
129     private boolean isAncestorOrSelf(Item ancestor, Item node) throws RepositoryException {
130         return (appendIfMissing(node.getPath(), "/")).startsWith(appendIfMissing(ancestor.getPath(), "/"));
131     }
132 
133     protected boolean allowedAsSibling(Item source, Item target) throws RepositoryException {
134         // Property is not movable
135         if (!source.isNode()) {
136             return false;
137         }
138         boolean movable = basicCheck(source, target) && !isRoot(target);
139         // Cannot move after/before node which has sibling with same name and different parent
140         return (!movable ||
141                 // Cannot move after/before node which has sibling with same name and different parent
142                 !target.getParent().hasNode(source.getName()) || target.getParent().isSame(source.getParent())) && movable;
143     }
144 }