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   * @see DeleteItemActionDefinition
63   */
64  public class DeleteItemAction extends AbstractMultiItemAction<DeleteItemActionDefinition> {
65  
66      private final Logger log = LoggerFactory.getLogger(getClass());
67  
68      private final UiContext uiContext;
69      private final EventBus eventBus;
70      private final SimpleTranslator i18n;
71  
72      public DeleteItemAction(DeleteItemActionDefinition definition, JcrItemAdapter item, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n) {
73          super(definition, Collections.singletonList(item), uiContext);
74          this.uiContext = uiContext;
75          this.eventBus = eventBus;
76          this.i18n = i18n;
77      }
78  
79      public DeleteItemAction(DeleteItemActionDefinition definition, List<JcrItemAdapter> items, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n) {
80          super(definition, items, uiContext);
81          this.uiContext = uiContext;
82          this.eventBus = eventBus;
83          this.i18n = i18n;
84      }
85  
86      @Override
87      public void execute() throws ActionExecutionException {
88  
89          uiContext.openConfirmation(
90                  MessageStyleTypeEnum.WARNING, getConfirmationQuestion(),
91                  i18n.translate("ui-framework.actions.deleteItem.warningText"),
92                  i18n.translate("ui-framework.actions.deleteItem.confirmText"),
93                  i18n.translate("ui-framework.actions.deleteItem.cancelText"),
94                  true,
95                  new ConfirmationCallback() {
96  
97                      @Override
98                      public void onSuccess() {
99                          DeleteItemAction.this.executeAfterConfirmation();
100                     }
101 
102                     @Override
103                     public void onCancel() {
104                     }
105                 });
106     }
107 
108     private String getConfirmationQuestion() {
109         if (getItems().size() == 1) {
110             return i18n.translate("ui-framework.actions.deleteItem.confirmationQuestionOneItem");
111         }
112         return String.format(i18n.translate("ui-framework.actions.deleteItem.confirmationQuestionManyItems"), getItems().size());
113     }
114 
115     protected void executeAfterConfirmation() {
116         try {
117             super.execute();
118         } catch (ActionExecutionException e) {
119             log.error("Problem occured during deleting items.", e);
120         }
121     }
122 
123     @Override
124     protected void executeOnItem(JcrItemAdapter item) throws Exception {
125         try {
126             final Item jcrItem = item.getJcrItem();
127             if (jcrItem.getDepth() == 0) {
128                 // cannot delete root node
129                 throw new IllegalArgumentException(i18n.translate("ui-framework.actions.deleteItem.cannotDeleteRootItem"));
130             }
131             JcrItemId itemIdOfChangedItem = JcrItemUtil.getItemId(jcrItem);
132             Session session = jcrItem.getSession();
133             jcrItem.remove();
134             session.save();
135 
136             eventBus.fireEvent(new ContentChangedEvent(itemIdOfChangedItem));
137         } catch (PathNotFoundException e) {
138             // ignore - the item has been probably already deleted (with a parent node)
139         }
140     }
141 
142     @Override
143     protected String getSuccessMessage() {
144         if (getItems().size() == 1) {
145             return i18n.translate("ui-framework.actions.deleteItem.successOneItemDeleted");
146         } else {
147             return i18n.translate("ui-framework.actions.deleteItem.successManyItemsDeleted", getItems().size());
148         }
149     }
150 
151     @Override
152     protected String getFailureMessage() {
153         return i18n.translate("ui-framework.actions.deleteItem.deletionfailure", getFailedItems().size(), getItems().size());
154     }
155 }