View Javadoc
1   /**
2    * This file Copyright (c) 2015-2017 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.ResourceOrigin;
44  import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
45  import info.magnolia.resources.app.workbench.ResourcesContainer;
46  import info.magnolia.ui.api.action.ActionExecutionException;
47  import info.magnolia.ui.api.app.AppContext;
48  import info.magnolia.ui.api.event.AdmincentralEventBus;
49  import info.magnolia.ui.api.event.ContentChangedEvent;
50  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
51  
52  import javax.inject.Named;
53  import javax.jcr.Node;
54  import javax.jcr.RepositoryException;
55  import javax.jcr.Session;
56  
57  import com.vaadin.v7.data.Item;
58  
59  /**
60   * Action for removing a hotfix from the resources app.
61   *
62   * @deprecated since 2.4.2 - use more generic {@link MarkResourceAsDeletedAction} instead.
63   */
64  @Deprecated
65  public class RemoveHotfixAction extends AbstractResourceAction<RemoveHotfixActionDefinition> {
66  
67      private final Item resourceItem;
68      private final Context context;
69      private final SimpleTranslator i18n;
70      private final AppContext appContext;
71      private final EventBus eventBus;
72      private final ContentConnector contentConnector;
73  
74      public RemoveHotfixAction(RemoveHotfixActionDefinition definition, Item resourceItem, Context context, AppContext appContext, SimpleTranslator i18n, @Named(AdmincentralEventBus.NAME) EventBus eventBus, ContentConnector contentConnector, ResourceOrigin origin) {
75          super(definition, origin);
76          this.resourceItem = resourceItem;
77          this.context = context;
78          this.i18n = i18n;
79          this.appContext = appContext;
80          this.eventBus = eventBus;
81          this.contentConnector = contentConnector;
82      }
83  
84      @Override
85      public void execute() throws ActionExecutionException {
86          String resourcePath = (String) resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH).getValue();
87          String origin = (String) resourceItem.getItemProperty(ResourcesContainer.ORIGIN_NAME_PROPERTY_ID).getValue();
88          boolean isOverriding = (boolean) resourceItem.getItemProperty(ResourcesContainer.OVERRIDING_PROPERTY_ID).getValue();
89  
90          if (!origin.equals("jcr") || !isOverriding) {
91              throw new ActionExecutionException(new IllegalStateException("Hotfix is not present"));
92          }
93  
94          try {
95              Session jcrSession = context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE);
96              Node node = jcrSession.getNode(resourcePath);
97              boolean hasSiblings = NodeUtil.getSiblings(node).iterator().hasNext();
98  
99              if (hasSiblings) {
100                 node.remove();
101             } else {
102                 // will recursively remove hotfix itself and all empty folders in the hierarchy containing it
103                 removeNodeAndEmptyAncestorsFromJcr(node);
104             }
105 
106             jcrSession.save();
107 
108             Object newResourcePath = contentConnector.getItemIdByUrlFragment(resourcePath);
109             reflectChangesForItem(newResourcePath);
110 
111             appContext.openNotification(INFO, true, i18n.translate("resources.actions.removeHotfix.notification.success"));
112         } catch (Exception e) {
113             appContext.openNotification(ERROR, false, i18n.translate("resources.actions.removeHotfix.notification.error"));
114             throw new ActionExecutionException(e);
115         }
116     }
117 
118     private void reflectChangesForItem(Object itemId) {
119         eventBus.fireEvent(new ContentChangedEvent(itemId));
120     }
121 
122     private void removeNodeAndEmptyAncestorsFromJcr(Node node) throws RepositoryException {
123         if (node.getDepth() == 0) {
124             return;
125         }
126         final boolean hasNoMoreResources = !NodeUtil.getNodes(node, NodeTypes.Content.NAME).iterator().hasNext();
127         if (hasNoMoreResources) {
128             Node parent = node.getParent();
129             node.remove();
130             removeNodeAndEmptyAncestorsFromJcr(parent);
131         }
132     }
133 }