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.log4j.Logger;
9   import org.apache.struts.action.Action;
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.logic.LibraryManager;
17  
18  /**
19   *
20   * Abstract base Action class. <br>
21   * All vlibrary Actions are derived from this class.
22   * @version $Revision$ $Date$
23   */
24  public abstract class LibraryAction extends Action {
25  
26      /** log4j category */
27      private static Logger logger =
28              Logger.getLogger(LibraryAction.class.getName());
29  
30      /** Message Resources */
31      private static MessageResources logMessages =
32              MessageResources.getMessageResources(Constants.APPLICATION_RESOURCES);
33  
34      protected LibraryManager libraryManager = null;
35  
36      /******************************************************************************
37       * Used for Spring Dependency Injection
38       *****************************************************************************/
39      public void setLibraryManager( LibraryManager libraryManager ) {
40          this.libraryManager = libraryManager;
41      }
42  
43      /**
44       * Execute method invoked by the struts framework.
45       * Gets message resources, handles cancellation, forwarding, basic logging
46       * and top level exception management.
47       * Delegates action processing to executeAction() method overridden by
48       * concrete Actions.
49       *
50       * @param mapping The ActionMapping used to select this instance
51       * @param form The optional ActionForm bean for this request (if any)
52       * @param request The HTTP request we are processing
53       * @param response The HTTP response we are creating
54       *
55       * @exception Exception
56       */
57      @Override
58      public ActionForward execute(ActionMapping mapping,
59              ActionForm form,
60              HttpServletRequest request,
61              HttpServletResponse response)
62                      throws Exception {
63  
64          if (logger.isDebugEnabled()) {
65              logger.debug(logMessages.getMessage("entering.execute")
66                      + "--" + mapping.getType() + " Action = " + request.getParameter(Constants.ACTION));
67          }
68  
69          logger.debug("libraryManager=" + libraryManager);
70  
71          if (isCancelled(request)) {
72              // Remove form bean and forward to cancellation target
73              if (mapping.getAttribute() != null) {
74                  request.removeAttribute(mapping.getAttribute());
75              }
76  
77              if (logger.isDebugEnabled()) {
78                  logger.debug(logMessages.getMessage("action.cancelled")
79                          + "--" + mapping.getType());
80              }
81  
82              return (mapping.findForward(Constants.CANCEL));
83          }
84  
85          final MessageResources messages = getResources(request);
86  
87          final ActionForward result =
88                  executeAction(mapping,form,request,response,messages);
89  
90          if (logger.isDebugEnabled()) {
91              if (result.getName().equals(Constants.ERROR)) {
92                  logger.debug(logMessages.getMessage("execute.failure")
93                          + "--"+ mapping.getType());
94              } else {
95                  logger.debug(logMessages.getMessage("execute.successful")
96                          + "--" + mapping.getType());
97              }
98          }
99  
100         return result;
101 
102     }
103 
104     /**
105      * Standard forward determination utility method
106      * Forwards to SUCCESS if errors ArrayList is empty, else to ERROR
107      *
108      * @param mapping The ActionMapping used to select this instance
109      * @param request The HTTP request we are processing
110      * @param errors The accumulated list of errors
111      *
112      */
113     protected ActionForward standardForward(ActionMapping mapping,
114             HttpServletRequest request,
115             ArrayList<LibraryException> errors) {
116         // If errors exist, direct to error page, else to success
117         if (!errors.isEmpty()) {
118             request.setAttribute(Constants.ERRORS,errors);
119             return (mapping.findForward(Constants.ERROR));
120         } else {
121             return (mapping.findForward(Constants.SUCCESS));
122         }
123     }
124 
125     /**
126      * Abstract action execution method to be overridden by concrete
127      * LibraryActions.
128      *
129      * @param mapping The ActionMapping used to select this instance
130      * @param form The optional ActionForm bean for this request (if any)
131      * @param request The HTTP request we are processing
132      * @param response The HTTP response we are creating
133      * @param messages message resources
134      *
135      * @exception Exception
136      */
137     abstract public ActionForward executeAction(ActionMapping mapping,
138             ActionForm form,
139             HttpServletRequest request,
140             HttpServletResponse response,
141             MessageResources messages)
142                     throws Exception;
143 
144 }