KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > editor > AbstractComponentFormController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.editor;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.riotfamily.common.util.ResourceUtils;
33 import org.riotfamily.common.web.transaction.TransactionalController;
34 import org.riotfamily.forms.Form;
35 import org.riotfamily.forms.controller.ButtonFactory;
36 import org.riotfamily.forms.controller.FormSubmissionHandler;
37 import org.riotfamily.forms.factory.FormRepository;
38 import org.riotfamily.forms.factory.RepositoryFormController;
39 import org.springframework.web.servlet.ModelAndView;
40
41 /**
42  * Controller that displays a form to edit the properties of a ComponentVersion.
43  *
44  * @author Felix Gnass [fgnass at neteye dot de]
45  * @since 6.5
46  */

47 public abstract class AbstractComponentFormController
48         extends RepositoryFormController
49         implements FormSubmissionHandler, TransactionalController {
50
51     private static final String JavaDoc SESSION_ATTRIBUTE = "componentForm";
52
53     private String JavaDoc viewName = ResourceUtils.getPath(
54             AbstractComponentFormController.class, "ComponentFormView.ftl");
55
56     private String JavaDoc successViewName = ResourceUtils.getPath(
57             AbstractComponentFormController.class, "ComponentFormSuccessView.ftl");
58
59     private String JavaDoc formIdAttribute = "formId";
60
61     public AbstractComponentFormController(FormRepository formRepository) {
62         super(formRepository);
63         ButtonFactory buttonFactory = new ButtonFactory(this);
64         buttonFactory.setLabelKey("label.form.button.save");
65         buttonFactory.setCssClass("button button-save");
66         addButton(buttonFactory);
67     }
68
69     public void setViewName(String JavaDoc viewName) {
70         this.viewName = viewName;
71     }
72
73     public void setSuccessViewName(String JavaDoc successViewName) {
74         this.successViewName = successViewName;
75     }
76
77     protected String JavaDoc getFormId(HttpServletRequest JavaDoc request) {
78         return (String JavaDoc) request.getAttribute(formIdAttribute);
79     }
80
81     protected String JavaDoc getSessionAttribute(HttpServletRequest JavaDoc request) {
82         return SESSION_ATTRIBUTE;
83     }
84
85     protected ModelAndView showForm(final Form form,
86             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
87             throws Exception JavaDoc {
88
89         StringWriter JavaDoc sw = new StringWriter JavaDoc();
90         renderForm(form, new PrintWriter JavaDoc(sw));
91         return new ModelAndView(viewName, "form", sw.toString());
92     }
93
94     public ModelAndView handleFormSubmission(Form form,
95             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
96             throws Exception JavaDoc {
97
98         onSave(form.populateBackingObject(), request);
99         return new ModelAndView(successViewName);
100     }
101     
102     protected abstract void onSave(Object JavaDoc object, HttpServletRequest JavaDoc request);
103
104 }
Popular Tags