View Javadoc
1   /**
2    * This file Copyright (c) 2013-2015 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.pages.app.action;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.event.EventBus;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.objectfactory.ComponentProvider;
41  import info.magnolia.registry.RegistrationException;
42  import info.magnolia.rendering.template.TemplateDefinition;
43  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
44  import info.magnolia.ui.api.action.AbstractAction;
45  import info.magnolia.ui.api.action.ActionExecutionException;
46  import info.magnolia.ui.api.app.SubAppContext;
47  import info.magnolia.ui.api.app.SubAppEventBus;
48  import info.magnolia.ui.api.event.ContentChangedEvent;
49  import info.magnolia.ui.dialog.DialogView;
50  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
51  import info.magnolia.ui.dialog.formdialog.FormDialogPresenterFactory;
52  import info.magnolia.ui.form.EditorCallback;
53  import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
54  import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
55  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
56  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
57  import info.magnolia.ui.vaadin.integration.jcr.ModelConstants;
58  
59  import javax.inject.Inject;
60  import javax.inject.Named;
61  import javax.jcr.Node;
62  import javax.jcr.RepositoryException;
63  import javax.jcr.Session;
64  
65  import org.apache.commons.lang3.StringUtils;
66  import org.slf4j.Logger;
67  import org.slf4j.LoggerFactory;
68  
69  /**
70   * Adds a component underneath the area passed in {@link AreaElement}.
71   * Gets a list of available components for this area and creates a select field.
72   */
73  public class CreateComponentAction extends AbstractAction<CreateComponentActionDefinition> {
74  
75      private static final Logger log = LoggerFactory.getLogger(CreateComponentAction.class);
76  
77      private static final String NEW_COMPONENT_DIALOG = "pages:newComponent";
78  
79      private AreaElement area;
80      private EventBus eventBus;
81      private TemplateDefinitionRegistry templateDefinitionRegistry;
82      private SubAppContext subAppContext;
83      private ComponentProvider componentProvider;
84      private DialogView dialogView;
85      private FormDialogPresenterFactory formDialogPresenterFactory;
86  
87      @Inject
88      public CreateComponentAction(CreateComponentActionDefinition definition, AreaElement area, @Named(SubAppEventBus.NAME) EventBus eventBus, TemplateDefinitionRegistry templateDefinitionRegistry,
89              SubAppContext subAppContext, ComponentProvider componentProvider, FormDialogPresenterFactory formDialogPresenterFactory) {
90          super(definition);
91          this.area = area;
92          this.eventBus = eventBus;
93          this.templateDefinitionRegistry = templateDefinitionRegistry;
94          this.subAppContext = subAppContext;
95          this.componentProvider = componentProvider;
96          this.formDialogPresenterFactory = formDialogPresenterFactory;
97      }
98  
99      /**
100      * @deprecated since 5.3.8 no need of i18n {@link SimpleTranslator} here, options are now built in {@link info.magnolia.pages.app.field.ComponentSelectorFieldFactory ComponentSelectorFieldFactory}.
101      * Use other constructor {@link #CreateComponentAction(CreateComponentActionDefinition, AreaElement, EventBus, TemplateDefinitionRegistry, SubAppContext, ComponentProvider, FormDialogPresenterFactory)} instead.
102      */
103     @Deprecated
104     public CreateComponentAction(CreateComponentActionDefinition definition, AreaElement area, @Named(SubAppEventBus.NAME) EventBus eventBus, TemplateDefinitionRegistry templateDefinitionRegistry,
105             SubAppContext subAppContext, ComponentProvider componentProvider, FormDialogPresenterFactory formDialogPresenterFactory, SimpleTranslator i18n) {
106         this(definition, area, eventBus, templateDefinitionRegistry, subAppContext, componentProvider, formDialogPresenterFactory);
107     }
108 
109     @Override
110     public void execute() throws ActionExecutionException {
111         final FormDialogPresenter formDialogPresenter = componentProvider.newInstance(FormDialogPresenter.class);
112         try {
113             String workspace = area.getWorkspace();
114             String path = area.getPath();
115             Session session = MgnlContext.getJCRSession(area.getWorkspace());
116             if (path == null || !session.itemExists(path)) {
117                 path = "/";
118             }
119             session = MgnlContext.getJCRSession(workspace);
120 
121             Node areaNode = session.getNode(path);
122 
123             final JcrNodeAdapter item = new JcrNewNodeAdapter(areaNode, NodeTypes.Component.NAME);
124             DefaultProperty<String> property = new DefaultProperty<String>(String.class, "0");
125             item.addItemProperty(ModelConstants.JCR_NAME, property);
126 
127             // perform custom chaining of dialogs
128             this.dialogView = formDialogPresenter.start(item, NEW_COMPONENT_DIALOG, subAppContext, new ComponentCreationCallback(item, formDialogPresenter));
129         } catch (RepositoryException e) {
130             throw new ActionExecutionException(e);
131         }
132     }
133 
134     private void openDialog(final JcrNodeAdapter item, String dialogId) {
135 
136         final FormDialogPresenter dialogPresenter = formDialogPresenterFactory.createFormDialogPresenter(dialogId);
137         dialogPresenter.start(item, dialogId, subAppContext, new EditorCallback() {
138 
139             @Override
140             public void onSuccess(String actionName) {
141                 eventBus.fireEvent(new ContentChangedEvent(item.getItemId()));
142                 dialogPresenter.closeDialog();
143             }
144 
145             @Override
146             public void onCancel() {
147                 dialogPresenter.closeDialog();
148             }
149         });
150     }
151 
152     private class ComponentCreationCallback implements EditorCallback {
153 
154         private final JcrNodeAdapter item;
155         private final FormDialogPresenter formDialogPresenter;
156 
157         public ComponentCreationCallback(JcrNodeAdapter item, FormDialogPresenter formDialogPresenter) {
158             this.item = item;
159             this.formDialogPresenter = formDialogPresenter;
160         }
161 
162         @Override
163         public void onSuccess(String actionName) {
164             String templateId = String.valueOf(item.getItemProperty(NodeTypes.Renderable.TEMPLATE).getValue());
165             try {
166                 TemplateDefinition templateDef = templateDefinitionRegistry.getTemplateDefinition(templateId);
167                 String dialogId = templateDef.getDialog();
168 
169                 if (StringUtils.isNotEmpty(dialogId)) {
170                     openDialog(item, dialogId);
171                 } else {
172                     // if there is no dialog defined for the component, persist the node as is and reload.
173                     try {
174                         final Node node = item.applyChanges();
175                         node.getSession().save();
176 
177                     } catch (RepositoryException e) {
178                         log.error("Exception caught: {}", e.getMessage(), e);
179                     }
180 
181                     eventBus.fireEvent(new ContentChangedEvent(item.getItemId()));
182                 }
183             } catch (RegistrationException e) {
184                 log.error("Exception caught: {}", e.getMessage(), e);
185             } finally {
186                 dialogView.close();
187             }
188         }
189 
190         @Override
191         public void onCancel() {
192             formDialogPresenter.closeDialog();
193         }
194     }
195 }