alertformcontroller.java

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

JAVA
261
字号
package mcaps.core.alert.webapp.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;

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

import mcap.core.alert.model.Alert;
import mcap.core.alert.model.AlertUser;
import mcap.core.alert.service.AlertManager;
import mcap.core.base.webapp.controller.BaseFormController;
import mcap.core.user.model.Role;
import mcap.core.user.model.User;
import mcap.core.user.service.RoleManager;
import mcap.core.alert.util.NameConstants;
import mcap.core.alert.webapp.util.AlertEvent;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;


/**
 * Implementation of BaseFormController that interacts with the 
 * AlertManager to handle request to retrieve/persist Alert info to 
 * data store.
 * 
 * @author Tan Beng Suang
 * @date 07-Sep-2005
 * @version 1.0.1.0
 */
public class AlertFormController extends BaseFormController {

	private AlertManager alertManager;
	private RoleManager roleManager;

	/**
	 * Returns the roleManager.
	 * @return RoleManager
	 */
	public RoleManager getRoleManager () {
		return roleManager;
	}

	/**
	 * Sets the roleManager.
	 * @param roleManager The roleManager to set.
	 */
	public void setRoleManager (RoleManager roleManager) {
		this.roleManager = roleManager;
	}
	
	/**
	 * Returns the alertManager.
	 * @return AlertManager
	 */
	public AlertManager getAlertManager () {
		return alertManager;
	}

	
	/**
	 * Sets the alertManager.
	 * @param alertManager The alertManager to set.
	 */
	public void setAlertManager (AlertManager alertManager) {
		this.alertManager = alertManager;
	}
	
	
	
	//----------------------------------------------------------------------
	// ----------------------------------------------------------------------

	/**
	 * First to be called.
	 */
	public ModelAndView processFormSubmission (HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		//	if the request is "Cancel"
		if (request.getParameter ("cancel") != null) {
			String from = request.getParameter("from");
			if (from == null || !from.equals("list")) {
				return new ModelAndView (getCancelView ());
			}
			else {
				return new ModelAndView (getSuccessView ());
			}
		}

		return super.processFormSubmission (request, response, command, errors);
	}

	
	/**
	 * Next to be called if processFormSubmission method called the
	 * super.processFormSubmission
	 */
	public ModelAndView onSubmit (HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		Alert alert = (Alert) command;
		
		Locale locale = request.getLocale ();
		
		if (request.getParameter ("delete") != null) {
			//request to delete alert.
			
			//only alert creator and alert admin can delete alert.
			if (!request.isUserInRole(NameConstants.ALERT_ADMIN_ROLE) &&
					!alert.getCreator().equals(request.getRemoteUser())){
				
				response.sendError (HttpServletResponse.SC_FORBIDDEN);
				return null;
			}
			
			this.getAlertManager().removeAlert (alert.getId());
			saveMessage (request, getText ("alert.deleted", alert.getSubject (),
										locale));

			return new ModelAndView (getSuccessView ());
		}
		else {
			
			if (alert.getId () != null && alert.getId ().length () > 0) {
				errors.reject ("errors.alert.noEdit", new Object[] {
						alert.getSubject ()}, "alert can not be updated");

				return showForm (request, response, errors);
			}
			
			String[] users = request.getParameterValues ("alertUsers");
			ArrayList userList = new ArrayList ();
			alert.setAlertUsers(new HashSet ());
			//add targeted user
			if (users != null) {
				for (int i = 0; i < users.length; i++) {
					String username = users[i];
					AlertUser alertUser = new AlertUser ();
					alertUser.setUsername(username);
					alert.addAlertUser(alertUser);
					userList.add(username);
				}
			}
			
			//add targeted user in role.
			String[] roles = request.getParameterValues ("alertRoles");
			
			if (roles != null) {
				for (int i = 0; i < roles.length; i++) {
					Role role = this.getRoleManager().getRole(roles [i]);
					if (role == null)
						continue;
					
					Iterator itr = role.getUsers().iterator();
					while (itr.hasNext()) {
						User user = (User)itr.next();
						String username = user.getUsername();
						if (!userList.contains(username)) {
							AlertUser alertUser = new AlertUser ();
							alertUser.setUsername(username);
							alert.addAlertUser(alertUser);
							userList.add (username);
						}
					}
				}
			}
			
			//there must be some user targetted by the alert.
			if (userList.size() == 0) {
				errors.reject ("errors.alert.noTargetUser", new Object[] {
						alert.getSubject ()}, "no user targetted");

				return showForm (request, response, errors);
			}
			
			//set the creator and creation date field.
			alert.setCreationDate(new Date ());
			alert.setCreator(request.getRemoteUser());
			
			//try saving the alert information.
			this.getAlertManager().saveAlert (alert);
			
			saveMessage (request, getText ("alert.saved", alert.getSubject(),
						locale));

			//publish event.
			this.getApplicationContext().publishEvent (new AlertEvent (this, userList));
			
			// return to main Menu
			return new ModelAndView (new RedirectView ("listAlert.action"));
		}
	}

	
	/**
	 * 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 editUser.html
		// unless a bind error exists.
		if ((request.getRequestURI ().indexOf ("addAlert") > -1)
				&& (!request.isUserInRole (NameConstants.ALERT_SENDER_ROLE)
						&& (errors.getErrorCount () == 0) && 
				(request.getRemoteUser () != null))) { 
			response.sendError (HttpServletResponse.SC_FORBIDDEN);

			return null;
		}

		return super.showForm (request, response, errors);
	}

	
	/**
	 * Retrieve a backing object for the current form from the given request.
	 */
	protected Object formBackingObject (HttpServletRequest request)
			throws Exception {
		
		String id = request.getParameter ("id");

		Alert alert = null;

		if (!StringUtils.isBlank (id)) {
			alert = this.getAlertManager().getAlert(id);
		}
		else {
			alert = new Alert ();
		}

		return alert;
	}

	protected void onBind (HttpServletRequest request, Object command)
			throws Exception {
		// if the user is being deleted, turn off validation
		if (request.getParameter ("delete") != null) {
			super.setValidateOnBinding (false);
		}
		else {
			super.setValidateOnBinding (true);
		}
	}
	
	
	
	
}

⌨️ 快捷键说明

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