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