taskformcontroller.java

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

JAVA
322
字号
package mcaps.apps.prrm.task.webapp.controller;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;

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

import mcaps.apps.prrm.roaddefect.service.RoadDefectManager;
import mcaps.apps.prrm.roadinspection.model.RoadInspection;
import mcaps.apps.prrm.roadinspection.service.RoadInspectionManager;
import mcaps.apps.prrm.task.model.Category;
import mcaps.apps.prrm.task.model.Priority;
import mcaps.apps.prrm.task.model.Status;
import mcaps.apps.prrm.task.model.Task;
import mcaps.apps.prrm.task.service.TaskManager;

import mcap.core.logging.Log;
import mcap.core.alert.model.Alert;
import mcap.core.alert.model.AlertUser;
import mcap.core.alert.service.AlertManager;
import mcap.core.base.webapp.controller.BaseFormController;
import mcap.core.user.model.User;
import mcap.core.user.service.UserManager;
import mcaps.apps.prrm.roadinspection.util.NameConstants;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;


/**
 * Implementation of BaseFormController that interacts with the 
 * TaskManager to handle request to retrieve/persist road defect
 * info to data store.
 * 
 * @author jov
 * @date 05-Sep-2005
 * @version 1.0.1.0
 */
public class TaskFormController extends BaseFormController {

	private String className="mcaps.apps.prrm.task.webapp.controller.TaskFormController";
	private RoadDefectManager roadDefectManager;
	private RoadInspectionManager roadInspectionManager;
	private TaskManager taskManager;
	private UserManager userManager;
	private AlertManager alertManager;
	
	
	/**
	 * 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 roadInspectionManager.
	 * @return RoadInspectionManager
	 */
	public RoadInspectionManager getRoadInspectionManager () {
		return roadInspectionManager;
	}

	
	/**
	 * Sets the roadInspectionManager.
	 * @param roadInspectionManager The roadInspectionManager to set.
	 */
	public void setRoadInspectionManager (RoadInspectionManager roadInspectionManager) {
		this.roadInspectionManager = roadInspectionManager;
	}
	

	/**
	 * 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;
	}
	
	
	/**
	 * Returns the userManager.
	 * @return UserManager
	 */
	public UserManager getUserManager () {
		return userManager;
	}

	
	/**
	 * Sets the userManager.
	 * @param userManager The userManager to set.
	 */
	public void setUserManager (UserManager userManager) {
		this.userManager = userManager;
	}
	
	
	/**
	 * Returns the alertManager.
	 * @return AlertManager
	 */
	public AlertManager getAlertManager () {
		return alertManager;
	}


	
	/**
	 * Sets the alertManager.
	 * @param alertManager The alertManager to set.
	 */
	public void setAlertManager (AlertManager alertManager) {
		this.alertManager = alertManager;
	}


	
	
	//----------------------------------------------------------------------
	// ----------------------------------------------------------------------

	

	/**
	 * First to be called.
	 */
	public ModelAndView processFormSubmission (HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		Enumeration paramNames = request.getParameterNames();
    
		while(paramNames.hasMoreElements()) {
      String parm = (String)paramNames.nextElement();
      System.out.println(parm);
    }
    
		//	if the request is "Cancel"
		if (request.getParameter ("cancel") != null) {	
//			System.out.println("*** cancelUrl  :: " + (String)request.getSession().getAttribute("taskCancelUrl"));
//			return new ModelAndView (new RedirectView ((String)request.getSession().getAttribute("taskCancelUrl")));
//					(String)request.getSession().getAttribute("taskCancelUrl"));
			return new ModelAndView (getCancelView());
		}
		return super.processFormSubmission (request, response, command, errors);
	}

	
	/**
	 * Next to be called if processFormSubmission method called the
	 * super.processFormSubmission
	 */
	public ModelAndView onSubmit (HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		System.out.println("TaskFormController :: onSubmit");
		System.out.println("TaskFormController :: params");
		
		Enumeration paramNames = request.getParameterNames();
    while(paramNames.hasMoreElements()) {
      String parm = (String)paramNames.nextElement();
      System.out.println(parm);
    }

    Task task = (Task) command;
		
		Locale locale = request.getLocale();
		
		if (request.getParameter ("delete") != null) {
      System.out.print("Delete [id] : " + task.getId());		
			this.getTaskManager().removeTask(task);
			saveMessage (request, getText ("task.deleted", task.getId().toString(),
										locale));

			return new ModelAndView (getSuccessView ());
		}
		else {
		Log.info(className + " onSubmit add or edit");
   		this.getTaskManager().saveTask(task);
	
   	   System.out.print("Save [id] : " + task.getId());
       System.out.print("Save [assignedUserId] : " + task.getAssignedUserId());
 	
			if(task.getCategory().equals(Category.INSPECTION)){
				this.getRoadDefectManager().assignRoadDefectForInspection(task.getRoadDefectId());
			}
			if(task.getCategory().equals(Category.REPAIR)){
				this.getRoadDefectManager().assignRoadDefectForRepair(task.getRoadDefectId());
			}
			
			if (StringUtils.equals (request.getParameter ("method"), "add")) {
				saveMessage (request, getText ("task.added", task.getId().toString(), locale));
				Alert alert = new Alert ();
				alert.setCreationDate(new Date ());
				alert.setCreator(request.getRemoteUser());
				if(task.getCategory().equals(Category.INSPECTION)) {
					alert.setSubject ("New Inspection Created");
					alert.setText ("New inspection task has been assigned to you.");
				}
				else if (task.getCategory().equals(Category.REPAIR)){
					alert.setSubject ("New Repair Created");
					alert.setText ("New repair task has been assigned to you.");
				}
				alert.setType(Alert.ALERT);
				
				HashSet list = new HashSet ();
				AlertUser alertUser = new AlertUser ();
				alertUser.setUsername(task.getAssignedUserId());
				list.add (alertUser);
				alert.setAlertUsers(list);
				
				this.alertManager.saveAlert(alert);
			}	else {
				saveMessage (request, getText ("task.saved", task.getId().toString(),
						locale));
			}	
			
			
			//end
			
			return new ModelAndView (new RedirectView ("task.action?taskId=" + task.getId()));
//			return new ModelAndView (new RedirectView ("taskForm.action?method=edit&taskId=" + id));
		}
	}

	
	/**
	 * calling in case of validation errors, to show the form view again.
	 * 
	 */
	protected ModelAndView showForm (HttpServletRequest request,
			HttpServletResponse response, BindException errors) throws Exception {
		// prevent ordinary users from calling a GET on taskForm.html
		// unless a bind error exists.
		if ((request.getRequestURI ().indexOf ("taskForm") > -1)
				&& (!request.isUserInRole (mcap.core.user.util.NameConstants.ADMIN_ROLE)
						&& (errors.getErrorCount () == 0) && 
				(request.getRemoteUser () != null))) { 
			response.sendError (HttpServletResponse.SC_FORBIDDEN);

			return null;
		}
		return super.showForm (request, response, errors);
	}

	
	/**
	 * Retrieve a backing object for the current form from the given request.
	 */
	protected Object formBackingObject (HttpServletRequest request)
			throws Exception {
		
		System.out.println("TaskFormController :: formBackingObject");
		System.out.println("request.getParameter :" + request.getParameter ("method"));
		Task task = null;
		if (StringUtils.equals (request.getParameter("method"), "add")) {
			task = new Task();
			System.out.println(request.getParameter ("roadDefectId"));
			//Road Defect Id is supplied when creating a task from the Road Defect Page/Form
			if (!StringUtils.equals (request.getParameter("roadDefectId"), null)) {
				task.setRoadDefectId(Integer.valueOf(request.getParameter ("roadDefectId")));
			}else{
				task.setRoadDefectId(new Integer(0));
			}
			if (!StringUtils.equals (request.getParameter("category"), "null")) {
				task.setCategory(request.getParameter ("category"));
			}
		}else{	
			task = taskManager.getTask(RequestUtils.getIntParameter(request, "taskId"));
			RoadInspection ri = new RoadInspection();
			ri.setTaskId(task.getId());
			request.setAttribute(NameConstants.ROADINSPECTION_LIST,roadInspectionManager.getRoadInspections(ri));
		}		
		return task;
	}
	

	
	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map refMap = new HashMap();
		refMap.put("taskPriorityList", Priority.list);
		refMap.put("taskCategoryList", Category.list);
		refMap.put("taskStatusList", Status.list);
		refMap.put("userList", userManager.getUsers(new User()));
		//TODO: Get only road defects which status = pend
//		refMap.put("roadDefectList", userManager.getUsers(new User()));
		return refMap;
	}
	
}

⌨️ 快捷键说明

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