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