View Javadoc

1   /**
2    * This file Copyright (c) 2011-2013 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.JcrItemUtil;
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   */
54  public abstract class AbstractRepositoryAction<D extends ActionDefinition> extends AbstractAction<D> {
55  
56      public static final String DEFAULT_NEW_ITEM_NAME = "untitled";
57  
58      protected final JcrItemAdapter item;
59  
60      private final EventBus eventBus;
61  
62      /**
63       * Holds the itemId to use for the ContentChangedEvent sent after the action is performed. If the action deletes
64       * an item it should set this to the itemId of its parent. If it adds an item it should set this to the itemId
65       * of the new item.
66       *
67       * @see ContentChangedEvent
68       */
69      private String itemIdOfChangedItem;
70  
71      private boolean itemContentChanged;
72  
73      protected AbstractRepositoryAction(D definition, JcrItemAdapter item, EventBus eventBus) {
74          super(definition);
75          this.item = item;
76          this.eventBus = eventBus;
77      }
78  
79      protected void setItemIdOfChangedItem(String itemIdOfChangedItem) {
80          this.itemIdOfChangedItem = itemIdOfChangedItem;
81      }
82  
83      protected void setItemContentChanged(boolean itemContentChanged) {
84          this.itemContentChanged = itemContentChanged;
85      }
86  
87      /**
88       * Executes the defined action on the passed in item. When successful, it will fire a {@link ContentChangedEvent}.
89       */
90      @Override
91      public void execute() throws ActionExecutionException {
92          try {
93              Session session = item.getJcrItem().getSession();
94              onExecute(item);
95              session.save();
96  
97              // If the subclass set it to null this means no change was performed so we won't send an event
98              if (itemIdOfChangedItem != null) {
99                  boolean propertyChange = JcrItemUtil.isPropertyItemId(itemIdOfChangedItem);
100                 eventBus.fireEvent(new ContentChangedEvent(session.getWorkspace().getName(), itemIdOfChangedItem, itemContentChanged));
101             }
102 
103         } catch (RepositoryException e) {
104             throw new ActionExecutionException("Can't execute repository operation.\n" + e.getMessage(), e);
105         }
106     }
107 
108     protected abstract void onExecute(JcrItemAdapter item) throws RepositoryException;
109 
110     protected String getUniqueNewItemName(Node parent) throws RepositoryException {
111         return getUniqueNewItemName(parent, DEFAULT_NEW_ITEM_NAME);
112     }
113 
114     protected String getUniqueNewItemName(Node parent, String name) throws RepositoryException {
115         if (item == null) {
116             throw new IllegalArgumentException("Item cannot be null.");
117         }
118         return Path.getUniqueLabel(parent.getSession(), parent.getPath(), name);
119     }
120 }