roaddefectmultiactioncontroller.java

来自「Java的框架」· Java 代码 · 共 295 行

JAVA
295
字号
package mcaps.apps.prrm.roaddefect.webapp.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import mcaps.apps.prrm.roaddefect.model.RoadDefect;
import mcaps.apps.prrm.roaddefect.service.RoadDefectManager;
import mcaps.apps.prrm.task.model.Task;
import mcaps.apps.prrm.task.service.TaskManager;
import mcap.core.base.webapp.controller.BaseMultiActionController;
import mcap.core.base.webapp.util.Result;
import mcap.core.logging.Log;
import mcaps.apps.prrm.roaddefect.util.NameConstants;

/**
 * Implementation of BaseMultiActionController that handle multiple 
 * request pertaining to road defect.
 * 
 * @author jov
 * @date 05-Sep-2005
 * @version 1.0.1.0
 */
public class RoadDefectMultiActionController extends BaseMultiActionController 
																							implements InitializingBean {

	private static final String ROADDEFECTS_VIEW = "prrm/roadDefect/roadDefects";
	private static final String ROADDEFECT_VIEW = "prrm/roadDefect/roadDefect";
	private static final String ROADDEFECTMAIN_VIEW = "prrm/roadDefect/roadDefectMain";

	private RoadDefectManager roadDefectManager;
	private TaskManager taskManager;


	/**
	 * Returns the roadDefectManager.
	 * @return RoadDefectManager
	 */
	public RoadDefectManager getRoadDefectManager () {
		return roadDefectManager;
	}
	
	/**
	 * Sets the roadDefectManager.
	 * @param roadDefectManager The roadDefectManager to set.
	 */
	public void setRoadDefectManager (RoadDefectManager roadDefectManager) {
		this.roadDefectManager = roadDefectManager;
	}
	
	/**
	 * Returns the taskManager.
	 * @return TaskManager
	 */
	public TaskManager getTaskManager () {
		return taskManager;
	}
	
	/**
	 * Sets the taskManager.
	 * @param taskManager The taskManager to set.
	 */
	public void setTaskManager (TaskManager taskManager) {
		this.taskManager = taskManager;
	}
	
	
//===========================================================================================================
// INITIALIZING BEAN IMPLEMENTATION
//===========================================================================================================


	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 * Invoked by a BeanFactory after it has set all bean properties supplied. This allows 
	 * the bean instance to perform initialization only possible when all bean properties 
	 * have been set and to throw an exception in the event of misconfiguration. 
	 */
	public void afterPropertiesSet() throws Exception {
		if (roadDefectManager == null)
			throw new ApplicationContextException(
					"Must set roadDefectManager bean property on " + getClass());
		if (taskManager == null)
			throw new ApplicationContextException(
					"Must set taskManager bean property on " + getClass());
	}

//===========================================================================================================
//INIT BINDER (OVERRIDE SUPER CLASS)
//===========================================================================================================

	/* (non-Javadoc)
	 * @see mcaps.core.base.webapp.controller.BaseMultiActionController#initBinder(javax.servlet.http.HttpServletRequest, org.springframework.web.bind.ServletRequestDataBinder)
	 * Initialize the given binder instance, for example with custom editors. Called by createBinder. 
	 * This enable the registering of custom editors for certain fields of your command class.
	 */
	protected void initBinder (HttpServletRequest request,
			ServletRequestDataBinder binder) {
		
		SimpleDateFormat dateFormat = new SimpleDateFormat (getText ("date.format",
				request.getLocale ()));
		dateFormat.setLenient (false);
		binder.registerCustomEditor (Date.class, null, new CustomDateEditor (
				dateFormat, true));
	}

//===========================================================================================================
// HANDLERS
//===========================================================================================================

	/**
	 * Custom handler for road defect display
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView roadDefectHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {

    if (request.getParameter ("roadDefectId") == null) {
    	Result result = new Result("roadDefect.title","roadDefect.heading",
																	"roadDefect.nullid","actionUrl");
			return new ModelAndView("result","result",result);
		}
		RoadDefect roadDefect = roadDefectManager.getRoadDefect(
				RequestUtils.getIntParameter(request, "roadDefectId"));
    if (roadDefect == null) {
    	Result result = new Result("roadDefect.title","roadDefect.heading",
																	"roadDefect.null","actionUrl");
			return new ModelAndView("result","result",result);
		}

		Task task = new Task();
		task.setRoadDefectId(roadDefect.getId());

//		TODO: To filter listview links based on user role, ADMIN can edit, and view,
//		owner can view and open and edit, others cannot do anything
			//Set also if its owner to allow to open
//		if (request.isUserInRole(NameConstants.ADMIN_ROLE)){
//			request.setAttribute(NameConstants.ROLE,NameConstants.ADMIN_ROLE);
//		}else{
//			request.setAttribute(NameConstants.ROLE,NameConstants.USER_ROLE);
//		}
		
		request.setAttribute(mcaps.apps.prrm.task.util.NameConstants.TASK_LIST,taskManager.getTasks(task));
		return new ModelAndView(ROADDEFECT_VIEW, NameConstants.ROADDEFECT_OBJ, roadDefect);
	}
	
	/**
	 * Custom handler for road defects display
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView roadDefectsHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		return new ModelAndView(ROADDEFECTS_VIEW, 
				NameConstants.ROADDEFECT_LIST, roadDefectManager.getAllRoadDefects());
	}
	

	/**
	 * Custom handler for road defects search
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView roadDefectsSearchHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		return new ModelAndView(ROADDEFECTS_VIEW, 
				NameConstants.ROADDEFECT_LIST, roadDefectManager.getAllRoadDefects());
	}


	/**
	 * Custom handler for road defect deletion
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView deleteRoadDefectHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {    

		Locale locale = request.getLocale();
 
    if (request.getParameter ("roadDefectId") == null) {
    	Result result = new Result("deleteRoadDefect.title","deleteRoadDefect.heading",
																	"roadDefect.delete.nullid","actionUrl");
			return new ModelAndView("result","result",result);
		}
		Integer id = Integer.valueOf(request.getParameter ("roadDefectId"));
		RoadDefect roadDefect = this.getRoadDefectManager().getRoadDefect(id);
		try{
			this.getRoadDefectManager().removeRoadDefect(roadDefect);
			saveMessage (request, getText ("roadDefect.deleted", id.toString(), locale));
			return new ModelAndView(ROADDEFECTS_VIEW, NameConstants.ROADDEFECT_LIST, roadDefectManager.getAllRoadDefects());
		} catch (Exception e) {
			Log.warn("Fail to delete road defect. " + e.getMessage());
			return getErrorView(NameConstants.DELETE_ROADDEFECT_TITLE, 
					NameConstants.DELETE_ROADDEFECT_HEADING,
					NameConstants.ERROR_DELETE_ROADDEFECT_FAIL,
					new Object[]{id},
					NameConstants.ROADDEFECTS_VIEW_URL);
		}	
	}
	
	/**
	 * Custom handler for change road defect status (open, close)
	 * 
	 * @author fenglei
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView statusRoadDefectHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {    


	    if (request.getParameter ("roadDefectId") == null) {
	    	Result result = new Result("roadDefect.title","roadDefect.heading",
																		"roadDefect.nullid","actionUrl");
				return new ModelAndView("result","result",result);
			}
		Integer id=RequestUtils.getIntParameter(request, "roadDefectId");
	    
	    if (id == null) {
	    	Result result = new Result("roadDefect.title","roadDefect.heading",
																		"roadDefect.null","actionUrl");
				return new ModelAndView("result","result",result);
			}
	    
		try{
		    //set road defect status to Opened
		    if("Opened".equals(request.getParameter("method"))) 
		    {
		    	
		    	this.getRoadDefectManager().openRoadDefect(id);
	
		    }
		    //set road defect status to Closed.
		    else if("Closed".equals(request.getParameter("method")))
		    {
		    	
		    	this.getRoadDefectManager().closeRoadDefect(id);
		    }
			
		} catch (Exception e) {
			Log.warn("Fail to change road defect status. " + e.getMessage());
			return getErrorView(NameConstants.STATUS_ROADDEFECT_TITLE, 
					NameConstants.STATUS_ROADDEFECT_HEADING,
					NameConstants.ERROR_STATUS_ROADDEFECT_FAIL,
					new Object[]{id},
					NameConstants.ROADDEFECTS_VIEW_URL);
		}	
		RoadDefect roadDefect = roadDefectManager.getRoadDefect(id);
	    Task task = new Task();
		task.setRoadDefectId(roadDefect.getId());
	
		request.setAttribute(mcaps.apps.prrm.task.util.NameConstants.TASK_LIST,taskManager.getTasks(task));
		return new ModelAndView(ROADDEFECT_VIEW, NameConstants.ROADDEFECT_OBJ, roadDefect);
	}
	
	/**
	 * Custom handler for road defect map display
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView roadDefectMainHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		return new ModelAndView(ROADDEFECTMAIN_VIEW);
	}
	

	
	
	

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?