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.cache.browser.app.action;
35
36 import info.magnolia.i18nsystem.SimpleTranslator;
37 import info.magnolia.ui.api.action.AbstractAction;
38 import info.magnolia.ui.api.action.ActionDefinition;
39 import info.magnolia.ui.api.action.ActionExecutionException;
40 import info.magnolia.ui.api.context.UiContext;
41 import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
42
43 import java.util.Collections;
44 import java.util.LinkedHashMap;
45 import java.util.List;
46 import java.util.Map;
47
48 import org.apache.commons.lang3.StringUtils;
49
50 import com.vaadin.v7.data.Item;
51
52
53
54
55
56
57 public abstract class AbstractMultiItemAction<D extends ActionDefinition> extends AbstractAction<D> {
58
59 private final D definition;
60 private final List<Item> relatedItems;
61 private final UiContext uiContext;
62 private final SimpleTranslator i18n;
63
64 public AbstractMultiItemAction(D definition, Item relatedItem, UiContext uiContext, SimpleTranslator i18n) {
65 this(definition, Collections.singletonList(relatedItem), uiContext, i18n);
66 }
67
68 public AbstractMultiItemAction(D definition, List<Item> relatedItems, UiContext uiContext, SimpleTranslator i18n) {
69 super(definition);
70 this.definition = definition;
71 this.relatedItems = relatedItems;
72 this.uiContext = uiContext;
73 this.i18n = i18n;
74 }
75
76 @Override
77 public void execute() throws ActionExecutionException {
78 Map<Item, String> failedItems = new LinkedHashMap<>();
79 for (Item item : getItems()) {
80 try {
81 executeOnItem(item);
82 } catch (Exception e) {
83 failedItems.put(item, e.getMessage());
84 }
85 }
86 if (failedItems.size() > 0 && StringUtils.isNotBlank(getFailureMessageKey())) {
87 getUiContext().openNotification(MessageStyleTypeEnum.ERROR, false, getTranslator().translate(getFailureMessageKey(), formatFailedItemsForMessage(failedItems)));
88 } else if (StringUtils.isNotBlank(getSuccessMessageKey())) {
89 getUiContext().openNotification(MessageStyleTypeEnum.INFO, true, getTranslator().translate(getSuccessMessageKey()));
90 }
91 }
92
93 protected abstract void executeOnItem(Item item) throws Exception;
94
95 protected abstract String getSuccessMessageKey();
96
97 protected abstract String getFailureMessageKey();
98
99 protected abstract String itemToString(Item item);
100
101 protected String formatFailedItemsForMessage(Map<Item, String> failedItems) {
102 StringBuilder sb = new StringBuilder();
103 sb.append("<ul>");
104 for (Item item : failedItems.keySet()) {
105 String error = failedItems.get(item);
106 sb.append("<li>").append("<b>");
107 sb.append(itemToString(item)).append("</b>: ").append(error);
108 sb.append("</li>");
109 }
110 sb.append("</ul>");
111 return sb.toString();
112 }
113
114 @Override
115 public D getDefinition() {
116 return definition;
117 }
118
119 public List<Item> getItems() {
120 return relatedItems;
121 }
122
123 public UiContext getUiContext() {
124 return uiContext;
125 }
126
127 public SimpleTranslator getTranslator() {
128 return i18n;
129 }
130 }