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.contentapp.browser.action;
35  
36  import info.magnolia.cms.core.version.VersionInfo;
37  import info.magnolia.event.SimpleEventBus;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.objectfactory.Components;
40  import info.magnolia.ui.dialog.action.CancelDialogActionDefinition;
41  import info.magnolia.ui.api.action.ActionDefinition;
42  import info.magnolia.ui.api.action.ActionExecutionException;
43  import info.magnolia.ui.api.app.AppContext;
44  import info.magnolia.ui.api.app.SubAppContext;
45  import info.magnolia.ui.api.context.UiContext;
46  import info.magnolia.ui.api.location.Location;
47  import info.magnolia.ui.api.location.LocationController;
48  import info.magnolia.ui.contentapp.contentconnector.ContentConnectorProvider;
49  import info.magnolia.ui.contentapp.detail.DetailLocation;
50  import info.magnolia.ui.contentapp.detail.DetailView;
51  import info.magnolia.ui.dialog.definition.ConfiguredFormDialogDefinition;
52  import info.magnolia.ui.dialog.definition.FormDialogDefinition;
53  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
54  import info.magnolia.ui.form.definition.ConfiguredFormDefinition;
55  import info.magnolia.ui.form.definition.ConfiguredTabDefinition;
56  import info.magnolia.ui.form.field.definition.SelectFieldDefinition;
57  import info.magnolia.ui.form.field.definition.SelectFieldOptionDefinition;
58  import info.magnolia.ui.framework.action.AbstractVersionAction;
59  import info.magnolia.ui.framework.action.EditorCallbackActionDefinition;
60  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
61  import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrNodeAdapter;
62  
63  import javax.inject.Inject;
64  import javax.jcr.Node;
65  import javax.jcr.RepositoryException;
66  
67  /**
68   * Opens a dialog with list of versions.
69   *
70   * @param <D> {@link ActionDefinition}.
71   */
72  public class ShowVersionsAction<D extends ActionDefinition> extends AbstractVersionAction<D> {
73  
74      protected final AbstractJcrNodeAdapter nodeAdapter;
75      private final ContentConnector contentConnector;
76      private final AppContext appContext;
77  
78      protected String dialogID;
79  
80      @Inject
81      public ShowVersionsAction(D definition, AppContext appContext, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, AbstractJcrNodeAdapter nodeAdapter, SimpleTranslator i18n, ContentConnector contentConnector) {
82          super(definition, locationController, uiContext, formDialogPresenter, i18n);
83          this.nodeAdapter = nodeAdapter;
84          this.appContext = appContext;
85          this.dialogID = "ui-contentapp:code:ShowVersionsAction.selectVersion";
86          this.contentConnector = contentConnector;
87      }
88  
89      /**
90       * @deprecated since 5.4.6 - use {@link ShowVersionsAction(D, AppContext, LocationController, UiContext, FormDialogPresenter, AbstractJcrNodeAdapter, SimpleTranslator, ContentConnector)} instead.
91       */
92      @Deprecated
93      public ShowVersionsAction(D definition, AppContext appContext, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, AbstractJcrNodeAdapter nodeAdapter, SimpleTranslator i18n) {
94          this(definition, appContext, locationController, uiContext, formDialogPresenter, nodeAdapter, i18n, getContentConnectorForDeprecations(uiContext));
95      }
96  
97      /**
98       * @deprecated since 5.3.5 - use {@link ShowVersionsAction(D, AppContext, LocationController, UiContext, FormDialogPresenter, AbstractJcrNodeAdapter, SimpleTranslator, ContentConnector)} instead.
99       */
100     @Deprecated
101     public ShowVersionsAction(ShowVersionsActionDefinition definition, AppContext appContext, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, AbstractJcrNodeAdapter nodeAdapter, SimpleTranslator i18n) {
102         this((D) definition, appContext, locationController, uiContext, formDialogPresenter, nodeAdapter, i18n, getContentConnectorForDeprecations(uiContext));
103     }
104 
105     /**
106      * Gets {@link ContentConnector} for deprecations from {@link UiContext}.
107      * Cannot get by {@link Components#getComponent(Class)} due to the scope of {@link ContentConnector} is sub-app.
108      * @see ContentConnectorProvider
109      */
110     protected static ContentConnector getContentConnectorForDeprecations(UiContext uiContext) {
111         if (uiContext instanceof SubAppContext) {
112             ContentConnectorProvider contentConnectorProvider = new ContentConnectorProvider((SubAppContext) uiContext, Components.getComponentProvider(), new SimpleEventBus());
113             return contentConnectorProvider.get();
114         } else {
115             throw new IllegalStateException("Cannot get ContentConnector from " + uiContext);
116         }
117     }
118 
119     @Override
120     protected Class getBeanItemClass() {
121         return VersionName.class;
122     }
123 
124     @Override
125     protected FormDialogDefinition buildNewComponentDialog() throws ActionExecutionException, RepositoryException {
126         ConfiguredFormDefinition form = new ConfiguredFormDefinition();
127 
128         ConfiguredTabDefinition tab = new ConfiguredTabDefinition();
129         tab.setName("versions");
130 
131         SelectFieldDefinition select = new SelectFieldDefinition();
132         select.setName(VersionName.PROPERTY_NAME_VERSION_NAME);
133         select.setSortOptions(false);
134         tab.addField(select);
135 
136         // All versions
137         for (VersionInfo versionInfo : getAvailableVersionInfoList()) {
138             SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
139             option.setValue(versionInfo.getVersionName());
140             option.setLabel(getVersionLabel(versionInfo));
141             select.addOption(option);
142         }
143 
144         ConfiguredFormDialogDefinition dialog = new ConfiguredFormDialogDefinition();
145         dialog.setModalityLevel(getModalityLevel());
146         dialog.setId(dialogID);
147         dialog.setForm(form);
148 
149         EditorCallbackActionDefinition callbackAction = new EditorCallbackActionDefinition();
150         callbackAction.setName("commit");
151         dialog.getActions().put(callbackAction.getName(), callbackAction);
152 
153         CancelDialogActionDefinition cancelAction = new CancelDialogActionDefinition();
154         cancelAction.setName("cancel");
155         dialog.getActions().put(cancelAction.getName(), cancelAction);
156 
157         form.addTab(tab);
158 
159         return dialog;
160     }
161 
162     @Override
163     protected Node getNode() throws RepositoryException {
164         return nodeAdapter.getJcrItem();
165     }
166 
167     @Override
168     protected Location getLocation() throws ActionExecutionException {
169         try {
170             Object itemId = contentConnector.getItemId(nodeAdapter);
171             String path = contentConnector.getItemUrlFragment(itemId);
172             return new DetailLocation(appContext.getName(), "detail", DetailView.ViewType.VIEW, path, getVersionName());
173         } catch (Exception e) {
174             throw new ActionExecutionException("Could not get location from nodeAdapter " + nodeAdapter.getItemId());
175         }
176     }
177 
178     protected String getVersionName() {
179         return (String) getItem().getItemProperty(VersionName.PROPERTY_NAME_VERSION_NAME).getValue();
180     }
181 
182     /**
183      * Simple POJO used to access user selection from dialog, see {@link com.vaadin.data.util.BeanItem}.
184      */
185     protected class VersionName {
186 
187         protected final static String PROPERTY_NAME_VERSION_NAME = "versionName";
188 
189         private String versionName;
190 
191         public String getVersionName() {
192             return versionName;
193         }
194 
195         public void setVersionName(String versionName) {
196             this.versionName = versionName;
197         }
198     }
199 
200 }