View Javadoc
1   /**
2    * This file Copyright (c) 2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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.jcr.util.NodeTypes;
42  import info.magnolia.jcr.util.NodeUtil;
43  import info.magnolia.resourceloader.Resource;
44  import info.magnolia.resourceloader.ResourceOrigin;
45  import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
46  import info.magnolia.resourceloader.layered.LayeredResource;
47  import info.magnolia.resources.app.workbench.ResourcesContainer;
48  import info.magnolia.ui.api.action.ActionExecutionException;
49  import info.magnolia.ui.api.app.AppContext;
50  import info.magnolia.ui.api.event.AdmincentralEventBus;
51  import info.magnolia.ui.api.event.ContentChangedEvent;
52  import info.magnolia.ui.api.location.LocationController;
53  import info.magnolia.ui.api.overlay.AlertCallback;
54  import info.magnolia.ui.contentapp.detail.DetailLocation;
55  import info.magnolia.ui.contentapp.detail.DetailView;
56  
57  import java.io.IOException;
58  import java.io.Reader;
59  
60  import javax.inject.Inject;
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   * Action for editing a file within the Resources app.
72   * <p>
73   * This action will try to find the file for editing within {@value JcrResourceOrigin#RESOURCES_WORKSPACE} JCR workspace.
74   * If no such file exist then a so-called 'hotfix' will be created first, which involves importing a resource from
75   * classpath or file system into the {@value JcrResourceOrigin#RESOURCES_WORKSPACE} JCR workspace.
76   * By doing this, the newly imported resource takes precedence over the original one in the resources loading cascade.
77   * It may then be edited, on a live instance, and it may eventually be published to a public instance as well.
78   */
79  public class EditResourceAction extends AbstractResourceAction<EditResourceActionDefinition> {
80  
81      private final Item resourceItem;
82      private final Context context;
83      private final AppContext appContext;
84      private final SimpleTranslator i18n;
85      private final LocationController locationController;
86      private final ResourceOrigin origin;
87      private final EventBus eventBus;
88  
89      @Inject
90      public EditResourceAction(EditResourceActionDefinition definition, Item resourceItem, Context context, AppContext appContext, SimpleTranslator i18n, LocationController locationController, ResourceOrigin origin, @Named(AdmincentralEventBus.NAME) EventBus eventBus) {
91          super(definition, origin);
92          this.resourceItem = resourceItem;
93          this.context = context;
94          this.appContext = appContext;
95          this.i18n = i18n;
96          this.locationController = locationController;
97          this.origin = origin;
98          this.eventBus = eventBus;
99      }
100 
101     @Override
102     public void execute() throws ActionExecutionException {
103         Resource resource = unwrapResource(resourceItem);
104         boolean isResourcePresentInJcr = ((LayeredResource) resource).getFirst().getOrigin() instanceof JcrResourceOrigin;
105 
106         // If resource is not in JCR, then copy resource to JCR first.
107         if (!isResourcePresentInJcr) {
108             copyResourceToJcr();
109         }
110 
111         String path = resource.getPath();
112         String appName = getDefinition().getAppName();
113 
114         DetailLocation location = new DetailLocation(appName, HOTFIX_SUBAPP_NAME, DetailView.ViewType.EDIT, path, null);
115         locationController.goTo(location);
116     }
117 
118     private void copyResourceToJcr() throws ActionExecutionException {
119         Resource resourceToHotfix = fetchResourceToHotfix();
120         Resource parent = resourceToHotfix.getParent();
121 
122         if (parent == null) {
123             throw new ActionExecutionException(new IllegalStateException("Could not get parent of resource to hotfix."));
124         }
125 
126         try {
127             Session jcrSession = context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE);
128             if (jcrSession.nodeExists(resourceToHotfix.getPath())) {
129                 String title = i18n.translate("resources.actions.hotfixResource.notification.alreadyExists.title");
130                 String body = i18n.translate("resources.actions.hotfixResource.notification.alreadyExists.body", resourceToHotfix.getPath());
131                 appContext.openAlert(WARNING, title, body, i18n.translate("button.ok"), new AlertCallback() {
132                     @Override
133                     public void onOk() {
134                     }
135                 });
136                 return;
137             }
138 
139             // create parent directories in JCR if they don't exist yet
140             Node parentNode = NodeUtil.createPath(jcrSession.getRootNode(), parent.getPath(), NodeTypes.Folder.NAME);
141             Node resourceNode = createResourceNode(resourceToHotfix, parentNode, resourceToHotfix.getName());
142             jcrSession.save();
143 
144             String resourceNodePath = resourceNode.getPath();
145             eventBus.fireEvent(new ContentChangedEvent(resourceNodePath));
146 
147             appContext.openNotification(INFO, true, i18n.translate("resources.actions.hotfixResource.notification.success"));
148 
149         } catch (Exception e) {
150             appContext.openNotification(ERROR, false, i18n.translate("resources.actions.hotfixResource.notification.error"));
151             throw new ActionExecutionException(e);
152         }
153     }
154 
155     private Resource fetchResourceToHotfix() {
156         String resourcePath = (String) resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH).getValue();
157         return ((LayeredResource) origin.getByPath(resourcePath)).getFirst();
158     }
159 
160     private Node createResourceNode(Resource resourceToHotfix, Node parentNode, String resourceName) throws RepositoryException, IOException {
161         try (Reader reader = resourceToHotfix.openReader()) {
162             String content = IOUtils.toString(reader);
163             Node resourceNode = parentNode.addNode(resourceName, NodeTypes.Content.NAME);
164             resourceNode.setProperty(JcrResourceOrigin.TEXT_PROPERTY, content);
165             return resourceNode;
166         }
167     }
168 }