jobwizardcontroller.java
来自「Java的框架」· Java 代码 · 共 372 行
JAVA
372 行
package mcaps.core.scheduling.webapp.controller;
import java.util.HashMap;
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.logging.Log;
import mcap.core.scheduling.model.Job;
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.1.0
*/
public class JobWizardController extends BaseWizardFormController implements
InitializingBean {
private ScheduleManager scheduleManager;
private RoleManager roleManager;
/**
* 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 {
Job job = null;
if (StringUtils.equals (request.getParameter ("method"), "edit")) {
job = getJob (
RequestUtils.getStringParameter (request, "jobName"),
RequestUtils.getStringParameter (request, "groupName"));
}
if (job == null) job = new Job();
request.setAttribute(NameConstants.JOB_GRP_LIST,this.getScheduleManager().getAllJobGroups());
return job;
}
/* (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 job 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) {
Job job = (Job) command;
//Do validation if buttons other than previous button was clicked
if (request.getParameter("_target0") == null){
//Validation for any existing job
Job tempJob = getJob(job.getName(),job.getGroup());
//Check if an existing job already exists; if true show duplicate job error
if (StringUtils.equals (request.getParameter ("method"), "add")) {
if (tempJob != null){
errors.reject( "errors.job.duplicate",
new Object[] {job.getName(),job.getGroup()}, "Duplicate job");
}
}else{
//Check if an existing job already exists; if false show job not found for edit
if (tempJob == null){
errors.reject( "errors.job.notfound",
new Object[] {job.getName(),job.getGroup()}, "Non-existing job");
}
}
if (this.getCurrentPage (request) == 0) {
}else if (this.getCurrentPage (request) == 1) {
//Get the parameters input and assign to command object
String[] paramNames = null;
try{
paramNames = SchedulingUtil.getParameterNames(job);
//If parameters are found from JobManagedParam; meaning that parameter input are required
if ((paramNames != null) && (paramNames.length > 0)){
boolean[] required = SchedulingUtil.getParameterRequiredFlags(job);
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);
}else{
if (required[i]){
errors.reject("errors.param.required",
new Object[]{key},"Parameter '" + key + "' is required.");
}
paramMap.put(key,"");
}
}
job.setParameters(paramMap);
}
}catch (Exception e){
Log.warn(e.getMessage());
errors.reject ("errors.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) {
Job job = (Job) command;
if (page == 0){
//Check for valid job class - must exist in class path and must implement JobManagedParam interface
if (!SchedulingUtil.isValidJob(job)){
errors.rejectValue ("jobClassName", "errors.job.invalidclass",
new Object[] {job.getJobClassName()},
"Class definition not found or job class does not implement JobManagedParam interface.");
}
}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 job list when navigating back to the
//first page so that the job combo can be populated
request.setAttribute(NameConstants.JOB_GRP_LIST, this
.getScheduleManager().getAllJobGroups());
Log.info("Current TargetPage : " + currentPage);
//If there are binding errors refrain from navigation
if (errors.getErrorCount() > 0)
return currentPage;
currentPage = super.getTargetPage (request, command, errors, currentPage);
Log.info("New TargetPage : " + currentPage);
Job job = (Job) command;
switch (currentPage) {
case 0:// Do Nothing
Log.debug("getTargetPage(0) : Do Nothing.");
break;
case 1:
Log.debug("getTargetPage(1) : Get parameters name from job class.");
// Get parameters name from job class NOT from job parameter
// properties
// This is ensure that when job class name is changed, the
// parameter names
// are changed accordingly
String[] paramNames = SchedulingUtil.getParameterNames(job);
Map paramMap = job.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) {
job.setParameters(newMap);
}
break;
case 2: // Do Nothing
Log.debug("getTargetPage(2) : Do Nothing.");
break;
default:
}
} catch (Exception e) {
Log.warn("Error in getTargetPage(" + currentPage + ").");
errors.reject("errors.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)
*/
protected ModelAndView processFinish (HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Job job = (Job) command;
Locale locale = request.getLocale ();
String jobName = job.getName ();
String jobGrp = job.getGroup ();
try {
if (StringUtils.equals (request.getParameter ("method"), "add")) {
this.getScheduleManager ().saveJob (job, false);
if (jobGrp != null && jobGrp.trim ().length () == 0)
jobGrp = Scheduler.DEFAULT_GROUP;
saveMessage (request, getText (NameConstants.SUCC_JOB_SAVE,
new Object[] { jobName, jobGrp }, locale));
}
else {
this.getScheduleManager ().saveJob (job, true);
saveMessage (request, getText (NameConstants.SUCC_JOB_UPDATE,
new Object[] { jobName, jobGrp }, locale));
}
if (StringUtils.isEmpty (request.getParameter ("from"))) {
return new ModelAndView (new RedirectView (NameConstants.JOB_VIEW_URL
+ "?jobName=" + jobName + "&groupName=" + jobGrp));
}
return new ModelAndView (new RedirectView (NameConstants.JOBS_VIEW_URL));
}catch (Exception e) {
Log.warn (e.getMessage ());
errors.reject (NameConstants.ERROR_PROCESS_REQUEST, new Object[] { e
.getMessage () }, e.getMessage ());
return showFormEx (request, response, 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{
Job job = (Job)command;
if (StringUtils.isEmpty(request.getParameter ("from"))){
return new ModelAndView (
new RedirectView(NameConstants.JOB_VIEW_URL + "?jobName=" +
job.getName() + "&groupName=" + job.getGroup()));
}
return new ModelAndView (new RedirectView(NameConstants.JOBS_VIEW_URL));
}
/**
* 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
* @param response
* @return
* @throws Exception
*/
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 errors Binding exception object
* @return Model and View object
* @throws Exception
*/
private ModelAndView showFormEx(HttpServletRequest request,
HttpServletResponse response, BindException errors)
throws Exception {
request.setAttribute(NameConstants.JOB_GRP_LIST, this
.getScheduleManager().getAllJobGroups());
return super.showForm(request,response,errors);
}
/**
* 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;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?