reportmultiactioncontroller.java

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

JAVA
216
字号
package mcaps.core.reporting.webapp.controller;

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

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

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;

import mcap.core.base.webapp.controller.BaseMultiActionController;
import mcap.core.logging.Log;
import mcap.core.reporting.model.Report;
import mcap.core.reporting.service.ReportManager;
import mcap.core.reporting.util.NameConstants;
import mcap.core.user.model.Role;
import mcap.core.user.model.User;
import mcap.core.util.FileUtil;

/**
 * Implementation of BaseMultiActionController that handle multiple 
 * request pertaining to report.
 * 
 * @author jov
 * @date Jan 12, 2006
 * @version 1.0.2.0
 */
public class ReportMultiActionController extends BaseMultiActionController 
		implements InitializingBean {
	
	private String baseDir;
	
	private ReportManager reportManager;


	/**
	 * Returns the baseDir.
	 * @return String
	 */
	public String getBaseDir () {
		return baseDir;
	}

	
	/**
	 * Sets the baseDir.
	 * @param baseDir The baseDir to set.
	 */
	public void setBaseDir (String baseDir) {
		this.baseDir = baseDir;
	}

	/**
	 * Returns the reportManager.
	 * @return ReportManager
	 */
	public ReportManager getReportManager () {
		return reportManager;
	}
	
	/**
	 * Sets the reportManager.
	 * @param reportManager The reportManager to set.
	 */
	public void setReportManager (ReportManager reportManager) {
		this.reportManager = reportManager;
	}
	
	
	
//===========================================================================================================
// 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 (reportManager == null){
			Log.warn("Must set reportManager bean property on " + getClass());
			throw new ApplicationContextException(
					"Must set reportManager bean property on " + getClass());	
		}
	}


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

	/**
	 * Custom handler for deleting of report template information for reporting 
	 * management purpose.
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView deleteReportHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
			
			Locale locale = request.getLocale();
			if (request.getParameter("reportName") == null) {
				saveError(request, getText(NameConstants.ERROR_DELETE_NULL_REPORTNAME,locale));
			}

			String reportName = RequestUtils.getStringParameter(request, "reportName");
			try {
				
				Report report = reportManager.getReport(reportName);
				if (report == null){
					Log.warn("Fail to delete report. Report not found.");
					saveError(request, getText(NameConstants.ERROR_REPORT_NOTFOUND,
							new Object[]{reportName},locale));
				}else{

					String reportDirPath = this.getServletContext()
							.getRealPath(new StringBuffer().append(this.baseDir)
							.append(File.separator).append(reportName).toString());
					File reportDir = new File(reportDirPath);
					if (reportDir.exists()){
						if (FileUtil.deleteDir(reportDir,true)){
							this.getReportManager ().removeReport (reportName);
							saveMessage(request, getText(NameConstants.SUCC_REPORT_DELETE, 
									new Object[] {reportName }, locale));
						}else{
							Log.warn("Fail to delete report. Unable the report from file system.");
							saveError(request, getText(NameConstants.ERROR_DELETE_REPORT_FAIL,
									new Object[]{reportName},locale));							
						}
					}else{
						Log.warn("Fail to delete report. Unable the locate the report from file system.");
						saveError(request, getText(NameConstants.ERROR_DELETE_REPORT_FAIL,
								new Object[]{reportName},locale));							
					}
				}
				return new ModelAndView (new RedirectView(NameConstants.REPORTS_VIEW_URL));
			} catch (Exception e) {
				Log.warn("Fail to delete report. " + e.getMessage());
				return getErrorView(NameConstants.DELETE_REPORT_TITLE, 
						NameConstants.DELETE_REPORT_HEADING,
						NameConstants.ERROR_DELETE_REPORT_FAIL,
						new Object[]{reportName},
						NameConstants.REPORTS_VIEW_URL);
			}
	}
	
	/**
	 * Custom handler for displaying of list of uploaded report
	 * templates for reporting management purpose.
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView reportsHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		try {
				return new ModelAndView(NameConstants.REPORTS_VIEW, 					
				NameConstants.REPORT_LIST, reportManager.getAllReports());
		} catch (Exception e) {
			Log.warn("Fail to list reports. " + e.getMessage());
			return getErrorView(NameConstants.LIST_REPORT_TITLE, 
					NameConstants.LIST_REPORT_HEADING,
					NameConstants.ERROR_LIST_REPORT_FAIL,
					"");
		}
	}
	

	/**
	 * Custom handler for displaying of list of uploaded report
	 * templates based on access control for user reporting generation 
	 * purpose.
	 *  
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render the response
	 */
	public ModelAndView userReportsHandler(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		try{
			User user = (User) request.getSession().getAttribute(mcap.core.user.util.NameConstants.USER_KEY);
			Set roles = user.getRoles();
			
			String[] roleStrAr = new String[roles.size()];
			if (roles != null) {
				int i = 0;
				for (Iterator it = roles.iterator (); it.hasNext ();i++) {
					Role role = (Role) it.next ();
					roleStrAr[i] = role.getName ();
				}
			}
			return new ModelAndView(NameConstants.USER_REPORTS_VIEW, 					
					NameConstants.USER_REPORT_LIST, reportManager.getUserReports(roleStrAr));
		} catch (Exception e) {
			Log.warn("Fail to list user reports. " + e.getMessage());
			return getErrorView(NameConstants.LIST_USERREPORT_TITLE, 
					NameConstants.LIST_USERREPORT_HEADING,
					NameConstants.ERROR_LIST_USERREPORT_FAIL,
					"");
		}
	}
	
}

⌨️ 快捷键说明

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