View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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.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   * 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   * @see ConfirmationActionDefinition
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      * Class that implement CommansActionBase should use
149      * this in order to perform tasks or notification in case of error.
150      */
151     protected void onError(Exception e) {
152         // it would be possible to use here i18n-izer framework: String message =  getDefinition().getErrorMessage();
153         // but this would require a key for every confirm-action; to keep things simple we are using SimpleTranslator now
154         // enhancement of ActionDefinitionKeyGenerator may allow to use i18n-izer but just with one key ...
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      * @return the Item linked to this action. If this action is used for multi Item return null.
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 }