View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.ui.framework.action;
35  
36  import info.magnolia.commands.CommandsManager;
37  import info.magnolia.context.Context;
38  import info.magnolia.event.EventBus;
39  import info.magnolia.i18nsystem.SimpleTranslator;
40  import info.magnolia.ui.api.action.ActionExecutionException;
41  import info.magnolia.ui.api.action.CommandActionDefinition;
42  import info.magnolia.ui.api.context.UiContext;
43  import info.magnolia.ui.api.event.AdmincentralEventBus;
44  import info.magnolia.ui.api.event.ContentChangedEvent;
45  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
46  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
47  
48  import java.util.List;
49  
50  import javax.inject.Named;
51  import javax.jcr.Item;
52  import javax.jcr.Property;
53  import javax.jcr.RepositoryException;
54  
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  /**
59   * Deletes a node from the repository using the delete command.
60   *
61   * @param <D> {@link info.magnolia.ui.api.action.CommandActionDefinition}.
62   */
63  public class DeleteAction<D extends CommandActionDefinition> extends AbstractCommandAction<D> {
64  
65      private final Logger log = LoggerFactory.getLogger(getClass());
66  
67      protected final UiContext uiContext;
68      protected final Item jcrItem;
69      protected final EventBus eventBus;
70      private String itemIdOfChangedItem;
71      private final SimpleTranslator i18n;
72  
73      public DeleteAction(D definition, JcrItemAdapter item, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n) {
74          super(definition, item, commandsManager, uiContext, i18n);
75          this.jcrItem = item.getJcrItem();
76          this.uiContext = uiContext;
77          this.eventBus = eventBus;
78          this.i18n = i18n;
79  
80          try {
81              itemIdOfChangedItem = JcrItemUtil.getItemId(jcrItem.getParent());
82          } catch (RepositoryException e) {
83              log.error("Could not execute repository operation.", e);
84              onError(e);
85          }
86      }
87  
88      public DeleteAction(D definition, List<JcrItemAdapter> items, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, UiContext uiContext, SimpleTranslator i18n) {
89          super(definition, items, commandsManager, uiContext, i18n);
90          this.jcrItem = items.get(0).getJcrItem();
91          this.uiContext = uiContext;
92          this.eventBus = eventBus;
93          this.i18n = i18n;
94          try {
95              itemIdOfChangedItem = JcrItemUtil.getItemId(jcrItem.getParent());
96          } catch (RepositoryException e) {
97              log.error("Could not execute repository operation.", e);
98              onError(e);
99          }
100     }
101 
102     @Override
103     protected void onPreExecute() throws Exception {
104         super.onPreExecute();
105 
106         if (getCurrentItem().getJcrItem().isNode() && getCurrentItem().getJcrItem().getDepth() == 0) {
107             throw new ActionExecutionException("Root node can't be deleted.");
108         }
109     }
110 
111     @Override
112     protected void onPostExecute() throws Exception {
113         // Propagate event
114         eventBus.fireEvent(new ContentChangedEvent((String) getParams().get(Context.ATTRIBUTE_REPOSITORY), itemIdOfChangedItem));
115     }
116 
117     @Override
118     protected void executeOnItem(JcrItemAdapter item) throws ActionExecutionException {
119         if (item.isNode()) {
120             super.executeOnItem(item);
121         } else {
122             try {
123                 onPreExecute();
124                 Property property = (Property) item.getJcrItem();
125                 property.remove();
126                 property.getSession().save();
127                 onPostExecute();
128             } catch (Exception e) {
129                 onError(e);
130                 throw new ActionExecutionException(e);
131             }
132         }
133     }
134 
135     @Override
136     protected String getSuccessMessage() {
137         return i18n.translate(getDefinition().getSuccessMessage(), "" + getItems().size());
138     }
139 
140     @Override
141     protected String getFailureMessage() {
142         return getDefinition().getFailureMessage();
143     }
144 
145     protected SimpleTranslator getI18n() {
146         return i18n;
147     }
148 
149 }