View Javadoc

1   package org.sourceforge.vlibrary.user.actions;
2   
3   import java.util.ArrayList;
4   import javax.servlet.http.HttpServletRequest;
5   import javax.servlet.http.HttpServletResponse;
6   import org.apache.struts.action.ActionForm;
7   import org.apache.struts.action.ActionForward;
8   import org.apache.struts.action.ActionMapping;
9   import org.apache.struts.util.MessageResources;
10  
11  import org.apache.log4j.Logger;
12  
13  import org.sourceforge.vlibrary.Constants;
14  
15  
16  import org.sourceforge.vlibrary.exceptions.LibraryException;
17  import org.sourceforge.vlibrary.user.domain.Book;
18  import org.sourceforge.vlibrary.user.domain.Reader;
19  import org.sourceforge.vlibrary.user.exceptions.BookAwaitingPickupException;
20  import org.sourceforge.vlibrary.user.exceptions.DuplicateRequestException;
21  import org.sourceforge.vlibrary.user.forms.TransactionForm;
22  import org.sourceforge.vlibrary.user.valuebeans.BookMoveTransaction;
23  
24  /**
25   * Action to process reader request for a book
26   * @version $Revision$ $Date$
27   */
28  public class RequestBookAction extends LibraryAction {
29      
30      /** log4j Logger */
31      private static Logger logger =
32       Logger.getLogger(RequestBookAction.class.getName());
33      
34      /**
35       * Process request for book using book property of input form
36       * and remote user id.
37       *
38       * @param mapping = ActionMapping used to select this instance
39       * @param form = TransactionForm associated with this requewst
40       * @param request = HTTP request
41       * @param response = HTTP response* @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          
52          ArrayList<LibraryException> errors = new ArrayList<LibraryException>();
53          TransactionForm frm = (TransactionForm) form; 
54          
55          Book bk = new Book();
56          long book = frm.getBook();
57          
58          // Get the book id 
59          try {
60              bk.setId(book);
61              bk = libraryManager.retrieveBook( book);
62          } catch (Exception e) {
63              String errString = messages.getMessage("error.book.retrieve");
64              logger.error(messages.getMessage(errString,e));
65              errors.add(new LibraryException(errString,e));
66              return standardForward(mapping,request,errors);
67          }
68          
69          // Get the the logged-in user
70          Reader rd = new Reader();
71          
72          try {
73              rd.setUid(request.getRemoteUser());
74              rd = libraryManager.retrieveByUid(request.getRemoteUser());
75          } catch (Exception e) {
76              String errString = messages.getMessage("error.reader.retrieve");
77              logger.error(errString,e);
78              errors.add(new LibraryException(errString));
79              return standardForward(mapping,request,errors);
80          }
81          
82          long reader = rd.getId();
83          
84          // Fire transaction -- request or cancel request
85          try {
86              if (frm.getTransaction().equals(Constants.REQUEST)) {
87                  libraryManager.processRequest(reader,book, frm.getLocation());
88              } else if (frm.getTransaction().equals(Constants.CANCEL)) {
89                  libraryManager.cancelRequest(reader,book);
90              } else {
91                  String errString = messages.getMessage(
92                   "error.trans.request.badrequest",
93                   (new Long(reader)).toString(),
94                   (new Long(book)).toString());
95                  logger.error(errString);
96                  errors.add(new LibraryException(errString));
97                  return standardForward(mapping,request,errors);
98              }
99          } catch (DuplicateRequestException e) {
100             request.setAttribute(Constants.USER_MESSAGE,
101              messages.getMessage("hosiery.duplicate.request"));
102             
103             errors.add(e);
104         } catch (BookAwaitingPickupException e) {
105             request.setAttribute(Constants.USER_MESSAGE,
106              messages.getMessage("trans.status.checkedin"));
107         } catch (LibraryException e) {
108             errors.add(e);
109         } catch (Throwable t) {
110             String errString = messages.getMessage(
111              "error.trans.request", (new Long(reader)).toString(),
112              (new Long(book)).toString());
113             logger.error(errString,t);
114             errors.add(new LibraryException(t));
115         }
116         
117         // Put a BookMoveTransaction VO in the request for confirmation page
118         BookMoveTransaction t = new BookMoveTransaction();
119         t.setReaderEmail(rd.getEmail());
120         t.setReaderName(rd.getFirstName() + " " + rd.getLastName());
121         t.setBookTitle(bk.getTitle());
122         t.setTransaction(frm.getTransaction());
123         
124         request.setAttribute(Constants.TRANS,t);
125         
126         return standardForward(mapping,request,errors);
127     }
128 }