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