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