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