simpleschedulewizardcontroller.java

来自「Java的框架」· Java 代码 · 共 545 行 · 第 1/2 页

JAVA
545
字号
package mcaps.core.scheduling.webapp.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

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

import org.apache.commons.lang.StringUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContextException;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import mcap.core.base.webapp.controller.BaseWizardFormController;
import mcap.core.base.webapp.util.DateTimeUtil;
import mcap.core.logging.Log;
import mcap.core.scheduling.model.Job;
import mcap.core.scheduling.model.Schedule;
import mcap.core.scheduling.model.SimpleSchedule;
import mcap.core.scheduling.service.ScheduleException;
import mcap.core.scheduling.service.ScheduleManager;
import mcap.core.scheduling.util.SchedulingUtil;
import mcap.core.scheduling.util.NameConstants;
import mcap.core.user.service.RoleManager;

/**
 * @author jov
 * @date Mar 6, 2006
 * @version 1.0.2.0
 */
public class SimpleScheduleWizardController extends BaseWizardFormController implements
		InitializingBean {

	private ScheduleManager scheduleManager;
	private RoleManager roleManager;
	
	private static final String CLASSNAME="mcaps.core.scheduling.webapp.controller.SimpleScheduleWizardController";
	/**
	 * Returns the scheduleManager.
	 * @return ScheduleManager
	 */
	public ScheduleManager getScheduleManager () {
		return scheduleManager;
	}

	/**
	 * Sets the scheduleManager.
	 * @param scheduleManager The scheduleManager to set.
	 */
	public void setScheduleManager (ScheduleManager scheduleManager) {
		this.scheduleManager = scheduleManager;
	}

	/**
	 * Returns the roleManager.
	 * @return RoleManager
	 */
	public RoleManager getRoleManager () {
		return roleManager;
	}

	/**
	 * Sets the roleManager.
	 * @param roleManager The roleManager to set.
	 */
	public void setRoleManager (RoleManager roleManager) {
		this.roleManager = roleManager;
	}

	// ===========================================================================================================
	// 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 (scheduleManager == null){
			Log.warn("Must set scheduleManager bean property on " + getClass ());
			throw new ApplicationContextException (
					"Must set scheduleManager bean property on " + getClass ());
		}
		if (roleManager == null){
			Log.warn("Must set roleManager bean property on " + getClass ());
			throw new ApplicationContextException (
					"Must set roleManager bean property on " + getClass ());
		}
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 * Retrieve a backing object for the current form from the given request.
	 */
	protected Object formBackingObject (HttpServletRequest request)
			throws Exception {

		SimpleSchedule schedule = null;
	
		String jobName = request.getParameter("jobName");
		String jobGrp = request.getParameter("jobGroupName");

		//Get the empty schedule object for add operation OR
		//Get the specific object for edit operation
		if (StringUtils.equals (request.getParameter("method"), "add")) {
			schedule = new SimpleSchedule ();
			if (jobName != null) schedule.setJob(jobName);
			if (jobGrp != null) schedule.setJobGroup(jobGrp);
		}else {
			schedule = (SimpleSchedule)getSchedule (
					RequestUtils.getStringParameter(request, "scheduleName"),
					RequestUtils.getStringParameter(request, "groupName"));
			if (schedule == null) schedule = new SimpleSchedule();
		}
		setOtherRequestAttributes(request);
		setRepeatIntervalRequestAttribute(request, schedule);
		return schedule;

	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.BaseCommandController#onBind(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.BindException)
	 * Primarily used to do data binding for schedule parameters. Also used to validate 
	 * the page entry each time a page is submitted for validation that requires 
	 * access to HttpServletRequest object.
	 */
	protected void onBind (HttpServletRequest request, Object command,
			BindException errors) {
		
		Schedule schedule = (Schedule) command;
		String jobName = schedule.getJob();
		String jobGrp = schedule.getJobGroup();
		Job job = null;

		//Do validation if buttons other than previous button was clicked
		if (request.getParameter("_target0") == null){
			//Validation for any existing schedule
			try{
				
				job = getJob(jobName,jobGrp);
				if (job == null){
					errors.reject( NameConstants.ERROR_JOB_NOTFOUND, 
							new Object[] {jobName,jobGrp}, "Non-existing job");						
				}
				performGeneralValidation(request,jobName,jobGrp,schedule,errors);
			}catch (Exception e){
				errors.reject (NameConstants.ERROR_PROCESS_REQUEST,new Object[]{e.getMessage()},e.getMessage());						
			}
			
			if (this.getCurrentPage (request) == 0) {
				//To combine the date and time. In JSP they date and time is displayed
				//using two separate controls. 
				//The date portion will be placed in the textbox according to the date 
				//format set in the initBinder(). Thus command object getStartTime only 
				//return the date portion.
				//The time portion will be placed in the mcaps:time tag.
				bindScheduleTime(request,schedule,errors);
				
			}else if (this.getCurrentPage (request) == 1) {
				//Bind parameter				
				//Get the non empty parameters input and assign to schedule parameters
				//Use the job param to loop thru every parameter
				String[] paramNames = null;
				try{
					paramNames = SchedulingUtil.getParameterNames(job);
					if ((paramNames != null) && (paramNames.length > 0)){
						Map paramMap = new HashMap();
					    for (int i = 0; i < paramNames.length; i++) {
					        String key = paramNames[i];
					        String value = request.getParameter("_" + key);
					        if (StringUtils.isNotEmpty(value)){
					        	paramMap.put(key,value);
					        }
					    }		
						schedule.setParameters(paramMap);
					}
				}catch (Exception e){
					errors.reject (NameConstants.ERROR_PROCESS_REQUEST,
							new Object[]{e.getMessage()},e.getMessage());						
				}
			}
	 	}
	}

	 /* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#validatePage(java.lang.Object, org.springframework.validation.Errors, int)
	 * Validate the page entry each time a page is submitted. onBind will be used for some validation that requires access
	 * to HttpServletRequest object.
	 */
	protected void validatePage(Object command, Errors errors, int page) {		
		 if (page == 0){
		 }else if (page == 1){
		 }else if (page == 2){
		 }
	 }

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#getTargetPage(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.Errors, int)
	 */
	protected int getTargetPage (HttpServletRequest request, Object command,
			Errors errors, int currentPage) {
		try{
			//Provide the page with the schedule list when navigating back to the
			//first page so that the combo boxes can be populated
			setOtherRequestAttributes(request);
			
			Log.info("Current TargetPage : " + currentPage);
			//If there is validation error on the current page, don navigate
			if (errors.getErrorCount() > 0) return currentPage;
	
			Schedule schedule = (Schedule) command;
			setRepeatIntervalRequestAttribute(request, (SimpleSchedule)schedule);
			currentPage = super.getTargetPage (request, command, errors, currentPage);
			Log.info("New TargetPage : " + currentPage);
			switch (currentPage) {
				case 0:
					Log.debug("getTargetPage(0) : Do Nothing.");
					//Do Nothing
					break;					
				case 1:	
					Log.debug("getTargetPage(1) : Get parameters from job class.");
					//Get parameters name from job class, if key found in schedule parameters
					//use the value of the schedule parameter, else use empty string
					Job job = getJob(schedule.getJob(),schedule.getJobGroup());
					if (job != null){
						String[] paramNames = SchedulingUtil.getParameterNames(job);
						Map paramMap = schedule.getParameters();
						Map newMap = new HashMap();
						if ((paramNames != null) && (paramNames.length > 0)){
							for (int i = 0; i < paramNames.length; i++){
							   String key = paramNames[i];
							   if (!paramMap.containsKey(key)){
								   newMap.put(paramNames[i],"");
							   }else{
								   newMap.put(paramNames[i],paramMap.get(key));
							   }
							}
						}
						if (newMap.size() > 0){
							schedule.setParameters(newMap);
						}
					}
					break;
				case 2: 
					Log.debug("getTargetPage(2) : Do Nothing.");
					//Do Nothing
					break;
				default:
			}
		}catch (Exception e){
			Log.warn("Error in getTargetPage(" + currentPage + ").");
			errors.reject (NameConstants.ERROR_PROCESS_REQUEST,
					new Object[]{e.getMessage()},e.getMessage());	
			return currentPage;
		}
		return currentPage;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#processFinish(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */

⌨️ 快捷键说明

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