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.cms.exchange.ExchangeException;
37  import info.magnolia.commands.CommandsManager;
38  import info.magnolia.context.Context;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.event.EventBus;
41  import info.magnolia.i18nsystem.SimpleTranslator;
42  import info.magnolia.ui.api.app.SubAppContext;
43  import info.magnolia.ui.api.context.UiContext;
44  import info.magnolia.ui.api.event.AdmincentralEventBus;
45  import info.magnolia.ui.api.event.ContentChangedEvent;
46  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
47  import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
48  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
49  
50  import java.util.Arrays;
51  import java.util.List;
52  import java.util.Map;
53  import java.util.regex.Matcher;
54  import java.util.regex.Pattern;
55  
56  import javax.inject.Named;
57  import javax.jcr.Item;
58  
59  import org.apache.commons.lang3.StringUtils;
60  
61  /**
62   * UI action that allows to activate a single page (node) or recursively with all its sub-nodes depending on the value of {@link ActivationActionDefinition#isRecursive()}.
63   *
64   * @param <D> {@link ActivationActionDefinition}.
65   */
66  public class ActivationAction<D extends ActivationActionDefinition> extends AbstractCommandAction<D> {
67  
68      public static final String ATTRIBUTE_MODIFIEDONLY = "modifiedOnly";
69  
70      private final EventBus admincentralEventBus;
71      private final UiContext uiContext;
72      private final SimpleTranslator i18n;
73      private final JcrItemId changedItemId;
74  
75      public ActivationAction(final D definition, final JcrItemAdapter item, final CommandsManager commandsManager,
76              @Named(AdmincentralEventBus.NAME) EventBus admincentralEventBus, SubAppContext uiContext, final SimpleTranslator i18n) {
77          this(definition, Arrays.asList(item), commandsManager, admincentralEventBus, uiContext, i18n);
78      }
79  
80      public ActivationAction(final D definition, final List<JcrItemAdapter> items, final CommandsManager commandsManager,
81              @Named(AdmincentralEventBus.NAME) EventBus admincentralEventBus, SubAppContext uiContext, final SimpleTranslator i18n) {
82          super(definition, items, commandsManager, uiContext, i18n);
83          this.admincentralEventBus = admincentralEventBus;
84          this.uiContext = uiContext;
85          this.i18n = i18n;
86          this.changedItemId = items.isEmpty() ? null : items.get(0).getItemId();
87      }
88  
89      @Override
90      protected Map<String, Object> buildParams(final Item jcrItem) {
91          Map<String, Object> params = super.buildParams(jcrItem);
92          params.put(Context.ATTRIBUTE_RECURSIVE, getDefinition().isRecursive());
93          params.put(ATTRIBUTE_MODIFIEDONLY, getDefinition().isModifiedOnly());
94  
95          return params;
96      }
97  
98      @Override
99      protected void onError(Exception e) {
100         if (e.getCause() != null && e.getCause() instanceof ExchangeException) {
101             String messageBody = shortenErrorMessage(e.getCause().getLocalizedMessage());
102             uiContext.openAlert(MessageStyleTypeEnum.ERROR, getErrorMessage(), messageBody, i18n.translate("button.ok"), () -> {});
103         } else {
104             uiContext.openNotification(MessageStyleTypeEnum.ERROR, true, getErrorMessage());
105         }
106     }
107 
108     @Override
109     protected void onPostExecute() throws Exception {
110         admincentralEventBus.fireEvent(new ContentChangedEvent(changedItemId));
111 
112         Context context = MgnlContext.getInstance();
113         // yes, this is inverted, because a chain returns false when it is finished.
114         boolean success = !(Boolean) context.getAttribute(COMMAND_RESULT);
115         String message = i18n.translate(getMessage(success));
116         MessageStyleTypeEnum messageStyleType = success ? MessageStyleTypeEnum.INFO : MessageStyleTypeEnum.ERROR;
117 
118         if (StringUtils.isNotBlank(message)) {
119             uiContext.openNotification(messageStyleType, true, message);
120         }
121     }
122 
123     protected String getMessage(boolean success) {
124         if (success) {
125             return getSuccessMessage() != null ? getSuccessMessage() : getDefinition().getSuccessMessage();
126         } else {
127             return getFailureMessage() != null ? getFailureMessage() : getDefinition().getFailureMessage();
128         }
129     }
130 
131     protected String getErrorMessage() {
132         return getDefinition().getErrorMessage();
133     }
134 
135     /**
136      * Make the shorter error by getting message from "error(s) detected:".
137      */
138     private String shortenErrorMessage(String errorMessage) {
139         if (StringUtils.isNotEmpty(errorMessage)) {
140             Matcher matcher = Pattern.compile("errors? detected:").matcher(errorMessage);
141             if (matcher.find()) {
142                 errorMessage = StringUtils.substring(errorMessage, matcher.start()).replaceAll("\n", "<br/>");
143             }
144         }
145 
146         return errorMessage;
147     }
148 }