1 package org.sourceforge.vlibrary.user.actions;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Set;
8
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12
13 import org.apache.log4j.Logger;
14 import org.apache.struts.action.ActionForm;
15 import org.apache.struts.action.ActionForward;
16 import org.apache.struts.action.ActionMapping;
17 import org.apache.struts.util.MessageResources;
18 import org.sourceforge.vlibrary.Constants;
19 import org.sourceforge.vlibrary.exceptions.LibraryException;
20 import org.sourceforge.vlibrary.user.domain.Book;
21 import org.sourceforge.vlibrary.user.domain.Location;
22 import org.sourceforge.vlibrary.user.domain.Reader;
23 import org.sourceforge.vlibrary.user.domain.Subject;
24 import org.sourceforge.vlibrary.user.forms.BookSearchForm;
25
26
27
28
29
30
31
32
33 public class ListBookAction extends LibraryAction {
34
35
36
37 private static Logger logger =
38 Logger.getLogger(ListBookAction.class.getName());
39
40
41
42
43
44
45
46
47
48
49
50
51 public ActionForward executeAction(ActionMapping mapping,
52 ActionForm form,
53 HttpServletRequest request,
54 HttpServletResponse response,
55 MessageResources messages)
56 throws Exception {
57
58 BookSearchForm frm = (BookSearchForm) form;
59 final ArrayList<LibraryException> errors = new ArrayList<LibraryException>();
60 ArrayList<Book> results = new ArrayList<Book>();
61 final ArrayList<Location> locations = libraryManager.getLocations();
62
63 String selector = Constants.NONE;
64 Reader rd = new Reader();
65
66 try {
67 selector=frm.getSelector();
68 rd.setUid(request.getRemoteUser());
69 rd = libraryManager.retrieveByUid(request.getRemoteUser());
70
71 if (selector.equals(Constants.NONE)) {
72 results = libraryManager.dump();
73 } else if (selector.equals(Constants.TITLE)) {
74 results = libraryManager.titleSearch(frm.getTitle());
75 } else if (selector.equals(Constants.SUBJECT)) {
76
77 ArrayList<Subject> subjects = frm.getSubjects();
78 libraryManager.createSubjectIds(subjects);
79 if (frm.getQueryType().equals(Constants.OR)) {
80 results = libraryManager.subjectOrSearch(subjects);
81 subjects.clear();
82 subjects = null;
83 } else {
84 results = libraryManager.subjectAndSearch(subjects);
85 }
86 } else if (selector.equals(Constants.AUTHOR)) {
87 results = libraryManager.authorSearch(frm.getAuthorLastName());
88 } else if (selector.equals(Constants.MINE)) {
89 results = libraryManager.ownerSearch(rd.getId());
90 }
91 } catch (Throwable t) {
92 String errString = messages.getMessage("error.book.search");
93
94 errors.add(new LibraryException(errString,t));
95
96 logger.error(errString,t);
97 }
98
99
100 if (!selector.equals(Constants.MINE)) {
101 Iterator<Book> it = results.iterator();
102
103 while (it.hasNext()) {
104 Book bk = it.next();
105
106 if (bk.getOwner() == rd.getId()) {
107 bk.setEditable(1);
108 }
109 }
110 }
111
112
113 if (!request.isUserInRole("administrator")) {
114 final Set<String> isbns = new HashSet<String>();
115 final List<Book> killList = new ArrayList<Book>();
116 for (Book book : results) {
117 final String isbn = book.getIsbn();
118 if (isbns.contains(isbn)) {
119 killList.add(book);
120 } else {
121 isbns.add(isbn);
122 }
123 }
124 for (Book book : killList) {
125 results.remove(book);
126 }
127 }
128
129 request.setAttribute(Constants.SEARCH_RESULTS, results);
130 request.setAttribute(Constants.LOCATIONS, locations);
131
132 HttpSession session = request.getSession();
133 session.setAttribute("bookList",results);
134 session.setAttribute(Constants.LOCATIONS, locations);
135
136 return standardForward(mapping,request,errors);
137 }
138 }