View Javadoc

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