Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/clra/web/EditParticipationAction.java


1   /*
2    * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3    * Distributed under the GPL license. See doc/COPYING.
4    * $RCSfile: EditParticipationAction.java,v $
5    * $Date: 2003/02/26 03:38:46 $
6    * $Revision: 1.4 $
7    */
8   
9   package com.clra.web;
10  
11  import com.clra.rowing.ParticipantSnapshot;
12  import com.clra.rowing.RowingSessionSnapshot;
13  import com.clra.rowing.RowingDBRead;
14  import java.io.IOException;
15  import java.util.Collection;
16  import javax.servlet.ServletException;
17  import javax.servlet.http.HttpServletRequest;
18  import javax.servlet.http.HttpSession;
19  import javax.servlet.http.HttpServletResponse;
20  import org.apache.log4j.Category;
21  import org.apache.struts.action.Action;
22  import org.apache.struts.action.ActionForm;
23  import org.apache.struts.action.ActionForward;
24  import org.apache.struts.action.ActionMapping;
25  
26  /**
27   * A workflow manager that sets up an input form which queries a user for
28   * information needed to create, edit, or view participation in a rowing
29   * session. See the related workflow manager, <tt>SaveParticipationAction</tt>,
30   * which pulls information from the input form and invokes the business logic
31   * that does the actual work of creating, editing, or viewing rowing-session
32   * participation.
33   *
34   * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
35   * @version $Revision: 1.4 $ $Date: 2003/02/26 03:38:46 $
36   * @see ParticipationForm
37   * @see SaveParticipationAction
38   */
39  
40  public final class EditParticipationAction extends Action {
41  
42    private final static String base = EditParticipationAction.class.getName();
43    private final static Category theLog = Category.getInstance( base );
44  
45    // Some local aliases, to make code lines shorter
46    private final static String CREATE   = ParticipationForm.CREATE;
47    private final static String EDIT     = ParticipationForm.EDIT;
48    private final static String VIEW     = ParticipationForm.VIEW;
49    private final static String MEMBER   = Constants.USER_KEY;
50    private final static String ROWINGID = Constants.ROWINGSESSION_KEY;
51    private final static String PARTICIPANTID = Constants.PARTICIPANT_KEY;
52  
53    /**
54     * Handle the workflow step in which a form is popluated with date from
55     * a rowing session data.
56     *
57     * @param mapping The ActionMapping used to select this instance
58     * @param actionForm The optional ActionForm bean for this request (if any)
59     * @param request The HTTP request we are processing
60     * @param response The HTTP response we are creating
61     *
62     * @exception IOException if an input/output error occurs
63     * @exception ServletException if a servlet exception occurs
64     */
65    public ActionForward perform( ActionMapping mapping, ActionForm form,
66      HttpServletRequest request, HttpServletResponse response )
67        throws IOException, ServletException {
68  
69      // A null return value indicates that processing should continue
70      ActionForward retVal = null;
71  
72      // Extract the workflow action
73      HttpSession session = request.getSession();
74      String action = request.getParameter("action");
75      if (action == null) {
76        theLog.error( "null action" );
77        // FIXME add error message
78        retVal = mapping.findForward("failure");
79      }
80  
81      Integer memberId = null;
82      Integer rowingId = null;
83      Integer participantId = null;
84  
85      // Extract participant id if the participant isn't being created
86      if ( retVal == null && action.equalsIgnoreCase(EDIT) ) {
87  
88        String strId = request.getParameter(PARTICIPANTID);
89        try {
90          participantId = new Integer( strId );
91        }
92        catch( Exception x ) {
93          theLog.error( "bad participantId", x );
94          // FIXME append error messages
95          retVal = mapping.findForward("failure");
96        }
97      
98        if ( theLog.isDebugEnabled() ) {
99          String msg = "Processing action/participantId"
100             + action + "/" + participantId;
101         theLog.debug( msg );
102       }
103     }
104 
105     // Otherwise use rowing id and member_id to create/find a participant
106     else if ( retVal == null ) {
107 
108       String strId = request.getParameter(ROWINGID);
109       try {
110         rowingId = new Integer( strId );
111       }
112       catch( Exception x ) {
113         theLog.error( "bad rowingId", x );
114         // FIXME append error messages
115         retVal = mapping.findForward("failure");
116       }
117 
118       Object o = request.getSession().getAttribute( MEMBER );
119       try {
120         memberId = ((MemberView) o).getId();
121       }
122       catch( Exception x ) {
123         theLog.error( "bad memberView", x );
124         // FIXME append error messages
125         retVal = mapping.findForward("failure");
126       }
127 
128       if ( theLog.isDebugEnabled() ) {
129         String msg = "Processing action/memberId/rowingId"
130             + action + "/" + memberId + "/" + rowingId;;
131         theLog.debug( msg );
132       }
133     }
134 
135     // Get snapshots of the current participant and rowing session
136     ParticipantSnapshot ps = null;
137     RowingSessionSnapshot rss = null;
138     if ( retVal == null ) {
139       try {
140         if ( participantId != null ) {
141           // FIXME assert action == EDIT
142           ps = RowingDBRead.loadParticipant(participantId);
143           memberId = ps.getMemberId();
144           rowingId = ps.getRowingId();
145         }
146         else {
147           ps = RowingDBRead.loadParticipant(memberId,rowingId);
148           if ( ps != null ) {
149             // FIXME assert action == VIEW
150             participantId = ps.getParticipantId();
151           }
152           else {
153             // FIXME assert action == CREATE
154           }
155         }
156         rss = RowingDBRead.loadRowingSession(rowingId);
157       }
158       catch( Exception x ) {
159         String msg = "action/memberId/rowingId/participantId == "
160             + action + "/" + memberId + "/" + rowingId + "/" + participantId;
161         theLog.debug( msg );
162         // FIXME append error messages
163         theLog.error( msg, x );
164         rowingId = null;
165         participantId = null;
166         retVal = mapping.findForward("failure");
167       }
168     }
169 
170     // (Re-)Store the participant and rowing id as a session attribute
171     if ( participantId == null ) {
172       session.removeAttribute( PARTICIPANTID );
173     }
174     else {
175       session.setAttribute( PARTICIPANTID, participantId );
176     }
177     if ( rowingId == null ) {
178       session.removeAttribute( ROWINGID );
179     }
180     else {
181       session.setAttribute( ROWINGID, rowingId );
182     }
183 
184     // Get the participants signed up for the session
185     Collection participants = null;
186     if ( retVal == null ) {
187       try {
188         participants =
189           RowingDBRead.findParticipant2SnapshotsForRowingSession(rowingId);
190       }
191       catch( Exception x ) {
192         String msg = "unable to load participants for rowingId == " + rowingId;
193         theLog.error( msg, x );
194         // FIXME append error messages
195         retVal = mapping.findForward("failure");
196       }
197     }
198 
199     // Populate the participation form
200     if ( retVal == null ) {
201 
202       if (form == null) {
203         form = new ParticipationForm();
204         if ("request".equals(mapping.getScope())) {
205           request.setAttribute(mapping.getAttribute(), form);
206         }
207         else {
208           session.setAttribute(mapping.getAttribute(), form);
209         }
210       } // if form == null
211 
212       ParticipationForm subform = (ParticipationForm) form;
213 
214       try {
215 
216         subform.setAction(action);
217         subform.setMemberId( memberId );
218         subform.setRowingId( rowingId );
219         subform.setParticipantId( participantId );
220 
221         subform.setDateTimeFromDate( rss.getDate() );
222         subform.setState( rss.getState().getName() );
223         subform.setLevel( rss.getLevel().getName() );
224         subform.setType( rss.getType().getName() );
225 
226         String strSeatPreference = null;
227         if ( ps != null && ps.getSeatPreference() != null ) {
228           strSeatPreference = ps.getSeatPreference().getName();
229         }
230         subform.setSeatPreference( strSeatPreference );
231 
232         subform.setParticipants( participants );
233 
234         // Processing is complete.
235         if ( theLog.isDebugEnabled() ) {
236           String msg = "Forwarding to 'success' page:" + rowingId;
237           theLog.debug( msg );
238         }
239         retVal = mapping.findForward("success");
240 
241       }
242       catch (Throwable t) {
243         String msg = Text.getMessage( "rsf.populate", t.getClass().getName() );
244         theLog.error( msg, t );
245         throw new ServletException(msg,t);
246       }
247 
248     } // End: Populate the rowing session form
249 
250     // Forward control to the appropriate page
251     if ( retVal == null ) {
252       throw new Error( "design error" );
253     }
254     if ( theLog.isDebugEnabled() ) {
255       theLog.debug( "Forwarding to " + retVal.toString() );
256     }
257 
258     return retVal;
259   } // perform
260 
261 
262 } // EditParticipationAction
263 
264 /*
265  * $Log: EditParticipationAction.java,v $
266  * Revision 1.4  2003/02/26 03:38:46  rphall
267  * Added copyright and GPL license
268  *
269  * Revision 1.3  2002/03/24 01:52:30  rphall
270  * Corrected comment
271  *
272  * Revision 1.2  2002/02/18 18:05:47  rphall
273  * Ran dos2unix to remove ^M (carriage return) from end of lines
274  *
275  * Revision 1.1.1.1  2002/01/03 21:57:28  rphall
276  * Initial load, 5th try, Jan-03-2002 4:57 PM
277  *
278  * Revision 1.3  2001/12/15 05:24:37  rphall
279  * Fixed bug in handling View requests
280  *
281  * Revision 1.2  2001/12/15 02:30:52  rphall
282  * Checkpt: compiles, starting debugging
283  *
284  */
285