mapdetailformcontroller.java

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

JAVA
408
字号
					//try saving the map detail into database.
					this.getMapDetailManager().saveMapDetail(mapDetail);
				}
				catch (MapDetailException e) {
					if (dir.exists())	FileUtil.deleteDir(dir,true);
					mapDetail.setVersion(null);
					errors.rejectValue ("name", NameConstants.ERROR_MAP_EXISTS, new Object[] {
							mapName}, "duplicate map detail");
					return showForm (request, response, errors);
				}

				if (StringUtils.equals (request.getParameter ("method"), "add")) {
					saveMessage (request, getText (NameConstants.SUCC_MAPDETAIL_SAVE, mapName, locale));
				}else{					
					saveMessage (request, getText (NameConstants.SUCC_MAPDETAIL_UPDATE, mapName, locale));
				}
			}
			return new ModelAndView(getSuccessView ());
		}catch (Exception e){
			errors.reject (NameConstants.ERROR_PROCESS_REQ,new Object[]{e.getMessage()},e.getMessage());						
			return showForm (request, response, errors);
		}
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.BaseCommandController#onBind(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.BindException)
	 */
	protected void onBind(HttpServletRequest request, Object command,
			BindException errors) {
		//TODO: Validate the xml file if method = add or edit
	}
	
	/* (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 on mapDetailForm.html
		// unless a bind error exists.
		if ((request.getRequestURI ().indexOf ("mapDetailForm") > -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 {
			
		MapDetail mapDetail = null;

		if (StringUtils.equals (request.getParameter ("method"), "edit")) {
			mapDetail = getMapDetail(RequestUtils.getStringParameter(request, "mapName"));
		}
		if (mapDetail == null) mapDetail = new MapDetail();
		
		//To allow selection of map config templates (Not used)
		//request.setAttribute(NameConstants.MAP_TEMPLATES,mapConfigTemplates);
		return mapDetail;

	}
	
	/**
	 * Process the report file being uploaded
	 * @param request HTTP request object
	 * @param response HTTP response object
	 * @param command Command object
	 * @param errors Errors object
	 * @return The uploaded report file
	 * @throws Exception
	 */
	private File processFile (String fileId, HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors){

		File file = null;
		MapDetail mapDetail = (MapDetail)command;
		
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		CommonsMultipartFile commonFile = (CommonsMultipartFile) multipartRequest.getFile (fileId);

		if (commonFile != null) {
			if (commonFile.getOriginalFilename().length () > 0) {
				String oriFileName = commonFile.getOriginalFilename ();
				String fullUploadPath = getMapDetailContextDir(mapDetail.getName());
				File dir = new File(fullUploadPath);
				if (!dir.exists()){
					dir.mkdirs();
				}
				String fileName = getMapDetailContextFile(mapDetail.getName(),oriFileName);
				file = new File(fileName);
				try {
					InputStream stream = commonFile.getInputStream ();
					FileOutputStream bos = new FileOutputStream (file);
					int bytesRead = 0;
					byte[] buffer = new byte[8192];
					while ( (bytesRead = stream.read (buffer, 0, 8192)) != -1) {
						bos.write (buffer, 0, bytesRead);
					}
					bos.close ();
					stream.close ();
				}
				catch (Exception e) {
					if (fileId.equals(NameConstants.CONTEXT_FILE)){
						errors.rejectValue ("contextFile", NameConstants.ERROR_FILE_UPL,
								new Object[] { oriFileName },
								"Error while uploading context file.");
					}
					if (fileId.equals(NameConstants.LOCATOR_CONTEXT_FILE)){
						errors.rejectValue ("locatorContextFile", NameConstants.ERROR_FILE_UPL,
								new Object[] { oriFileName },
								"Error while uploading locator context file.");
					}
				}
				return file;
			}
		}
		return null;
	}
	
	/**
	 * Returns the virtual context file path for map detail context file value.
	 * The map detail is using the virtual context file because it will be needed
	 * by the jsp page using c:url
	 * 
	 * @param mapName The map name
	 * @param fileName The context file name
	 * @return
	 */
	private String getVirtualContextFilePath(String mapName, String fileName){
		String str = new StringBuffer()
			.append(mapConfig.getContextDir())	//.append("/")
			.append(mapName).append("/")
			.append(fileName)
			.toString();
		return str;
	}
	
	/**
	 * Returns the full real path that comprises of the root context path and the
	 * map name as the subdirectory to enable the saving of context file to the 
	 * server filesystem
	 * 
	 * @param mapName the name of the map
	 * @return the map detail context path
	 */
	private String getMapDetailContextDir(String mapName){
		return new StringBuffer()
			.append(fullContextDir).append("/")
			.append(mapName).append("/")
			.toString();
	}
	
	/**
	 * Returns the full real file path that comprises of the root context path and the
	 * map name as the subdirectory and the file name to enable the saving of context 
	 * file to the server filesystem
	 * 
	 * @param mapName the name of the map
	 * @param fileName the name of the context file
	 * @return the map detail context file
	 */
	private String getMapDetailContextFile(String mapName, String fileName){
		return new StringBuffer()
			.append(fullContextDir).append("/")
			.append(mapName).append("/")
			.append(fileName)
			.toString();
	}
	
	
	/**
	 * Get the map detail object. This method enable the getMapDetail by handling the
	 * DataRetrievalFailureException exception.
	 * @param mapName The name of the map detail.
	 * @return The map detail object.
	 */
	private MapDetail getMapDetail(String mapName){
		MapDetail mapDetail = null;
		try{
			mapDetail = this.getMapDetailManager ().getMapDetail(mapName);
		}catch (DataRetrievalFailureException e){
			//Do nothing; DataRetrievalFailureException is thrown when getReport returns null
		}
		return mapDetail;
	}





}

⌨️ 快捷键说明

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