EmailAction.java
资源名称:(J2EE)oa.rar [点击查看]
上传用户:lm2018
上传日期:2015-12-12
资源大小:30449k
文件大小:23k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.oa.module.email.action;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.lang.reflect.InvocationTargetException;
- import java.util.Enumeration;
- import java.util.HashSet;
- import java.util.Hashtable;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Set;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.apache.commons.beanutils.BeanUtils;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.actions.DispatchAction;
- import org.apache.struts.upload.FormFile;
- import com.oa.module.email.bean.ReceiveLetterBean;
- import com.oa.module.email.blacklist.mapping.Tblacklist;
- import com.oa.module.email.company.Tcompany;
- import com.oa.module.email.company.TcompanyDao;
- import com.oa.module.email.dao.IEmailDao;
- import com.oa.module.email.form.EmailForm;
- import com.oa.module.email.fujian.mapping.Tfujian;
- import com.oa.module.email.mapping.Tmailinfo;
- import com.oa.module.office.user.Tuser;
- import com.oa.util.PubUtil;
- import com.oa.util.XPage;
- /**
- * 邮件处理ACtion
- * @author admin
- *
- */
- public class EmailAction extends DispatchAction {
- // --------------------------------------------------------- Instance
- // Variables
- private IEmailDao emaildao;
- // --------------------------------------------------------- Methods
- /**
- * Method execute
- * 收件箱
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward receiveBox(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- String cpage = request.getParameter("currentPage");
- int currentPage = 0;
- try {
- currentPage = Integer.parseInt(cpage);
- } catch (Exception e) {
- currentPage = 1;
- }
- int count = 10;
- ReceiveLetterBean bean = new ReceiveLetterBean();
- try {
- BeanUtils.copyProperties(bean, emailForm);
- HttpSession session = request.getSession();
- Tuser user = (Tuser)session.getAttribute("user");
- bean.setReceiveid(user.getUno());
- } catch (IllegalAccessException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- XPage page = emaildao.getEmailList(currentPage, count, bean);
- request.setAttribute("letterpage", page);
- return mapping.findForward("letterList");
- }
- /**
- * 写信
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward writeEmail(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- if(emailForm.getSendid()!=null){
- String[] check=emailForm.getCheckeduserList();
- check[0] = check[0]+","+ emailForm.getSendid();
- emailForm.setCheckeduserList(check);
- System.out.println(check[0]);
- }
- TcompanyDao dao = new TcompanyDao();
- Tcompany company = dao.getCompany();
- request.setAttribute("company",company);
- return mapping.findForward("writeEmail");
- }
- /**
- * 发信
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward sendEmail(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- emailForm.setIsfujian("0");
- String[] checked = emailForm.getCheckeduserList();
- Tmailinfo mail = null;
- boolean flag = false;
- boolean xxxx = false;
- boolean temps = false;
- Set set = new HashSet();
- for (int i = 0; i < checked.length; i++) {
- int temp = checked[i].indexOf(",");
- String username = checked[i].substring(0,temp);
- String uno = checked[i].substring(temp+1,checked[i].length());
- if(username!=null && uno!=null){
- emailForm.setReceiveid(uno);
- emailForm.setReceivename(username);
- }else{
- continue;
- }
- emailForm.setSendtime(PubUtil.getNowDate());
- HttpSession session = request.getSession();
- Tuser user = (Tuser)session.getAttribute("user");
- emailForm.setSendid(user.getUno());
- emailForm.setSendname(user.getUname());
- emailForm.setEmailtype("0");
- //对方是否把邮件放入黑名单
- List list = emaildao.getBlackListById(uno);
- for (Iterator iter = list.iterator(); iter.hasNext();) {
- Tblacklist element = (Tblacklist) iter.next();
- /*
- *这里换成登录用户
- */
- if(element.getBlackuserid().equals("1")){
- emailForm.setEmailtype("3");
- break;
- }
- }
- mail = new Tmailinfo();
- try {
- BeanUtils.copyProperties(mail , emailForm);
- //上传文件
- Hashtable fileh = emailForm.getMultipartRequestHandler().getFileElements();
- //判断每个文件的大小
- for (Enumeration e = fileh.keys(); e.hasMoreElements();) {
- String key = (String) e.nextElement();
- FormFile file = (FormFile) fileh.get(key);
- if(file.getFileSize()>5*1024*1024){
- PrintWriter out = response.getWriter();
- out.print("<script>");
- out.print("alert('上传文件不能超过5M');");
- out.print("history.back();");
- out.print("</script>");
- return null;
- }
- }
- //
- for (Enumeration e = fileh.keys(); e.hasMoreElements();) {
- String key = (String) e.nextElement();
- FormFile file = (FormFile) fileh.get(key);
- String filename = file.getFileName().trim(); // 文件名
- if(filename!= null && !(filename.equals("")) ){
- String extendname = filename.substring(filename.lastIndexOf('.'), filename.length()-1);
- String realpath = request.getSession().getServletContext()
- .getRealPath("");
- realpath = realpath.replace('\', '/');
- String filepath = "/upload/emailfile"; ;
- String newFile = realpath + filepath+"/"+filename;
- Tfujian fuj = new Tfujian();
- fuj.setFilename(filename);
- fuj.setFilepath(filepath);
- fuj.setMail(mail);
- mail.getFujian().add(fuj);
- mail.setIsfujian("1");
- if(temps==false)
- set.add(fuj);
- emailForm.setIsfujian("1");
- xxxx = true;
- InputStream input = file.getInputStream();
- OutputStream output = new FileOutputStream(newFile);
- byte[] b = new byte[1024];
- int j = 0;
- while ((j = input.read(b, 0, b.length)) != -1) {
- output.write(b, 0, j);
- }
- output.flush();
- output.close();
- input.close();
- }
- }
- temps = true;
- } catch (IllegalAccessException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (IOException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- emaildao.addEamil(mail);
- }
- //保存发送记录
- String checkedusername="";
- String checkeduserid = "";
- for(int i=0; i<checked.length; i++){
- int temp = checked[i].indexOf(",");
- String username = checked[i].substring(0,temp);
- String uno = checked[i].substring(temp+1,checked[i].length());
- checkedusername += username+",";
- checkeduserid += uno+",";
- }
- checkedusername = checkedusername.substring(0, checkedusername.length()-1);
- checkeduserid = checkeduserid.substring(0, checkeduserid.length()-1);
- mail = new Tmailinfo();
- try {
- BeanUtils.copyProperties(mail, emailForm);
- mail.setReceiveid(checkeduserid);
- mail.setReceivename(checkedusername);
- mail.setEmailtype("2");
- if(xxxx==true){
- mail.setIsfujian("1");
- }
- } catch (IllegalAccessException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- for (Iterator iter = set.iterator(); iter.hasNext();) {
- Tfujian element = (Tfujian) iter.next();
- Tfujian ss = new Tfujian();
- ss.setFilename(element.getFilename());
- ss.setFilepath(element.getFilepath());
- mail.getFujian().add(ss);
- ss.setMail(mail);
- }
- flag = emaildao.addEamil(mail);
- if(flag ==true){
- //成功
- request.setAttribute("task","sendsuc");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","senderror");
- return mapping.findForward("error");
- }
- }
- /**
- * 转发给其他用户
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward sendToOther(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- Tmailinfo info = emaildao.getEmailByEmailid(emailForm.getEmailid());
- String[] sendUser = emailForm.getSenduser();
- String id = "";
- String name="";
- boolean flag = false;
- for(int i = 0 ; i < sendUser.length; i++){
- int j = sendUser[i].indexOf(",");
- id = sendUser[i].substring(0, j);
- name = sendUser[i].substring(j+1,sendUser[i].length());
- info.setReceiveid(id);
- info.setReceivename(name);
- flag = emaildao.addEamil(info);
- }
- if(flag ==true){
- //成功
- request.setAttribute("task","sendtoorhersuc");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","sendtoothererror");
- return mapping.findForward("error");
- }
- }
- /**
- * 读取信件
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward readEmail(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- emailForm.setEmailtype("0");
- Tmailinfo mail = emaildao.getEmailByEmailid(emailForm.getEmailid());
- //如果邮件是未读的,则修改状态
- if(emailForm.getIsread().equals("0")){
- boolean flag = emaildao.UpdateEmail(emailForm, 0);
- if(flag==false)
- System.out.println("修改email状态出错");
- }
- try {
- BeanUtils.copyProperties(emailForm, mail);
- } catch (IllegalAccessException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- /**
- * 获取登陆用户
- */
- HttpSession session = request.getSession();
- Tuser user = (Tuser)session.getAttribute("user");
- String id = user.getUno();
- /*String id = "1";*/
- List list = emaildao.getUserList(id);
- request.setAttribute("userList", list);
- return mapping.findForward("readEmail");
- }
- /**
- * 读取草稿
- */
- public ActionForward readEmailCaoGao(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- Tmailinfo mail = emaildao.getEmailByEmailid(emailForm.getEmailid());
- try {
- BeanUtils.copyProperties(emailForm, mail);
- } catch (IllegalAccessException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- return mapping.findForward("readEmailcaogao");
- }
- /**
- * 存草稿
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward saveletter(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- emailForm.setEmailtype("1");
- emailForm.setSendtime(PubUtil.getNowDate());
- HttpSession session = request.getSession();
- Tuser user = (Tuser)session.getAttribute("user");
- emailForm.setSendid(user.getUno());
- emailForm.setSendname(user.getUname());
- String[] checked = emailForm.getCheckeduserList();
- String checkedusername="";
- String checkeduserid = "";
- for(int i=0; i<checked.length; i++){
- int temp = checked[i].indexOf(",");
- String username = checked[i].substring(0,temp);
- String uno = checked[i].substring(temp+1,checked[i].length());
- checkedusername += username+",";
- checkeduserid += uno+",";
- }
- checkedusername = checkedusername.substring(0, checkedusername.length()-1);
- checkeduserid = checkeduserid.substring(0, checkeduserid.length()-1);
- Tmailinfo mail = new Tmailinfo();
- try {
- BeanUtils.copyProperties(mail, emailForm);
- } catch (IllegalAccessException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- mail.setReceiveid(checkeduserid);
- mail.setReceivename(checkedusername);
- boolean flag = false;
- flag = emaildao.addEamil(mail);
- if(flag ==true){
- //成功
- request.setAttribute("task","savesuc");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","saveerror");
- return mapping.findForward("error");
- }
- }
- /**
- * 发件箱
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward sendedBox(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- HttpSession session = request.getSession();
- Tuser user = (Tuser)session.getAttribute("user");
- String cpage = request.getParameter("currentPage");
- int currentPage = 0;
- try {
- currentPage = Integer.parseInt(cpage);
- } catch (Exception e) {
- currentPage = 1;
- }
- int count = 10;
- ReceiveLetterBean bean = new ReceiveLetterBean();
- try {
- BeanUtils.copyProperties(bean, emailForm);
- bean.setSendid(user.getUno());
- } catch (IllegalAccessException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- XPage page = emaildao.getEmailList(currentPage, count, bean);
- request.setAttribute("letterpage", page);
- return mapping.findForward("sendList");
- }
- /**
- * 草稿箱
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward caogaobox(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- HttpSession session = request.getSession();
- Tuser user = (Tuser)session.getAttribute("user");
- emailForm.setEmailtype(request.getParameter("emailtype"));
- ReceiveLetterBean bean = null;
- String cpage = request.getParameter("currentPage");
- int currentPage = 1;
- try {
- currentPage = Integer.parseInt(cpage);
- } catch (Exception e) {
- // TODO: handle exception
- currentPage =1;
- }
- int count =10;
- bean = new ReceiveLetterBean();
- try {
- BeanUtils.copyProperties(bean, emailForm);
- bean.setSendid(user.getUno());
- } catch (IllegalAccessException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- XPage page = emaildao.getEmailList(currentPage, count, bean);
- request.setAttribute("letterpage", page);
- return mapping.findForward("caogaobox");
- }
- /**
- * 编辑邮件
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward Edit(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- String receiveid = emailForm.getReceiveid();
- String receivename = emailForm.getReceivename();
- int temp1 = receiveid.indexOf(",", 0);
- int temp2 = receivename.indexOf(",", 0);
- int i = 0;
- int j = 0;
- String id = "";
- String name = "";
- String[] value = new String[receiveid.length()];
- int next = 0;
- while(temp1 != -1){
- id = receiveid.substring(i,temp1);
- name= receivename.substring(j, temp2);
- value[next] = name+","+id;
- next ++;
- i= temp1+1;
- j= temp2+1;
- temp1 = receiveid.indexOf(",", i);
- temp2 = receivename.indexOf(",", j);
- }
- id = receiveid.substring(i, receiveid.length());
- name = receivename.substring(j, receivename.length());
- value[next] = name+","+id;
- emailForm.setCheckeduserList(value);
- TcompanyDao dao = new TcompanyDao();
- Tcompany company = dao.getCompany();
- request.setAttribute("company",company);
- return mapping.findForward("writeEmail");
- }
- /**
- * 垃圾箱
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward rublishbox(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- ReceiveLetterBean bean = null;
- emailForm.setEmailtype(request.getParameter("emailtype"));
- String cpage = request.getParameter("currentPage");
- HttpSession session = request.getSession();
- Tuser user = (Tuser)session.getAttribute("user");
- int currentPage = 1;
- try {
- currentPage = Integer.parseInt(cpage);
- } catch (Exception e) {
- currentPage = 1;
- }
- int count = 10;
- bean = new ReceiveLetterBean();
- try {
- BeanUtils.copyProperties(bean, emailForm);
- bean.setReceiveid(user.getUno());
- } catch (IllegalAccessException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- XPage page = emaildao.getEmailList(currentPage, count, bean);
- request.setAttribute("rublishList", page);
- return mapping.findForward("rublishbox");
- }
- /**
- * 删除到垃圾箱
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward delToRublish(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- String id = request.getParameter("emailid");
- emailForm.setEmailtype("3");
- emailForm.setEmailid(id);
- boolean flag = false;
- //修改成已读
- emaildao.UpdateEmail(emailForm, 0);
- //放到垃圾箱
- flag = emaildao.UpdateEmail(emailForm, 1);
- if(flag == true){
- request.setAttribute("task","deltorubsuc");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","deltoruberror");
- return mapping.findForward("error");
- }
- }
- /**
- * 彻底删除
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward delForever(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- boolean flag = emaildao.delemail(emailForm.getEmailid());
- if(flag == true){
- request.setAttribute("task","delforeversuc");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","delforevererror");
- return mapping.findForward("error");
- }
- }
- /**
- * 批量移到垃圾箱
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward removesToRublish(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- String[] check = emailForm.getCheck();
- boolean flag = false;
- if(check==null || check.length<=0){
- try {
- PrintWriter out = response.getWriter();
- out.print("<script>");
- out.print("alert('你还没有选择');");
- out.print("history.back();");
- out.print("</script>");
- return null;
- } catch (IOException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- }
- for(int i=0; i<check.length; i++){
- emailForm.setEmailtype("3");
- emailForm.setEmailid(check[i]);
- //放到垃圾箱
- flag = emaildao.UpdateEmail(emailForm, 1);
- }
- if(flag == true){
- request.setAttribute("task","deltorubsuc");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","deltoruberror");
- return mapping.findForward("error");
- }
- }
- /**
- * 批量彻底删除
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward delsForever(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- String[] checked = emailForm.getCheck();
- boolean flag = false;
- for(int i=0; i<checked.length; i++){
- flag = emaildao.delemail(checked[i]);
- }
- if(flag == true){
- request.setAttribute("task","delforeversuc");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","delforevererror");
- return mapping.findForward("error");
- }
- }
- /**
- * 移动到收件箱
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward moveToReceiverBox(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- String[] check = emailForm.getCheck();
- boolean flag = false;
- for(int i=0; i<check.length; i++){
- emailForm.setEmailtype("0");
- emailForm.setEmailid(check[i]);
- //放到收件箱
- flag = emaildao.UpdateEmail(emailForm, 1);
- }
- if(flag == true){
- request.setAttribute("task","toService");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","toServiceerror");
- return mapping.findForward("error");
- }
- }
- /**
- * 根据查询条件(是否已读) 得到邮件
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward isRead(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmailForm emailForm = (EmailForm) form;
- String cpage = request.getParameter("currentPage");
- int currentpage = 1;
- try {
- currentpage = Integer.parseInt(cpage);
- } catch (Exception e) {
- currentpage = 1;
- }
- int count = 10;
- ReceiveLetterBean bean = new ReceiveLetterBean();
- try {
- BeanUtils.copyProperties(bean , emailForm);
- } catch (IllegalAccessException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- XPage page = emaildao.getIsReadList(currentpage, count, bean);
- request.setAttribute("letterpage", page);
- return mapping.findForward("letterList");
- }
- public IEmailDao getEmaildao() {
- return emaildao;
- }
- public void setEmaildao(IEmailDao emaildao) {
- this.emaildao = emaildao;
- }
- }