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