mapmultiactioncontroller.java

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

JAVA
198
字号
package mcaps.core.map.webapp.controller;

import java.io.File;
import java.util.Locale;

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

import mcap.core.base.webapp.controller.BaseMultiActionController;
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.MapDetailManager;
import mcap.core.map.util.NameConstants;
import mcap.core.util.FileUtil;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;


/**
 * @deprecated Mapbuilder is no longer supported.
 * 
 * Implementation of BaseMultiActionController that handle different 
 * requests pertaining to map management.
 * 
 * @author jov
 * @date Mar 17, 2006
 * @version 1.2.1.0
 */
public class MapMultiActionController extends BaseMultiActionController implements InitializingBean {

	private MapDetailManager mapDetailManager;
	private MapConfig mapConfig;
	
	private String fullContextDir;

	/**
	 * @return Returns the mapDetailManager.
	 */
	public MapDetailManager getMapDetailManager() {
		return mapDetailManager;
	}

	/**
	 * @param mapDetailManager The mapDetailManager to set.
	 */
	public void setMapDetailManager(MapDetailManager mapConfigManager) {
		this.mapDetailManager = mapConfigManager;
	}

	/**
	 * @return Returns the mapConfig.
	 */
	public MapConfig getMapConfig() {
		return mapConfig;
	}

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

	
	//===========================================================================================================
	// 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 (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());
		Log.info("Full Context Dir : " + fullContextDir);
		File file = new File(fullContextDir);
		if (!file.exists()){
			file.mkdir();
		}
	
	}

	//===========================================================================================================
	// HANDLERS
	//===========================================================================================================

	/**
	 * Custom handler for displaying of map page.
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView mapHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		Locale locale = request.getLocale();
		
	    request.setAttribute("mapConfig",this.getMapConfig());

		if (request.getParameter("mapName") == null ||
				request.getParameter("mapName").length() == 0) {
				saveError(request, getText(NameConstants.ERROR_NULL_CONFIG,locale));
		}
		MapDetail mapDetail = null;
		String mapName = RequestUtils.getStringParameter(request, "mapName");
		mapDetail = this.getMapDetailManager().getMapDetail(mapName);
		if (mapDetail == null) {
			saveError(request, getText(NameConstants.ERROR_MAP_VIEW_NULL,
					new Object[]{mapName},locale));
		}else{
			return new ModelAndView(NameConstants.MAP_VIEW,
					NameConstants.MAP_OBJ,mapDetail);
		}
		return new ModelAndView(NameConstants.MAPS_VIEW,
				NameConstants.MAP_LIST,this.getMapDetailManager().getAllMapDetails());
		
	}
	
	/**
	 * Custom handler for deleting of map detail.
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView deleteMapHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		Locale locale = request.getLocale();
		if (request.getParameter("mapName") == null ||
			request.getParameter("mapName").length() == 0) {
			saveError(request, getText(NameConstants.ERROR_NULL_NAME,locale));
		}
		MapDetail mapDetail = null;
		String mapName = RequestUtils.getStringParameter(request, "mapName");
		mapDetail = this.getMapDetailManager().getMapDetail(mapName);
		if (mapDetail == null) {
			saveError(request, getText(NameConstants.ERROR_MAP_DELETE_NULL,
					new Object[]{mapName},locale));
		}else{
			this.getMapDetailManager().removeMapDetail(mapName);
			File reportDir = new File(getFullContextPath(mapDetail.getName()));
			if (reportDir.exists()){
				FileUtil.deleteDir(reportDir,true);
			}
			saveMessage(request, getText(NameConstants.SUCC_MAPDETAIL_DELETE, 
					new Object[] {mapName}, locale));
		}
		return new ModelAndView (new RedirectView(NameConstants.MAPS_VIEW_URL));
	}
	
	/**
	 * Custom handler for displaying of map list page.
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView mapDetailsHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		return new ModelAndView(NameConstants.MAPS_VIEW,
				NameConstants.MAP_LIST,this.getMapDetailManager().getAllMapDetails());
	}

	
	/**
	 * Returns the full real file path that comprises of the root context path and the
	 * map name as the subdirectory and the file name of the map context file 
	 * 
	 * @param mapName The map name
	 * @return The full path of the context file
	 */
	private String getFullContextPath(String mapName){
		return new StringBuffer()
			.append(fullContextDir).append("/")
			.append(mapName).append("/").toString();
	}



}

⌨️ 快捷键说明

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