View Javadoc

1   package org.sourceforge.vlibrary.user.actions;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   
9   import org.apache.log4j.Logger;
10  import org.apache.struts.action.ActionForm;
11  import org.apache.struts.action.ActionForward;
12  import org.apache.struts.action.ActionMapping;
13  import org.apache.struts.util.MessageResources;
14  import org.sourceforge.vlibrary.Constants;
15  import org.sourceforge.vlibrary.exceptions.LibraryException;
16  import org.sourceforge.vlibrary.user.domain.Book;
17  import org.sourceforge.vlibrary.user.domain.Reader;
18  import org.sourceforge.vlibrary.user.valuebeans.LibraryTransaction;
19  
20  public class StatusBookAction extends LibraryAction {
21  
22      /**
23       * Implementation of <strong>Action</strong> that gets book status <br>
24       * Shoves entire transaction history into ArrayList with key
25       * "transactions" in request attributes. <br>
26       *  Requires "id" Request parameter identifying book.
27       *
28       * @version $Revision$ $Date$
29       *
30       */
31  
32      /** log4j Logger */
33      private static Logger logger =
34              Logger.getLogger(StatusBookAction.class.getName());
35  
36  
37      /**
38       * Get book status (actually entire trans history) and store as ArrayList
39       * of LibraryTransaction objects in statusList request attribute.
40       * Requires "id" Request parameter identifying book.
41       *
42       * @param mapping = ActionMapping used to select this instance
43       * @param form = TransactionForm associated with this request
44       * @param request = HTTP request
45       * @param response = HTTP esponse
46       * @param messages message resources
47       *
48       * @exception Exception
49       */
50      @Override
51      public ActionForward executeAction(ActionMapping mapping,
52              ActionForm form,
53              HttpServletRequest request,
54              HttpServletResponse response,
55              MessageResources messages)
56                      throws Exception {
57          final ArrayList<LibraryException> errors = new ArrayList<LibraryException>();
58  
59          // Get book ID from request
60          long book = 0;
61          try {
62              book = Long.parseLong(request.getParameter(Constants.ID));
63          } catch (final Exception e) {
64              final String errString = messages.getMessage("error.trans.bad.id");
65              logger.error(errString,e);
66              errors.add(new LibraryException(errString,e));
67              return standardForward(mapping,request,errors);
68          }
69  
70          // Get book,possessor and transactions and put into request
71          try {
72              // get book and put it into request under "book"
73              Book bk = new Book();
74              bk.setId(book);
75              bk = libraryManager.retrieveBook( book);
76              request.setAttribute(Constants.BOOK,bk);
77  
78              // put List of LibraryTransactions under "transactions"
79              final List<LibraryTransaction> transactions = libraryManager.getTransactions(book);
80              request.setAttribute(Constants.TRANSACTIONS,transactions.toArray());
81  
82              // put Reader object with possessor data under "possessor"
83              final long reader = libraryManager.getPossessor(book);
84              Reader rd = new Reader();
85              rd.setId(reader);
86              if (reader != Constants.CHECKED_IN) {
87                  rd = libraryManager.retrieveReader(reader);
88              }
89              request.setAttribute(Constants.POSSESSOR,rd);
90  
91          } catch (final Throwable t) {
92              final String errString = messages.getMessage("error.trans.retrieve.sql");
93              logger.error(errString,t);
94              errors.add(new LibraryException(errString,t));
95          }
96          return standardForward(mapping,request,errors);
97      }
98  }