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 static info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum.*;
37
38 import info.magnolia.context.Context;
39 import info.magnolia.event.EventBus;
40 import info.magnolia.i18nsystem.SimpleTranslator;
41 import info.magnolia.resourceloader.ResourceOrigin;
42 import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
43 import info.magnolia.resources.app.workbench.ResourcesContainer;
44 import info.magnolia.ui.api.action.ActionExecutionException;
45 import info.magnolia.ui.api.app.AppContext;
46 import info.magnolia.ui.api.event.AdmincentralEventBus;
47 import info.magnolia.ui.api.event.ContentChangedEvent;
48 import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
49
50 import javax.inject.Named;
51 import javax.jcr.Node;
52 import javax.jcr.Session;
53
54 import com.vaadin.data.Item;
55
56
57
58
59 public class RemoveHotfixAction extends AbstractResourceAction<RemoveHotfixActionDefinition> {
60
61 private final Item resourceItem;
62 private final Context context;
63 private final SimpleTranslator i18n;
64 private final AppContext appContext;
65 private final EventBus eventBus;
66 private final ContentConnector contentConnector;
67
68 public RemoveHotfixAction(RemoveHotfixActionDefinition definition, Item resourceItem, Context context, AppContext appContext, SimpleTranslator i18n, @Named(AdmincentralEventBus.NAME) EventBus eventBus, ContentConnector contentConnector, ResourceOrigin origin) {
69 super(definition, origin);
70 this.resourceItem = resourceItem;
71 this.context = context;
72 this.i18n = i18n;
73 this.appContext = appContext;
74 this.eventBus = eventBus;
75 this.contentConnector = contentConnector;
76 }
77
78 @Override
79 public void execute() throws ActionExecutionException {
80 String resourcePath = (String) resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH).getValue();
81 String origin = (String) resourceItem.getItemProperty(ResourcesContainer.ORIGIN_NAME_PROPERTY_ID).getValue();
82 boolean isOverriding = (boolean) resourceItem.getItemProperty(ResourcesContainer.OVERRIDING_PROPERTY_ID).getValue();
83
84 if (!origin.equals("jcr") || !isOverriding) {
85 throw new ActionExecutionException(new IllegalStateException("Hotfix is not present"));
86 }
87
88 try {
89 Session jcrSession = context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE);
90 Node node = jcrSession.getNode(resourcePath);
91 node.remove();
92
93 jcrSession.save();
94
95 Object newResourcePath = contentConnector.getItemIdByUrlFragment(resourcePath);
96 reflectChangesForItem(newResourcePath);
97
98 appContext.openNotification(INFO, true, i18n.translate("resources.actions.removeHotfix.notification.success"));
99 } catch (Exception e) {
100 appContext.openNotification(ERROR, false, i18n.translate("resources.actions.removeHotfix.notification.error"));
101 throw new ActionExecutionException(e);
102 }
103 }
104
105 private void reflectChangesForItem(Object itemId) {
106 eventBus.fireEvent(new ContentChangedEvent(itemId));
107 }
108 }