View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.ui.api.action.AbstractAction;
37  import info.magnolia.ui.api.action.ActionDefinition;
38  import info.magnolia.ui.api.action.ActionExecutionException;
39  import info.magnolia.ui.api.context.UiContext;
40  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
41  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
42  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
43  
44  import java.util.Comparator;
45  import java.util.LinkedHashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  import javax.jcr.RepositoryException;
50  
51  import org.apache.commons.lang3.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  import com.google.common.collect.Lists;
56  import com.google.common.collect.Ordering;
57  
58  /**
59   * Abstract multi-item Action that defines the default behavior.
60   *
61   * @param <D> the action definition type
62   *
63   * @deprecated since 6.2 - all actions can now inject {@link info.magnolia.ui.ValueContext} and implement
64   * multi-item support as they require.
65   *
66   * @see <a href="https://documentation.magnolia-cms.com/display/DOCS62/Upgrading+to+Magnolia+6.2.x">Upgrading to Magnolia 6.2.x</a>
67   */
68  @Deprecated
69  public abstract class AbstractMultiItemAction<D extends ActionDefinition> extends AbstractAction<D> {
70  
71      private final Logger log = LoggerFactory.getLogger(getClass());
72  
73      private final List<JcrItemAdapter> items;
74      private Map<JcrItemAdapter, Exception> failedItems;
75      private final UiContext uiContext;
76      // the item that is currently BEING processed
77      private JcrItemAdapter currentItem;
78  
79      protected AbstractMultiItemAction(D definition, JcrItemAdapter item, UiContext uiContext) {
80          this(definition, Lists.newArrayList(item), uiContext);
81      }
82  
83      protected AbstractMultiItemAction(D definition, List<JcrItemAdapter> items, UiContext uiContext) {
84          super(definition);
85          this.items = items;
86          this.uiContext = uiContext;
87      }
88  
89      /**
90       * Executes the action on ONE item.
91       */
92      protected abstract void executeOnItem(JcrItemAdapter item) throws Exception;
93  
94      /**
95       * Returns the message to display, if the execution succeeds on ALL items. May return <code>null</code>,
96       * if the implementing action handles the user notification on its own.
97       */
98      protected abstract String getSuccessMessage();
99  
100     /**
101      * Returns the message to display, if the execution fails on at least ONE item. May return <code>null</code>,
102      * if the implementing action handles the user notification on its own.
103      */
104     protected abstract String getFailureMessage();
105 
106     @Override
107     public void execute() throws ActionExecutionException {
108         failedItems = new LinkedHashMap<JcrItemAdapter, Exception>();
109 
110         for (JcrItemAdapter item : getSortedItems(getItemComparator())) {
111             this.currentItem = item;
112             try {
113                 executeOnItem(item);
114             } catch (Exception ex) {
115                 failedItems.put(item, ex);
116             }
117         }
118         this.currentItem = null;
119 
120         if (failedItems.isEmpty()) {
121             String message = getSuccessMessage();
122             if (StringUtils.isNotBlank(message)) {
123                 uiContext.openNotification(MessageStyleTypeEnum.INFO, true, message);
124             }
125         } else {
126             String message = getErrorNotification();
127             if (StringUtils.isNotBlank(message)) {
128                 uiContext.openNotification(MessageStyleTypeEnum.ERROR, false, message);
129             }
130         }
131     }
132 
133     protected String getErrorNotification() {
134         String failureMessage = getFailureMessage();
135         if (failureMessage != null) {
136             StringBuilder notification = new StringBuilder(failureMessage);
137             notification.append("<ul>");
138             for (JcrItemAdapter item : failedItems.keySet()) {
139                 Exception ex = failedItems.get(item);
140                 notification.append("<li>").append("<b>");
141                 notification.append(JcrItemUtil.getItemPath(item.getJcrItem())).append("</b>: ").append(ex.getMessage());
142                 notification.append("</li>");
143             }
144             notification.append("</ul>");
145             return notification.toString();
146         }
147         return null;
148     }
149 
150     protected List<JcrItemAdapter> getItems() {
151         return this.items;
152     }
153 
154     /**
155      * @return the sorted Items list based on the desired {@link Comparator}.
156      */
157     protected List<JcrItemAdapter> getSortedItems(Comparator<JcrItemAdapter> comparator) {
158         return Ordering.from(comparator).sortedCopy(this.items);
159     }
160 
161     protected UiContext getUiContext() {
162         return this.uiContext;
163     }
164 
165     protected Map<JcrItemAdapter, Exception> getFailedItems() {
166         return this.failedItems;
167     }
168 
169     /**
170      * Returns the item that is currently <b>being</b> processed - i.e. <code>null</code> if the {@link #execute()} method is not running.
171      */
172     protected JcrItemAdapter getCurrentItem() {
173         return this.currentItem;
174     }
175 
176     /**
177      * This method should be used <b>only in tests</b> (when the test does not call the {@link #execute()} method but e.g. only the {@link AbstractCommandAction#onPreExecute()} so the current item hasn't been set.
178      */
179     protected void setCurrentItem(JcrItemAdapter item) {
180         this.currentItem = item;
181     }
182 
183     /**
184      * Create a {@link Comparator} used to sort {@link JcrItemAdapter}.
185      * First element of the list are child items:
186      * - /a/b.property
187      * - /a/b
188      * - /a
189      * - /
190      */
191     protected Comparator<JcrItemAdapter> getItemComparator() {
192         return new Comparator<JcrItemAdapter>() {
193             @Override
194             public int compare(JcrItemAdapter/../../../../info/magnolia/ui/vaadin/integration/jcr/JcrItemAdapter.html#JcrItemAdapter">JcrItemAdapter o1, JcrItemAdapter o2) {
195                 try {
196                     int res = o2.getJcrItem().getDepth() - o1.getJcrItem().getDepth();
197                     return res;
198                 } catch (RepositoryException e) {
199                     return 0;
200                 }
201             }
202         };
203     }
204 }