roaddefectsearchformcontroller.java

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

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

import java.util.Locale;

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

import mcaps.apps.prrm.roaddefect.service.RoadDefectManager;
import mcaps.apps.prrm.roaddefect.webapp.command.RoadDefectSearchDateCommand;
import mcap.core.base.webapp.controller.BaseFormController;
import mcap.core.docman.service.impl.DocManManagerImpl;
import mcaps.apps.prrm.roaddefect.util.NameConstants;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;


/**
 * Implementation of BaseFormController that handles request 
 * to search and then purge or archive road defects being 
 * searched.
 * 
 * @author jov
 * @date 05-Sep-2005
 * @version 1.0.1.0
 */
public class RoadDefectSearchFormController extends BaseFormController {

	private final static Log logger = LogFactory.getLog(DocManManagerImpl.class);

	private static final String URL_ID = "searchRoadDefect";
	private static final String SEARCHDATE_ACTION = "searchdate";
	private static final String EXECUTE_ACTION = "execute";
	private static final String PURGE_METHOD = "purge";
	private static final String ARCHIVE_METHOD = "archive";
	
	private RoadDefectManager roadDefectManager;
	
	
	/**
	 * 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;
	}
	
	
	//----------------------------------------------------------------------
	// ----------------------------------------------------------------------

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#processFormSubmission(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 * Checks for an action that should be executed without respect to binding errors, like a cancel action.
	 */
	public ModelAndView processFormSubmission (HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		if (request.getParameter ("cancel") != null) {
			return new ModelAndView (getCancelView ());
		}
		return super.processFormSubmission (request, response, command, errors);
	}

	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 * Next to be called if processFormSubmission method called the
	 * super.processFormSubmission
	 */
	public ModelAndView onSubmit (HttpServletRequest request, HttpServletResponse response, 
			Object command, BindException errors)
			throws Exception {
		
		RoadDefectSearchDateCommand param = (RoadDefectSearchDateCommand) command;

		if ((param.getStartDate() == null) && (param.getEndDate() == null)) {
			errors.reject("roadDefect.search.nulldates", "Either Start Date or End Date is required");
			return showForm (request, response, errors);
		} 
		if ((param.getStartDate() == "") && (param.getEndDate() == "")) {
			errors.reject("roadDefect.search.nulldates", "Either Start Date or End Date is required");
			return showForm (request, response, errors);
		} 

		String method = request.getParameter ("method");
		String action = request.getParameter ("do");
		
		if (StringUtils.equals (method, null))  {
			errors.reject("roadDefect.nullmethod", 
					"Method parameter value required. Null method parameter not supported");
			return showForm (request, response, errors);
		}
		if (StringUtils.equals (action, null))  {
			errors.reject("roadDefect.nullmethod", 
					"Method parameter value required. Null method parameter not supported");
			return showForm (request, response, errors);
		}
		if (StringUtils.equals (action, SEARCHDATE_ACTION))  {
			return onSubmitSearch (request,response,command,errors);
		}else if (StringUtils.equals (action, EXECUTE_ACTION)){
			return onSubmitExecute (request,response,command,errors);
		}	
		return showForm (request, response, errors);

	}
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#isFormSubmission(javax.servlet.http.HttpServletRequest)
	 */
	protected boolean isFormSubmission(HttpServletRequest request){
		return (request.getParameter ("do") != null);
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#showForm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.validation.BindException)
	 * 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 searchRoadDefect.html
		// unless a bind error exists.
		if ((request.getRequestURI ().indexOf (URL_ID) > -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);
	}
	
	/* (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 {
		Locale locale = request.getLocale();
		RoadDefectSearchDateCommand command = new RoadDefectSearchDateCommand();
		command.setDateFormat(getText ("date.format",locale));
		return command;
	}
	
	//=======================================================================================
	// Custom implementation
	//=======================================================================================
	
	/**
	 * Handles the request for searching road defects
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @param command the command object
	 * @param errors binding exceptions object
	 * @return the model and view
	 * @throws Exception
	 */
	private ModelAndView onSubmitSearch(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String action = request.getParameter ("do");
		if (StringUtils.equals (action, SEARCHDATE_ACTION)) {
			RoadDefectSearchDateCommand param = (RoadDefectSearchDateCommand) command;
			param.setRoadDefectList(
					getRoadDefectManager().getClosedRoadDefects(
					param.convertStartDate(),param.convertEndDate()));
			return new ModelAndView(getSuccessView(),NameConstants.ROADDEFECT_SEARCHDATE, param);
		}else{
			//TODO For search by criteria
			return null;
		}
	}

	/**
	 * Handles the request for purging or archiving road defects
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @param command the command object
	 * @param errors binding exceptions object
	 * @return the model and view
	 * @throws Exception
	 */
	private ModelAndView onSubmitExecute(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)throws Exception {
			
		String method = request.getParameter ("method");
		RoadDefectSearchDateCommand param = (RoadDefectSearchDateCommand) command;
		Locale locale = request.getLocale();

		if (StringUtils.equals (method, PURGE_METHOD)) {
				int recordCount = getRoadDefectManager().purgeRoadDefect(param.convertStartDate(),param.convertEndDate());
				logger.info("Total records purged = " + recordCount);
				if (recordCount>0){			
					saveMessage (request, getText ("roadDefect.purged",String.valueOf(recordCount),locale));
				}else{
				errors.reject("roadDefect.purge.fail", "Fail to purge road defects");
				}
				return new ModelAndView(new RedirectView(NameConstants.ROADDEFECTS_SEARCH_URL + "?method=" + PURGE_METHOD));
			}else if (StringUtils.equals (method, ARCHIVE_METHOD)) {
				int recordCount = getRoadDefectManager().copyRoadDefectToFile(param.convertStartDate(),param.convertEndDate());
				logger.info("Total records archived = " + recordCount);
				if (recordCount>0){			
					saveMessage (request, getText ("roadDefect.archived",String.valueOf(recordCount),locale));
				}else{
					errors.reject("roadDefect.archive.fail", "Fail to archive road defects");
				}
				return new ModelAndView(new RedirectView(NameConstants.ROADDEFECTS_SEARCH_URL + "?method=" + ARCHIVE_METHOD));
			}else{
				//TODO: To include search capability
				return null;
			}
	}
	
}

⌨️ 快捷键说明

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