View Javadoc

1   package org.sourceforge.vlibrary.user.actions;
2   
3   import java.util.ArrayList;
4   
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletResponse;
7   
8   import org.apache.commons.beanutils.PropertyUtils;
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.forms.BookForm;
18  import org.sourceforge.vlibrary.user.valuebeans.BookEditTransaction;
19  
20  /**
21   * Implementation of <strong>Action</strong> that validates and creates or
22   * updates book information entered by the user.
23   * @version $Revision$ $Date$
24   *
25   */
26  
27  public class SaveBookAction extends LibraryAction {
28      /** log4j Logger */
29      private static Logger logger =
30              Logger.getLogger(SaveBookAction.class.getName());
31  
32      /**
33       * Saves newly created book or updates existing book using data on BookForm.
34       *
35       * @param mapping The ActionMapping used to select this instance
36       * @param form The optional ActionForm bean for this request (if any)
37       * @param request The HTTP request we are processing
38       * @param response The HTTP response we are creating
39       * @param messages message resources
40       *
41       * @exception Exception
42       */
43      @Override
44      public ActionForward executeAction(ActionMapping mapping,
45              ActionForm form,
46              HttpServletRequest request,
47              HttpServletResponse response,
48              MessageResources messages)
49                      throws Exception {
50  
51          final ArrayList<LibraryException> errors = new ArrayList<LibraryException>();
52  
53          final BookForm frm = (BookForm) form;
54  
55          String action = request.getParameter(Constants.ACTION);
56  
57          if (action == null) {
58              action = Constants.CREATE;
59          }
60  
61          Book bk = new Book();
62  
63          // Set ids for authors, subjects -- not set by form
64          try {
65              libraryManager.createAuthorIds(frm.getAuthors());
66              libraryManager.createSubjectIds(frm.getSubjects());
67          } catch (final Throwable t) {
68              final String errString = messages.getMessage("error.book.properties");
69              logger.error(errString,t);
70              errors.add(new LibraryException(errString,t));
71              return standardForward(mapping,request,errors); // log and -> error
72          }
73  
74          if (action.equals(Constants.UPDATE)) {
75              // Copy properties from the book form
76              try {
77                  bk.setId(frm.getId());
78                  PropertyUtils.copyProperties(bk, frm);
79              } catch (final NoSuchMethodException e) {
80                  // Gulp -- swallow hard now! (Form Bean has more properties)
81                  logger.debug("Form Bean has more properties, exception ignored:" + e.getMessage());
82              } catch (final Throwable t) {
83                  final String errString = messages.getMessage("error.book.properties");
84                  logger.error(errString,t);
85                  errors.add(new LibraryException(errString,t));
86                  return standardForward(mapping,request,errors);
87              }
88          }
89  
90          // If action is create, need to create a record and set the ID
91          if (action.equals(Constants.CREATE)) {
92              try {
93                  PropertyUtils.copyProperties(bk, frm);
94  
95                  bk = libraryManager.createBook(bk, frm.getSubjects(), frm.getAuthors());
96  
97                  frm.setId(bk.getId());
98              } catch (final Throwable t) {
99                  final String errString = messages.getMessage("error.book.insert");
100                 logger.error(errString,t);
101                 errors.add(new LibraryException(errString,t));
102                 return standardForward(mapping,request,errors);
103             }
104         }
105 
106         // Update book properties in the database
107         try {
108             libraryManager.updateBook(bk );
109         } catch (final Throwable t) {
110             final String errString = messages.getMessage("error.book.update");
111             logger.error(errString,t);
112             errors.add(new LibraryException(errString,t));
113         }
114 
115         /* Put a BookEditTransaction VO into the request for
116            use by the confirmation page. */
117         final BookEditTransaction t = new BookEditTransaction();
118         t.setTitle(bk.getTitle());
119         t.setId(bk.getId());
120         t.setAction(action);
121         request.setAttribute(Constants.BOOK,t);
122         bk = null;
123 
124         return standardForward(mapping,request,errors);
125     }
126 }