KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > form > ui > FormController


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.riot.form.ui;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.riotfamily.common.web.util.ServletUtils;
33 import org.riotfamily.forms.Form;
34 import org.riotfamily.forms.factory.FormRepository;
35 import org.riotfamily.riot.editor.ObjectEditorDefinition;
36 import org.riotfamily.riot.editor.EditorDefinitionUtils;
37 import org.riotfamily.riot.editor.EditorRepository;
38 import org.riotfamily.riot.editor.ListDefinition;
39 import org.riotfamily.riot.list.ui.ListService;
40 import org.riotfamily.riot.list.ui.ListSession;
41 import org.riotfamily.riot.security.AccessController;
42 import org.springframework.transaction.PlatformTransactionManager;
43 import org.springframework.web.servlet.ModelAndView;
44 import org.springframework.web.servlet.view.RedirectView;
45
46 /**
47  *
48  */

49 public class FormController extends BaseFormController {
50
51     private static final String JavaDoc PARAM_SAVED = "saved";
52
53     private ListService listService;
54
55     public FormController(EditorRepository editorRepository,
56             FormRepository formRepository,
57             PlatformTransactionManager transactionManager,
58             ListService listService) {
59
60         super(editorRepository, formRepository, transactionManager);
61         this.listService = listService;
62     }
63
64     protected Map JavaDoc createModel(Form form, ObjectEditorDefinition editorDefinition,
65             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
66
67         Map JavaDoc model = super.createModel(form, editorDefinition, request, response);
68         model.put(PARAM_SAVED, Boolean.valueOf(request.getParameter(PARAM_SAVED) != null));
69
70         Object JavaDoc object = null;
71         if (!form.isNew()) {
72             object = form.getBackingObject();
73         }
74         
75         if (!AccessController.isGranted("view", object)
76                 || !AccessController.isGranted("use-editor", editorDefinition)) {
77             
78             try {
79                 response.sendError(HttpServletResponse.SC_FORBIDDEN);
80             }
81             catch (IOException JavaDoc e) {
82             }
83             return null;
84         }
85         
86
87         model.put("childLists", editorDefinition.getChildEditorReferences(object,
88                 form.getFormContext().getMessageResolver()));
89
90         ListDefinition parentListDef = EditorDefinitionUtils
91                 .getParentListDefinition(editorDefinition);
92
93         if (parentListDef != null) {
94             ListSession session = listService.getOrCreateListSession(
95                 parentListDef.getId(), FormUtils.getParentId(form),
96                 null, request);
97
98             model.put("listKey", session.getKey());
99         }
100
101         return model;
102     }
103
104     protected ModelAndView afterSave(Form form, ObjectEditorDefinition editorDefinition,
105             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
106
107         if (!editorDefinition.getChildEditorDefinitions().isEmpty()) {
108             return reloadForm(form, editorDefinition);
109         }
110         else {
111             return showParentList(form, editorDefinition);
112         }
113     }
114
115     protected ModelAndView afterUpdate(Form form, ObjectEditorDefinition editorDefinition,
116             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
117
118         return showParentList(form, editorDefinition);
119     }
120
121     protected ModelAndView showParentList(Form form,
122             ObjectEditorDefinition editorDefinition) {
123
124         String JavaDoc listUrl = editorDefinition.createEditorPath(
125                 form.getBackingObject(),
126                 form.getFormContext().getMessageResolver())
127                 .getParent().getEditorUrl();
128
129         return new ModelAndView(new RedirectView(listUrl, true));
130     }
131
132     protected ModelAndView reloadForm(Form form,
133             ObjectEditorDefinition editorDefinition) {
134
135         String JavaDoc formUrl = editorDefinition.createEditorPath(
136                 form.getBackingObject(),
137                 form.getFormContext().getMessageResolver())
138                 .getEditorUrl();
139
140         formUrl = ServletUtils.addParameter(formUrl, "saved", "true");
141         return new ModelAndView(new RedirectView(formUrl, true));
142     }
143     
144     public static String JavaDoc getUrl(String JavaDoc editorId, String JavaDoc objectId, String JavaDoc parentId) {
145         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
146         sb.append("/form/").append(editorId);
147         if (objectId != null) {
148             sb.append('/').append(objectId);
149         }
150         if (parentId != null) {
151             sb.append("?parentId=").append(parentId);
152         }
153         return sb.toString();
154     }
155 }
156
Popular Tags