simpleschedulewizardcontroller.java

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

JAVA
545
字号
	protected ModelAndView processFinish (HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		Schedule schedule = (Schedule) command;	
		schedule.setMode(NameConstants.SIMPLE);
		Locale locale = request.getLocale ();
		String scheduleName = schedule.getName();
		String scheduleGrp = schedule.getGroup();
		try{
			if (StringUtils.equals (request.getParameter ("method"), "add")) {
				
				Log.info(CLASSNAME + " processFinish before saveSchedule");
				
				this.getScheduleManager().saveSchedule(schedule);
				
				Log.info(CLASSNAME + " processFinish after saveSchedule");
				
				if (scheduleGrp != null && scheduleGrp.trim().length() == 0) 
					scheduleGrp = Scheduler.DEFAULT_GROUP;
				saveMessage (request, getText (NameConstants.SUCC_SCHEDULE_SAVE, 
						new Object[]{scheduleName, scheduleGrp}, locale));
			}else{
				//Reschedule
				Date dt = this.getScheduleManager().reSchedule(schedule);
				//If schedule was not found or has been removed, dt will be null
				if (dt == null){
					errors.rejectValue ("name", NameConstants.ERROR_SCHEDULE_NOTFOUND, 
						new Object[] {scheduleName,scheduleGrp,schedule.getJobGroup(),schedule.getJob()}, 
						"non existing schedule");						
					return showFormEx (request, response, command, errors);
				}
				saveMessage (request, getText (NameConstants.SUCC_SCHEDULE_UPDATE, new Object[]{
						scheduleName , scheduleGrp}, locale));
			}
			return getRedirectView(request,command);
		}catch (Exception e){
			Log.warn(e.getMessage());
			errors.reject (NameConstants.ERROR_PROCESS_REQUEST,new Object[]{e.getMessage()},
				e.getMessage());						
			return showFormEx (request, response, command, errors);
		}
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#processCancel(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	protected ModelAndView processCancel (HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception{
		return getRedirectView(request,command);
	}

	/**
	 * Method disallows duplicate form submission. Typically used to prevent
	 * duplicate insertion of entities into the datastore. Shows a new form with
	 * an error message.
	 * @param request The HTTP request object
	 * @param response The HTTP response object
	 * @return Model and View object
	 * @throws Exception The exception object
	 */
	protected ModelAndView disallowDuplicateFormSubmission (
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		BindException errors = getErrorsForNewForm (request);
		errors.reject ("errors.duplicateFormSubmission",
				"Duplicate form submission");
		return showForm (request, response, errors);
	}

	/**
	 * Function that sets the request attribute before calling the superclass showForm method.
	 * @param request HTTP request object
	 * @param response HTTP response object
	 * @param command The object bound to this controller
	 * @param errors Binding exception object
	 * @return Model and View object
	 * @throws Exception
	 */
	private ModelAndView showFormEx(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
	throws Exception {
		setOtherRequestAttributes(request);
		setRepeatIntervalRequestAttribute(request, (SimpleSchedule)command);
		return super.showForm(request,response,errors);
	}
	
	/**
	 * Perform general validation
	 * @param request The HTTP request object
	 * @param jobName The job name
	 * @param jobGrp The job group
	 * @param schedule The schedule object
	 * @param errors The binding exception object
	 * @throws ScheduleException The schedule exception
	 */
	private void performGeneralValidation(HttpServletRequest request, String jobName,
			String jobGrp,Schedule schedule, 
			BindException errors)throws ScheduleException{
				
		Schedule tempSchedule = getSchedule(schedule.getName(),schedule.getGroup());
		//Check if an existing schedule already exists; if true show duplicate schedule error
		if (StringUtils.equals (request.getParameter ("method"), "add")) {
			if (tempSchedule != null){
				errors.reject( NameConstants.ERROR_SCHEDULE_DUPLICATE, 
						new Object[] {schedule.getName(),schedule.getGroup(),
						schedule.getJobGroup(), schedule.getJob()}, "Duplicate schedule");						
			}
		}else{
		//Check if an existing schedule already exists; if false show schedule not found for edit
			if (tempSchedule == null){
				errors.reject( NameConstants.ERROR_SCHEDULE_NOTFOUND, 
						new Object[] {schedule.getName(),schedule.getGroup(),
						schedule.getJobGroup(), schedule.getJob()}, "Non-existing schedule");						
			}
		}
	}
	
	/**
	 * Bind the schedule time using the date text box control and the time tag control.
	 * @param request The HTTP request object
	 * @param schedule The schedule object
	 * @param errors The binding exception object
	 */
	private void bindScheduleTime(HttpServletRequest request, Schedule schedule, BindException errors){
		Date startDate = schedule.getStartTime();
		Date endDate = schedule.getEndTime();
		//Bind start date time
		if (startDate != null){
			try {
				startDate = DateTimeUtil.getDateTime(request,startDate,NameConstants.STARTTIME);
				if (StringUtils.equals (request.getParameter ("method"), "add")) {
					//Reject if the startTime is before the current time
					if (startDate.before(new Date())) {
						errors.reject( NameConstants.ERROR_BINDING_BEFORESTARTTIME, "Binding error.");						
					}
				}
				schedule.setStartTime(startDate);
			} catch (Exception e) {
				Log.warn("Error binding schedule start time. " + e.getMessage());
				errors.reject( NameConstants.ERROR_BINDING, 
						new Object[] {NameConstants.STARTTIME}, "Binding error.");						
			}
		}
		//Bind end date time
		if (endDate != null){
			try {
				endDate = DateTimeUtil.getDateTime(request,endDate,NameConstants.ENDTIME);
				if (StringUtils.equals (request.getParameter ("method"), "add")) {
					//Reject if the startTime is before the current time
					//Reject if the endTime is before the current time
					if (endDate.before(new Date())) {
						errors.reject( NameConstants.ERROR_BINDING_BEFOREENDTIME, "Binding error.");						
					}
					if (endDate.before(startDate)) {
						errors.reject( NameConstants.ERROR_BINDING_ENDTIMEBEFORESTARTTIME, "Binding error.");						
					}
				}
				schedule.setEndTime(endDate);
			} catch (Exception e) {
				Log.warn("Error binding schedule end time. " + e.getMessage());
				errors.reject( NameConstants.ERROR_BINDING, 
						new Object[] {NameConstants.ENDTIME}, "Binding error.");						
			}
		}
	}
	
	/**
	 * Gets the a redirect model and view when the cancal button or finish button is clicked.
	 * @param request The HTTP request object
	 * @param command The command object
	 * @return The redirect model and view object
	 * @throws ScheduleException The schedule exception
	 */
	private ModelAndView getRedirectView(HttpServletRequest request, Object command) throws ScheduleException{
		Schedule schedule = (Schedule)command;
		if(StringUtils.equals (request.getParameter ("from"), "job")) {
			return new ModelAndView (
					new RedirectView(NameConstants.JOB_VIEW_URL + "?jobName=" + schedule.getJob() + 
							"&groupName=" + schedule.getJobGroup()));
		}else if(StringUtils.equals (request.getParameter ("from"), "joblist")) {
			return new ModelAndView (new RedirectView(NameConstants.JOBS_VIEW_URL));
		}else{
			return new ModelAndView (new RedirectView(NameConstants.SCHEDULES_VIEW_URL));
		}
	}

	/**
	 * Get the job object. This method enable the getJob by handling the
	 * DataRetrievalFailureException exception.
	 * @param jobName The name of the job.
	 * @param jobGrp The name of the job group.
	 * @return The job object.
	 */
	private Job getJob(String jobName, String jobGrp){
		Job job = null;
		try{
			job = this.getScheduleManager ().getJob(jobName,jobGrp);
		}catch (Exception e){
			//Do nothing; DataRetrievalFailureException is thrown when getJob returns null
		}
		return job;
	}
	
	/**
	 * Get the schedule object. This method enable the getSchedule by handling the
	 * DataRetrievalFailureException exception.
	 * @param scheduleName The name of the schedule
	 * @param scheduleGrp The group name of the schedule
	 * @param jobName The job name
	 * @param jobGrp The job group name
	 * @return The advanced schedule object
	 */
	private SimpleSchedule getSchedule(String scheduleName, String scheduleGrp){
		SimpleSchedule schedule = null;
		try{
			schedule = (SimpleSchedule)this.getScheduleManager ().getSchedule(scheduleName, scheduleGrp);
		}catch (Exception e){
			//Do nothing; DataRetrievalFailureException is thrown when getJob returns null
		}
		return schedule;
	}
	
	/**
	 * Set other request attributes.
	 * @param request The HTTP request object
	 * @throws ScheduleException 
	 */
	private void setOtherRequestAttributes(HttpServletRequest request) throws ScheduleException{
		List jobList = this.getScheduleManager().getAllJobs();
		request.setAttribute(NameConstants.JOB_LIST,jobList);
		request.setAttribute(NameConstants.JOB_GRP_LIST,this.getScheduleManager().getAllJobGroups());
		request.setAttribute(NameConstants.SCHEDULE_GRP_LIST,this.getScheduleManager().getAllScheduleGroups());
	}

	/**
	 * Set the repeat interval in miliseconds to the repeat interval value in terms
	 * of the measurement and also sets measurement value
	 * @param request The HTTP request object
	 * @param schedule The schedule object
	 */
	private void setRepeatIntervalRequestAttribute(HttpServletRequest request, SimpleSchedule schedule){
		long interval = schedule.getRepeatInterval();

		long val = 0;
		int measurement = 0;
		if (interval > 0){
			if (interval < 1000){
				val = interval;
				measurement = 0;
			}else if (interval > 999 && interval < 60000){
				val = interval / 1000;
				measurement = 1;
			}else if (interval > 59999 && interval < 3600000){
				val = interval / 1000 / 60;
				measurement = 2;
			}else if (interval > 3599999 && interval < 86400000){
				val = interval / 1000 / 60 / 60;
				measurement = 3;
			}else if (interval > 86399999 && interval < 8640000000L){
				val = interval / 1000 / 60 / 60 / 24;
				measurement = 4;
			}
		}
		Log.debug("_Repeat Interval : " + val);
		Log.debug("_Repeat Interval Measurement : " + measurement);
		request.setAttribute(NameConstants.REPEAT_INTERVAL, new Long(val));
		request.setAttribute(NameConstants.REPEAT_INTERVAL_MEASUREMENT, new Long(measurement));
	}

}

⌨️ 快捷键说明

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