package com.bsw.team9.struts.action;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
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.bsw.hibernate.dao.BookDAO;
import com.bsw.hibernate.pojo.Book;
import com.bsw.team9.struts.form.DetailSearchForm;
/**
* MyEclipse Struts Creation date: 08-21-2009
*
* XDoclet definition:
*
* @struts.action path="/detailSearch" name="detailSearchForm"
* input="/detailSearch.jsp" scope="request" validate="true"
*/
public class DetailSearchAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DetailSearchForm df = (DetailSearchForm) form;// TODO Auto-generated
BookDAO bd = new BookDAO();
List booklist1 = null;
List booklist2 = null;
List booklist3 = null;
List booklist4 = null;
List booklist = null;
Book obj1 = null;
Book obj2 = null;
Book obj3 = null;
Book obj4 = null;
String author = request.getParameter("author");
String bookname = request.getParameter("bookname");
String bookkind = request.getParameter("bookkind");
String publisher = request.getParameter("publisher");
if (((author == null) || ("".equals(author)))
&& ((bookname == null) || ("".equals(bookname)))
&& ((bookkind == null) || ("".equals(bookkind)))
&& ((publisher == null) || ("".equals(publisher)))) {// 四项全为空返回上一页
String o = "您输入的内容为空,请输入其他图书----谢谢!";
request.setAttribute("m", o);
return mapping.getInputForward();
}
if ((bookname != null) && !("".equals(bookname))) {
booklist2 = bd.findByBookname(bookname);
Iterator it2 = booklist2.iterator();
while (it2.hasNext()) {
obj2 = (Book) it2.next();
}
}
if ((author != null) && !("".equals(author))) {
booklist1 = bd.findByAuthor(author);
Iterator it1 = booklist1.iterator();
while (it1.hasNext()) {
obj1 = (Book) it1.next();
}
}
if ((bookkind != null) && !("".equals(bookkind))) {
booklist3 = bd.findByBookkind(bookkind);
Iterator it3 = booklist3.iterator();
while (it3.hasNext()) {
obj3 = (Book) it3.next();
}
}
if ((publisher != null) && !("".equals(publisher))) {
booklist4 = bd.findByPublisher(publisher);
Iterator it4 = booklist4.iterator();
while (it4.hasNext()) {
obj4 = (Book) it4.next();
System.out.print(obj4.getAuthor());
}
}
if ((obj1 != null) || (obj2 != null) || (obj3 != null)
|| (obj4 != null)) {
// boolean b1 = ((obj1.getAuthor() == obj2.getAuthor())
// && (obj2.getAuthor() == obj3.getAuthor()) && (obj3.getAuthor() ==
// obj4
// .getAuthor()));
// boolean b2 = ((obj2.getBookname() == obj1.getBookname())
// && (obj1.getBookname() == obj3.getBookname()) && (obj3
// .getBookname() == obj4.getBookname()));
// boolean b3 = ((obj3.getPublisher() == obj2.getPublisher())
// && (obj2.getPublisher() == obj3.getPublisher()) && (obj3
// .getPublisher() == obj4.getPublisher()));
// boolean b4 = ((obj4.getBookkind() == obj2.getBookkind())
// && (obj3.getBookkind() == obj4.getBookkind()) && (obj1
// .getBookkind() == obj4.getBookkind()));
if ((author != null) && !("".equals(author)) && (bookname != null)
&& !("".equals(bookname)) && (bookkind != null)
&& !("".equals(bookkind)) && (publisher != null)
&& !("".equals(publisher)) && (obj2 != null)
&& (obj1 != null) && (obj3 != null) && (obj4 != null)
&& (obj1.getAuthor() == obj2.getAuthor())
&& (obj2.getAuthor() == obj3.getAuthor())
&& (obj3.getAuthor() == obj4.getAuthor())
&& (obj2.getBookname() == obj1.getBookname())
&& (obj1.getBookname() == obj3.getBookname())
&& (obj3.getBookname() == obj4.getBookname())
&& (obj3.getPublisher() == obj2.getPublisher())
&& (obj2.getPublisher() == obj1.getPublisher())
&& (obj3.getPublisher() == obj4.getPublisher())
&& (obj4.getBookkind() == obj2.getBookkind())
&& (obj3.getBookkind() == obj4.getBookkind())
&& (obj1.getBookkind() == obj4.getBookkind())) {// 四项都不为空时按四项书名,作者,图书种类和出版社查询
booklist = booklist2;
} else if ((author != null) && !("".equals(author))
&& (bookname != null) && !("".equals(bookname))
&& (bookkind != null) && !("".equals(bookkind))
&& (obj2 != null) && (obj1 != null) && (obj3 != null)
&& (obj1.getAuthor() == obj2.getAuthor())
&& (obj2.getAuthor() == obj3.getAuthor())
&& (obj2.getBookname() == obj1.getBookname())
&& (obj1.getBookname() == obj3.getBookname())
&& (obj1.getBookkind() == obj2.getBookkind())
&& (obj3.getBookkind() == obj2.getBookkind())) {// 不满足四项按则按前三项书名,作者,图书种类查询
booklist = booklist2;
} else if ((author != null) && !("".equals(author))
&& (bookname != null) && !("".equals(bookname))
&& (obj2 != null) && (obj1 != null)
&& (obj1.getAuthor() == obj2.getAuthor())
&& (obj2.getBookname() == obj1.getBookname())) {// 不满足四项,三项则按其中前两项查询
booklist = booklist2;
} else if ((bookname != null) && !("".equals(bookname))
&& (obj2 != null) ) {// 若不满足四项,三项,两项则按第一项查询
booklist = booklist2;
} else if ((author != null) && !("".equals(author))
&& (obj1 != null)&& (obj2 == null)) {// 不满足四项,三项和两项则按第二项查询
booklist = booklist1;
} else if ((bookkind != null) && !("".equals(bookkind))
&& (obj3 != null) && (obj2 == null) && (obj1 == null)) {// 若不满足四项,三项,两项,第一项和第二项则按第三项查询
booklist = booklist3;
} else if ((publisher != null) && !("".equals(publisher))
&& (obj4 != null) && (obj3 == null) && (obj2 == null)
&& (obj1 == null)) {// 若不满足四项,三项,两项,第一项,第二项和第三项则按第四项查询
booklist = booklist4;
} else {// 若其中四项无任一项在数据库中有与之匹配的数据即书名,作者,出版社,图书种类都不匹配则返回上一页(查询页)
String o = "本网暂无此类图书,请输入其他图书----谢谢";
request.setAttribute("m", o);
return mapping.getInputForward();
}
} else {
String o = "本网暂无此类图书,请输入其他图书----谢谢";
request.setAttribute("m", o);
return mapping.getInputForward();
}
int pageNum = 1;
if (request.getParameter("page") != null)
pageNum = Integer.parseInt(request.getParameter("page"));
int base = 3;
if (booklist.size() <= 0 || booklist == null) {
request.setAttribute("totalPage", 1);
request.setAttribute("pageNum", 1);
request.setAttribute("booklist", null);
} else {
int totalPage = (booklist.size() / base);
if (booklist.size() % base > 0)
totalPage++;
if (pageNum < 1)
pageNum = 1;
if (pageNum > totalPage)
pageNum = totalPage;
int currPage = pageNum;
--pageNum;
int i = 0;
int begin = pageNum * base;
int end = begin + base;
Iterator it = booklist.iterator();
Set bookSet = new HashSet();
while (it.hasNext()) {
Book book = (Book) it.next();
if (i >= begin && i < end)
bookSet.add(book);
i++;
}
request.setAttribute("totalPage", totalPage);
request.setAttribute("pageNum", pageNum);
request.setAttribute("currPage", currPage);
request.setAttribute("booklist", bookSet);
request.setAttribute("flag", "sab");
评论1
最新资源