View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.movedialog.predicate;
35  
36  
37  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
38  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
39  import info.magnolia.ui.workbench.tree.drop.DropConstraint;
40  
41  import java.util.Iterator;
42  import java.util.List;
43  
44  import javax.jcr.RepositoryException;
45  
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  import com.vaadin.v7.data.Item;
50  
51  /**
52   * Checks whether it is possible to place the collection of nodes relatively to the tested node.
53   *
54   * @deprecated since 5.5.4, MovePossibilityPredicates have been unified and replaced with DropConstraints.
55   */
56  @Deprecated
57  public class MovePossibilityPredicate {
58  
59      private Logger log = LoggerFactory.getLogger(getClass());
60  
61      private List<? extends Item> candidates;
62  
63      protected DropConstraint constraint;
64  
65      protected MovePossibilityPredicate(DropConstraint constraint, List<? extends Item> candidates) {
66          this.constraint = constraint;
67          this.candidates = candidates;
68      }
69  
70      public boolean isMovePossible(Item hostCandidate) {
71          boolean isPossible = true;
72          Iterator<? extends Item> it = candidates.iterator();
73          while (it.hasNext() && isPossible) {
74              Item item = it.next();
75              isPossible &= checkItem(item, hostCandidate);
76          }
77          return isPossible;
78      }
79  
80      protected boolean checkItem(Item item, Item hostCandidate) {
81          if ((item instanceof JcrItemAdapteraadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter) && (hostCandidate instanceof JcrItemAdapter)) {
82              JcrItemAdapter../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter jcrItem = (JcrItemAdapter) item;
83              JcrItemAdapter../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter jcrHost = (JcrItemAdapter) hostCandidate;
84              try {
85                  return basicMoveCheck(jcrItem.getJcrItem(), jcrHost.getJcrItem());
86              } catch (RepositoryException e) {
87                  log.warn("Error occurred during basic move check: ", e);
88                  return false;
89              }
90          }
91          return true;
92      }
93  
94      protected boolean hostIsRoot(Item hostCandidate) {
95          if (hostCandidate instanceof JcrNodeAdapter) {
96              JcrNodeAdapter../../../info/magnolia/ui/vaadin/integration/jcr/JcrNodeAdapter.html#JcrNodeAdapter">JcrNodeAdapter jcrItem = (JcrNodeAdapter) hostCandidate;
97  
98              return isRoot(jcrItem.getJcrItem());
99          }
100         return false;
101     }
102 
103     protected boolean isRoot(javax.jcr.Item item) {
104         try {
105             if (item != null && item.isNode() && item.getParent() == null) {
106                 return true;
107             }
108         } catch (RepositoryException e) {
109             return true;
110         }
111 
112         return false;
113     }
114 
115     protected boolean supportsProperties() {
116         // do not support properties by default
117         return false;
118     }
119 
120     protected boolean basicMoveCheck(javax.jcr.Item source, javax.jcr.Item target) throws RepositoryException {
121         if (!target.isNode()) {
122             return false;
123         }
124         if (!source.isNode() && !supportsProperties()) {
125             return false;
126         }
127         // Cannot move node to itself or its ancestor
128         if (source.isNode() && isAncestorOf(source, target)) {
129             return false;
130         }
131         return true;
132     }
133 
134     private boolean isAncestorOf(javax.jcr.Item ancestor, javax.jcr.Item node) {
135         try {
136             for (javax.jcr.Item n = node; !isRoot(n); n = n.getParent()) {
137                 if (ancestor.isSame(n)) {
138                     return true;
139                 }
140             }
141         } catch (RepositoryException e) {
142             log.warn("Cannot get ancestor of item:", e);
143         }
144         return false;
145     }
146 }