SearchAction.java
上传用户:liangcc
上传日期:2019-05-24
资源大小:4412k
文件大小:5k
- package com.softeem.struts.action;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Locale;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import com.softeem.struts.form.SearchForm;
- import com.softeem.webmail.dao.mail.Mail;
- import com.softeem.webmail.dao.search.QueryMails;
- public class SearchAction extends Action {
- private String createSql(String stat, String value) {
- String sql = "";
- if ("1".equals(stat)) {
- sql = " like '%" + value + "%'";
- } else if ("2".equals(stat)) {
- sql = " ='" + value + "'";
- } else if ("3".equals(stat)) {
- }
- return sql;
- }
- private String createSqlSize(String stat, String value) {
- String sql = "";
- if ("1".equals(stat)) {
- sql = " and size > '" + value + "'";
- } else if ("2".equals(stat)) {
- sql = " and size < '" + value + "'";
- } else if ("3".equals(stat)) {
- sql = " and size = '" + value + "'";
- }
- return sql;
- }
- private String createSqlBox(String[] boxs) {
- String sql = "";
- if (boxs != null && boxs.length > 0) {
- int stat = 1;
- for (String boxname : boxs) {
- if (stat == 1) {
- if ("in".equals(boxname)) {
- sql = " and (box_name = 'recbox'";
- } else if ("out".equals(boxname)) {
- sql = " and (box_name = 'drafts'";
- } else if ("sed".equals(boxname)) {
- sql = " and (box_name = 'sendbox'";
- } else if ("del".equals(boxname)) {
- sql = " and (box_name = 'laji'";
- }
- stat = 0;
- } else {
- if ("in".equals(boxname)) {
- sql = sql + " or box_name = 'recbox'";
- } else if ("out".equals(boxname)) {
- sql = sql + " or box_name = 'drafts'";
- } else if ("sed".equals(boxname)) {
- sql = sql + " or box_name = 'sendbox'";
- } else if ("del".equals(boxname)) {
- sql = sql + " or box_name = 'laji'";
- }
- }
- }
- sql = sql + ")";
- }
- return sql;
- }
-
- private Date getDateForm(String year,String month,String day){
- String s = year +"-" + month + "-" + day;
- SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");
- try {
- return sformat.parse(s);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- private Date getStringToDate(String s){
- String strr = s.substring(0,s.indexOf(":")-2);
- String year = s.substring(s.length()-4);
- String date = strr+year;
- SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy",Locale.US);
- try {
- return format.parse(date);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
-
- }
-
- private List<Mail> getList(List<Mail> list,String stat,Date t){
- List<Mail> list1 = new ArrayList<Mail>();
- for(Iterator<Mail> item = list.iterator();item.hasNext();){
- Mail mail = item.next();
- String date = mail.getDate();
- Date dateDB = getStringToDate(date);
- if("1".equals(stat)){
- if(dateDB.before(t)){
- list1.add(mail);
- }
- }else if("2".equals(stat)){
- if(dateDB.after(t)){
- list1.add(mail);
- }
- }else if("3".equals(3)){
- if(dateDB.equals(t)){
- list1.add(mail);
- }
- }
- }
- return list1;
- }
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- SearchForm searchForm = (SearchForm) form;
- // String sql = "select * from mail where (mail_from='"+username+"' or
- // mail_to='"+username+"')";
- String sql = "select * from mail ";
- if ("Subject".equals(searchForm.getShead())) {
- sql = sql + " where subject"
- + createSql(searchForm.getSmode(), searchForm.getStext());
- } else if ("FromMail".equals(searchForm.getShead())) {
- sql = sql + " where mail_to"
- + createSql(searchForm.getSmode(), searchForm.getStext());
- } else if ("FromName".equals(searchForm.getShead())) {
- sql = sql + " where mial_from"
- + createSql(searchForm.getSmode(), searchForm.getStext());
- }
- sql = sql
- + createSqlSize(searchForm.getSsizemode(), searchForm
- .getSsize());
- if ("1".equals(searchForm.getSread())) {
- ;
- } else if ("2".equals(searchForm.getSread())) {
- sql = sql + " and stat='0'";
- } else if ("3".equals(searchForm.getSread())) {
- sql = sql + " and stat='1'";
- }
- sql = sql + createSqlBox(searchForm.getCheckbox());
- List<Mail> list = new QueryMails().queryMails(sql);
- Date t = getDateForm(searchForm.getSyear(), searchForm.getSmonth(), searchForm.getSday());
- List<Mail> list1 = getList(list, searchForm.getSdatemode(),t);
- System.out.println(list1.size());
- request.setAttribute("searchMails", list1);
- return mapping.findForward("success");
- }
- // public static void main(String[] args) {
- // System.out.println(new SearchAction().getStringToDate(new Date().toString()));
- // }
- }