View Javadoc
1   /**
2    * This file Copyright (c) 2014-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.contentapp.browser.action;
35  
36  import info.magnolia.cms.beans.config.VersionConfig;
37  import info.magnolia.cms.core.version.VersionManager;
38  import info.magnolia.commands.impl.VersionCommand;
39  import info.magnolia.context.Context;
40  import info.magnolia.context.SimpleContext;
41  import info.magnolia.event.EventBus;
42  import info.magnolia.i18nsystem.SimpleTranslator;
43  import info.magnolia.jcr.util.VersionUtil;
44  import info.magnolia.objectfactory.Components;
45  import info.magnolia.ui.api.app.AppContext;
46  import info.magnolia.ui.api.context.UiContext;
47  import info.magnolia.ui.api.event.AdmincentralEventBus;
48  import info.magnolia.ui.api.event.ContentChangedEvent;
49  import info.magnolia.ui.api.location.LocationController;
50  import info.magnolia.ui.api.overlay.ConfirmationCallback;
51  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
52  import info.magnolia.ui.form.EditorCallback;
53  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
54  import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrNodeAdapter;
55  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
56  
57  import javax.inject.Inject;
58  import javax.inject.Named;
59  import javax.jcr.Node;
60  import javax.jcr.RepositoryException;
61  import javax.jcr.version.Version;
62  import javax.jcr.version.VersionHistory;
63  import javax.jcr.version.VersionIterator;
64  
65  import org.slf4j.Logger;
66  import org.slf4j.LoggerFactory;
67  
68  /**
69   * UI action which allow to restore node to the state defined by the selected version.
70   */
71  public class RestoreVersionAction extends ShowVersionsAction<RestoreVersionActionDefinition> {
72  
73      private static final Logger log = LoggerFactory.getLogger(RestoreVersionAction.class);
74  
75      private final VersionManager versionManager;
76  
77      private final EventBus eventBus;
78  
79      private final VersionConfig versionConfig;
80  
81      private final Context context;
82  
83      @Inject
84      public RestoreVersionAction(RestoreVersionActionDefinition definition, AppContext appContext, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, AbstractJcrNodeAdapter nodeAdapter, SimpleTranslator i18n, VersionManager versionManager, final @Named(AdmincentralEventBus.NAME) EventBus eventBus, VersionConfig versionConfig, ContentConnector contentConnector, Context context) {
85          super(definition, appContext, locationController, uiContext, formDialogPresenter, nodeAdapter, i18n, contentConnector);
86          this.versionManager = versionManager;
87          this.eventBus = eventBus;
88          this.versionConfig = versionConfig;
89          this.dialogID = "ui-contentapp:code:RestoreVersionAction.selectVersion";
90          this.context = context;
91      }
92      /**
93       * @deprecated since 5.4.8 - use {@link #RestoreVersionAction(RestoreVersionActionDefinition, AppContext, LocationController, UiContext, FormDialogPresenter, AbstractJcrNodeAdapter, SimpleTranslator, VersionManager, EventBus, VersionConfig, ContentConnector, Context)}
94       */
95      @Deprecated
96      public RestoreVersionAction(RestoreVersionActionDefinition definition, AppContext appContext, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, AbstractJcrNodeAdapter nodeAdapter, SimpleTranslator i18n, VersionManager versionManager, final @Named(AdmincentralEventBus.NAME) EventBus eventBus, VersionConfig versionConfig, ContentConnector contentConnector) {
97          this(definition, appContext, locationController, uiContext, formDialogPresenter, nodeAdapter, i18n, versionManager, eventBus, versionConfig, contentConnector, Components.getComponent(Context.class));
98      }
99  
100     /**
101      * @deprecated since 5.4.6 - use {@link RestoreVersionAction(RestoreVersionActionDefinition, AppContext, LocationController, UiContext, FormDialogPresenter, AbstractJcrNodeAdapter, SimpleTranslator, VersionManager, EventBus, VersionConfig, ContentConnector)} instead.
102      */
103     @Deprecated
104     public RestoreVersionAction(RestoreVersionActionDefinition definition, AppContext appContext, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, AbstractJcrNodeAdapter nodeAdapter, SimpleTranslator i18n, VersionManager versionManager, final @Named(AdmincentralEventBus.NAME) EventBus eventBus, VersionConfig versionConfig) {
105         this(definition, appContext, locationController, uiContext, formDialogPresenter, nodeAdapter, i18n, versionManager, eventBus, versionConfig, getContentConnectorForDeprecations(uiContext));
106     }
107 
108     /**
109      * @deprecated since 5.3.5 - use {@link RestoreVersionAction(RestoreVersionActionDefinition, AppContext, LocationController, UiContext, FormDialogPresenter, AbstractJcrNodeAdapter, SimpleTranslator, VersionManager, EventBus, VersionConfig, ContentConnector)} instead.
110      */
111     @Deprecated
112     public RestoreVersionAction(RestoreVersionActionDefinition definition, AppContext appContext, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, AbstractJcrNodeAdapter nodeAdapter, SimpleTranslator i18n, VersionManager versionManager, final @Named(AdmincentralEventBus.NAME) EventBus eventBus) {
113         this(definition, appContext, locationController, uiContext, formDialogPresenter, nodeAdapter, i18n, versionManager, eventBus, Components.getComponent(VersionConfig.class), getContentConnectorForDeprecations(uiContext));
114     }
115 
116     @Override
117     protected EditorCallback getEditorCallback() {
118         return new EditorCallback() {
119             @Override
120             public void onSuccess(String actionName) {
121                 try {
122                     Node node = getNode();
123                     String versionName = getVersionName();
124                     VersionHistory versionHistory = versionManager.getVersionHistory(node);
125 
126                     if (getDefinition().isCreateVersionBeforeRestore()) {
127                         long versionsSize = versionHistory.getAllVersions().getSize() - 2; // Do not consider the root version
128 
129                         if (!VersionUtil.hasPreviousVersion(node, versionName) && versionsSize >= versionConfig.getMaxVersionAllowed()) {
130                             uiContext.openConfirmation(
131                                     MessageStyleTypeEnum.WARNING,
132                                     i18n.translate("ui-contentapp.actions.restoreVersion.confirmation.title"),
133                                     i18n.translate("ui-contentapp.actions.restoreVersion.confirmation.body"),
134                                     i18n.translate("ui-contentapp.actions.restoreVersion.confirmation.confirmButton"),
135                                     i18n.translate("ui-contentapp.actions.restoreVersion.confirmation.cancelButton"),
136                                     false,
137                                     getConfirmationCallback());
138                         } else {
139                             restoreVersion(node, versionName, true);
140                         }
141                     } else {
142                         restoreVersion(node, versionName, false);
143                     }
144                 } catch (RepositoryException e) {
145                     uiContext.openNotification(MessageStyleTypeEnum.ERROR, true, i18n.translate("ui-contentapp.actions.restoreVersion.notification.error", e.getMessage()));
146                     log.error(i18n.translate("ui-contentapp.actions.restoreVersion.notification.error", e.getMessage()), e);
147                 }
148             }
149 
150             @Override
151             public void onCancel() {
152                 formDialogPresenter.closeDialog();
153             }
154         };
155     }
156 
157     /**
158      * Confirmation callback when version store is full and user has to decide whether to restore the last version
159      * without creating another version of the page before restoring.
160      */
161     protected ConfirmationCallback getConfirmationCallback() {
162         return new ConfirmationCallback() {
163             @Override
164             public void onSuccess() {
165                 try {
166                     restoreVersion(getNode(), getVersionName(), false);
167                 } catch (RepositoryException e) {
168                     log.error(i18n.translate("ui-contentapp.actions.restoreVersion.notification.error", e.getMessage()), e);
169                 }
170             }
171 
172             @Override
173             public void onCancel() {
174                 // Just close the confirmation but do not close the dialog
175             }
176         };
177     }
178 
179     /**
180      * Creates a version with an extra comment before restoring.
181      */
182     protected Version createVersionBeforeRestore(Node node) throws RepositoryException {
183         VersionCommand versionCommand = new VersionCommand(versionManager);
184         versionCommand.setRepository(node.getSession().getWorkspace().getName());
185         versionCommand.setPath(node.getPath());
186         versionCommand.setUuid(node.getIdentifier());
187         versionCommand.setComment("ui-contentapp.actions.restoreVersion.comment.restore");
188         versionCommand.setRecursive(false);
189         versionCommand.setUserName(context.getUser().getName());
190         try {
191             versionCommand.execute(new SimpleContext());
192         } catch (Exception e) {
193             throw new RepositoryException(e);
194         }
195         Version newestVersion = null;
196         VersionIterator versionIterator = versionManager.getAllVersions(node);
197         while (versionIterator.hasNext()) {
198             newestVersion = versionIterator.nextVersion();
199         }
200         return newestVersion;
201     }
202 
203     /**
204      * Restores a version by its version name.
205      *
206      * By default {@link RestoreVersionActionDefinition#createVersionBeforeRestore} is true thus a version is created
207      * before restore. When {@link VersionConfig#maxVersions} is reached such a version cannot be created when restoring
208      * the oldest version (without deleting that same version). In this case {@link #getConfirmationCallback()} is shown
209      * and once confirmed the version gets restored without "backup" of the current content.
210      *
211      * @see <a href="http://jira.magnolia-cms.com/browse/MGNLUI-3220">MGNLUI-3220</a>
212      */
213     private void restoreVersion(Node node, String versionName, boolean createVersionBeforeRestore) throws RepositoryException {
214         final Version versionToRestore = versionManager.getVersion(node, versionName);
215 
216         // Create another version before restore
217         if (createVersionBeforeRestore) {
218             createVersionBeforeRestore(node);
219         }
220 
221         // Restore version
222         versionManager.restore(node, versionToRestore, true);
223 
224         eventBus.fireEvent(new ContentChangedEvent(nodeAdapter.getItemId()));
225         uiContext.openNotification(MessageStyleTypeEnum.INFO, true, i18n.translate("ui-contentapp.actions.restoreVersion.notification.success"));
226 
227         formDialogPresenter.closeDialog();
228 
229         log.debug("Restored version [{}]", versionName);
230     }
231 
232 }