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.framework.action;
35
36 import info.magnolia.ui.api.action.AbstractAction;
37 import info.magnolia.ui.api.action.ActionDefinition;
38 import info.magnolia.ui.api.action.ActionExecutionException;
39 import info.magnolia.ui.api.context.UiContext;
40 import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
41 import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
42 import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
43
44 import java.util.ArrayList;
45 import java.util.Comparator;
46 import java.util.LinkedHashMap;
47 import java.util.List;
48 import java.util.Map;
49
50 import javax.jcr.RepositoryException;
51
52 import org.apache.commons.lang3.StringUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 import com.google.common.collect.Ordering;
57
58
59
60
61
62
63 public abstract class AbstractMultiItemAction<D extends ActionDefinition> extends AbstractAction<D> {
64
65 private final Logger log = LoggerFactory.getLogger(getClass());
66
67 private final List<JcrItemAdapter> items;
68 private Map<JcrItemAdapter, Exception> failedItems;
69 private final UiContext uiContext;
70
71 private JcrItemAdapter currentItem;
72
73 protected AbstractMultiItemAction(D definition, JcrItemAdapter item, UiContext uiContext) {
74 super(definition);
75 this.items = new ArrayList<JcrItemAdapter>(1);
76 this.items.add(item);
77 this.uiContext = uiContext;
78 }
79
80 protected AbstractMultiItemAction(D definition, List<JcrItemAdapter> items, UiContext uiContext) {
81 super(definition);
82 this.items = items;
83 this.uiContext = uiContext;
84 }
85
86
87
88
89 protected abstract void executeOnItem(JcrItemAdapter item) throws Exception;
90
91
92
93
94
95 protected abstract String getSuccessMessage();
96
97
98
99
100
101 protected abstract String getFailureMessage();
102
103 @Override
104 public void execute() throws ActionExecutionException {
105 failedItems = new LinkedHashMap<JcrItemAdapter, Exception>();
106
107 for (JcrItemAdapter item : getSortedItems(getItemComparator())) {
108 this.currentItem = item;
109 try {
110 executeOnItem(item);
111 } catch (Exception ex) {
112 failedItems.put(item, ex);
113 }
114 }
115 this.currentItem = null;
116
117 if (failedItems.isEmpty()) {
118 String message = getSuccessMessage();
119 if (StringUtils.isNotBlank(message)) {
120 uiContext.openNotification(MessageStyleTypeEnum.INFO, true, message);
121 }
122 } else {
123 String message = getErrorNotification();
124 if (StringUtils.isNotBlank(message)) {
125 uiContext.openNotification(MessageStyleTypeEnum.ERROR, false, message);
126 }
127 }
128 }
129
130 protected String getErrorNotification() {
131 String failureMessage = getFailureMessage();
132 if (failureMessage != null) {
133 StringBuilder notification = new StringBuilder(failureMessage);
134 notification.append("<ul>");
135 for (JcrItemAdapter item : failedItems.keySet()) {
136 Exception ex = failedItems.get(item);
137 notification.append("<li>").append("<b>");
138 notification.append(JcrItemUtil.getItemPath(item.getJcrItem())).append("</b>: ").append(ex.getMessage());
139 notification.append("</li>");
140 }
141 notification.append("</ul>");
142 return notification.toString();
143 }
144 return null;
145 }
146
147 protected List<JcrItemAdapter> getItems() {
148 return this.items;
149 }
150
151
152
153
154 protected List<JcrItemAdapter> getSortedItems(Comparator<JcrItemAdapter> comparator) {
155 return Ordering.from(comparator).sortedCopy(this.items);
156 }
157
158 protected UiContext getUiContext() {
159 return this.uiContext;
160 }
161
162 protected Map<JcrItemAdapter, Exception> getFailedItems() {
163 return this.failedItems;
164 }
165
166
167
168
169 protected JcrItemAdapter getCurrentItem() {
170 return this.currentItem;
171 }
172
173
174
175
176 protected void setCurrentItem(JcrItemAdapter item) {
177 this.currentItem = item;
178 }
179
180
181
182
183
184
185
186
187
188 protected Comparator<JcrItemAdapter> getItemComparator() {
189 return new Comparator<JcrItemAdapter>() {
190 @Override
191 public int compare(JcrItemAdapter o1, JcrItemAdapter o2) {
192 try {
193 int res = o2.getJcrItem().getDepth() - o1.getJcrItem().getDepth();
194 return res;
195 } catch (RepositoryException e) {
196 return 0;
197 }
198 }
199 };
200 }
201 }