View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 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   * @deprecated since 6.0. Use info.magnolia.ui.contentapp.action.ConfirmationAction instead.
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      * Class that implement CommansActionBase should use
156      * this in order to perform tasks or notification in case of error.
157      */
158     protected void onError(Exception e) {
159         // it would be possible to use here i18n-izer framework: String message = getDefinition().getErrorMessage();
160         // but this would require a key for every confirm-action; to keep things simple we are using SimpleTranslator now
161         // enhancement of ActionDefinitionKeyGenerator may allow to use i18n-izer but just with one key ...
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      * @return the Item linked to this action. If this action is used for multi Item return null.
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 }