1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
53
54
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 JcrItemAdapter) && (hostCandidate instanceof JcrItemAdapter)) {
82 JcrItemAdapter jcrItem = (JcrItemAdapter) item;
83 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 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
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
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 }