Source code: com/clra/web/SaveRowingSessionAction.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: SaveRowingSessionAction.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.RowingException;
13 import com.clra.rowing.RowingSessionLevel;
14 import com.clra.rowing.RowingSessionState;
15 import com.clra.rowing.RowingSessionStateException;
16 import com.clra.rowing.RowingSessionType;
17 import com.clra.rowing.RowingUtils;
18 import java.io.IOException;
19 import java.rmi.RemoteException;
20 import java.text.SimpleDateFormat;
21 import java.util.Calendar;
22 import java.util.Date;
23 import javax.ejb.CreateException;
24 import javax.naming.NamingException;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpSession;
28 import javax.servlet.http.HttpServletResponse;
29 import org.apache.log4j.Category;
30 import org.apache.struts.action.Action;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34
35 /**
36 * A workflow manager that pulls data about a rowing session from
37 * an input form. The data is used to invoke business logic that
38 * creates, edits, publishes, views, deletes or cancels a rowing session.
39 * See the related workflow manager, <tt>EditRowingSessionAction</tt>, which
40 * initializes an input form with data from a rowing session.
41 *
42 * @author <a href="mailto:rphall@pluto.njcc.com>Rick Hall</a>
43 * @version $Revision: 1.4 $ $Date: 2003/02/26 03:38:46 $
44 */
45 public final class SaveRowingSessionAction extends Action {
46
47 private final static String base = SaveRowingSessionAction.class.getName();
48 private final static Category theLog = Category.getInstance( base );
49
50 // Some local aliases, to make code lines shorter
51 private final static String CREATE = RowingSessionForm.CREATE;
52 private final static String EDIT = RowingSessionForm.EDIT;
53 private final static String PUBLISH = RowingSessionForm.PUBLISH;
54 private final static String LOCK = RowingSessionForm.LOCK;
55 private final static String VIEW = RowingSessionForm.VIEW;
56 private final static String SESSIONCANCEL = RowingSessionForm.SESSIONCANCEL;
57 private final static String DELETE = RowingSessionForm.DELETE;
58 private final static String CANCEL = RowingSessionForm.CANCEL;
59 private final static String ROWINGID = Constants.ROWINGSESSION_KEY;
60
61 // Used to format debug and log messages
62 private final static SimpleDateFormat dateFormat =
63 new SimpleDateFormat("MM/dd/yy h:mm a");
64
65 /**
66 * Handle the workflow step in data about a rowing session is pulled
67 * from an input form. The data is used to invoke business logic that
68 * creates, edits, publishes, views, deletes or cancels a rowing session.
69 *
70 * @param mapping The ActionMapping used to select this instance
71 * @param actionForm The optional ActionForm bean for this request (if any)
72 * @param request The HTTP request we are processing
73 * @param response The HTTP response we are creating
74 *
75 * @exception IOException if an input/output error occurs
76 * @exception ServletException if a servlet exception occurs
77 */
78 public ActionForward perform(ActionMapping mapping, ActionForm form,
79 HttpServletRequest request, HttpServletResponse response)
80 throws IOException, ServletException {
81
82 // A null return value indicates that processing should continue
83 ActionForward retVal = null;
84
85 // Extract the workflow
86 HttpSession session = request.getSession();
87 String workflow = request.getParameter("action");
88 if (workflow == null) {
89 // The workflow is cancelled by default
90 theLog.info( "null workflow: cancelled 'SaveRowingSession'" );
91 // FIXME add status message
92 retVal = mapping.findForward("success");
93 }
94 else if ( isCancelled(request) ) {
95 theLog.info("'" + workflow + "' workflow: cancelled 'SaveRowingSession'");
96 // FIXME add status message
97 retVal = mapping.findForward("success");
98 }
99 else {
100 theLog.info( "proceeding with 'SaveRowingSession'" );
101 if ( retVal != null ) { throw new Error( "design error" ); }
102 }
103
104 // Extract the form action
105 RowingSessionForm subform = (RowingSessionForm) form;
106 if ( theLog.isDebugEnabled() ) {
107 StringBuffer sb = new StringBuffer();
108 sb.append( "RowingSessionForm: action == " + subform.getAction() );
109 sb.append( ", rowingId == " + subform.getRowingId() );
110 sb.append( ", state == " + subform.getState() );
111 sb.append( ", date == "
112 + dateFormat.format(subform.getDateTimeAsDateObject()) );
113 sb.append( ", level == " + subform.getLevel() );
114 sb.append( ", type == " + subform.getType() );
115 theLog.debug( new String(sb) );
116 }
117 String formAction = subform.getAction();
118 if ( retVal == null && formAction == null ) {
119 theLog.error( "null formAction" );
120 // FIXME add error message
121 retVal = mapping.findForward("failure");
122 }
123
124 // Perform the action specified by the form
125 if ( retVal == null && formAction != null ) {
126
127 // Trim the (non-null) form action
128 formAction = formAction.trim();
129
130 // Get the id of the targetted rowing session
131 Integer rowingId = (Integer) session.getAttribute(ROWINGID);
132 if ( rowingId == null && !CREATE.equalsIgnoreCase(formAction) ) {
133 String msg = "Missing rowing id for formAction '" + formAction + "'";
134 theLog.error( msg );
135 // response.sendError(HttpServletResponse.SC_BAD_REQUEST,
136 // messages.getMessage("error.norowingId"));
137 // FIXME note that continuing with retVal==null won't work here
138 // return null;
139 //
140 // FIXME append error messages
141 retVal = mapping.findForward("failure");
142 }
143
144 if ( theLog.isDebugEnabled() ) {
145 String msg = "Processing formAction/rowingId"
146 + formAction + "/" + rowingId;
147 theLog.debug( msg );
148 }
149
150 // All required validations were done by the form itself
151
152 try {
153
154 if ( CREATE.equalsIgnoreCase(formAction) ) {
155 rowingId = createSession( subform );
156 // FIXME append status messages
157 }
158 else if ( VIEW.equalsIgnoreCase(formAction) ) {
159 // Nothing to do
160 }
161 else { // (not CREATE, not VIEW)
162
163 IRowingSession irs = RowingUtils.findRowingSession( rowingId );
164
165 if ( EDIT.equalsIgnoreCase(formAction) ) {
166 editSession( irs, subform );
167 }
168 else if ( PUBLISH.equalsIgnoreCase(formAction) ) {
169 irs.publish();
170 }
171 else if ( SESSIONCANCEL.equalsIgnoreCase(formAction) ) {
172 irs.cancel();
173 }
174 else if ( DELETE.equalsIgnoreCase(formAction) ) {
175 irs.delete();
176 }
177 else if ( LOCK.equalsIgnoreCase(formAction) ) {
178 irs.lock();
179 }
180 else { // unknown
181 throw new Error( "design error: formAction == " + formAction );
182 }
183
184 // FIXME append status messages
185
186 } // end else (not CREATE, not VIEW)
187
188 // FIXME et the month attribute so that relevant data is displayed
189 // MonthViewSelectorTag.resetMonthInContexts( request, month );
190 // FIXME set the year attribute
191
192 // Processing is complete.
193 if ( theLog.isDebugEnabled() ) {
194 String msg = "Forwarding to 'success' page:" + rowingId;
195 theLog.debug( msg );
196 }
197 retVal = mapping.findForward("success");
198
199 }
200 catch( Exception x ) {
201 theLog.error( x.getMessage(), x );
202 // FIXME append error messages
203 retVal = mapping.findForward("failure");
204 }
205
206 } // End: Perform the action specified by the form
207
208 // Remove the obsolete form bean and rowingId
209 if (mapping.getAttribute() != null) {
210 if ("request".equals(mapping.getScope())) {
211 request.removeAttribute(mapping.getAttribute());
212 }
213 else {
214 session.removeAttribute(mapping.getAttribute());
215 }
216 }
217 session.removeAttribute(ROWINGID);
218
219 // Forward control to the appropriate page
220 if ( retVal == null ) {
221 String msg = "null retVal for workflow/formAction == '" + workflow
222 + "'/'" + formAction + "'";
223 throw new Error( msg );
224 }
225 if ( theLog.isDebugEnabled() ) {
226 theLog.debug( "Forwarding to " + retVal.toString() );
227 }
228
229 return retVal;
230 } // perform
231
232 private static Integer createSession( RowingSessionForm form )
233 throws RemoteException, CreateException, NamingException, RowingException {
234
235 Date date = form.getCalendar().getTime();
236
237 String s = form.getLevel();
238 RowingSessionLevel level = RowingSessionLevel.getLevel( s );
239
240 s = form.getType();
241 RowingSessionType type = RowingSessionType.getType( s );
242
243 IRowingSession irs = RowingUtils.createRowingSession( date, level, type );
244 Integer retVal = irs.getId();
245 if ( theLog.isInfoEnabled() ) {
246 String msg = "rowing session created: id == " + retVal
247 + ", date == '" + dateFormat.format( date )
248 + "', level == " + level.getName() + ", type == " + type.getName();
249 theLog.info( msg );
250 }
251
252 return retVal;
253 } // createSession(RowingSessionForm)
254
255 private static void editSession( IRowingSession irs, RowingSessionForm form )
256 throws RemoteException, RowingSessionStateException, RowingException {
257
258 Date date = form.getCalendar().getTime();
259 irs.setDate( date );
260
261 String s = form.getLevel();
262 RowingSessionLevel level = RowingSessionLevel.getLevel( s );
263 irs.setLevel( level );
264
265 s = form.getType();
266 RowingSessionType type = RowingSessionType.getType( s );
267 irs.setType( type );
268
269 if ( theLog.isInfoEnabled() ) {
270 String msg = "rowing session edited: id == " + irs.getId()
271 + ", date == '" + dateFormat.format( date )
272 + "', level == " + level.getName() + ", type == " + type.getName();
273 theLog.info( msg );
274 }
275
276 return;
277 } // editSession(Integer,RowingSessionForm)
278
279 } // SaveRowingSessionAction
280
281 /*
282 * $Log: SaveRowingSessionAction.java,v $
283 * Revision 1.4 2003/02/26 03:38:46 rphall
284 * Added copyright and GPL license
285 *
286 * Revision 1.3 2002/03/24 01:51:28 rphall
287 * Locking and participant changes
288 *
289 * Revision 1.2 2002/02/18 18:07:16 rphall
290 * Ran dos2unix to remove ^M (carriage return) from end of lines
291 *
292 * Revision 1.1.1.1 2002/01/03 21:57:28 rphall
293 * Initial load, 5th try, Jan-03-2002 4:57 PM
294 *
295 * Revision 1.10 2001/12/17 00:41:45 rphall
296 * Documentation and whitespace
297 *
298 * Revision 1.9 2001/12/15 02:27:04 rphall
299 * Removed unnecessary import statements
300 *
301 * Revision 1.8 2001/12/10 21:44:06 rphall
302 * Fixed cancellation bug
303 *
304 * Revision 1.7 2001/12/09 20:28:50 rphall
305 * Added fixme related to workflow
306 *
307 * Revision 1.6 2001/12/04 02:47:13 rphall
308 * Added logging. Fixed processing logic. Beginning to work.
309 * Some documentation changes. Some whitespace tinkering.
310 *
311 * Revision 1.5 2001/12/03 17:52:24 rphall
312 * Checkpt: before revision to workflow/formAction logic
313 *
314 * Revision 1.3 2001/12/03 02:29:26 rphall
315 * Checkpt: some stubbing remains
316 *
317 * Revision 1.2 2001/12/02 22:09:44 rphall
318 * Checkpt: started replacing stub with valid implementation
319 *
320 */
321