Source code: com/clra/web/EditRowingSessionAction.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: EditRowingSessionAction.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.IRowingSession;
12 import com.clra.rowing.RowingDBRead;
13 import com.clra.rowing.RowingSessionSnapshot;
14 import com.clra.rowing.RowingUtils;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import javax.servlet.ServletException;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpSession;
21 import javax.servlet.http.HttpServletResponse;
22 import org.apache.log4j.Category;
23 import org.apache.struts.action.Action;
24 import org.apache.struts.action.ActionForm;
25 import org.apache.struts.action.ActionForward;
26 import org.apache.struts.action.ActionMapping;
27
28 /**
29 * A workflow manager that sets up an input form which queries a user for
30 * information needed to create, edit, publish, view, delete or
31 * cancel a rowing session. See the related workflow manager,
32 * <tt>SaveRowingSessionAction</tt>, which pulls information from the
33 * input form and invokes the business logic that does the actual work
34 * of creating, editing, publishing, viewing, deleting or cancelling a
35 * rowing session.
36 *
37 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
38 * @version $Revision: 1.4 $ $Date: 2003/02/26 03:38:46 $
39 * @see RowingSessionForm
40 * @see SaveRowingSessionAction
41 */
42
43 public final class EditRowingSessionAction extends Action {
44
45 private final static String base = EditRowingSessionAction.class.getName();
46 private final static Category theLog = Category.getInstance( base );
47
48 // Some local aliases, to make code lines shorter
49 private final static String CREATE = RowingSessionForm.CREATE;
50 private final static String ROWINGID = Constants.ROWINGSESSION_KEY;
51
52 /**
53 * Handle the workflow step in which a form is popluated with date from
54 * a rowing session data.
55 *
56 * @param mapping The ActionMapping used to select this instance
57 * @param actionForm The optional ActionForm bean for this request (if any)
58 * @param request The HTTP request we are processing
59 * @param response The HTTP response we are creating
60 *
61 * @exception IOException if an input/output error occurs
62 * @exception ServletException if a servlet exception occurs
63 */
64 public ActionForward perform( ActionMapping mapping, ActionForm form,
65 HttpServletRequest request, HttpServletResponse response )
66 throws IOException, ServletException {
67
68 // A null return value indicates that processing should continue
69 ActionForward retVal = null;
70
71 /*
72 * Implementation note: there's a confusing overuse of the word
73 * "action" in two different contexts:
74 *
75 * -- the query parameter "action" which can take two allowed values:
76 * "Submit" and "Cancel".
77 *
78 * -- the form property "action" which can take six allowed values:
79 * CREATE, EDIT, PUBLISH, VIEW, CANCEL and DELETE.
80 *
81 * What can make this particularly confusing is that the form action can
82 * specify CANCEL, which means "mark a rowing session as cancelled",
83 * whereas when the query action specifies "Cancel", then the
84 * workflow should be cancelled without modifying or creating a
85 * rowing session.
86 */
87
88 // Extract the workflow action
89 HttpSession session = request.getSession();
90 String action = request.getParameter("action");
91 if (action == null) {
92 theLog.error( "null action" );
93 // FIXME add error message
94 retVal = mapping.findForward("failure");
95 }
96
97 // Extract rowing id if the rowing session isn't being created
98 Integer rowingId = null;
99 if ( retVal == null && !action.equalsIgnoreCase(CREATE) ) {
100 String strRowingId = request.getParameter(ROWINGID);
101 try {
102 rowingId = new Integer( strRowingId );
103 }
104 catch( Exception x ) {
105 theLog.error( "bad rowingId", x );
106 // FIXME append error messages
107 retVal = mapping.findForward("failure");
108 }
109 }
110
111 if ( theLog.isDebugEnabled() ) {
112 String msg = "Processing action/rowingId" + action + "/" + rowingId;
113 theLog.debug( msg );
114 }
115
116 // Find the rowing session or set up default properties
117 RowingSessionSnapshot snapshot = null;
118 if ( retVal == null ) {
119 try {
120 if (action.equals(CREATE)) {
121 snapshot = RowingUtils.createRowingSessionDefaults();
122 } else {
123 IRowingSession rowingsession = null;
124 rowingsession = RowingUtils.findRowingSession( rowingId );
125 snapshot = rowingsession.getData();
126 }
127 }
128 catch( Exception x ) {
129 String msg = "action/rowingId == " + action + "/" + rowingId;
130 // FIXME append error messages
131 theLog.error( msg, x );
132 rowingId = null;
133 retVal = mapping.findForward("failure");
134 }
135 }
136
137 // (Re-)Store the rowing id as a session attribute
138 if ( rowingId == null ) {
139 session.removeAttribute( ROWINGID );
140 }
141 else {
142 session.setAttribute( ROWINGID, rowingId );
143 }
144
145 // Get the participants signed up for the session (empty by default)
146 Collection participants = new ArrayList();
147 if ( rowingId != null && retVal == null ) {
148 try {
149 participants =
150 RowingDBRead.findParticipant2SnapshotsForRowingSession(rowingId);
151 }
152 catch( Exception x ) {
153 String msg = "unable to load participants for rowingId == " + rowingId;
154 theLog.warn( msg, x );
155 }
156 }
157
158 // Populate the rowing session form
159 if ( retVal == null ) {
160
161 if (form == null) {
162 String msg = " Creating new RowingSessionForm bean under key "
163 + mapping.getAttribute();
164 form = new RowingSessionForm();
165 if ("request".equals(mapping.getScope())) {
166 theLog.info( msg + " in 'request' scope.");
167 request.setAttribute(mapping.getAttribute(), form);
168 }
169 else {
170 theLog.info( msg + " in 'session' scope.");
171 session.setAttribute(mapping.getAttribute(), form);
172 }
173 } // if form == null
174
175 RowingSessionForm subform = (RowingSessionForm) form;
176 subform.setAction(action);
177
178 if ( theLog.isDebugEnabled() ) {
179 theLog.debug(" Populating form from " + snapshot);
180 }
181
182 try {
183 if ( theLog.isDebugEnabled() ) {
184 String msg = "setting action == " + action;
185 theLog.debug( msg );
186 msg = "setting rowingId == " + snapshot.getId();
187 theLog.debug( msg );
188 msg = new FormattedDate("MM/dd/yy h:mm a",snapshot.getDate())
189 .getValue();
190 theLog.debug( msg );
191 msg = "setting state == " + snapshot.getState().getName();
192 theLog.debug( msg );
193 msg = "setting level == " + snapshot.getLevel().getName();
194 theLog.debug( msg );
195 msg = "setting type == " + snapshot.getType().getName();
196 }
197 subform.setAction(action);
198 subform.setRowingId( snapshot.getId() );
199 subform.setDateTimeFromDate( snapshot.getDate() );
200 subform.setState( snapshot.getState().getName() );
201 subform.setLevel( snapshot.getLevel().getName() );
202 subform.setType( snapshot.getType().getName() );
203
204 subform.setParticipants( participants );
205
206 // Processing is complete.
207 if ( theLog.isDebugEnabled() ) {
208 String msg = "Forwarding to 'success' page:" + rowingId;
209 theLog.debug( msg );
210 }
211 retVal = mapping.findForward("success");
212
213 }
214 catch (Throwable t) {
215 String msg = Text.getMessage( "rsf.populate", t.getClass().getName() );
216 theLog.error( msg, t );
217 throw new ServletException(msg,t);
218 }
219
220 } // End: Populate the rowing session form
221
222 // Forward control to the appropriate page
223 if ( retVal == null ) {
224 throw new Error( "design error" );
225 }
226 if ( theLog.isDebugEnabled() ) {
227 theLog.debug( "Forwarding to " + retVal.toString() );
228 }
229
230 return retVal;
231 } // perform
232
233
234 } // EditRowingSessionAction
235
236 /*
237 * $Log: EditRowingSessionAction.java,v $
238 * Revision 1.4 2003/02/26 03:38:46 rphall
239 * Added copyright and GPL license
240 *
241 * Revision 1.3 2002/03/24 01:51:28 rphall
242 * Locking and participant changes
243 *
244 * Revision 1.2 2002/02/18 18:05:51 rphall
245 * Ran dos2unix to remove ^M (carriage return) from end of lines
246 *
247 * Revision 1.1.1.1 2002/01/03 21:57:28 rphall
248 * Initial load, 5th try, Jan-03-2002 4:57 PM
249 *
250 * Revision 1.6 2001/12/15 02:27:04 rphall
251 * Removed unnecessary import statements
252 *
253 */
254