View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 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.ui.framework.action;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.ui.api.action.ActionExecutionException;
39  import info.magnolia.ui.api.context.UiContext;
40  import info.magnolia.ui.api.event.AdmincentralEventBus;
41  import info.magnolia.ui.api.event.ContentChangedEvent;
42  import info.magnolia.ui.api.overlay.ConfirmationCallback;
43  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
44  import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
45  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
46  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
47  
48  import java.util.Collections;
49  import java.util.List;
50  
51  import javax.inject.Named;
52  import javax.jcr.Item;
53  import javax.jcr.PathNotFoundException;
54  import javax.jcr.Session;
55  
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  /**
60   * Deletes a node or property from the repository.
61   *
62   * @deprecated since 6.2 - use {@link info.magnolia.ui.contentapp.action.CommandAction} instead.
63   *
64   * @see <a href="https://documentation.magnolia-cms.com/display/DOCS62/Upgrading+to+Magnolia+6.2.x">Upgrading to Magnolia 6.2.x</a>
65   */
66  @Deprecated
67  public class DeleteItemAction extends AbstractMultiItemAction<DeleteItemActionDefinition> {
68  
69      private final Logger log = LoggerFactory.getLogger(getClass());
70  
71      private final UiContext uiContext;
72      private final EventBus eventBus;
73      private final SimpleTranslator i18n;
74  
75      public DeleteItemAction(DeleteItemActionDefinition definition, JcrItemAdapter item, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n) {
76          super(definition, Collections.singletonList(item), uiContext);
77          this.uiContext = uiContext;
78          this.eventBus = eventBus;
79          this.i18n = i18n;
80      }
81  
82      public DeleteItemAction(DeleteItemActionDefinition definition, List<JcrItemAdapter> items, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n) {
83          super(definition, items, uiContext);
84          this.uiContext = uiContext;
85          this.eventBus = eventBus;
86          this.i18n = i18n;
87      }
88  
89      @Override
90      public void execute() throws ActionExecutionException {
91  
92          uiContext.openConfirmation(
93                  MessageStyleTypeEnum.WARNING, getConfirmationQuestion(),
94                  i18n.translate("ui-framework.actions.deleteItem.warningText"),
95                  i18n.translate("ui-framework.actions.deleteItem.confirmText"),
96                  i18n.translate("ui-framework.actions.deleteItem.cancelText"),
97                  true,
98                  new ConfirmationCallback() {
99  
100                     @Override
101                     public void onSuccess() {
102                         DeleteItemAction.this.executeAfterConfirmation();
103                     }
104 
105                     @Override
106                     public void onCancel() {
107                     }
108                 });
109     }
110 
111     private String getConfirmationQuestion() {
112         if (getItems().size() == 1) {
113             return i18n.translate("ui-framework.actions.deleteItem.confirmationQuestionOneItem");
114         }
115         return String.format(i18n.translate("ui-framework.actions.deleteItem.confirmationQuestionManyItems"), getItems().size());
116     }
117 
118     protected void executeAfterConfirmation() {
119         try {
120             super.execute();
121         } catch (ActionExecutionException e) {
122             log.error("Problem occured during deleting items.", e);
123         }
124     }
125 
126     @Override
127     protected void executeOnItem(JcrItemAdapter item) throws Exception {
128         try {
129             final Item jcrItem = item.getJcrItem();
130             if (jcrItem.getDepth() == 0) {
131                 // cannot delete root node
132                 throw new IllegalArgumentException(i18n.translate("ui-framework.actions.deleteItem.cannotDeleteRootItem"));
133             }
134             JcrItemId itemIdOfChangedItem = JcrItemUtil.getItemId(jcrItem);
135             Session session = jcrItem.getSession();
136             jcrItem.remove();
137             session.save();
138 
139             eventBus.fireEvent(new ContentChangedEvent(itemIdOfChangedItem));
140         } catch (PathNotFoundException e) {
141             // ignore - the item has been probably already deleted (with a parent node)
142         }
143     }
144 
145     @Override
146     protected String getSuccessMessage() {
147         if (getItems().size() == 1) {
148             return i18n.translate("ui-framework.actions.deleteItem.successOneItemDeleted");
149         } else {
150             return i18n.translate("ui-framework.actions.deleteItem.successManyItemsDeleted", getItems().size());
151         }
152     }
153 
154     @Override
155     protected String getFailureMessage() {
156         return i18n.translate("ui-framework.actions.deleteItem.deletionfailure", getFailedItems().size(), getItems().size());
157     }
158 }