1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
64
65
66
67 public class MoveNodeAction extends AbstractMultiItemAction<MoveNodeActionDefinition> {
68
69
70
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
150 Collections.reverse(sortedItems);
151 }
152 return sortedItems;
153 }
154 }