View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.cms.core.Path;
37  import info.magnolia.event.EventBus;
38  import info.magnolia.ui.api.action.AbstractAction;
39  import info.magnolia.ui.api.action.ActionDefinition;
40  import info.magnolia.ui.api.action.ActionExecutionException;
41  import info.magnolia.ui.api.event.ContentChangedEvent;
42  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
43  import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
44  
45  import javax.jcr.Node;
46  import javax.jcr.RepositoryException;
47  import javax.jcr.Session;
48  
49  /**
50   * A repository operation action which saves the changes and informs the event bus.
51   *
52   * @param <D> The {@link ActionDefinition} used by the action.
53   * @deprecated since 6.0. Use info.magnolia.ui.contentapp.action.AbstractJcrAction instead.
54   */
55  @Deprecated
56  public abstract class AbstractRepositoryAction<D extends ActionDefinition> extends AbstractAction<D> {
57  
58      public static final String DEFAULT_NEW_ITEM_NAME = "untitled";
59  
60      protected final JcrItemAdapter item;
61  
62      private final EventBus eventBus;
63  
64      /**
65       * Holds the itemId to use for the ContentChangedEvent sent after the action is performed. If the action deletes
66       * an item it should set this to the itemId of its parent. If it adds an item it should set this to the itemId
67       * of the new item.
68       *
69       * @see ContentChangedEvent
70       */
71      private JcrItemId itemIdOfChangedItem;
72  
73      private boolean itemContentChanged;
74  
75      protected AbstractRepositoryAction(D definition, JcrItemAdapter item, EventBus eventBus) {
76          super(definition);
77          this.item = item;
78          this.eventBus = eventBus;
79      }
80  
81      protected void setItemIdOfChangedItem(JcrItemId itemIdOfChangedItem) {
82          this.itemIdOfChangedItem = itemIdOfChangedItem;
83      }
84  
85      protected void setItemContentChanged(boolean itemContentChanged) {
86          this.itemContentChanged = itemContentChanged;
87      }
88  
89      /**
90       * Executes the defined action on the passed in item. When successful, it will fire a {@link ContentChangedEvent}.
91       */
92      @Override
93      public void execute() throws ActionExecutionException {
94          try {
95              Session session = item.getJcrItem().getSession();
96              onExecute(item);
97              session.save();
98  
99              // If the subclass set it to null this means no change was performed so we won't send an event
100             if (itemIdOfChangedItem != null) {
101                 eventBus.fireEvent(new ContentChangedEvent(itemIdOfChangedItem, itemContentChanged));
102             }
103 
104         } catch (RepositoryException e) {
105             throw new ActionExecutionException("Can't execute repository operation.\n" + e.getMessage(), e);
106         }
107     }
108 
109     protected abstract void onExecute(JcrItemAdapter item) throws RepositoryException;
110 
111     protected String getUniqueNewItemName(Node parent) throws RepositoryException {
112         return getUniqueNewItemName(parent, DEFAULT_NEW_ITEM_NAME);
113     }
114 
115     protected String getUniqueNewItemName(Node parent, String name) throws RepositoryException {
116         if (item == null) {
117             throw new IllegalArgumentException("Item cannot be null.");
118         }
119         return Path.getUniqueLabel(parent.getSession(), parent.getPath(), name);
120     }
121 }