001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/utils/mvc/impl/servlet/FormControllerImpl.java $
003: * $Id: FormControllerImpl.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.utils.mvc.impl.servlet;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import org.sakaiproject.component.cover.ComponentManager;
032: import org.sakaiproject.metaobj.utils.mvc.impl.ControllerFilterManager;
033: import org.sakaiproject.metaobj.utils.mvc.impl.HttpServletHelper;
034: import org.sakaiproject.metaobj.utils.mvc.intf.CancelableController;
035: import org.sakaiproject.metaobj.utils.mvc.intf.Controller;
036: import org.sakaiproject.metaobj.utils.mvc.intf.CustomCommandController;
037: import org.sakaiproject.metaobj.utils.mvc.intf.FormController;
038: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
039: import org.sakaiproject.metaobj.utils.mvc.intf.TypedPropertyEditor;
040: import org.springframework.validation.BindException;
041: import org.springframework.validation.Errors;
042: import org.springframework.web.bind.ServletRequestDataBinder;
043: import org.springframework.web.servlet.ModelAndView;
044: import org.springframework.web.servlet.mvc.SimpleFormController;
045:
046: /**
047: * Created by IntelliJ IDEA.
048: * User: John Ellis
049: * Date: Mar 25, 2004
050: * Time: 3:45:21 PM
051: * To change this template use File | Settings | File Templates.
052: */
053: public class FormControllerImpl extends SimpleFormController {
054:
055: private Controller controller = null;
056: private Map screenMappings = null;
057: private ServletRequestDataBinder servletRequestMapDataBinder = null;
058: private String homeName;
059: private List customTypedEditors = new ArrayList();
060: private String formMethod;
061: private String[] requiredFields = null;
062: private Collection filters;
063:
064: protected ModelAndView onSubmit(HttpServletRequest request,
065: HttpServletResponse response, Object command,
066: BindException errors) throws Exception {
067:
068: Map requestMap = HttpServletHelper.getInstance()
069: .createRequestMap(request);
070: Map session = HttpServletHelper.getInstance().createSessionMap(
071: request);
072: Map application = HttpServletHelper.getInstance()
073: .createApplicationMap(request);
074:
075: ModelAndView returnedMv;
076:
077: if (controller instanceof CancelableController
078: && ((CancelableController) controller)
079: .isCancel(requestMap)) {
080: returnedMv = ((CancelableController) controller)
081: .processCancel(requestMap, session, application,
082: command, errors);
083: } else {
084: returnedMv = controller.handleRequest(command, requestMap,
085: session, application, errors);
086: }
087:
088: if (errors.hasErrors()) {
089: logger.debug("Form submission errors: "
090: + errors.getErrorCount());
091: HttpServletHelper.getInstance().reloadApplicationMap(
092: request, application);
093: HttpServletHelper.getInstance().reloadSessionMap(request,
094: session);
095: HttpServletHelper.getInstance().reloadRequestMap(request,
096: requestMap);
097: return showForm(request, response, errors);
098: }
099:
100: if (returnedMv.getViewName() != null) {
101: // should get from mappings
102: String mappedView = (String) screenMappings.get(returnedMv
103: .getViewName());
104:
105: if (mappedView == null) {
106: mappedView = returnedMv.getViewName();
107: }
108:
109: //getControllerFilterManager().processFilters(requestMap, session, application, returnedMv, mappedView);
110:
111: returnedMv = new ModelAndView(mappedView, returnedMv
112: .getModel());
113: }
114:
115: HttpServletHelper.getInstance().reloadApplicationMap(request,
116: application);
117: HttpServletHelper.getInstance().reloadSessionMap(request,
118: session);
119: HttpServletHelper.getInstance().reloadRequestMap(request,
120: requestMap);
121:
122: return returnedMv;
123: }
124:
125: protected Map referenceData(HttpServletRequest request,
126: Object command, Errors errors) {
127: if (getController() instanceof FormController) {
128: Map requestMap = HttpServletHelper.getInstance()
129: .createRequestMap(request);
130: Map referenceData = ((FormController) getController())
131: .referenceData(requestMap, command, errors);
132: HttpServletHelper.getInstance().reloadRequestMap(request,
133: requestMap);
134: return referenceData;
135: }
136: return null;
137: }
138:
139: protected boolean isFormSubmission(HttpServletRequest request) {
140: if (getFormMethod() != null
141: && getFormMethod().equalsIgnoreCase("get")
142: && request.getMethod().equalsIgnoreCase("get")) {
143: return true;
144: }
145: if (getFormMethod() != null
146: && getFormMethod().equalsIgnoreCase("post")
147: && request.getMethod().equalsIgnoreCase("post")) {
148: return true;
149: }
150: return super .isFormSubmission(request);
151: }
152:
153: protected Object formBackingObject(HttpServletRequest request)
154: throws Exception {
155: Map requestMap = HttpServletHelper.getInstance()
156: .createRequestMap(request);
157: Map session = HttpServletHelper.getInstance().createSessionMap(
158: request);
159: Map application = HttpServletHelper.getInstance()
160: .createApplicationMap(request);
161:
162: Object lightObject = null;
163:
164: if (controller instanceof CustomCommandController) {
165: lightObject = ((CustomCommandController) controller)
166: .formBackingObject(requestMap, session, application);
167: } else {
168: lightObject = super .formBackingObject(request);
169: }
170:
171: Object returned = lightObject;
172:
173: if (controller instanceof LoadObjectController) {
174: // need to bind variables to fill in lightweight object
175: // then pass object to real control to fill in
176: // this will get the info from the backing store
177: ServletRequestDataBinder binder = createBinder(request,
178: lightObject);
179: binder.bind(request);
180:
181: returned = ((LoadObjectController) controller)
182: .fillBackingObject(lightObject, requestMap,
183: session, application);
184: }
185:
186: /*
187: if (controller instanceof ContextAwareController){
188: ((ContextAwareController) controller).addContexts(getHelpManager().getActiveContexts(session), requestMap, getFormView());
189: } else {
190: getHelpManager().addContexts(session, getFormView());
191: }
192: */
193: //getControllerFilterManager().processFilters(requestMap, session, application, null, getFormView());
194: HttpServletHelper.getInstance().reloadApplicationMap(request,
195: application);
196: HttpServletHelper.getInstance().reloadSessionMap(request,
197: session);
198: HttpServletHelper.getInstance().reloadRequestMap(request,
199: requestMap);
200: return returned;
201: }
202:
203: protected ServletRequestDataBinder createBinder(
204: HttpServletRequest request, Object command)
205: throws Exception {
206: ServletRequestDataBinder binder = null;
207: binder = new ServletRequestBeanDataBinder(command,
208: getCommandName());
209: initBinder(request, binder);
210: return binder;
211: }
212:
213: /**
214: * Set up a custom property editor for the application's date format.
215: */
216: protected void initBinder(HttpServletRequest request,
217: ServletRequestDataBinder binder) {
218: for (Iterator i = getCustomTypedEditors().iterator(); i
219: .hasNext();) {
220: TypedPropertyEditor editor = (TypedPropertyEditor) i.next();
221: binder.registerCustomEditor(editor.getType(), editor);
222: }
223:
224: if (getRequiredFields() != null) {
225: binder.setRequiredFields(getRequiredFields());
226: }
227: }
228:
229: public Controller getController() {
230: return controller;
231: }
232:
233: public void setController(Controller controller) {
234: this .controller = controller;
235: }
236:
237: public Map getScreenMappings() {
238: return screenMappings;
239: }
240:
241: public void setScreenMappings(Map screenMappings) {
242: this .screenMappings = screenMappings;
243: }
244:
245: public ServletRequestDataBinder getServletRequestMapDataBinder() {
246: return servletRequestMapDataBinder;
247: }
248:
249: public void setServletRequestMapDataBinder(
250: ServletRequestDataBinder servletRequestMapDataBinder) {
251: this .servletRequestMapDataBinder = servletRequestMapDataBinder;
252: }
253:
254: public String getHomeName() {
255: return homeName;
256: }
257:
258: public void setHomeName(String homeName) {
259: this .homeName = homeName;
260: }
261:
262: public List getCustomTypedEditors() {
263: return customTypedEditors;
264: }
265:
266: public void setCustomTypedEditors(List customTypedEditors) {
267: this .customTypedEditors = customTypedEditors;
268: }
269:
270: public String getFormMethod() {
271: return formMethod;
272: }
273:
274: public void setFormMethod(String formMethod) {
275: this .formMethod = formMethod;
276: }
277:
278: public String[] getRequiredFields() {
279: return requiredFields;
280: }
281:
282: public void setRequiredFields(String[] requiredFields) {
283: this .requiredFields = requiredFields;
284: }
285:
286: protected ControllerFilterManager getControllerFilterManager() {
287: return (ControllerFilterManager) ComponentManager.getInstance()
288: .get("controllerFilterManager");
289: }
290: }
|