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