View Javadoc
1   /**
2    * This file Copyright (c) 2013-2016 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.config.registry.DefinitionProvider;
37  import info.magnolia.config.registry.Registry;
38  import info.magnolia.context.Context;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.event.EventBus;
41  import info.magnolia.i18nsystem.SimpleTranslator;
42  import info.magnolia.jcr.util.NodeTypes;
43  import info.magnolia.jcr.util.NodeUtil;
44  import info.magnolia.objectfactory.ComponentProvider;
45  import info.magnolia.rendering.template.TemplateDefinition;
46  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
47  import info.magnolia.ui.api.action.AbstractAction;
48  import info.magnolia.ui.api.action.ActionExecutionException;
49  import info.magnolia.ui.api.app.SubAppContext;
50  import info.magnolia.ui.api.app.SubAppEventBus;
51  import info.magnolia.ui.api.event.ContentChangedEvent;
52  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
53  import info.magnolia.ui.dialog.formdialog.FormDialogPresenterFactory;
54  import info.magnolia.ui.form.EditorCallback;
55  import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
56  import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
57  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
58  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
59  import info.magnolia.ui.vaadin.integration.jcr.ModelConstants;
60  
61  import javax.inject.Inject;
62  import javax.inject.Named;
63  import javax.jcr.Node;
64  import javax.jcr.RepositoryException;
65  import javax.jcr.Session;
66  
67  import org.apache.commons.lang3.StringUtils;
68  import org.slf4j.Logger;
69  import org.slf4j.LoggerFactory;
70  
71  import com.vaadin.data.Property;
72  
73  /**
74   * Adds a component underneath the area passed in {@link AreaElement}.
75   * Gets a list of available components for this area and creates a select field.
76   */
77  public class CreateComponentAction extends AbstractAction<CreateComponentActionDefinition> {
78  
79      private static final Logger log = LoggerFactory.getLogger(CreateComponentAction.class);
80  
81      private static final String NEW_COMPONENT_DIALOG = "pages:newComponent";
82      private static final String NEW_COMPONENT_POSITION = "position";
83      /**
84       * Represents position of a component within the area.
85       */
86      private static final String COMPONENT_POSITION_TOP = "top";
87      private static final String COMPONENT_POSITION_BOTTOM = "bottom";
88  
89      private final AreaElement area;
90      private final EventBus eventBus;
91      private final TemplateDefinitionRegistry templateDefinitionRegistry;
92      private final SubAppContext subAppContext;
93      private final FormDialogPresenterFactory formDialogPresenterFactory;
94      private final Context ctx;
95  
96      private String newComponentPosition = COMPONENT_POSITION_BOTTOM;
97  
98      @Inject
99      public CreateComponentAction(CreateComponentActionDefinition definition, AreaElement area, @Named(SubAppEventBus.NAME) EventBus eventBus, TemplateDefinitionRegistry templateDefinitionRegistry,
100                                  SubAppContext subAppContext, FormDialogPresenterFactory formDialogPresenterFactory, Context ctx) {
101         super(definition);
102         this.area = area;
103         this.eventBus = eventBus;
104         this.templateDefinitionRegistry = templateDefinitionRegistry;
105         this.subAppContext = subAppContext;
106         this.formDialogPresenterFactory = formDialogPresenterFactory;
107         this.ctx = ctx;
108     }
109 
110     /**
111      * @deprecated since 5.4.2, use {@link #CreateComponentAction(CreateComponentActionDefinition, AreaElement, EventBus, TemplateDefinitionRegistry, SubAppContext, FormDialogPresenterFactory, Context)}
112      */
113     @Deprecated
114     public CreateComponentAction(CreateComponentActionDefinition definition, AreaElement area, @Named(SubAppEventBus.NAME) EventBus eventBus, TemplateDefinitionRegistry templateDefinitionRegistry,
115                                  SubAppContext subAppContext, ComponentProvider componentProvider, FormDialogPresenterFactory formDialogPresenterFactory) {
116         this(definition, area, eventBus, templateDefinitionRegistry, subAppContext, formDialogPresenterFactory, MgnlContext.getInstance());
117     }
118 
119     /**
120      * @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}.
121      *             Use other constructor {@link #CreateComponentAction(CreateComponentActionDefinition, AreaElement, EventBus, TemplateDefinitionRegistry, SubAppContext, ComponentProvider, FormDialogPresenterFactory)} instead.
122      */
123     @Deprecated
124     public CreateComponentAction(CreateComponentActionDefinition definition, AreaElement area, @Named(SubAppEventBus.NAME) EventBus eventBus, TemplateDefinitionRegistry templateDefinitionRegistry,
125                                  SubAppContext subAppContext, ComponentProvider componentProvider, FormDialogPresenterFactory formDialogPresenterFactory, SimpleTranslator i18n) {
126         this(definition, area, eventBus, templateDefinitionRegistry, subAppContext, formDialogPresenterFactory, MgnlContext.getInstance());
127     }
128 
129     @Override
130     public void execute() throws ActionExecutionException {
131         String workspace = area.getWorkspace();
132         String path = area.getPath();
133 
134         try {
135             Session session = ctx.getJCRSession(workspace);
136             if (path == null || !session.itemExists(path)) {
137                 path = "/";
138             }
139 
140             if (StringUtils.isNotBlank(area.getAvailableComponents())) {
141                 Node areaNode = session.getNode(path);
142 
143                 // set the nodeName as item property, otherwise JcrNewNodeAdapter#applyChanges doesn't guarantee uniqueness of the node name
144                 final JcrNodeAdapter item = new JcrNewNodeAdapter(areaNode, NodeTypes.Component.NAME) {
145                     @Override
146                     public Node applyChanges() throws RepositoryException {
147                         Node node = super.applyChanges();
148                         // reorder upon applyChanges, as this will be done right before session save
149                         if (COMPONENT_POSITION_TOP.equals(newComponentPosition)) {
150                             NodeUtil.orderFirst(node);
151                         }
152                         return node;
153                     }
154                 };
155                 item.addItemProperty(ModelConstants.JCR_NAME, new DefaultProperty<>(String.class, "0"));
156 
157                 // perform custom chaining of dialogs
158                 final FormDialogPresenter formDialogPresenter = formDialogPresenterFactory.createFormDialogPresenter(NEW_COMPONENT_DIALOG);
159                 final EditorCallback callback = new TemplateSelected(item, formDialogPresenter);
160                 // open dialog if there is more then one component, otherwise skip component chooser
161 
162                 if (area.getAvailableComponents().split(",").length > 1) {
163                     formDialogPresenter.start(item, NEW_COMPONENT_DIALOG, subAppContext, callback);
164                 } else {
165                     item.addItemProperty(NodeTypes.Renderable.TEMPLATE, new DefaultProperty<>(String.class, area.getAvailableComponents()));
166                     callback.onSuccess(getDefinition().getName());
167                 }
168             } else {
169                 log.warn("No available components defined for {} area.", area.getPath());
170             }
171         } catch (RepositoryException e) {
172             throw new ActionExecutionException(e);
173         }
174     }
175 
176     private class TemplateSelected implements EditorCallback {
177 
178         private final JcrNodeAdapter item;
179         private final FormDialogPresenter formDialogPresenter;
180 
181         public TemplateSelected(JcrNodeAdapter item, FormDialogPresenter formDialogPresenter) {
182             this.item = item;
183             this.formDialogPresenter = formDialogPresenter;
184         }
185 
186         @Override
187         public void onSuccess(String actionName) {
188             // template
189             String templateId = String.valueOf(item.getItemProperty(NodeTypes.Renderable.TEMPLATE).getValue());
190             TemplateDefinition templateDefinition = null;
191             try {
192                 DefinitionProvider<TemplateDefinition> templateProvider = templateDefinitionRegistry.getProvider(templateId);
193                 if (templateProvider.isValid()) {
194                     templateDefinition = templateProvider.get();
195                 }
196             } catch (Registry.NoSuchDefinitionException e) {
197                 log.error("Could not find template definition provider for id {}", templateId, e);
198             }
199 
200             // position
201             Property positionProperty = item.getItemProperty(NEW_COMPONENT_POSITION);
202             newComponentPosition = positionProperty != null ? (String) positionProperty.getValue() : COMPONENT_POSITION_BOTTOM;
203 
204             // open dialog
205             if (templateDefinition != null) {
206                 String dialogId = templateDefinition.getDialog();
207                 if (StringUtils.isNotEmpty(dialogId)) {
208                     final FormDialogPresenter nextDialogPresenter = formDialogPresenterFactory.createFormDialogPresenter(dialogId);
209                     nextDialogPresenter.start(item, dialogId, subAppContext, new ComponentSaved(item, nextDialogPresenter));
210                 } else {
211                     // if there is no dialog defined for the component, persist the node as is and reload.
212                     try {
213                         final Node node = item.applyChanges();
214                         node.getSession().save();
215                     } catch (RepositoryException e) {
216                         log.error("Exception caught: {}", e.getMessage(), e);
217                     }
218                     eventBus.fireEvent(new ContentChangedEvent(item.getItemId()));
219                 }
220             }
221 
222             if (formDialogPresenter != null) {
223                 formDialogPresenter.closeDialog();
224             }
225         }
226 
227         @Override
228         public void onCancel() {
229             formDialogPresenter.closeDialog();
230         }
231     }
232 
233     private class ComponentSaved implements EditorCallback {
234 
235         private final JcrNodeAdapter item;
236         private final FormDialogPresenter dialogPresenter;
237 
238         public ComponentSaved(JcrNodeAdapter item, FormDialogPresenter dialogPresenter) {
239             this.item = item;
240             this.dialogPresenter = dialogPresenter;
241         }
242 
243         @Override
244         public void onSuccess(String actionName) {
245             eventBus.fireEvent(new ContentChangedEvent(item.getItemId()));
246             dialogPresenter.closeDialog();
247         }
248 
249         @Override
250         public void onCancel() {
251             dialogPresenter.closeDialog();
252         }
253     }
254 }