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.i18nsystem.SimpleTranslator;
37 import info.magnolia.i18nsystem.util.MessageFormatterUtils;
38 import info.magnolia.ui.api.action.AbstractAction;
39 import info.magnolia.ui.api.action.ActionExecutionException;
40 import info.magnolia.ui.api.action.ActionExecutor;
41 import info.magnolia.ui.api.context.UiContext;
42 import info.magnolia.ui.api.overlay.ConfirmationCallback;
43 import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
44 import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
45
46 import java.util.Arrays;
47 import java.util.List;
48
49 import javax.inject.Inject;
50 import javax.jcr.RepositoryException;
51
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 import com.vaadin.v7.data.Item;
56
57
58
59
60
61
62
63 public class ConfirmationAction<D extends ConfirmationActionDefinition> extends AbstractAction<D> {
64
65 private static final Logger log = LoggerFactory.getLogger(ConfirmationAction.class);
66
67 private final List<? extends Item> items;
68 private final UiContext uiContext;
69 private final ActionExecutor actionExecutor;
70 private final SimpleTranslator i18n;
71 private final ContentConnector contentConnector;
72
73 public ConfirmationAction(D definition, Item item, UiContext uiContext, ActionExecutor actionExecutor, SimpleTranslator i18n, ContentConnector contentConnector) {
74 super(definition);
75 this.contentConnector = contentConnector;
76 this.items = Arrays.asList(item);
77 this.uiContext = uiContext;
78 this.actionExecutor = actionExecutor;
79 this.i18n = i18n;
80 }
81
82 @Inject
83 public ConfirmationAction(D definition, List<? extends Item> items, UiContext uiContext, ActionExecutor actionExecutor, SimpleTranslator i18n, ContentConnector contentConnector) {
84 super(definition);
85 this.items = items;
86 this.uiContext = uiContext;
87 this.actionExecutor = actionExecutor;
88 this.i18n = i18n;
89 this.contentConnector = contentConnector;
90 }
91
92 @Override
93 public void execute() throws ActionExecutionException {
94 try {
95 uiContext.openConfirmation(
96 MessageStyleTypeEnum.WARNING, getConfirmationHeader(), getConfirmationMessage(), getDefinition().getProceedLabel(), getDefinition().getCancelLabel(), getDefinition().isDefaultCancel(),
97 new ConfirmationCallback() {
98 @Override
99 public void onSuccess() {
100 if (getDefinition().getSuccessActionName() != null) {
101 try {
102 if (items.size() == 1) {
103 actionExecutor.execute(getDefinition().getSuccessActionName(), items.get(0));
104 } else {
105 actionExecutor.execute(getDefinition().getSuccessActionName(), items);
106 }
107 } catch (ActionExecutionException e) {
108 onError(e);
109 }
110 }
111 }
112
113 @Override
114 public void onCancel() {
115 if (getDefinition().getCancelActionName() != null) {
116 try {
117 if (items.size() == 1) {
118 actionExecutor.execute(getDefinition().getCancelActionName(), items.get(0));
119 } else {
120 actionExecutor.execute(getDefinition().getCancelActionName(), items);
121 }
122 } catch (ActionExecutionException e) {
123 onError(e);
124 }
125 }
126 }
127 });
128 } catch (Exception e) {
129 throw new ActionExecutionException(e);
130 }
131 }
132
133 protected String getConfirmationHeader() throws Exception {
134 return formatMessage(getDefinition().getConfirmationHeader());
135 }
136
137 protected String getConfirmationMessage() throws Exception {
138 return formatMessage(getDefinition().getConfirmationMessage());
139 }
140
141 protected String getListOfItemPaths() {
142 StringBuilder builder = new StringBuilder("<ul>");
143 for (Item item : items) {
144 Object itemId = contentConnector.getItemId(item);
145 String path = contentConnector.getItemUrlFragment(itemId);
146 builder.append("<li>").append(path).append("</li>");
147 }
148 builder.append("</ul>");
149 return builder.toString();
150 }
151
152
153
154
155
156 protected void onError(Exception e) {
157
158
159
160 String message = i18n.translate("ui-framework.actions.confirmAction.errorMessage");
161 log.error(message, e);
162 uiContext.openNotification(MessageStyleTypeEnum.ERROR, true, message);
163 }
164
165 private String formatMessage(final String message) throws RepositoryException {
166 long howMany = items.size();
167 return MessageFormatterUtils.format(message, howMany, howMany);
168 }
169
170
171
172
173 protected Item getItem() {
174 if (items != null && items.size() == 1) {
175 return items.get(0);
176 } else {
177 return null;
178 }
179 }
180
181 protected List<? extends Item> getItems() {
182 return items;
183 }
184
185 protected SimpleTranslator getSimpleTranslator() {
186 return i18n;
187 }
188
189 }