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.jcr.util.NodeTypes;
40  import info.magnolia.objectfactory.Components;
41  import info.magnolia.ui.api.action.AbstractAction;
42  import info.magnolia.ui.api.action.ActionExecutionException;
43  import info.magnolia.ui.api.app.SubAppEventBus;
44  import info.magnolia.ui.api.context.UiContext;
45  import info.magnolia.ui.api.event.ContentChangedEvent;
46  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
47  import info.magnolia.ui.dialog.formdialog.FormDialogPresenterFactory;
48  import info.magnolia.ui.form.EditorCallback;
49  import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
50  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
51  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
52  
53  import javax.inject.Inject;
54  import javax.inject.Named;
55  import javax.jcr.Node;
56  import javax.jcr.RepositoryException;
57  import javax.jcr.Session;
58  
59  import org.apache.commons.lang3.StringUtils;
60  
61  /**
62   * Creates an area which was optional and not yet existing.
63   *
64   * @see CreateAreaAction
65   */
66  public class CreateAreaAction extends AbstractAction<CreateAreaActionDefinition> {
67  
68      private final AreaElement area;
69      private final EventBus eventBus;
70      private final FormDialogPresenterFactory formDialogPresenterFactory;
71      private final UiContext uiContext;
72      private final Context ctx;
73  
74      @Inject
75      public CreateAreaAction(CreateAreaActionDefinition definition, AreaElement area, @Named(SubAppEventBus.NAME) EventBus eventBus,
76                              FormDialogPresenterFactory formDialogPresenterFactory, UiContext uiContext, Context ctx) {
77          super(definition);
78          this.area = area;
79          this.eventBus = eventBus;
80          this.formDialogPresenterFactory = formDialogPresenterFactory;
81          this.uiContext = uiContext;
82          this.ctx = ctx;
83      }
84  
85      /**
86       * @deprecated since 5.4.2, use {@link CreateAreaAction#CreateAreaAction(CreateAreaActionDefinition, AreaElement, EventBus, FormDialogPresenterFactory, UiContext, Context)}
87       */
88      @Deprecated
89      public CreateAreaAction(CreateAreaActionDefinition definition, AreaElement area, @Named(SubAppEventBus.NAME) EventBus eventBus) {
90          this(definition, area, eventBus, Components.getComponent(FormDialogPresenterFactory.class), Components.getComponent(UiContext.class), MgnlContext.getInstance());
91      }
92  
93      @Override
94      public void execute() throws ActionExecutionException {
95          String workspace = area.getWorkspace();
96          String path = area.getPath();
97          String parent = StringUtils.substringBeforeLast(path, "/");
98          String areaName = StringUtils.substringAfterLast(area.getPath(), "/");
99  
100         try {
101             Session session = ctx.getJCRSession(workspace);
102             Node parentNode = session.getNode(parent);
103 
104             final JcrNodeAdapter item = new JcrNewNodeAdapter(parentNode, NodeTypes.Area.NAME, areaName);
105 
106             if (StringUtils.isBlank(area.getDialog())) {
107                 Node area = item.applyChanges();
108                 area.getSession().save();
109                 eventBus.fireEvent(new ContentChangedEvent(item.getItemId()));
110             } else {
111                 final FormDialogPresenter formDialogPresenter = formDialogPresenterFactory.createFormDialogPresenter(area.getDialog());
112                 formDialogPresenter.start(item, area.getDialog(), uiContext, new EditorCallback() {
113 
114                     @Override
115                     public void onSuccess(String actionName) {
116                         formDialogPresenter.closeDialog();
117                         eventBus.fireEvent(new ContentChangedEvent(item.getItemId()));
118                     }
119 
120                     @Override
121                     public void onCancel() {
122                         formDialogPresenter.closeDialog();
123                     }
124                 });
125             }
126         } catch (RepositoryException e) {
127             throw new ActionExecutionException(e);
128         }
129     }
130 }