managecpaction.java

来自「这个是完整的wap项目的源码 开发语言 Java 系统架构 Struts +」· Java 代码 · 共 414 行

JAVA
414
字号
package com.longtime.wap.module.cost.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

import org.apache.commons.validator.GenericValidator;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;
import org.springframework.web.struts.DispatchActionSupport;

import com.longtime.wap.common.WapConstant;
import com.longtime.wap.common.web.Page;
import com.longtime.wap.common.web.PageConstant;
import com.longtime.wap.model.Cp;
import com.longtime.wap.model.UserInfo;
import com.longtime.wap.module.cost.service.CpService;
import com.longtime.wap.module.cost.service.UserInfoService;
import com.longtime.wap.module.cost.web.form.CpForm;

/**
 * 处理Cp管理员维护请求
 * 
 * @author chenxq
 * @date 2007-11-26
 */
public class ManageCpAction extends DispatchActionSupport {

	private UserInfoService userInfoService;

	private CpService cpService;

	/**
	 * 注入用户信息服务对象
	 * 
	 * @param userInfoService
	 *            用户信息服务对象
	 */
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	/**
	 * 注入公司信息服务对象
	 * 
	 * @param cpService
	 *            公司信息服务对象
	 */
	public void setCpService(CpService cpService) {
		this.cpService = cpService;
	}

	/**
	 * 处理获取cp管理员列表请求
	 * 
	 * @param mapping
	 *            ActionMapping对象
	 * @param form
	 *            ActionForm对象
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse对象
	 * @return ActionForward对象
	 * @throws Exception
	 *             异常对象
	 */
	public ActionForward listCpAdmin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			CpForm cpForm = (CpForm) form;
			Page page = null;

			// 获取页码
			String pageNumber = request.getParameter(PageConstant.PARA_PAGE);
			// 判断页码是否为空
			if (GenericValidator.isBlankOrNull(pageNumber)) {
				// 若为空或"",则进入第一页,否则则进入提交的页码
				if (GenericValidator.isBlankOrNull(cpForm.getCurrentPage())) {
					page = new Page(1);
				} else {
					page = new Page(Integer.parseInt(cpForm.getCurrentPage()));
				}
			} else {
				page = new Page(Integer.parseInt(pageNumber));
			}
			request
					.setAttribute("cps", this.userInfoService
							.getUserInfos(page));
			request.setAttribute(PageConstant.ATTR_PAGE, page);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mapping.findForward("listCps");
	}

	/**
	 * 处理到达添加,更新页面的请求
	 * 
	 * @param mapping
	 *            ActionMapping对象
	 * @param form
	 *            ActionForm对象
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse对象
	 * @return ActionForward对象
	 * @throws Exception
	 *             异常对象
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getCpAdmin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		/*
		 * flag为进入create或者edit页面的标识, 
		 * flag为1时进入添加cp管理员页面
		 * flag为2时进入编辑cp管理员页面,并加载表单数据 
		 * flag为3,4时分别为添加、 编辑验证出错返回的页面,确保表单数据不丢失。
		 */
		int flag = Integer.valueOf(((CpForm) form).getFlag());

		if (flag == 1) {
			// 取出公司信息列表
			List cpList = cpService.getCps();
			List cpList1 = new ArrayList();
			LabelValueBean lvb0 = new LabelValueBean("---请选择---",
					null);
			cpList1.add(lvb0);
			for (int i = 0; i < cpList.size(); i++) {
				Cp team = (Cp) cpList.get(i);
				LabelValueBean lvb = new LabelValueBean(team.getCompanyName(),
						String.valueOf(team.getCpId()));
				cpList1.add(lvb);
			}
			request.setAttribute("cpList", cpList1);

			CpForm cpForm = (CpForm) form;
			setFormNull(cpForm);
			return mapping.findForward("create");
		}
		if (flag == 4) {
			// 取出公司信息列表
			List cpList = cpService.getCps();
			List cpList1 = new ArrayList();
			LabelValueBean lvb0 = new LabelValueBean("---请选择---",
					null);
			cpList1.add(lvb0);
			for (int i = 0; i < cpList.size(); i++) {
				Cp team = (Cp) cpList.get(i);
				LabelValueBean lvb = new LabelValueBean(team.getCompanyName(),
						String.valueOf(team.getCpId()));
				cpList1.add(lvb);
			}
			request.setAttribute("cpList", cpList1);

			return mapping.findForward("create");
		}
		if (flag == 2) {
			// 取出公司信息列表
			List cpList = cpService.getCps();
			List cpList1 = new ArrayList();
			LabelValueBean lvb0 = new LabelValueBean("---请选择---",
					null);
			cpList1.add(lvb0);
			for (int i = 0; i < cpList.size(); i++) {
				Cp team = (Cp) cpList.get(i);
				LabelValueBean lvb = new LabelValueBean(team.getCompanyName(),
						String.valueOf(team.getCpId()));
				cpList1.add(lvb);
			}
			request.setAttribute("cpList", cpList1);

			// 设置表单的值
			CpForm cpForm = (CpForm) form;
			Long id = Long.valueOf(cpForm.getUserId());
			UserInfo userInfo = this.userInfoService.getUserInfoById(id);
			cpForm.setAdminName(userInfo.getNickname());
			cpForm.setConfirm(userInfo.getPassword());
			cpForm.setCpName(String.valueOf(userInfo.getCp().getCpId()));
			cpForm.setDescription(userInfo.getMemo());
			cpForm.setLogName(userInfo.getUserName());
			cpForm.setPassword(userInfo.getPassword());
			cpForm.setRegDate(userInfo.getRegDate().toString());
			cpForm.setUserId(userInfo.getUserId());
			request.setAttribute("userInfo", userInfo);
			return mapping.findForward("edit");
		}

		if (flag == 3) {
			// 取出公司信息列表
			List cpList = cpService.getCps();
			List cpList1 = new ArrayList();
			LabelValueBean lvb0 = new LabelValueBean("---请选择---",
					null);
			cpList1.add(lvb0);
			for (int i = 0; i < cpList.size(); i++) {
				Cp team = (Cp) cpList.get(i);
				LabelValueBean lvb = new LabelValueBean(team.getCompanyName(),
						String.valueOf(team.getCpId()));
				cpList1.add(lvb);
			}
			request.setAttribute("cpList", cpList1);

			CpForm cpForm = (CpForm) form;
			Long id = Long.valueOf(cpForm.getUserId());
			UserInfo userInfo = this.userInfoService.getUserInfoById(id);
			request.setAttribute("userInfo", userInfo);
			return mapping.findForward("edit");
		}

		return mapping.findForward("listCps");
	}

	/**
	 * 处理添加cp管理员的请求
	 * 
	 * @param mapping
	 *            ActionMapping对象
	 * @param form
	 *            ActionForm对象
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse对象
	 * @return ActionForward对象
	 * @throws Exception
	 *             异常对象
	 */
	public ActionForward createCpAdmin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 反馈消息列表
		List<String> messages = new ArrayList<String>();

		// 获取表单数据
		CpForm cpForm = (CpForm) form;
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(cpForm.getLogName());
		userInfo.setCp(this.cpService.getCpById(Long
				.valueOf(cpForm.getCpName())));
		userInfo.setPassword(cpForm.getPassword());
		userInfo.setMemo(cpForm.getDescription());
		userInfo.setNickname(cpForm.getAdminName());
		userInfo.setRegDate(new Date());
		userInfo.setGroupId(1);

		// 判断两次输入密码是否相符
		if (!cpForm.getConfirm().equals(cpForm.getPassword())) {
			messages.add("两次密码输入不吻合!");
			request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
			return mapping.getInputForward();
		}

		// 确保没有相同的用户名或昵称
		if (this.userInfoService.checkUserInfoExisted(userInfo)) {
			messages.add("登录名或管理员名称已经存在,请重新输入!");
			request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
			return mapping.getInputForward();
		}

		// 保存用户
		this.userInfoService.saveUserInfo(userInfo);
		// 将表单重置
		setFormNull(cpForm);
		messages.add("添加成功!");
		request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
		return mapping.findForward("success");
	}

	/**
	 * 处理查看cp管理员请求
	 * 
	 * @param mapping
	 *            ActionMapping对象
	 * @param form
	 *            ActionForm对象
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse对象
	 * @return ActionForward对象
	 * @throws Exception
	 *             异常对象
	 */
	public ActionForward viewCpAdmin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Long id = Long.valueOf(request.getParameter("id"));
		UserInfo userInfo = this.userInfoService.getUserInfoById(id);
		request.setAttribute("userInfo", userInfo);
		return mapping.findForward("view");
	}

	/**
	 * 处理编辑cp管理员请求
	 * 
	 * @param mapping
	 *            ActionMapping对象
	 * @param form
	 *            ActionForm对象
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse对象
	 * @return ActionForward对象
	 * @throws Exception
	 *             异常对象
	 */
	@SuppressWarnings("deprecation")
	public ActionForward editCpAdmin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CpForm cpForm = (CpForm) form;

		// 获取表单数据
		List<String> messages = new ArrayList<String>();

		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(cpForm.getUserId());
		userInfo.setUserName(cpForm.getLogName());
		userInfo.setCp(this.cpService.getCpById(Long
				.valueOf(cpForm.getCpName())));
		userInfo.setPassword(cpForm.getPassword());
		userInfo.setMemo(cpForm.getDescription());
		userInfo.setNickname(cpForm.getAdminName());
		String[] date = cpForm.getRegDate().split("-");
		Date d = new Date(Integer.valueOf(date[0]).intValue() - 1900, Integer
				.valueOf(date[1]).intValue() - 1, Integer.valueOf(date[2]));
		userInfo.setRegDate(d);
		userInfo.setGroupId(1);

		// 判断两次输入密码是否相符
		if (!cpForm.getConfirm().equals(cpForm.getPassword())) {
			messages.add("两次密码输入不吻合!");
			request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
			return mapping.getInputForward();
		}

		// 获取修改的cp管理员信息
		UserInfo user = this.userInfoService
				.getUserInfoById(cpForm.getUserId());
		// 判断昵称是否被修改
		if (!user.getNickname().equals(cpForm.getAdminName())) {

			// 若被修改,则判断新修改的昵称是否存在
			if (this.userInfoService.checkUserInfoExisted(userInfo)) {
				messages.add("管理员名称已经存在,请重新输入!");
				request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
				return mapping.getInputForward();
			}
		}
		this.userInfoService.saveUserInfo(userInfo);
		setFormNull(cpForm);
		messages.add("保存成功!");

		request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
		return mapping.findForward("success");
	}

	/**
	 * 处理删除cp管理员请求
	 * 
	 * @param mapping
	 *            ActionMapping对象
	 * @param form
	 *            ActionForm对象
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse对象
	 * @return ActionForward对象
	 * @throws Exception
	 *             异常对象
	 */
	public ActionForward deleteCpAdmins(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<String> messages = new ArrayList<String>();
		String[] ids = request.getParameterValues("delIds");
		this.userInfoService.deleteUserInfos(ids);
		messages.add("删除成功!");
		request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
		return mapping.findForward("success");
	}

	/**
	 * 将表单设置为空表单
	 * 
	 * @param cpForm
	 *            设置的表单
	 */
	protected void setFormNull(CpForm cpForm) {
		cpForm.setAdminName("");
		cpForm.setConfirm("");
		cpForm.setCpName("");
		cpForm.setCurrentPage("");
		cpForm.setDescription("");
		cpForm.setFlag("");
		cpForm.setLogName("");
		cpForm.setPassword("");
		cpForm.setRegDate(null);
		cpForm.setUserId(null);
	}

}

⌨️ 快捷键说明

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