⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addressaction.java

📁 电子邮件收发系统,采用各种技术进行研究开发适用大学生课程设计
💻 JAVA
字号:
package net.meybo.mail.action;

import java.math.BigDecimal;
import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import net.meybo.mail.ActiveUser;
import net.meybo.mail.client.AddressGroup;
import net.meybo.mail.client.EmailAddress;
import net.meybo.mail.client.EmailAddressGroup;
import net.meybo.mail.client.MailAddress;

import com.easyjf.util.CommUtil;
import com.easyjf.web.ActionContext;
import com.easyjf.web.IWebAction;
import com.easyjf.web.Module;
import com.easyjf.web.Page;
import com.easyjf.web.WebForm;
import com.easyjf.web.tools.IPageList;
/**
 * 地址管理处理Action
 * <p>Title: </p>
 * <p>Description: 基于EasyJWeb框架,实现IWebAction接口,处理地址的添加,删除修改及选择等.</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: 脉博软件</p>
 * @author 蔡世友
 * @version 1.0
 */
public class AddressAction implements IWebAction {

	public Page execute(WebForm form, Module module) throws Exception{
		String method = CommUtil.null2String(form.get("easyJWebCommand"));
		ActiveUser user = (ActiveUser)ActionContext.getContext().getSession().getAttribute("user");
		if (user == null) {
			form.addResult("msg", "您没有登录或者是超时,请重登录!");
			return new Page("noLoin", "/login.ejf", "html");
		}
		Page forward = null;
		if (method.equals("") || "list".equals(method)) {
			forward = doList(form, module, user);
		} else if ("new".equals(method))// 新
		{
			forward = module.findPage("edit");
		} else if ("add".equals(method))// 增加
		{
			forward = doAdd(form, module, user);
		} else if ("choice".equals(method)) {
			forward = doChoice(form, module, user,ActionContext.getContext().getResponse());
		} else if ("edit".equals(method))// 编辑
		{

			forward = doEdit(form, module, user);
		} else if ("update".equals(method))// 修改
		{
			forward = doUpdate(form, module, user);
		} else if ("del".equals(method)) {
			forward = doDel(form, module, user);
		}
		return forward;
	}

	private Page doChoice(WebForm form, Module module, ActiveUser user,
			HttpServletResponse response) {
		String group = CommUtil.null2String(form.get("group"));
		EmailAddress addrM = new EmailAddress(user.getUserName(), user
				.getServerDomain());
		EmailAddressGroup groupM = new EmailAddressGroup(user.getUserName(),
				user.getServerDomain());
		List list = null;
		if (!group.equals(""))// 通过组查询
		{
			try {
				Document doc = DocumentHelper.parseText("<root/>");
				if (group.equals("all"))// 查询全部
				{
					IPageList pList = addrM.list();
					pList.doList(-1, -1,"", "");
					list = EmailAddress.domList2ObjList(pList.getResult());
					if (list != null) {
						for (int i = 0; i < list.size(); i++) {
							MailAddress addr=(MailAddress)list.get(i);
							Element el = DocumentHelper
							.createElement("address");
					el.addAttribute("name", addr.getName());
					el.addAttribute("email", addr.getEmail());
					doc.getRootElement().add(el);
						}
					}
				} else {
					AddressGroup g = groupM.get(group);
					if (g != null) {
						List emails = g.getEmails();
						if (emails != null) {

							list = new ArrayList();

							for (int i = 0; i < emails.size(); i++) {
								MailAddress addr = addrM
										.readByMail((String) emails.get(i));
								if (addr != null) {
									Element el = DocumentHelper
											.createElement("address");
									el.addAttribute("name", addr.getName());
									el.addAttribute("email", addr.getEmail());
									doc.getRootElement().add(el);
								}
								// if(addr!=null)list.add(addr);
							}
						}
					}
				}
				if (doc != null) {
					response
							.setContentType("text/xml;charset=utf-8");
					response.getWriter().write(doc.asXML());
					response.getWriter().flush();
					response.getWriter().close();
					return null;
				}
			}

			catch (Exception e) {
				System.out.println("出错:" + e);
			}
		} else {
			IPageList pList = addrM.list();
			pList.doList(-1, -1, "", "");
			list = EmailAddress.domList2ObjList(pList.getResult());
		}
		IPageList gPList = groupM.list();
		gPList.doList(-1,-1, "", "");
		List allGroup = EmailAddressGroup.domList2ObjList(gPList.getResult());
		form.addResult("list", list);
		form.addResult("allGroup", allGroup);
		return module.findPage("choice");
	}

	private Page doDel(WebForm form, Module module, ActiveUser user) {
		String cid = CommUtil.null2String(form.get("cid"));
		EmailAddress addrM = new EmailAddress(user.getUserName(), user
				.getServerDomain());
		MailAddress addr = addrM.get(cid);
		System.out.println(cid);
		if (addr == null) {
			form.addResult("msg", "找不到数据!");
		}
		if (addrM.del(addr) > 0) {
			form.addResult("msg", "信息删除成功");
		} else {
			form.addResult("msg", "信息删除失败");
		}
		return doList(form, module, user);
	}

	private Page doEdit(WebForm form, Module module, ActiveUser user) {
		String cid = CommUtil.null2String(form.get("cid"));
		EmailAddress addrM = new EmailAddress(user.getUserName(), user
				.getServerDomain());
		MailAddress addr = addrM.get(cid);
		// System.out.println(cid);
		if (addr == null) {
			form.addResult("msg", "找不到数据!");
		} else
			CommUtil.Obj2Map(addr, form.getTextElement());
		return module.findPage("edit");
	}

	private Page doAdd(WebForm form, Module module, ActiveUser user) {
		String name = CommUtil.null2String(form.get("name"));
		String email = CommUtil.null2String(form.get("email"));
		MailAddress addr = new MailAddress(name, email, user.getUserName()
				+ "@" + user.getServerDomain());
		EmailAddress addrM = new EmailAddress(user.getUserName(), user
				.getServerDomain());
		if (addrM.add(addr) > 0) {
			form.addResult("msg", "信息添加成功!");
			return doList(form, module, user);
		} else {
			form.addResult("msg", "信息添加失败");
			return module.findPage("edit");
		}
	}

	private Page doUpdate(WebForm form, Module module, ActiveUser user) {
		String cid = CommUtil.null2String(form.get("cid"));
		String name = CommUtil.null2String(form.get("name"));
		String email = CommUtil.null2String(form.get("email"));
		MailAddress addr = new MailAddress(name, email, user.getUserName()
				+ "@" + user.getServerDomain());
		addr.setCid(cid);
		EmailAddress addrM = new EmailAddress(user.getUserName(), user
				.getServerDomain());
		if (addrM.update(addr) > 0) {
			form.addResult("msg", "信息修改成功!");
			return doList(form, module, user);
		} else {
			form.addResult("msg", "信息修改失败");
			return module.findPage("edit");
		}
	}

	private Page doList(WebForm form, Module module, ActiveUser user) {
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		EmailAddress addrM = new EmailAddress(user.getUserName(), user
				.getServerDomain());
		IPageList pList = addrM.list();
		if (pList != null) {
			 pList.doList(pageSize, currentPage, "", "");
			 List list = pList.getResult();
			form.addResult("list", EmailAddress.domList2ObjList(list));
			form.addResult("pages", new Integer(pList.getPages()));
			form.addResult("rows", new Integer(pList.getRowCount()));
			form.addResult("page", new Integer(pList.getCurrentPage()));
			form.addResult("gotoPageHTML", CommUtil.showPageHtml(pList
					.getCurrentPage(), pList.getPages()));
		}
		return module.findPage("list");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		double s=14444/1024f;
		BigDecimal b=new BigDecimal(s);
		System.out.println(Math.round(s));
		b.setScale(0,BigDecimal.ROUND_HALF_DOWN);
		System.out.println(b.floatValue());
	}

}

⌨️ 快捷键说明

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