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.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
54
55
56
57
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
73
74
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
98 if (target.isSame(source.getParent())
99
100 || ((Node) target).hasNode(source.getName())
101 || ((Node) target).hasProperty(source.getName())) {
102 return false;
103 }
104
105
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
124 if (!target.isNode()) {
125 log.debug("Could not move a node under a property");
126 return false;
127 }
128
129
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
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
178 if (!source.isNode()) {
179 return false;
180 }
181
182 boolean movable = basicCheck(source, target);
183 if (movable &&
184
185 (isRoot(target) ||
186
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 }