View Javadoc

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   * Action that takes search parameters from incoming SearchForm and dispatches
29   * to search methods in BookSearch class to execute search.
30   * @version $Revision$ $Date$
31   */
32  
33  public class ListBookAction extends LibraryAction {
34  
35  
36      /** log4j Logger */
37      private static Logger logger =
38       Logger.getLogger(ListBookAction.class.getName());
39  
40      /**
41       * Book List Action implementation
42       *
43       * @param mapping The ActionMapping used to select this instance
44       * @param form The optional ActionForm bean for this request (if any)
45       * @param request The HTTP request we are processing
46       * @param response The HTTP response we are creating
47       * @param messages message resources
48       *
49       * @exception Exception
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                  //   Set Subject IDs
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          // want to disable Edit for those books not owned by the current viewer/user
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         // Only show one copy of each book to non-administrators
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 }