View Javadoc
1   /**
2    * This file Copyright (c) 2013-2014 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.i18nsystem.SimpleTranslator;
38  import info.magnolia.ui.admincentral.dialog.action.CancelDialogActionDefinition;
39  import info.magnolia.ui.api.action.ActionDefinition;
40  import info.magnolia.ui.api.action.ActionExecutionException;
41  import info.magnolia.ui.api.app.AppContext;
42  import info.magnolia.ui.api.context.UiContext;
43  import info.magnolia.ui.api.location.Location;
44  import info.magnolia.ui.api.location.LocationController;
45  import info.magnolia.ui.contentapp.detail.DetailLocation;
46  import info.magnolia.ui.contentapp.detail.DetailView;
47  import info.magnolia.ui.dialog.action.CallbackDialogActionDefinition;
48  import info.magnolia.ui.dialog.definition.ConfiguredFormDialogDefinition;
49  import info.magnolia.ui.dialog.definition.FormDialogDefinition;
50  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
51  import info.magnolia.ui.form.definition.ConfiguredFormDefinition;
52  import info.magnolia.ui.form.definition.ConfiguredTabDefinition;
53  import info.magnolia.ui.form.field.definition.SelectFieldDefinition;
54  import info.magnolia.ui.form.field.definition.SelectFieldOptionDefinition;
55  import info.magnolia.ui.framework.action.AbstractVersionAction;
56  import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrNodeAdapter;
57  
58  import javax.inject.Inject;
59  import javax.jcr.Node;
60  import javax.jcr.RepositoryException;
61  
62  /**
63   * Opens a dialog with list of versions.
64   * 
65   * @param <D> {@link ActionDefinition}.
66   */
67  public class ShowVersionsAction<D extends ActionDefinition> extends AbstractVersionAction<D> {
68  
69      private final AppContext appContext;
70      protected final AbstractJcrNodeAdapter nodeAdapter;
71  
72      protected String dialogID;
73  
74      @Inject
75      public ShowVersionsAction(D definition, AppContext appContext, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, AbstractJcrNodeAdapter nodeAdapter, SimpleTranslator i18n) {
76          super(definition, locationController, uiContext, formDialogPresenter, i18n);
77  
78          this.nodeAdapter = nodeAdapter;
79          this.appContext = appContext;
80          this.dialogID = "ui-contentapp:code:ShowVersionsAction.selectVersion";
81      }
82  
83      /**
84       * @deprecated since 5.3.5 - use {@link ShowVersionsAction(D, AppContext, LocationController, UiContext, FormDialogPresenter, AbstractJcrNodeAdapter, SimpleTranslator)} instead.
85       */
86      public ShowVersionsAction(ShowVersionsActionDefinition definition, AppContext appContext, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, AbstractJcrNodeAdapter nodeAdapter, SimpleTranslator i18n) {
87          this((D) definition, appContext, locationController, uiContext, formDialogPresenter, nodeAdapter, i18n);
88      }
89  
90      @Override
91      protected Class getBeanItemClass() {
92          return VersionName.class;
93      }
94  
95      @Override
96      protected FormDialogDefinition buildNewComponentDialog() throws ActionExecutionException, RepositoryException {
97          ConfiguredFormDefinition form = new ConfiguredFormDefinition();
98  
99          ConfiguredTabDefinition tab = new ConfiguredTabDefinition();
100         tab.setName("versions");
101 
102         SelectFieldDefinition select = new SelectFieldDefinition();
103         select.setName(VersionName.PROPERTY_NAME_VERSION_NAME);
104         select.setSortOptions(false);
105         tab.addField(select);
106 
107         // All versions
108         for (VersionInfo versionInfo : getAvailableVersionInfoList()) {
109             SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
110             option.setValue(versionInfo.getVersionName());
111             option.setLabel(getVersionLabel(versionInfo));
112             select.addOption(option);
113         }
114 
115         ConfiguredFormDialogDefinition dialog = new ConfiguredFormDialogDefinition();
116         dialog.setModalityLevel(getModalityLevel());
117         dialog.setId(dialogID);
118         dialog.setForm(form);
119 
120         CallbackDialogActionDefinition callbackAction = new CallbackDialogActionDefinition();
121         callbackAction.setName("commit");
122         dialog.getActions().put(callbackAction.getName(), callbackAction);
123 
124         CancelDialogActionDefinition cancelAction = new CancelDialogActionDefinition();
125         cancelAction.setName("cancel");
126         dialog.getActions().put(cancelAction.getName(), cancelAction);
127 
128         form.addTab(tab);
129 
130         return dialog;
131     }
132 
133     @Override
134     protected Node getNode() throws RepositoryException {
135         return nodeAdapter.getJcrItem();
136     }
137 
138     @Override
139     protected Location getLocation() throws ActionExecutionException {
140         try {
141             final Node node = getNode();
142             final String path = node.getPath();
143             final String appName = appContext.getName();
144             final String versionName = getVersionName();
145 
146             return new DetailLocation(appName, "detail", DetailView.ViewType.VIEW, path, versionName);
147         } catch (RepositoryException e) {
148             throw new ActionExecutionException("Could not get node from nodeAdapter " + nodeAdapter.getItemId());
149         }
150     }
151 
152     protected String getVersionName() {
153         return (String) getItem().getItemProperty(VersionName.PROPERTY_NAME_VERSION_NAME).getValue();
154     }
155 
156     /**
157      * Simple POJO used to access user selection from dialog, see {@link com.vaadin.data.util.BeanItem}.
158      */
159     protected class VersionName {
160 
161         protected final static String PROPERTY_NAME_VERSION_NAME = "versionName";
162 
163         private String versionName;
164 
165         public String getVersionName() {
166             return versionName;
167         }
168 
169         public void setVersionName(String versionName) {
170             this.versionName = versionName;
171         }
172     }
173 
174 }