View Javadoc
1   /**
2    * This file Copyright (c) 2015 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.cache.browser.app.action;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.ui.api.action.AbstractAction;
38  import info.magnolia.ui.api.action.ActionDefinition;
39  import info.magnolia.ui.api.action.ActionExecutionException;
40  import info.magnolia.ui.api.context.UiContext;
41  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
42  
43  import java.util.Collections;
44  import java.util.LinkedHashMap;
45  import java.util.List;
46  import java.util.Map;
47  
48  import org.apache.commons.lang3.StringUtils;
49  
50  import com.vaadin.data.Item;
51  
52  /**
53   * Base class that can work over multiple {@link Item}s.
54   *
55   * @param <D> Definition
56   */
57  public abstract class AbstractMultiItemAction<D extends ActionDefinition> extends AbstractAction<D> {
58  
59      private final D definition;
60      private final List<Item> relatedItems;
61      private final UiContext uiContext;
62      private final SimpleTranslator i18n;
63  
64      public AbstractMultiItemAction(D definition, Item relatedItem, UiContext uiContext, SimpleTranslator i18n) {
65          this(definition, Collections.singletonList(relatedItem), uiContext, i18n);
66      }
67  
68      public AbstractMultiItemAction(D definition, List<Item> relatedItems, UiContext uiContext, SimpleTranslator i18n) {
69          super(definition);
70          this.definition = definition;
71          this.relatedItems = relatedItems;
72          this.uiContext = uiContext;
73          this.i18n = i18n;
74      }
75  
76      @Override
77      public void execute() throws ActionExecutionException {
78          Map<Item, String> failedItems = new LinkedHashMap<>();
79          for (Item item : getItems()) {
80              try {
81                  executeOnItem(item);
82              } catch (Exception e) {
83                  failedItems.put(item, e.getMessage());
84              }
85          }
86          if (failedItems.size() > 0 && StringUtils.isNotBlank(getFailureMessageKey())) {
87              getUiContext().openNotification(MessageStyleTypeEnum.ERROR, false, getTranslator().translate(getFailureMessageKey(), formatFailedItemsForMessage(failedItems)));
88          } else if (StringUtils.isNotBlank(getSuccessMessageKey())) {
89              getUiContext().openNotification(MessageStyleTypeEnum.INFO, true, getTranslator().translate(getSuccessMessageKey()));
90          }
91      }
92  
93      protected abstract void executeOnItem(Item item) throws Exception;
94  
95      protected abstract String getSuccessMessageKey();
96  
97      protected abstract String getFailureMessageKey();
98  
99      protected abstract String itemToString(Item item);
100 
101     protected String formatFailedItemsForMessage(Map<Item, String> failedItems) {
102         StringBuilder sb = new StringBuilder();
103         sb.append("<ul>");
104         for (Item item : failedItems.keySet()) {
105             String error = failedItems.get(item);
106             sb.append("<li>").append("<b>");
107             sb.append(itemToString(item)).append("</b>: ").append(error);
108             sb.append("</li>");
109         }
110         sb.append("</ul>");
111         return sb.toString();
112     }
113 
114     @Override
115     public D getDefinition() {
116         return definition;
117     }
118 
119     public List<Item> getItems() {
120         return relatedItems;
121     }
122 
123     public UiContext getUiContext() {
124         return uiContext;
125     }
126 
127     public SimpleTranslator getTranslator() {
128         return i18n;
129     }
130 }