View Javadoc

1   package org.sourceforge.vlibrary.user.actions;
2   
3   import java.util.Map;
4   import java.util.ArrayList;
5   
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   
9   import org.apache.struts.action.ActionForm;
10  import org.apache.struts.action.ActionForward;
11  import org.apache.struts.action.ActionMapping;
12  import org.apache.struts.util.MessageResources;
13  
14  import org.apache.commons.beanutils.PropertyUtils;
15  import org.apache.commons.lang.StringUtils;
16  
17  import org.apache.log4j.Logger;
18  
19  import org.sourceforge.vlibrary.Constants;
20  import org.sourceforge.vlibrary.user.domain.Book;
21  import org.sourceforge.vlibrary.user.domain.Reader;
22  import org.sourceforge.vlibrary.user.forms.BookForm;
23  import org.sourceforge.vlibrary.exceptions.LibraryException;
24  
25  import org.sourceforge.vlibrary.user.domain.Author;
26  import org.sourceforge.vlibrary.user.domain.Subject;
27  
28  import org.sourceforge.vlibrary.util.Utils;
29  
30  /**
31   * Action used to create or update boooks
32   * @version $Revision$ $Date$
33   */
34  
35  public class EditBookAction extends LibraryAction {
36      
37      /** log4j Logger */
38      private static Logger logger =
39       Logger.getLogger(EditBookAction.class.getName());
40      
41      
42      /**
43       * Sets up Book edit page
44       *
45       * Loads subjectList property of BookForm with ArrayList of all subjects.
46       * If action = create, puts logged on user's deskPhone into deskPhone prop.
47       * If action = update, loads all book data
48       * If action is null and ISBN is present in request, prefill book details;
49       * otherwise assume CREATE.
50       *
51       * @param mapping The ActionMapping used to select this instance
52       * @param form The optional ActionForm bean for this request (if any)
53       * @param request The HTTP request we are processing
54       * @param response The HTTP response we are creating
55       * @param messages message resources
56       *
57       * @exception Exception
58       */
59      public ActionForward executeAction(ActionMapping mapping,
60       ActionForm form,
61       HttpServletRequest request,
62       HttpServletResponse response,
63       MessageResources messages)
64       throws Exception {
65          ArrayList errors = new ArrayList();
66          String action = request.getParameter(Constants.ACTION);
67          String isbn = null;
68          
69          /*  action values:
70           Create  <--->   Create a new book
71           Update  <--->   Edit an existing book
72           null    <---> Prefill book details if request has ISBN; else CREATE
73           */
74          
75          if (action == null) {  
76              Object queryString = request.getParameterMap().get(Constants.ISBN);
77              if ( queryString!= null && 
78                   queryString instanceof String[] &&
79                   ((String[])queryString).length > 0) {
80                  action = Constants.PREFILLBOOKDETAILS;
81                  // Strip "ISBN" prefix and embedded dashes
82                  isbn = ((String[])queryString)[0];
83                  isbn = StringUtils.remove(
84                          StringUtils.removeStart(isbn, "ISBN"), '-').trim();
85              } else {
86                  action = Constants.CREATE;
87              }
88          }
89          
90          BookForm frm = (BookForm) form;
91          
92          // load subjectList
93          try {
94              frm.setSubjectList(libraryManager.getSubjects());
95          } catch (Throwable t) {
96              String errString = messages.getMessage("error.subjects.retrieve");
97              logger.error(errString,t);
98              errors.add(new LibraryException(errString,t));
99              return standardForward(mapping,request,errors); // log and -> error
100         }
101         
102         if (action.equals(Constants.CREATE)) {
103             
104             // Set owner to logged in user's id
105             try {
106                 Reader rd = libraryManager.retrieveByUid(request.getRemoteUser());
107                 //frm.setOwnerPhone(rd.getDeskPhone());
108                 frm.setOwner(rd.getId());
109                 rd = null;
110             } catch (Throwable e) {
111                 String errString = messages.getMessage("error.reader.retrieve");
112                 logger.error(messages.getMessage(errString,e));
113                 errors.add(new LibraryException(errString,e));
114             }
115         }
116         
117         if (action.equals(Constants.UPDATE)) {
118             try {
119                 // Retrieve book
120                 Book bk = new Book();
121                 bk.setId(frm.getId());
122                 bk = libraryManager.retrieveBook(bk.getId());
123                 
124                 // Populate form with basic Book data
125                 PropertyUtils.copyProperties(frm, bk);
126                 
127                 // Add Authors and Subjects
128                 frm.setAuthors(libraryManager.getAuthors( bk));
129                 frm.setSubjects(libraryManager.getSubjects( bk ));
130                 
131                 // Set Owner's desk phone
132                 Reader rd = libraryManager.retrieveReader(bk.getOwner());
133                 frm.setOwnerPhone(rd.getDeskPhone());
134                 rd = null;
135                 
136             } catch (Throwable t) {
137                 String errString = messages.getMessage("error.book.retrieve");
138                 logger.error(messages.getMessage(errString,t));
139                 errors.add(new LibraryException(errString,t));
140             }
141         }
142         
143         if (action.equals(Constants.PREFILLBOOKDETAILS)) {
144             try {
145                 // Get the reader
146                 Reader rd = libraryManager.retrieveByUid(
147                         request.getRemoteUser());
148                 
149                 // Retrieve book
150                 Map bookDetails = libraryManager.lookupBook(isbn);
151                 String title = (String) Utils.getIgnoreCase(
152                         bookDetails, Constants.BOOKTITLE);
153                 String author =  (String) Utils.getIgnoreCase(
154                         bookDetails, Constants.BOOKAUTHOR);
155                 String publisher = (String) Utils.getIgnoreCase(
156                         bookDetails, Constants.BOOKPUBLISHER);
157                 String date = (String) Utils.getIgnoreCase(
158                         bookDetails, Constants.BOOKDATE);
159                 ArrayList subjectStrings = (ArrayList) Utils.getIgnoreCase(
160                         bookDetails, Constants.BOOKSUBJECTSLIST);
161                 
162                 // Populate subjects
163                 ArrayList subjects = new ArrayList();
164                 for (int i = 0; i < subjectStrings.size(); i++) {
165                     subjects.add( new Subject((String)subjectStrings.get(i)));
166                 }
167                 
168                 // Create a book instance to use to populate form
169                 Book bk = new Book( title, 0, publisher, date, "", rd.getId(), isbn);
170                 
171                 // Populate form with basic Book data
172                 PropertyUtils.copyProperties(frm, bk);
173                 
174                 /*
175                  *  Add Authors and Subjects
176                  *  LOC SRU/SRW seems to only retrieve the first author
177                  *  TODO:  tokenize Last and First Names from LOC String
178                  */
179                 ArrayList authors = new ArrayList();
180                 authors.add (new Author(author, "") ); 
181                 frm.setAuthors(authors);               
182                 frm.setSubjects(subjects);
183                 
184                 // Set Owner
185                 frm.setOwner(rd.getId());
186                 
187                 rd = null;
188                 
189             } catch (Throwable t) {
190                 String errString = messages.getMessage("error.book.prefill", t);
191                 logger.error(messages.getMessage(errString,t));
192                 errors.add(new LibraryException(errString,t));
193             }
194         }
195         
196         return standardForward(mapping,request,errors);
197     }
198 }