View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.module.admininterface.dialogs;
35  
36  import info.magnolia.cms.beans.runtime.MultipartForm;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.ItemType;
39  import info.magnolia.module.admininterface.DialogHandlerManager;
40  import info.magnolia.module.admininterface.DialogMVCHandler;
41  import info.magnolia.module.admininterface.SaveHandler;
42  import info.magnolia.objectfactory.Components;
43  import info.magnolia.registry.RegistrationException;
44  import info.magnolia.rendering.template.TemplateDefinition;
45  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
46  
47  import java.io.IOException;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  import org.apache.commons.lang.StringUtils;
53  
54  /**
55   * This dialog hander delegates to a dialog handler representing the dialog assigned to the paragraph we edit (or create).
56   *
57   * @author philipp
58   */
59  public class ParagraphEditDialog extends ConfiguredDialog {
60  
61      private DialogMVCHandler dialogHandler;
62  
63      public ParagraphEditDialog(String name, HttpServletRequest request, HttpServletResponse response, Content configNode) {
64          super(name, request, response, configNode);
65  
66          // build the dialog handler we will delegate to
67          String paragraphName = params.getParameter("mgnlParagraph");
68  
69          final String dialogName = getDialogUsedByTemplate(paragraphName);
70  
71          if (StringUtils.isEmpty(dialogName)) {
72              dialogHandler = new NoDialogMVCHandler(request, response);
73          } else {
74  
75              dialogHandler = DialogHandlerManager.getInstance().getDialogHandler(dialogName, request, response);
76  
77              // needed for the creation of new paragraphs
78              dialogHandler.getDialog().setConfig("paragraph", paragraphName);
79              dialogHandler.getDialog().setConfig("collectionNodeCreationItemType", ItemType.AREA.getSystemName());
80              dialogHandler.getDialog().setConfig("creationItemType", ItemType.COMPONENT.getSystemName());
81          }
82      }
83  
84      private String getDialogUsedByTemplate(String templateId) {
85          if (StringUtils.isEmpty(templateId)) {
86              throw new IllegalStateException("No paragraph selected.");
87          }
88          TemplateDefinition templateDefinition;
89          try {
90              templateDefinition = Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(templateId);
91          } catch (RegistrationException e) {
92              throw new IllegalStateException(e);
93          }
94          if (templateDefinition == null) {
95              throw new IllegalStateException("No template registered with name " + templateId);
96          }
97          return templateDefinition.getDialog();
98      }
99  
100     // methods delegating to the paragraph's dialog handler
101     // this are the only methods called by the dialog mvc servlet
102 
103     @Override
104     public String getCommand() {
105         return dialogHandler.getCommand();
106     }
107 
108     @Override
109     public String execute(String command) {
110         return dialogHandler.execute(command);
111     }
112 
113     @Override
114     public void renderHtml(String view) throws IOException {
115         dialogHandler.renderHtml(view);
116     }
117 
118     public static class NoDialogMVCHandler extends DialogMVCHandler {
119         public NoDialogMVCHandler(HttpServletRequest request, HttpServletResponse response) {
120             // we don't really need this call to super, we'll fake the form !
121             super("auto-save", request, response);
122             // at this stage, this.params is essentially a wrapper around the request parameters, and this.form is null.
123         }
124 
125         @Override
126         public String getCommand() {
127             // bypass COMMAND_SHOW_DIALOG
128             return DialogMVCHandler.COMMAND_SAVE;
129         }
130 
131         @Override
132         public String save() {
133             // completely bypass the regular save() method - we have no validation and dialog, so we fake everything
134             final SaveHandler saveHandler = Components.getComponentProvider().newInstance(SaveHandler.class);
135             final MultipartForm dummyForm = new MultipartForm();
136             dummyForm.addparameterValues("mgnlSaveInfo", new String[0]);
137             saveHandler.init(dummyForm);
138 
139             // copied/adapted from info.magnolia.module.admininterface.DialogMVCHandler#DialogMVCHandler
140             saveHandler.setPath(path); // protected variable init'd in constructor
141             saveHandler.setNodeCollectionName(params.getParameter("mgnlNodeCollection")); // private, so we do this
142             saveHandler.setNodeName(nodeName); // protected variable init'd in constructor
143             saveHandler.setParagraph(params.getParameter("mgnlParagraph"));
144             saveHandler.setRepository(repository); // protected variable init'd in constructor
145             saveHandler.setCollectionNodeCreationItemType(ItemType.AREA);
146             saveHandler.setCreationItemType(ItemType.COMPONENT);
147 
148 
149             boolean result = saveHandler.save();
150             return result ? DialogMVCHandler.VIEW_CLOSE_WINDOW : DialogMVCHandler.VIEW_ERROR;
151         }
152 
153     }
154 }