View Javadoc
1   /**
2    * This file Copyright (c) 2012-2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Configurable confirmation action. Can be used to intercept the actual action with user feedback.
59   * Allows configuration of a success action and a cancel action.
60   * 
61   * @param <D> {@link ConfirmationActionDefinition}.
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      * Class that implement CommansActionBase should use
154      * this in order to perform tasks or notification in case of error.
155      */
156     protected void onError(Exception e) {
157         // it would be possible to use here i18n-izer framework: String message = getDefinition().getErrorMessage();
158         // but this would require a key for every confirm-action; to keep things simple we are using SimpleTranslator now
159         // enhancement of ActionDefinitionKeyGenerator may allow to use i18n-izer but just with one key ...
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      * @return the Item linked to this action. If this action is used for multi Item return null.
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 }