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/ViewControllerImpl.java $
003: * $Id: ViewControllerImpl.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.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.sakaiproject.component.cover.ComponentManager;
031: import org.sakaiproject.metaobj.utils.mvc.impl.ControllerFilterManager;
032: import org.sakaiproject.metaobj.utils.mvc.impl.HttpServletHelper;
033: import org.sakaiproject.metaobj.utils.mvc.intf.Controller;
034: import org.sakaiproject.metaobj.utils.mvc.intf.CustomCommandController;
035: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
036: import org.sakaiproject.metaobj.utils.mvc.intf.TypedPropertyEditor;
037: import org.springframework.validation.BindException;
038: import org.springframework.web.bind.ServletRequestDataBinder;
039: import org.springframework.web.servlet.ModelAndView;
040: import org.springframework.web.servlet.mvc.AbstractCommandController;
041:
042: /**
043: * Created by IntelliJ IDEA.
044: * User: John Ellis
045: * Date: Mar 30, 2004
046: * Time: 11:14:09 AM
047: * To change this template use File | Settings | File Templates.
048: */
049: public class ViewControllerImpl extends AbstractCommandController {
050:
051: private Controller controller = null;
052: private Map screenMappings = null;
053: private String homeName = null;
054: private List customTypedEditors = new ArrayList();
055:
056: protected ModelAndView handle(
057: HttpServletRequest httpServletRequest,
058: HttpServletResponse httpServletResponse, Object command,
059: BindException errors) throws Exception {
060:
061: Map request = HttpServletHelper.getInstance().createRequestMap(
062: httpServletRequest);
063: Map session = HttpServletHelper.getInstance().createSessionMap(
064: httpServletRequest);
065: Map application = HttpServletHelper.getInstance()
066: .createApplicationMap(httpServletRequest);
067:
068: if (controller instanceof CustomCommandController) {
069: command = ((CustomCommandController) controller)
070: .formBackingObject(request, session, application);
071: }
072:
073: if (command != null) {
074: ServletRequestDataBinder binder = createBinder(
075: httpServletRequest, command);
076: binder.bind(httpServletRequest);
077: }
078:
079: if (controller instanceof LoadObjectController) {
080: command = ((LoadObjectController) controller)
081: .fillBackingObject(command, request, session,
082: application);
083: }
084:
085: ModelAndView returnedMv = controller.handleRequest(command,
086: request, session, application, errors);
087:
088: if (returnedMv.getViewName() != null) {
089: // should get from mappings
090: String mappedView = (String) screenMappings.get(returnedMv
091: .getViewName());
092:
093: if (mappedView == null) {
094: mappedView = returnedMv.getViewName();
095: }
096: /*
097: if (controller instanceof ContextAwareController){
098: ((ContextAwareController) controller).addContexts(getHelpManager().getActiveContexts(session), returnedMv.getModel(), mappedView);
099: } else {
100: getHelpManager().addContexts(session, mappedView);
101: }
102: getControllerFilterManager().processFilters(request, session, application, returnedMv, mappedView);
103: */
104:
105: returnedMv = new ModelAndView(mappedView, returnedMv
106: .getModel());
107: }
108:
109: HttpServletHelper.getInstance().reloadApplicationMap(
110: httpServletRequest, application);
111: HttpServletHelper.getInstance().reloadSessionMap(
112: httpServletRequest, session);
113: HttpServletHelper.getInstance().reloadRequestMap(
114: httpServletRequest, request);
115:
116: return returnedMv;
117: }
118:
119: protected ControllerFilterManager getControllerFilterManager() {
120: return (ControllerFilterManager) ComponentManager.getInstance()
121: .get("controllerFilterManager");
122: }
123:
124: protected ServletRequestDataBinder createBinder(
125: HttpServletRequest request, Object command)
126: throws Exception {
127: ServletRequestDataBinder binder = null;
128: binder = new ServletRequestBeanDataBinder(command,
129: getCommandName());
130: initBinder(request, binder);
131: return binder;
132: }
133:
134: /**
135: * Set up a custom property editor for the application's date format.
136: */
137: protected void initBinder(HttpServletRequest request,
138: ServletRequestDataBinder binder) {
139: for (Iterator i = getCustomTypedEditors().iterator(); i
140: .hasNext();) {
141: TypedPropertyEditor editor = (TypedPropertyEditor) i.next();
142: binder.registerCustomEditor(editor.getType(), editor);
143: }
144: }
145:
146: protected Object getCommand(HttpServletRequest request)
147: throws Exception {
148: Object lightObject = null;
149:
150: if (controller instanceof CustomCommandController) {
151: lightObject = ((CustomCommandController) controller)
152: .formBackingObject(HttpServletHelper.getInstance()
153: .createRequestMap(request),
154:
155: HttpServletHelper.getInstance().createSessionMap(
156: request), HttpServletHelper.getInstance()
157: .createApplicationMap(request));
158: return lightObject;
159: }
160:
161: if (super .getCommandClass() == null) {
162: return new Object();
163: }
164:
165: return super .getCommand(request);
166: }
167:
168: public Controller getController() {
169: return controller;
170: }
171:
172: public void setController(Controller controller) {
173: this .controller = controller;
174: }
175:
176: public Map getScreenMappings() {
177: return screenMappings;
178: }
179:
180: public void setScreenMappings(Map screenMappings) {
181: this .screenMappings = screenMappings;
182: }
183:
184: public String getHomeName() {
185: return homeName;
186: }
187:
188: public void setHomeName(String homeName) {
189: this .homeName = homeName;
190: }
191:
192: public List getCustomTypedEditors() {
193: return customTypedEditors;
194: }
195:
196: public void setCustomTypedEditors(List customTypedEditors) {
197: this.customTypedEditors = customTypedEditors;
198: }
199:
200: }
|