001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/tool-lib/src/java/org/theospi/utils/mvc/impl/MultiModelViewController.java $
003: * $Id:MultiModelViewController.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.theospi.utils.mvc.impl;
021:
022: import java.util.ArrayList;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.metaobj.utils.mvc.intf.Controller;
031: import org.sakaiproject.metaobj.utils.mvc.intf.CustomCommandController;
032: import org.sakaiproject.metaobj.utils.mvc.intf.LoadObjectController;
033: import org.springframework.validation.Errors;
034: import org.springframework.web.servlet.ModelAndView;
035:
036: public class MultiModelViewController implements LoadObjectController,
037: CustomCommandController {
038: protected final Log logger = LogFactory.getLog(getClass());
039:
040: private List controllers = null;
041:
042: public Object formBackingObject(Map request, Map session,
043: Map application) {
044: List currentList = new ArrayList();
045:
046: for (Iterator i = controllers.iterator(); i.hasNext();) {
047: Controller controller = (Controller) i.next();
048: ControllerWrapper wrapper = new ControllerWrapper();
049: wrapper.controller = controller;
050: if (controller instanceof CustomCommandController) {
051: wrapper.currentObject = ((CustomCommandController) controller)
052: .formBackingObject(request, session,
053: application);
054: }
055: currentList.add(wrapper);
056: }
057:
058: return currentList;
059: }
060:
061: public Object fillBackingObject(Object incomingModel, Map request,
062: Map session, Map application) throws Exception {
063:
064: List currentList = (List) incomingModel;
065:
066: for (Iterator i = currentList.iterator(); i.hasNext();) {
067: ControllerWrapper controller = (ControllerWrapper) i.next();
068:
069: if (controller instanceof LoadObjectController) {
070: controller.currentObject = ((LoadObjectController) controller.controller)
071: .fillBackingObject(controller.currentObject,
072: request, session, application);
073: }
074: }
075:
076: return currentList;
077: }
078:
079: public ModelAndView handleRequest(Object requestModel, Map request,
080: Map session, Map application, Errors errors) {
081:
082: List currentList = (List) requestModel;
083: Hashtable globalMap = new Hashtable();
084:
085: for (Iterator i = currentList.iterator(); i.hasNext();) {
086: ControllerWrapper controller = (ControllerWrapper) i.next();
087: ModelAndView controllerMv = controller.controller
088: .handleRequest(controller.currentObject, request,
089: session, application, errors);
090: globalMap.putAll(controllerMv.getModel());
091: }
092:
093: return new ModelAndView("success", globalMap);
094: }
095:
096: private class ControllerWrapper {
097: public Controller controller;
098: public Object currentObject;
099: }
100:
101: public List getControllers() {
102: return controllers;
103: }
104:
105: public void setControllers(List controllers) {
106: this.controllers = controllers;
107: }
108:
109: }
|