View Javadoc

1   package org.sourceforge.vlibrary.user.actions;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletResponse;
7   import org.apache.struts.action.ActionForm;
8   import org.apache.struts.action.ActionForward;
9   import org.apache.struts.action.ActionMapping;
10  import org.apache.struts.util.MessageResources;
11  
12  import org.apache.log4j.Logger;
13  
14  import org.sourceforge.vlibrary.Constants;
15  
16  import org.sourceforge.vlibrary.exceptions.LibraryException;
17  import org.sourceforge.vlibrary.user.domain.Book;
18  
19  public class BookListSortAction extends LibraryAction {
20      
21      /**
22       * Implementation of <strong>Action</strong> that re-sorts book list based
23       * on reader selection - Title, ID, ISBN
24       *  Requires ArrayList of books listed
25       *
26       * @version $Revision$ $Date$
27       *
28       */
29      
30      /** log4j Logger */
31      private static Logger logger =
32       Logger.getLogger(BookListSortAction.class.getName());
33      
34      
35      /**
36       *
37       * @param mapping = ActionMapping used to select this instance
38       * @param form = TransactionForm associated with this requewst
39       * @param request = HTTP request
40       * @param response = HTTP esponse
41       * @param messages message resources
42       *
43       * @exception Exception
44       */
45      public ActionForward executeAction(ActionMapping mapping,
46       ActionForm form,
47       HttpServletRequest request,
48       HttpServletResponse response,
49       MessageResources messages)
50       throws Exception {
51          String sortCriteria = "";
52          
53          ArrayList bookList = (ArrayList)request.getSession().getAttribute("bookList");
54          
55          ArrayList errors = new ArrayList();
56          
57          ArrayList results = new ArrayList();
58          
59          sortCriteria = (String)request.getParameter("action");
60          
61          try {
62              if ( (sortCriteria != null && !sortCriteria.equals("")) &&
63               ( bookList != null && !bookList.isEmpty()) ) {
64                  results = bookList;  //make sure there is something to display, even unsorted :-))
65                  
66                  if (sortCriteria.equalsIgnoreCase("sortBookListByTitle")) {
67                      results = sortBookListByTitle( bookList );
68                  }
69                  
70                  if (sortCriteria.equalsIgnoreCase("sortBookListById")) {
71                      results = sortBookListById( bookList );
72                  }
73                  
74                  if (sortCriteria.equalsIgnoreCase("sortBookListByISBN")) {
75                      results = sortBookListByISBN( bookList );
76                  }
77                  request.setAttribute(Constants.SEARCH_RESULTS,results);
78                  request.setAttribute(Constants.LOCATIONS, request.getSession().getAttribute(Constants.LOCATIONS));
79                  request.getSession().setAttribute("bookList",results);
80              }
81          } catch (Throwable t) {
82              String errString = messages.getMessage("error.book.search");
83              
84              errors.add(new LibraryException(errString,t));
85              
86              logger.error(errString,t);
87          }
88          
89          return standardForward(mapping,request,errors);
90      }
91      
92      public ArrayList sortBookListByTitle( ArrayList unsorted ) {
93          Collections.sort( unsorted, new BookTitleComparator());
94          
95          return unsorted;
96      }
97      
98      public ArrayList sortBookListById( ArrayList unsorted ) {
99          Collections.sort( unsorted, new BookIdComparator());
100         
101         return unsorted;
102     }
103     
104     public ArrayList sortBookListByISBN( ArrayList unsorted ) {
105         Collections.sort( unsorted, new BookIsbnComparator());
106         
107         return unsorted;
108     }
109     
110     private class BookTitleComparator implements java.util.Comparator {
111         public int compare(Object b1, Object b2) {
112             return ((Book)b1).getTitle().compareToIgnoreCase(((Book)b2).getTitle());
113         }
114         
115         public boolean equals( Object b) {
116             return false;
117         }
118         
119         public int hashCode() {
120             return this.toString().hashCode();
121         }
122     }
123     
124     private class BookIdComparator implements java.util.Comparator {
125         public int compare(Object b1, Object b2) {
126             if ( ((Book)b1).getId() > ((Book)b2).getId()) {
127                 return 1;
128             }
129             
130             if ( ((Book)b1).getId() == ((Book)b2).getId()) {
131                 return 0;
132             }
133             
134             return -1;
135         }
136         
137         public boolean equals( Object b) {
138             return false;
139         }
140         
141         public int hashCode() {
142             return this.toString().hashCode();
143         }
144     }
145     
146     private class BookIsbnComparator implements java.util.Comparator {
147         public int compare(Object b1, Object b2) {
148             return ((Book)b1).getIsbn().compareToIgnoreCase(((Book)b2).getIsbn());
149         }
150         
151         public boolean equals( Object b) {
152             return false;
153         }
154         
155         public int hashCode() {
156             return this.toString().hashCode();
157         }
158     }
159 }