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