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.struts.action.ActionForm;
9   import org.apache.struts.action.ActionForward;
10  import org.apache.struts.action.ActionMapping;
11  import org.apache.struts.util.MessageResources;
12  
13  import org.apache.commons.beanutils.PropertyUtils;
14  
15  import org.apache.log4j.Logger;
16  
17  import org.sourceforge.vlibrary.Constants;
18  import org.sourceforge.vlibrary.exceptions.LibraryException;
19  import org.sourceforge.vlibrary.user.domain.Reader;
20  import org.sourceforge.vlibrary.user.forms.ReaderForm;
21  
22  import java.lang.reflect.InvocationTargetException;
23  /**
24   * Action used to prepare reader entry/update form
25   * @version $Revision$ $Date$
26   */
27  
28  public class EditReaderAction extends LibraryAction {
29      
30      /** log4j Logger */
31      private static Logger logger =
32       Logger.getLogger(EditReaderAction.class.getName());
33      
34      
35      /**
36       * If action=Update, retrieves data for existing reader and populates
37       * the incoming ReaderForm with the reader's data.  If action=Create,
38       * does nothing.
39       *
40       * @param mapping The ActionMapping used to select this instance
41       * @param form The optional ActionForm bean for this request (if any)
42       * @param request The HTTP request we are processing
43       * @param response The HTTP response we are creating
44       * @param messages message resources
45       *
46       * @exception Exception
47       */
48      public ActionForward executeAction(ActionMapping mapping,
49       ActionForm form,
50       HttpServletRequest request,
51       HttpServletResponse response,
52       MessageResources messages)
53       throws Exception {
54          
55          ArrayList errors = new ArrayList();
56          String action = request.getParameter(Constants.ACTION);
57          /*  action values:
58           Create  <--->   Create a new reader (Default)
59           Update  <--->   Edit an existing reader
60           */
61          if (action == null) action = Constants.CREATE;
62          
63          if (action.equals(Constants.UPDATE)) {
64              try {
65                  // Retrieve user
66                  Reader rd = new Reader();
67                  ReaderForm frm = (ReaderForm)form;
68                  
69                  /* If this is action is being invoked by an administrator
70                   * use the uid request parameter to look up the reader;
71                   * otherwise use getRemoteUser() to get the uid  */
72                  if ((request.getParameter("uid") != null)
73                  && (request.isUserInRole(Constants.ADMINISTRATOR))) {
74                      rd.setUid(request.getParameter("uid"));
75                  } else {
76                      rd.setUid(request.getRemoteUser());
77                  }
78                  
79                  rd = libraryManager.retrieveByUid( rd.getUid());
80                  
81                  logger.debug("propertyutils:" + frm + ", " + rd);
82                  
83                  PropertyUtils.copyProperties(frm, rd);
84                  
85              } catch (InvocationTargetException e) {
86                  Throwable t = e.getTargetException();
87                  if (t == null)
88                      t = e;
89                  String errString = messages.getMessage
90                   ("error.reader.reflection");
91                  logger.error(errString,t);
92                  errors.add(new LibraryException(errString,t));
93              } catch (Throwable t) {
94                  String errString = messages.getMessage
95                   ("error.reader.retrieve");
96                  logger.error(errString,t);
97                  errors.add(new LibraryException(errString,t));
98              }
99          }
100         return standardForward(mapping,request,errors);
101     }
102 }