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.resources.app.action.OpenResourceAction.HOTFIX_SUBAPP_NAME;
37 import static info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum.*;
38
39 import info.magnolia.context.Context;
40 import info.magnolia.event.EventBus;
41 import info.magnolia.i18nsystem.SimpleTranslator;
42 import info.magnolia.jcr.util.NodeTypes;
43 import info.magnolia.jcr.util.NodeUtil;
44 import info.magnolia.resourceloader.Resource;
45 import info.magnolia.resourceloader.ResourceOrigin;
46 import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
47 import info.magnolia.resourceloader.layered.LayeredResource;
48 import info.magnolia.resources.app.workbench.ResourcesContainer;
49 import info.magnolia.ui.api.action.ActionExecutionException;
50 import info.magnolia.ui.api.app.AppContext;
51 import info.magnolia.ui.api.event.AdmincentralEventBus;
52 import info.magnolia.ui.api.event.ContentChangedEvent;
53 import info.magnolia.ui.api.location.LocationController;
54 import info.magnolia.ui.api.overlay.AlertCallback;
55 import info.magnolia.ui.contentapp.detail.DetailLocation;
56 import info.magnolia.ui.contentapp.detail.DetailView.ViewType;
57
58 import java.io.IOException;
59 import java.io.Reader;
60
61 import javax.inject.Named;
62 import javax.jcr.Node;
63 import javax.jcr.RepositoryException;
64 import javax.jcr.Session;
65
66 import org.apache.commons.io.IOUtils;
67
68 import com.vaadin.data.Item;
69
70
71
72
73
74
75
76
77 public class HotfixResourceAction extends AbstractResourceAction<HotfixResourceActionDefinition> {
78
79 private final Item resourceItem;
80 private final Context context;
81 private final SimpleTranslator i18n;
82 private final AppContext appContext;
83 private final LocationController locationController;
84 private final ResourceOrigin origin;
85 private final EventBus eventBus;
86
87 public HotfixResourceAction(HotfixResourceActionDefinition definition, Item resourceItem, Context context, AppContext appContext, SimpleTranslator i18n, LocationController locationController, ResourceOrigin origin, @Named(AdmincentralEventBus.NAME) EventBus eventBus) {
88 super(definition, origin);
89 this.resourceItem = resourceItem;
90 this.context = context;
91 this.i18n = i18n;
92 this.appContext = appContext;
93 this.locationController = locationController;
94 this.origin = origin;
95 this.eventBus = eventBus;
96 }
97
98 @Override
99 public void execute() throws ActionExecutionException {
100 Resource resourceToHotfix = fetchResourceToHotfix();
101 Resource parent = resourceToHotfix.getParent();
102
103 if (parent == null) {
104 throw new ActionExecutionException(new IllegalStateException("Could not get parent of resource to hotfix."));
105 }
106
107 try {
108 Session jcrSession = context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE);
109 if (jcrSession.nodeExists(resourceToHotfix.getPath())) {
110 String title = i18n.translate("resources.actions.hotfixResource.notification.alreadyExists.title");
111 String body = i18n.translate("resources.actions.hotfixResource.notification.alreadyExists.body", resourceToHotfix.getPath());
112 appContext.openAlert(WARNING, title, body, i18n.translate("button.ok"), new AlertCallback() {
113 @Override
114 public void onOk() {
115 }
116 });
117 return;
118 }
119
120
121 Node parentNode = NodeUtil.createPath(jcrSession.getRootNode(), parent.getPath(), NodeTypes.Folder.NAME);
122 Node resourceNode = createResourceNode(resourceToHotfix, parentNode, resourceToHotfix.getName());
123 jcrSession.save();
124
125
126 String resourceNodePath = resourceNode.getPath();
127 DetailLocation detailLocation = new DetailLocation(appContext.getName(), HOTFIX_SUBAPP_NAME, ViewType.EDIT, resourceNodePath, null);
128 locationController.goTo(detailLocation);
129
130 eventBus.fireEvent(new ContentChangedEvent(resourceNodePath));
131
132 appContext.openNotification(INFO, true, i18n.translate("resources.actions.hotfixResource.notification.success"));
133
134 } catch (Exception e) {
135 appContext.openNotification(ERROR, false, i18n.translate("resources.actions.hotfixResource.notification.error"));
136 throw new ActionExecutionException(e);
137 }
138 }
139
140 private Resource fetchResourceToHotfix() {
141 String resourcePath = (String) resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH).getValue();
142 return ((LayeredResource) origin.getByPath(resourcePath)).getFirst();
143 }
144
145 private Node createResourceNode(Resource resourceToHotfix, Node parentNode, String resourceName) throws RepositoryException, IOException {
146 try (Reader reader = resourceToHotfix.openReader()) {
147 String content = IOUtils.toString(reader);
148 Node resourceNode = parentNode.addNode(resourceName, NodeTypes.Content.NAME);
149 resourceNode.setProperty(JcrResourceOrigin.TEXT_PROPERTY, content);
150 return resourceNode;
151 }
152 }
153 }