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.resources.app.action; |
35 |
|
|
36 |
|
import info.magnolia.commands.CommandsManager; |
37 |
|
import info.magnolia.context.Context; |
38 |
|
import info.magnolia.event.EventBus; |
39 |
|
import info.magnolia.i18nsystem.SimpleTranslator; |
40 |
|
import info.magnolia.jcr.RuntimeRepositoryException; |
41 |
|
import info.magnolia.jcr.util.NodeTypes; |
42 |
|
import info.magnolia.jcr.util.NodeUtil; |
43 |
|
import info.magnolia.resources.app.utils.ResourceUtils; |
44 |
|
import info.magnolia.resources.app.workbench.ResourcesContainer; |
45 |
|
import info.magnolia.ui.api.context.UiContext; |
46 |
|
import info.magnolia.ui.api.event.AdmincentralEventBus; |
47 |
|
import info.magnolia.ui.api.event.ContentChangedEvent; |
48 |
|
import info.magnolia.ui.framework.action.MarkNodeAsDeletedAction; |
49 |
|
import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter; |
50 |
|
import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter; |
51 |
|
import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum; |
52 |
|
|
53 |
|
import java.util.Comparator; |
54 |
|
import java.util.List; |
55 |
|
|
56 |
|
import javax.inject.Named; |
57 |
|
import javax.jcr.Node; |
58 |
|
import javax.jcr.RepositoryException; |
59 |
|
import javax.jcr.Session; |
60 |
|
|
61 |
|
import org.apache.commons.lang3.StringUtils; |
62 |
|
|
63 |
|
import com.google.common.base.Predicate; |
64 |
|
import com.google.common.collect.FluentIterable; |
65 |
|
import com.google.common.collect.Lists; |
66 |
|
import com.google.common.collect.Ordering; |
67 |
|
import com.vaadin.data.Item; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@see |
75 |
|
|
|
|
| 77.7% |
Uncovered Elements: 21 (94) |
Complexity: 29 |
Complexity Density: 0.47 |
|
76 |
|
public class MarkResourceAsDeletedAction extends MarkNodeAsDeletedAction { |
77 |
|
|
78 |
|
private final List<JcrItemAdapter> jcrItemAdapters; |
79 |
|
private final SimpleTranslator i18n; |
80 |
|
private final boolean isDirectory; |
81 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
0 |
public MarkResourceAsDeletedAction(MarkResourceAsDeletedActionDefinition definition, List<Item> items, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n, Context context) {... |
83 |
0 |
this(definition, ResourceUtils.getJcrItemAdaptersFor(context, items), commandsManager, eventBus, uiContext, i18n, items.get(0)); |
84 |
|
} |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
86 |
3 |
public MarkResourceAsDeletedAction(MarkResourceAsDeletedActionDefinition definition, Item item, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n, Context context) {... |
87 |
3 |
this(definition, ResourceUtils.getJcrItemAdaptersFor(context, Lists.newArrayList(item)), commandsManager, eventBus, uiContext, i18n, item); |
88 |
|
} |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
90 |
3 |
private MarkResourceAsDeletedAction(MarkResourceAsDeletedActionDefinition definition, List<JcrItemAdapter> jcrItemAdapters, CommandsManager commandsManager, EventBus eventBus, UiContext uiContext, SimpleTranslator i18n, Item item) {... |
91 |
3 |
super(definition, jcrItemAdapters, commandsManager, eventBus, uiContext, i18n); |
92 |
3 |
this.isDirectory = ResourceUtils.isDirectory(item); |
93 |
3 |
this.jcrItemAdapters = jcrItemAdapters; |
94 |
3 |
this.i18n = i18n; |
95 |
|
} |
96 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
97 |
7 |
@Override... |
98 |
|
protected void onPostExecute() throws Exception { |
99 |
7 |
super.onPostExecute(); |
100 |
7 |
for (JcrItemAdapter itemAdapter : jcrItemAdapters) { |
101 |
7 |
javax.jcr.Item jcrItem = itemAdapter.getJcrItem(); |
102 |
7 |
if (jcrItem != null) { |
103 |
7 |
eventBus.fireEvent(new ContentChangedEvent(jcrItem.getPath())); |
104 |
|
} else { |
105 |
0 |
eventBus.fireEvent(new ContentChangedEvent(ResourcesContainer.ROOT_DIRECTORY)); |
106 |
|
} |
107 |
|
} |
108 |
|
} |
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
110 |
6 |
@Override... |
111 |
|
protected List<JcrItemAdapter> getItems() { |
112 |
6 |
List<JcrItemAdapter> items = Lists.newArrayList(); |
113 |
6 |
for (JcrItemAdapter jcrItemAdapter : jcrItemAdapters) { |
114 |
6 |
items.addAll(getItems(jcrItemAdapter)); |
115 |
|
} |
116 |
6 |
return items; |
117 |
|
} |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
119 |
3 |
@Override... |
120 |
|
protected List<JcrItemAdapter> getSortedItems(Comparator<JcrItemAdapter> comparator) { |
121 |
3 |
return Ordering.from(comparator).sortedCopy(getItems()); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
124 |
3 |
@Override... |
125 |
|
protected Comparator<JcrItemAdapter> getItemComparator() { |
126 |
3 |
return new Comparator<JcrItemAdapter>() { |
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
127 |
4 |
@Override... |
128 |
|
public int compare(JcrItemAdapter o1, JcrItemAdapter o2) { |
129 |
4 |
try { |
130 |
4 |
return o2.getJcrItem().getDepth() - o1.getJcrItem().getDepth(); |
131 |
|
} catch (RepositoryException e) { |
132 |
0 |
return 0; |
133 |
|
} |
134 |
|
} |
135 |
|
}; |
136 |
|
} |
137 |
|
|
|
|
| 69.2% |
Uncovered Elements: 4 (13) |
Complexity: 3 |
Complexity Density: 0.27 |
|
138 |
6 |
private List<JcrItemAdapter> getItems(JcrItemAdapter jcrItemAdapter) {... |
139 |
6 |
List<JcrItemAdapter> items = Lists.newArrayList(); |
140 |
|
|
141 |
6 |
try { |
142 |
6 |
Session jcrSession = jcrItemAdapter.getJcrItem().getSession(); |
143 |
6 |
Node node = jcrSession.getNode(jcrItemAdapter.getJcrItem().getPath()); |
144 |
|
|
145 |
6 |
boolean hasSiblings = !getSiblings(node).isEmpty(); |
146 |
6 |
if (hasSiblings) { |
147 |
0 |
items.add(jcrItemAdapter); |
148 |
|
} else { |
149 |
6 |
findNodeAndEmptyAncestorsFromJcr(items, node, null); |
150 |
|
} |
151 |
|
} catch (RepositoryException e) { |
152 |
0 |
uiContext.openNotification(MessageStyleTypeEnum.ERROR, false, i18n.translate("resources.actions.deleteResource.notification.error")); |
153 |
0 |
throw new RuntimeException(e); |
154 |
|
} |
155 |
6 |
return items; |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
158 |
20 |
private void findNodeAndEmptyAncestorsFromJcr(List<JcrItemAdapter> items, Node node, final Node removedChildNode) throws RepositoryException {... |
159 |
20 |
if (node.getDepth() == 0) { |
160 |
4 |
return; |
161 |
|
} |
162 |
|
|
163 |
16 |
List<Node> siblings = getSiblings(node); |
164 |
16 |
List<Node> filteredSiblings = filterSiblings(siblings, removedChildNode); |
165 |
|
|
166 |
16 |
boolean hasNoMoreResources = filteredSiblings.size() < 1; |
167 |
16 |
if (hasNoMoreResources) { |
168 |
14 |
Node parent = node.getParent(); |
169 |
14 |
items.add(new JcrNodeAdapter(node)); |
170 |
14 |
findNodeAndEmptyAncestorsFromJcr(items, parent, node); |
171 |
|
} |
172 |
|
} |
173 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
174 |
16 |
private List<Node> filterSiblings(List<Node> siblings, final Node removedChildNode) {... |
175 |
16 |
return FluentIterable |
176 |
|
.from(siblings) |
177 |
|
.filter(filterRemovedNode(removedChildNode)) |
178 |
|
.filter(markedAsDeleted()) |
179 |
|
.toList(); |
180 |
|
} |
181 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
182 |
22 |
private List<Node> getSiblings(Node node) throws RepositoryException {... |
183 |
22 |
List<Node> resources = Lists.newArrayList(NodeUtil.getNodes(node, NodeTypes.Content.NAME)); |
184 |
22 |
List<Node> folders = Lists.newArrayList(NodeUtil.getNodes(node, NodeTypes.Folder.NAME)); |
185 |
22 |
resources.addAll(folders); |
186 |
|
|
187 |
22 |
return resources; |
188 |
|
} |
189 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
190 |
3 |
@Override... |
191 |
|
protected String getSuccessMessage() { |
192 |
3 |
if (isDirectory) { |
193 |
0 |
return i18n.translate("resources.actions.markFolderAsDeleted.notification.success", super.getItems().size()); |
194 |
|
} else { |
195 |
3 |
return i18n.translate("resources.actions.markFileAsDeleted.notification.success", super.getItems().size()); |
196 |
|
} |
197 |
|
} |
198 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
199 |
0 |
@Override... |
200 |
|
protected String getFailureMessage() { |
201 |
0 |
if (isDirectory) { |
202 |
0 |
return i18n.translate("resources.actions.markFolderAsDeleted.notification.error", super.getItems().size()); |
203 |
|
} else { |
204 |
0 |
return i18n.translate("resources.actions.markFileAsDeleted.notification.error", super.getItems().size()); |
205 |
|
} |
206 |
|
} |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
208 |
16 |
private Predicate<Node> markedAsDeleted() {... |
209 |
16 |
return new Predicate<Node>() { |
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
210 |
2 |
@Override... |
211 |
|
public boolean apply(Node node) { |
212 |
2 |
try { |
213 |
2 |
return !node.hasProperty(NodeTypes.Deleted.NAME); |
214 |
|
} catch (RepositoryException e) { |
215 |
0 |
throw new RuntimeRepositoryException(e); |
216 |
|
} |
217 |
|
} |
218 |
|
}; |
219 |
|
} |
220 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
221 |
16 |
private Predicate<Node> filterRemovedNode(final Node removedChildNode) {... |
222 |
16 |
return new Predicate<Node>() { |
|
|
| 57.1% |
Uncovered Elements: 3 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
223 |
2 |
@Override... |
224 |
|
public boolean apply(Node node) { |
225 |
2 |
try { |
226 |
2 |
if (StringUtils.equals(node.getPath(), removedChildNode.getPath())) { |
227 |
0 |
return false; |
228 |
|
} |
229 |
|
} catch (RepositoryException e) { |
230 |
0 |
throw new RuntimeRepositoryException(e); |
231 |
|
} |
232 |
2 |
return true; |
233 |
|
} |
234 |
|
}; |
235 |
|
} |
236 |
|
} |