mapdetailformcontroller.java

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

JAVA
408
字号

package mcaps.core.map.webapp.controller;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Locale;
import java.util.Map;

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

import mcap.core.base.webapp.controller.BaseFormController;
import mcap.core.logging.Log;
import mcap.core.map.mapbuilder.model.MapConfig;
import mcap.core.map.mapbuilder.model.MapDetail;
import mcap.core.map.mapbuilder.service.MapDetailException;
import mcap.core.map.mapbuilder.service.MapDetailManager;
import mcap.core.map.util.NameConstants;
import mcap.core.util.FileUtil;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContextException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;



/**
 * @deprecated Mapbuilder is no longer supported.
 *
 * Implementation of BaseFormController that interacts with the 
 * MapDetailManager to handle request to for map related tasks. 
 * 
 * @author jov
 * @date Mar 21, 2006
 * @version 1.2.2.0
 */
public class MapDetailFormController extends BaseFormController implements InitializingBean{

	private MapDetailManager mapDetailManager;
	private MapConfig mapConfig;
	private Map mapConfigTemplates;
	
	private String fullContextDir;
	
	/**
	 * Returns the mapDetailManager.
	 * @return MapDetailManager
	 */
	public MapDetailManager getMapDetailManager () {
		return mapDetailManager;
	}
	
	/**
	 * Sets the mapDetailManager.
	 * @param mapDetailManager The mapDetailManager to set.
	 */
	public void setMapDetailManager (MapDetailManager mapDetailManager) {
		this.mapDetailManager = mapDetailManager;
	}
	
	/**
	 * @return Returns the mapConfig.
	 */
	public MapConfig getMapConfig() {
		return mapConfig;
	}

	/**
	 * @param mapConfig The mapConfig to set.
	 */
	public void setMapConfig(MapConfig mapConfig) {
		this.mapConfig = mapConfig;
	}

	/**
	 * @return Returns the mapConfigTemplates.
	 */
	public Map getMapConfigTemplates() {
		return mapConfigTemplates;
	}

	/**
	 * @param mapConfigTemplates The mapConfigTemplates to set.
	 */
	public void setMapConfigTemplates(Map mapConfigTemplates) {
		this.mapConfigTemplates = mapConfigTemplates;
	}
	
//===========================================================================================================
//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 (mapDetailManager == null){
			Log.warn("Must set mapDetailManager bean property on " + getClass());
			throw new ApplicationContextException(
				"Must set mapDetailManager bean property on " + getClass());
		}
		if (mapConfigTemplates == null){
			Log.warn("Must set mapConfigTemplates bean property on " + getClass());
			throw new ApplicationContextException(
				"Must set mapConfigTemplates bean property on " + getClass());		
		}
		if (mapConfig == null){
			Log.warn("Must set mapConfig bean property on " + getClass());
			throw new ApplicationContextException(
				"Must set mapConfig bean property on " + getClass());
		}
		fullContextDir = getServletContext().getRealPath(mapConfig.getContextDir());
		File file = new File(fullContextDir);
		if (!file.exists()){
			file.mkdir();
		}
			
	}

	/* (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 the request is "Cancel"
		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 {
		
		MapDetail mapDetail = (MapDetail) command;
		
		Locale locale = request.getLocale ();
		String mapName = mapDetail.getName();
		try{
			File dir = new File(getMapDetailContextDir(mapName));
			if (request.getParameter ("delete") != null) {
				
				//If map detail does not exist, show error to user stating that unable to delete
				if (getMapDetail(mapName) == null) {
					errors.reject(getText(NameConstants.ERROR_MAP_DELETE_NULL,
							new Object[]{mapName},locale));
					return showForm (request, response, errors);
				}
				//If the map detail can be found, remove the map detail
				this.getMapDetailManager().removeMapDetail(mapName);
				if (dir.exists())	FileUtil.deleteDir(dir,true);				
				saveMessage (request, getText (NameConstants.SUCC_MAPDETAIL_DELETE, new Object[]{mapName}, locale));
			}else {
				try {
					//try saving the map context file
					File file = processFile(NameConstants.CONTEXT_FILE,request,response,command,errors);
					File locator = processFile(NameConstants.LOCATOR_CONTEXT_FILE,request,response,command,errors);
					//If there is a file being uploaded for add or edit, update the virtual path
					if (file != null){
						mapDetail.setContextFile(getVirtualContextFilePath(mapName,file.getName()));
					}
					//If there is a file being uploaded for add or edit, update the locator virtual path
					//Else use the context file as locator as well
					//For edit operation, if no locator was uploaded, keep the old locator
					if (locator != null){ 
						mapDetail.setLocatorContextFile(getVirtualContextFilePath(mapName,locator.getName()));
					}else{
						//Update locator if file is null and mapDetail.getLocatorContextFile is null
						if (file != null){
							if ((mapDetail.getLocatorContextFile() == null) || 
								(mapDetail.getLocatorContextFile().trim().length() == 0)){
								mapDetail.setLocatorContextFile(getVirtualContextFilePath(mapName,file.getName()));
							}
						}
					}
					//Note: Remove codes for deletion of old files that will cause error during edit operation when no 
					//      new context/locator files are uploaded. 
					//TODO: Reimplementation is required to handle file saving for rollback
					//Follow the report implementation that implements a temp dir.
//					//Delete the old files, if its edit and no new file is uploaded the
//					//set the file to the old file so that it will not be deleted.
//					if (file != null) file = new File(getServletContext().getRealPath(mapDetail.getContextFile()));
//					if (locator != null) locator = new File(getServletContext().getRealPath(mapDetail.getLocatorContextFile()));
//					//File dir = new File(getMapDetailContextDir(mapDetail.getName()));
//					FileUtil.deleteFilesInDirectory(dir,new File[]{file,locator});

⌨️ 快捷键说明

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