View Javadoc
1   /**
2    * This file Copyright (c) 2017-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.workbench.tree.drop;
35  
36  import info.magnolia.jcr.util.NodeTypes;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
39  import info.magnolia.ui.vaadin.integration.contentconnector.JcrContentConnector;
40  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
41  
42  import javax.inject.Inject;
43  import javax.jcr.Node;
44  import javax.jcr.RepositoryException;
45  
46  import org.apache.commons.lang3.StringUtils;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  import com.vaadin.v7.data.Item;
51  
52  /**
53   * Default drop constraint for content apps.
54   *
55   * Compares node types of source and target nodes.
56   *
57   * @deprecated since 6.0. Use new framework and {@link info.magnolia.ui.contentapp.drop.JcrDropConstraint} instead.
58   */
59  @Deprecated
60  public class JcrDropConstraint extends AlwaysTrueDropConstraint implements DropConstraint {
61  
62      private static final Logger log = LoggerFactory.getLogger(JcrDropConstraint.class);
63  
64      private final ContentConnector contentConnector;
65  
66      @Inject
67      public JcrDropConstraint(ContentConnector contentConnector) {
68          this.contentConnector = contentConnector;
69      }
70  
71      /**
72       * Backward compatibility constructor.
73       *
74       * @deprecated since 5.5.5, use {@link #JcrDropConstraint(ContentConnector)} instead.
75       */
76      @Deprecated
77      public JcrDropConstraint() {
78          this(null);
79      }
80  
81      @Override
82      public boolean allowedAsChild(Item sourceItem, Item targetItem) {
83          if ((sourceItem instanceof JcrItemAdapteri/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter) && (targetItem instanceof JcrItemAdapter)) {
84              JcrItemAdapter/../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter source = (JcrItemAdapter) sourceItem;
85              JcrItemAdapter/../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter target = (JcrItemAdapter) targetItem;
86  
87              return allowedAsChild(source.getJcrItem(), target.getJcrItem());
88          }
89  
90          return true;
91      }
92  
93      protected boolean allowedAsChild(javax.jcr.Item source, javax.jcr.Item target) {
94          try {
95              boolean movable = basicCheck(source, target);
96              if (movable) {
97                  // Cannot move node/property to its parent
98                  if (target.isSame(source.getParent())
99                              // Forbid same-name siblings
100                             || ((Node) target).hasNode(source.getName())
101                             || ((Node) target).hasProperty(source.getName())) {
102                     return false;
103                 }
104 
105                 // If source is a folder and target is not a folder or root, return false
106                 if (source.isNode()
107                         && NodeUtil.isNodeType((Node) source, NodeTypes.Folder.NAME)
108                         && !(isRoot(target) || NodeUtil.isNodeType((Node) target, NodeTypes.Folder.NAME))) {
109                     log.debug("Could not move a Folder under a node", ((Node) source).getPrimaryNodeType().getName());
110                     return false;
111                 }
112             }
113 
114             return movable;
115         } catch (RepositoryException e) {
116             log.warn("Cannot check allow as child due to exception: ", e);
117         }
118 
119         return true;
120     }
121 
122     protected boolean basicCheck(javax.jcr.Item source, javax.jcr.Item target) {
123         // Cannot move anythings to property
124         if (!target.isNode()) {
125             log.debug("Could not move a node under a property");
126             return false;
127         }
128 
129         // Destination is not itself nor a descendant of source
130         if (isAncestorOf(source, target)) {
131             return false;
132         }
133 
134         return true;
135     }
136 
137     protected boolean isAncestorOf(javax.jcr.Item ancestor, javax.jcr.Item node) {
138         try {
139             for (javax.jcr.Item n = node; !isRoot(n); n = n.getParent()) {
140                 if (ancestor.isSame(n)) {
141                     return true;
142                 }
143             }
144         } catch (RepositoryException e) {
145             log.warn("Cannot get ancestor of item:", e);
146         }
147         return false;
148     }
149 
150     protected boolean isRoot(javax.jcr.Item item) {
151         try {
152             // The check makes sure backward compatibility in injecting ContentConnector in {@link #JcrDropConstraint()}.
153             String rootPath = contentConnector instanceof JcrContentConnector./../../../info/magnolia/ui/vaadin/integration/contentconnector/JcrContentConnector.html#JcrContentConnector">JcrContentConnector ? ((JcrContentConnector) contentConnector).getContentConnectorDefinition().getRootPath() : "/";
154             if (item != null && item.isNode() && (item.getParent() == null || StringUtils.equals(item.getPath(), rootPath))) {
155                 return true;
156             }
157         } catch (RepositoryException e) {
158             return true;
159         }
160 
161         return false;
162     }
163 
164     @Override
165     public boolean allowedBefore(Item sourceItem, Item targetItem) {
166         if ((sourceItem instanceof JcrItemAdapteri/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter) && (targetItem instanceof JcrItemAdapter)) {
167             JcrItemAdapter/../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter source = (JcrItemAdapter) sourceItem;
168             JcrItemAdapter/../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter target = (JcrItemAdapter) targetItem;
169 
170             return allowedSibling(source.getJcrItem(), target.getJcrItem());
171         }
172         return true;
173     }
174 
175     protected boolean allowedSibling(javax.jcr.Item source, javax.jcr.Item target) {
176         try {
177             // Property is not movable
178             if (!source.isNode()) {
179                 return false;
180             }
181 
182             boolean movable = basicCheck(source, target);
183             if (movable &&
184                     // Cannot move before/after on root
185                     (isRoot(target) ||
186                     // Cannot move after/before node which has sibling with same name and different parent
187                     target.getParent().hasNode(source.getName()) && !target.getParent().isSame(source.getParent()))) {
188                 return false;
189             }
190 
191             return movable;
192         } catch (RepositoryException e) {
193             log.warn("Cannot check allow before due to exception: ", e);
194         }
195 
196         return true;
197     }
198 
199     @Override
200     public boolean allowedAfter(Item sourceItem, Item targetItem) {
201         if ((sourceItem instanceof JcrItemAdapteri/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter) && (targetItem instanceof JcrItemAdapter)) {
202             JcrItemAdapter/../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter source = (JcrItemAdapter) sourceItem;
203             JcrItemAdapter/../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter target = (JcrItemAdapter) targetItem;
204 
205             return allowedSibling(source.getJcrItem(), target.getJcrItem());
206         }
207         return true;
208     }
209 
210     @Override
211     public boolean allowedToMove(Item sourceItem) {
212         JcrItemAdapter../info/magnolia/ui/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter jcrSourceItem = (JcrItemAdapter) sourceItem;
213         return jcrSourceItem != null;
214     }
215 }