View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.contentapp.movedialog.action;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.ui.api.action.ActionExecutionException;
38  import info.magnolia.ui.api.context.UiContext;
39  import info.magnolia.ui.api.event.AdmincentralEventBus;
40  import info.magnolia.ui.api.event.ContentChangedEvent;
41  import info.magnolia.ui.contentapp.movedialog.MoveActionCallback;
42  import info.magnolia.ui.framework.action.AbstractMultiItemAction;
43  import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrAdapter;
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.JcrNodeAdapter;
47  import info.magnolia.ui.vaadin.integration.jcr.JcrPropertyAdapter;
48  import info.magnolia.ui.vaadin.integration.jcr.JcrPropertyItemId;
49  import info.magnolia.ui.workbench.tree.MoveHandler;
50  import info.magnolia.ui.workbench.tree.MoveLocation;
51  
52  import java.util.Collections;
53  import java.util.Comparator;
54  import java.util.List;
55  
56  import javax.inject.Named;
57  import javax.jcr.Property;
58  import javax.jcr.RepositoryException;
59  
60  import com.vaadin.v7.data.Item;
61  
62  /**
63   * Action that moves a node.
64   *
65   * @see MoveNodeActionDefinition
66   */
67  public class MoveNodeAction extends AbstractMultiItemAction<MoveNodeActionDefinition> {
68  
69      /**
70       * The item where the items should be moved relative to.
71       */
72      private final JcrNodeAdapter targetItem;
73  
74      protected final EventBus admincentralEventBus;
75  
76      private MoveActionCallback callback;
77  
78      private MoveLocation moveLocation = MoveLocation.BEFORE;
79  
80      private MoveHandler moveHandler;
81  
82      public MoveNodeAction(
83              MoveNodeActionDefinition definition,
84              List<JcrItemAdapter> items,
85              JcrNodeAdapter targetItem,
86              @Named(AdmincentralEventBus.NAME) EventBus admincentralEventBus,
87              UiContext uiContext,
88              MoveActionCallback callback, MoveHandler moveHandler) {
89          super(definition, items, uiContext);
90          this.targetItem = targetItem;
91          this.admincentralEventBus = admincentralEventBus;
92          this.callback = callback;
93          this.moveHandler = moveHandler;
94      }
95  
96      @Override
97      public void execute() throws ActionExecutionException {
98          Item firstItem = getItems().get(0);
99          AbstractJcrAdapter itemAdapter = null;
100         if (firstItem instanceof AbstractJcrAdapter) {
101             itemAdapter = (AbstractJcrAdapter) firstItem;
102         }
103         super.execute();
104         if (itemAdapter != null) {
105             admincentralEventBus.fireEvent(new ContentChangedEvent(getChangedItemId(itemAdapter)));
106             callback.onMovePerformed(targetItem, moveLocation);
107         }
108     }
109 
110     private JcrItemId getChangedItemId(AbstractJcrAdapter itemAdapter) throws ActionExecutionException {
111         try {
112             if (itemAdapter.isNode()) {
113                 return itemAdapter.getItemId();
114             }
115 
116             String propertyName = ((JcrPropertyItemId) itemAdapter.getItemId()).getPropertyName();
117             Property property = targetItem.getJcrItem().getProperty(propertyName);
118             JcrPropertyAdapter propertyAdapter = new JcrPropertyAdapter(property);
119             return propertyAdapter.getItemId();
120 
121         } catch (RepositoryException e) {
122             throw new ActionExecutionException(e);
123         }
124     }
125 
126     @Override
127     protected void executeOnItem(JcrItemAdapter item) throws Exception {
128         moveLocation = getDefinition().getMoveLocation();
129         if (!moveHandler.moveItem(item, targetItem, moveLocation)) {
130             callback.onMoveCancelled();
131             throw new IllegalArgumentException("Move operation was not completed due to failed move validation.");
132         }
133     }
134 
135     @Override
136     protected String getSuccessMessage() {
137         return getDefinition().getSuccessMessage();
138     }
139 
140     @Override
141     protected String getFailureMessage() {
142         return getDefinition().getFailureMessage();
143     }
144 
145     @Override
146     protected List<JcrItemAdapter> getSortedItems(Comparator<JcrItemAdapter> comparator) {
147         final List<JcrItemAdapter> sortedItems = super.getSortedItems(comparator);
148         if (MoveLocation.AFTER.equals(getDefinition().getMoveLocation())) {
149             // moving multiple items after another one would implicitly revert the order of those items - this can be avoided by reverting the collection
150             Collections.reverse(sortedItems);
151         }
152         return sortedItems;
153     }
154 }