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.framework.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.ui.api.action.ActionExecutionException;
41 import info.magnolia.ui.api.action.CommandActionDefinition;
42 import info.magnolia.ui.api.context.UiContext;
43 import info.magnolia.ui.api.event.AdmincentralEventBus;
44 import info.magnolia.ui.api.event.ContentChangedEvent;
45 import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
46 import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
47
48 import java.util.List;
49
50 import javax.inject.Named;
51 import javax.jcr.Item;
52 import javax.jcr.Property;
53 import javax.jcr.RepositoryException;
54
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58
59
60
61
62
63 public class DeleteAction<D extends CommandActionDefinition> extends AbstractCommandAction<D> {
64
65 private final Logger log = LoggerFactory.getLogger(getClass());
66
67 protected final UiContext uiContext;
68 protected final Item jcrItem;
69 protected final EventBus eventBus;
70 private String itemIdOfChangedItem;
71 private final SimpleTranslator i18n;
72
73 public DeleteAction(D definition, JcrItemAdapter item, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n) {
74 super(definition, item, commandsManager, uiContext, i18n);
75 this.jcrItem = item.getJcrItem();
76 this.uiContext = uiContext;
77 this.eventBus = eventBus;
78 this.i18n = i18n;
79
80 try {
81 itemIdOfChangedItem = JcrItemUtil.getItemId(jcrItem.getParent());
82 } catch (RepositoryException e) {
83 log.error("Could not execute repository operation.", e);
84 onError(e);
85 }
86 }
87
88 public DeleteAction(D definition, List<JcrItemAdapter> items, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n) {
89 super(definition, items, commandsManager, uiContext, i18n);
90 this.jcrItem = items.get(0).getJcrItem();
91 this.uiContext = uiContext;
92 this.eventBus = eventBus;
93 this.i18n = i18n;
94 try {
95 itemIdOfChangedItem = JcrItemUtil.getItemId(jcrItem.getParent());
96 } catch (RepositoryException e) {
97 log.error("Could not execute repository operation.", e);
98 onError(e);
99 }
100 }
101
102 @Override
103 protected void onPreExecute() throws Exception {
104 super.onPreExecute();
105
106 if (getCurrentItem().getJcrItem().isNode() && getCurrentItem().getJcrItem().getDepth() == 0) {
107 throw new ActionExecutionException("Root node can't be deleted.");
108 }
109 }
110
111 @Override
112 protected void onPostExecute() throws Exception {
113
114 eventBus.fireEvent(new ContentChangedEvent((String) getParams().get(Context.ATTRIBUTE_REPOSITORY), itemIdOfChangedItem));
115 }
116
117 @Override
118 protected void executeOnItem(JcrItemAdapter item) throws ActionExecutionException {
119 if (item.isNode()) {
120 super.executeOnItem(item);
121 } else {
122 try {
123 onPreExecute();
124 Property property = (Property) item.getJcrItem();
125 property.remove();
126 property.getSession().save();
127 onPostExecute();
128 } catch (Exception e) {
129 onError(e);
130 throw new ActionExecutionException(e);
131 }
132 }
133 }
134
135 @Override
136 protected String getSuccessMessage() {
137 return i18n.translate(getDefinition().getSuccessMessage(), "" + getItems().size());
138 }
139
140 @Override
141 protected String getFailureMessage() {
142 return getDefinition().getFailureMessage();
143 }
144
145 protected SimpleTranslator getI18n() {
146 return i18n;
147 }
148
149 }