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.core.Content;
37  import info.magnolia.cms.gui.control.Button;
38  import info.magnolia.cms.gui.control.ControlImpl;
39  import info.magnolia.cms.gui.dialog.Dialog;
40  import info.magnolia.cms.gui.dialog.DialogBox;
41  import info.magnolia.cms.gui.dialog.DialogButtonSet;
42  import info.magnolia.cms.gui.dialog.DialogFactory;
43  import info.magnolia.cms.gui.dialog.DialogHidden;
44  import info.magnolia.cms.gui.dialog.DialogStatic;
45  import info.magnolia.cms.gui.dialog.DialogTab;
46  import info.magnolia.cms.i18n.Messages;
47  import info.magnolia.cms.i18n.MessagesManager;
48  import info.magnolia.module.admininterface.DialogMVCHandler;
49  import info.magnolia.objectfactory.Components;
50  import info.magnolia.registry.RegistrationException;
51  import info.magnolia.rendering.template.TemplateDefinition;
52  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
53  
54  import java.io.IOException;
55  import java.net.URLEncoder;
56  import java.util.Iterator;
57  
58  import javax.jcr.RepositoryException;
59  import javax.servlet.http.HttpServletRequest;
60  import javax.servlet.http.HttpServletResponse;
61  
62  import org.apache.commons.lang.StringUtils;
63  import org.slf4j.Logger;
64  import org.slf4j.LoggerFactory;
65  
66  
67  /**
68   * If there are more than one paragraph available you have first to choose one.
69   * @author philipp
70   */
71  public class ParagraphSelectDialog extends DialogMVCHandler {
72      private static final Logger log = LoggerFactory.getLogger(ParagraphSelectDialog.class);
73  
74      private static final String EDIT_PARAGRAPH_DIALOGNAME = "editParagraph";
75      public static final String EDITPARAGRAPH_DIALOG_URL = ".magnolia/dialogs/" + EDIT_PARAGRAPH_DIALOGNAME + ".html";
76  
77      private final String paragraph;
78  
79      public ParagraphSelectDialog(String name, HttpServletRequest request, HttpServletResponse response) {
80          super(name, request, response);
81          paragraph = params.getParameter("mgnlParagraph"); //$NON-NLS-1$
82      }
83  
84      @Override
85      protected Dialog createDialog(Content configNode, Content websiteNode) throws RepositoryException {
86          Dialog dialog = DialogFactory.getDialogInstance(request, response, null, null);
87  
88          dialog.setConfig("paragraph", paragraph); //$NON-NLS-1$
89  
90          dialog.setLabel(msgs.get("dialog.paragraph.createNew")); //$NON-NLS-1$
91          dialog.setConfig("saveLabel", msgs.get("buttons.ok")); //$NON-NLS-1$ //$NON-NLS-2$
92  
93          DialogHidden h1 = DialogFactory.getDialogHiddenInstance(request, response, null, null);
94          h1.setName("mgnlParagraphSelected"); //$NON-NLS-1$
95          h1.setValue("true"); //$NON-NLS-1$
96          h1.setConfig("saveInfo", "false"); //$NON-NLS-1$ //$NON-NLS-2$
97          dialog.addSub(h1);
98  
99          DialogTab tab = dialog.addTab();
100 
101         DialogStatic c0 = DialogFactory.getDialogStaticInstance(request, response, null, null);
102 
103         c0.setConfig("line", "false"); //$NON-NLS-1$ //$NON-NLS-2$
104         c0.setValue(msgs.get("dialog.paragraph.select")); //$NON-NLS-1$
105         c0.setBoxType((DialogBox.BOXTYPE_1COL));
106         tab.addSub(c0);
107 
108         DialogButtonSet c1 = DialogFactory.getDialogButtonSetInstance(request, response, null, null);
109         c1.setName("mgnlParagraph"); //$NON-NLS-1$
110         c1.setButtonType(ControlImpl.BUTTONTYPE_RADIO);
111         c1.setBoxType(DialogBox.BOXTYPE_1COL);
112         c1.setConfig("saveInfo", "false"); //$NON-NLS-1$ //$NON-NLS-2$
113         c1.setConfig("width", "100%"); //$NON-NLS-1$ //$NON-NLS-2$
114 
115         String[] pars = paragraph.split(","); //$NON-NLS-1$
116         for (int i = 0; i < pars.length; i++) {
117             try {
118                 addParagraph(c1, pars[i]);
119 
120             } catch (Exception e) {
121                 // paragraph definition does not exist
122                 log.warn("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
123             }
124         }
125 
126         tab.addSub(c1);
127 
128         return dialog;
129     }
130 
131     protected void addParagraph(DialogButtonSet radioButtonSet, String paragraphName) {
132         TemplateDefinition paragraphInfo;
133         try {
134             paragraphInfo = Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(paragraphName);
135         } catch (RegistrationException e) {
136             // TODO dlipp: apply consistent ExceptionHandling.
137             throw new RuntimeException(e);
138         }
139 
140         // prevent NPEs
141         if (paragraphInfo == null) {
142             log.error("Unable to load paragraph {}", paragraph);
143             return;
144         }
145 
146         final Messages msgs = MessagesManager.getMessages(paragraphInfo.getI18nBasename());
147 
148         final StringBuffer label = new StringBuffer();
149         label.append("<strong>" //$NON-NLS-1$
150             + msgs.getWithDefault(paragraphInfo.getTitle(), paragraphInfo.getTitle())
151             + "</strong><br />"); //$NON-NLS-1$
152 
153         final String description = paragraphInfo.getDescription();
154         if (StringUtils.isNotEmpty(description)) {
155             label.append(msgs.getWithDefault(description, description));
156         }
157         label.append("<br /><br />"); //$NON-NLS-1$
158 
159         final Button button = new Button(radioButtonSet.getName(), paragraphInfo.getId());
160         button.setLabel(label.toString());
161         button.setOnclick("document.getElementById('mgnlFormMain').submit();"); //$NON-NLS-1$
162         radioButtonSet.addOption(button);
163     }
164 
165     @Override
166     public Content getStorageNode() {
167         return null;
168     }
169 
170     @Override
171     public Content getConfigNode() {
172         return null;
173     }
174 
175     @Override
176     public String save() {
177         try {
178             // copy all parameters except mgnlDialog (which we switch to "editParagraph")
179             StringBuffer query = new StringBuffer();
180             for (Iterator iter = form.getParameters().keySet().iterator(); iter.hasNext();) {
181                 String key = (String) iter.next();
182                 if (!key.equals("mgnlDialog")) { //$NON-NLS-1$
183                     if (query.length() != 0) {
184                         query.append("&"); //$NON-NLS-1$
185                     }
186                     query.append(key);
187                     query.append("="); //$NON-NLS-1$
188                     query.append(URLEncoder.encode(form.getParameter(key),"UTF-8"));
189                 }
190 
191             }
192             response.sendRedirect(request.getContextPath() + "/" + EDITPARAGRAPH_DIALOG_URL + "?" + query); //$NON-NLS-1$ //$NON-NLS-2$
193         }
194         catch (IOException e) {
195             log.error("can't redirect to the paragraph-dialog", e); //$NON-NLS-1$
196         }
197         return VIEW_NOTHING;
198     }
199 
200 }