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.core.version.VersionInfo;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.i18nsystem.util.MessageFormatterUtils;
39  import info.magnolia.jcr.util.VersionUtil;
40  import info.magnolia.ui.api.action.AbstractAction;
41  import info.magnolia.ui.api.action.ActionDefinition;
42  import info.magnolia.ui.api.action.ActionExecutionException;
43  import info.magnolia.ui.api.context.UiContext;
44  import info.magnolia.ui.api.location.Location;
45  import info.magnolia.ui.api.location.LocationController;
46  import info.magnolia.ui.api.overlay.OverlayLayer;
47  import info.magnolia.ui.dialog.definition.FormDialogDefinition;
48  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
49  import info.magnolia.ui.form.EditorCallback;
50  import info.magnolia.ui.vaadin.integration.contentconnector.DefaultContentConnector;
51  import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrNodeAdapter;
52  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
53  
54  import java.util.List;
55  
56  import javax.jcr.Node;
57  import javax.jcr.RepositoryException;
58  
59  import org.apache.commons.lang3.StringUtils;
60  
61  import com.vaadin.v7.data.util.BeanItem;
62  
63  /**
64   * Abstract version action showing available versions of an item.
65   *
66   * @param <D> {@link info.magnolia.ui.api.action.ActionDefinition}.
67   */
68  public abstract class AbstractVersionAction<D extends ActionDefinition> extends AbstractAction<D> {
69  
70      /**
71       * Will display: VersionNumber (Date) (User: Comment), i.e. <code>1.1 (MM/dd/YYYY HH:mm) (User: Comment)</code>.
72       */
73      public final String MESSAGE_FORMAT_VERSION_OPTION_LABEL = "{0} ({1}) ({2}: {3})";
74      /**
75       * Will display: VersionNumber (Date) (User), i.e. <code>1.1 (MM/dd/YYYY HH:mm) (User)</code>.
76       */
77      public final String MESSAGE_FORMAT_VERSION_OPTION_LABEL_NO_COMMENT = "{0} ({1}) ({2})";
78  
79      protected final LocationController locationController;
80      protected final UiContext uiContext;
81      protected final SimpleTranslator i18n;
82      protected final FormDialogPresenter formDialogPresenter;
83      /**
84       * @deprecated since 5.3.4 - define and inject {@link AbstractJcrNodeAdapter} in your subclass.
85       */
86      @Deprecated
87      protected final AbstractJcrNodeAdapter nodeAdapter;
88      private BeanItem<?> item;
89  
90      /**
91       * @deprecated since 5.3.4 - use {@link AbstractVersionAction(D, LocationController, UiContext, FormDialogPresenter, SimpleTranslator)} instead.
92       */
93      @Deprecated
94      protected AbstractVersionAction(D definition, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, AbstractJcrNodeAdapter nodeAdapter, SimpleTranslator i18n) {
95          super(definition);
96          this.locationController = locationController;
97          this.uiContext = uiContext;
98          this.formDialogPresenter = formDialogPresenter;
99          this.nodeAdapter = nodeAdapter;
100         this.i18n = i18n;
101     }
102 
103     protected AbstractVersionAction(D definition, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, SimpleTranslator i18n) {
104         super(definition);
105         this.locationController = locationController;
106         this.uiContext = uiContext;
107         this.formDialogPresenter = formDialogPresenter;
108         this.i18n = i18n;
109 
110         this.nodeAdapter = null;
111     }
112 
113     @Override
114     public void execute() throws ActionExecutionException {
115         try {
116             final FormDialogDefinition dialogDefinition = buildNewComponentDialog();
117             // Using a beanItem instead of an jcrItem
118             item = new BeanItem(getBeanItemClass());
119             // Perform custom chaining of dialogs
120             formDialogPresenter.start(item, dialogDefinition, uiContext, getEditorCallback(), new DefaultContentConnector());
121         } catch (RepositoryException e) {
122             throw new ActionExecutionException("Could not execute action", e);
123         }
124     }
125 
126     protected EditorCallback getEditorCallback() {
127         return new EditorCallback() {
128             @Override
129             public void onSuccess(String actionName) {
130                 try {
131                     // Build the new location to go to
132                     Location location = getLocation();
133 
134                     // Open location
135                     locationController.goTo(location);
136                 } catch (ActionExecutionException e) {
137                     uiContext.openNotification(MessageStyleTypeEnum.ERROR, true, i18n.translate("ui-framework.version.executionException.noValidVersion"));
138                 }
139 
140                 // Close the dialog
141                 formDialogPresenter.closeDialog();
142             }
143 
144             @Override
145             public void onCancel() {
146                 formDialogPresenter.closeDialog();
147             }
148         };
149     }
150 
151     protected List<VersionInfo> getAvailableVersionInfoList() throws ActionExecutionException {
152         Node node;
153         try {
154             node = getNode();
155         } catch (RepositoryException e) {
156             throw new ActionExecutionException(e);
157         }
158 
159         if (node != null) {
160             List<VersionInfo> versionInfoList = VersionUtil.getVersionInfoList(node);
161 
162             // This should not happen, as we use action availability for this action
163             if (versionInfoList == null || versionInfoList.isEmpty()) {
164                 throw new ActionExecutionException(String.format(i18n.translate("ui-framework.version.infoList.noListForItem"), node));
165             }
166 
167             return versionInfoList;
168         }
169 
170         return null;
171     }
172 
173     protected String getVersionLabel(VersionInfo versionInfo) {
174         if (StringUtils.isEmpty(versionInfo.getVersionComment())) {
175             return MessageFormatterUtils.format(MESSAGE_FORMAT_VERSION_OPTION_LABEL_NO_COMMENT, versionInfo.getVersionName(), versionInfo.getVersionDate(), versionInfo.getVersionUser());
176         } else {
177             return MessageFormatterUtils.format(MESSAGE_FORMAT_VERSION_OPTION_LABEL, versionInfo.getVersionName(), versionInfo.getVersionDate(), versionInfo.getVersionUser(), i18n.translate(versionInfo.getVersionComment()));
178         }
179     }
180 
181     protected BeanItem<?> getItem() {
182         return item;
183     }
184 
185     /**
186      * Returns the {@link info.magnolia.ui.api.overlay.OverlayLayer.ModalityLevel} for versions dialogs.
187      */
188     protected OverlayLayer.ModalityLevel getModalityLevel() {
189         return OverlayLayer.ModalityLevel.LIGHT;
190     }
191 
192     protected abstract Class getBeanItemClass();
193 
194     protected abstract FormDialogDefinition buildNewComponentDialog() throws ActionExecutionException, RepositoryException;
195 
196     protected abstract Node getNode() throws RepositoryException;
197 
198     protected abstract Location getLocation() throws ActionExecutionException;
199 
200 }