Source code: com/clra/web/SaveMemberInfoAction.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: SaveMemberInfoAction.java,v $
5 * $Date: 2003/02/26 03:38:46 $
6 * $Revision: 1.11 $
7 */
8
9 package com.clra.web;
10
11 import com.clra.web.MemberView;
12 import com.clra.web.MemberTag;
13 import com.clra.member.Address;
14 import com.clra.member.AccountType;
15 import com.clra.member.Email;
16 import com.clra.member.MemberName;
17 import com.clra.member.MemberRole;
18 import com.clra.member.IMember;
19 import com.clra.member.IMemberHome;
20 import com.clra.member.MemberUtils;
21 import com.clra.member.MemberSnapshot;
22 import com.clra.member.Telephone;
23 import com.clra.util.ErrorUtils;
24 import com.clra.util.ValidationException;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.Calendar;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Map;
33 import java.util.Set;
34 import javax.ejb.CreateException;
35 import javax.naming.NamingException;
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpSession;
39 import javax.servlet.http.HttpServletResponse;
40 import org.apache.log4j.Category;
41 import org.apache.struts.action.Action;
42 import org.apache.struts.action.ActionError;
43 import org.apache.struts.action.ActionErrors;
44 import org.apache.struts.action.ActionForm;
45 import org.apache.struts.action.ActionForward;
46 import org.apache.struts.action.ActionMapping;
47 import org.apache.struts.action.ActionServlet;
48
49 /**
50 * A workflow manager that pulls a member's personal information from
51 * an input form. The data is used to invoke business logic that
52 * maintains the member information in a database.
53 * See the related workflow manager, <tt>MemberInfoAction</tt>, which
54 * initializes an input form with data for a member.
55 *
56 * @author <a href="mailto:jmstone@nerc.com"> Jan Stone </a>
57 * @see MemberInfoForm
58 * @see EditMemberInfoAction
59 */
60 public final class SaveMemberInfoAction extends Action {
61
62 private final static String base = SaveMemberInfoAction.class.getName();
63 private final static Category theLog = Category.getInstance( base );
64
65 /**
66 * Handle the workflow step in which a form is popluated with date from
67 * a member's data.
68 */
69 public ActionForward perform( ActionMapping mapping, ActionForm form,
70 HttpServletRequest request, HttpServletResponse response )
71 throws IOException, ServletException {
72
73 theLog.info( "Invoked" );
74
75 // A null return value indicates that processing should continue
76 ActionForward retVal = null;
77
78 // Get the form, the form function, and the submission parameter
79 final MemberInfoForm theForm = (MemberInfoForm)(form);
80 final String function = theForm.getFunction();
81 final String submission = request.getParameter("submit");
82 if (submission == null) {
83 theLog.debug("submission = " + submission);
84 if ( theForm.isAdminEdit() ) {
85 retVal = mapping.findForward("browse");
86 } else {
87 retVal = mapping.findForward("success");
88 }
89 }
90
91 Integer memberId = null;
92 MemberName memberName = null;
93 Address memberAddress = null;
94 Map memberPhones = null;
95 Email memberEmail = null;
96 String memberEmailString = null;
97 Date memberBirthDate = null;
98
99 String accountName = null;
100 String accountNameOriginal = null;
101 boolean isAccountNameModified = false;
102 String accountPassword = null;
103 AccountType accountType = null;
104 Date accountDate = null;
105 MemberRole[] accountRoles = null;
106
107 // Retrieve the (relevant) form data
108 if ( retVal == null ) {
109
110 memberId = theForm.getId(); // may be null iff ADMINCREATE
111
112 memberPhones = new HashMap();
113
114 String ac = theForm.getPhoneEveningAreaCode();
115 String ech = theForm.getPhoneEveningExchange();
116 String lcl = theForm.getPhoneEveningLocal();
117 String ext = theForm.getPhoneEveningExt();
118 Telephone phone = Telephone.createTelephone(ac,ech,lcl,ext);
119 memberPhones.put( Telephone.EVENING, phone );
120
121 ac = theForm.getPhoneDayAreaCode();
122 ech = theForm.getPhoneDayExchange();
123 lcl = theForm.getPhoneDayLocal();
124 ext = theForm.getPhoneDayExt();
125 phone = Telephone.createTelephone(ac,ech,lcl,ext);
126 if ( phone != null ) {
127 memberPhones.put( Telephone.DAY, phone );
128 }
129
130 ac = theForm.getPhoneOtherAreaCode();
131 ech = theForm.getPhoneOtherExchange();
132 lcl = theForm.getPhoneOtherLocal();
133 ext = theForm.getPhoneOtherExt();
134 phone = Telephone.createTelephone(ac,ech,lcl,ext);
135 if ( phone != null ) {
136 memberPhones.put( Telephone.OTHER, phone );
137 }
138
139 memberName = new MemberName( theForm.getFirstName(),
140 theForm.getMiddleName(), theForm.getLastName(), theForm.getSuffix() );
141
142 memberAddress = new Address( theForm.getStreet1(), theForm.getStreet2(),
143 theForm.getCity(), theForm.getState(), theForm.getZip() );
144
145 int memberBirthMonth = theForm.getBirthMonthInt();
146 int memberBirthDay = theForm.getBirthDayInt();
147 int memberBirthYear = theForm.getBirthYearInt();
148 Calendar c = Calendar.getInstance();
149 c.set(memberBirthYear, memberBirthMonth-1, memberBirthDay);
150 memberBirthDate = c.getTime();
151
152 memberEmailString = theForm.getEmail();
153 if ( memberEmailString != null ) {
154 memberEmail = new Email( memberEmailString );
155 }
156
157 accountName = theForm.getAccountName();
158 accountNameOriginal = theForm.getAccountNameOriginal();
159 isAccountNameModified =
160 !accountName.equalsIgnoreCase(accountNameOriginal);
161
162 if ( theForm.isAdminEdit() || theForm.isAdminCreate() ) {
163
164 accountPassword = theForm.getAccountPassword();
165 String strAccountType = theForm.getAccountTypeStr();
166 accountType = new AccountType( strAccountType );
167 accountRoles = theForm.getMemberRoles();
168
169 int accountYear = theForm.getAccountYear();
170 Calendar accountCalendar = Calendar.getInstance();
171 accountCalendar.set( accountYear, 0, 1 );
172 accountDate = accountCalendar.getTime();
173
174 } // if ADMINEDIT or ADMINCREATE
175
176 } // if retVal == null
177
178 try {
179
180 // Edit a restricted set of data for an existing member
181 if ( retVal == null && theForm.isMemberEdit() ) {
182
183 IMemberHome mh = MemberUtils.getMemberEJBHome();
184 IMember m = mh.findByPrimaryKey(memberId);
185
186 MemberSnapshot ms = m.getData();
187
188 MemberSnapshot update = new MemberSnapshot(
189 memberId, accountName, ms.getAccountPassword(), ms.getAccountType(),
190 memberName, memberEmail, memberPhones, memberAddress,
191 ms.getAccountDate(), ms.getBirthDate(), ms.getMemberRoles());
192 m.setData( update );
193
194 retVal = mapping.findForward("success");
195 if ( isAccountNameModified ) {
196 request.setAttribute( "accountNameModified", new Boolean(true) );
197 retVal = mapping.findForward("logout");
198 }
199
200 } // end if MEMBEREDIT
201
202 // Edit all data for an existing member
203 else if ( retVal == null && theForm.isAdminEdit() ) {
204
205 IMemberHome mh = MemberUtils.getMemberEJBHome();
206 IMember m = mh.findByPrimaryKey(memberId);
207 MemberSnapshot ms = new MemberSnapshot(
208 memberId, accountName, accountPassword, accountType,
209 memberName, memberEmail, memberPhones, memberAddress,
210 accountDate, memberBirthDate, accountRoles );
211 m.setData( ms );
212
213 retVal = mapping.findForward("browse");
214
215 } // end ADMINEDIT
216
217 // Create a new member
218 else if ( retVal == null && theForm.isAdminCreate() ) {
219
220 Telephone evening = (Telephone)memberPhones.get(Telephone.EVENING);
221 Telephone day = (Telephone) memberPhones.get( Telephone.DAY );
222 Telephone other = (Telephone) memberPhones.get( Telephone.OTHER );
223
224 IMemberHome mh = MemberUtils.getMemberEJBHome();
225 IMember m = mh.create( accountName, accountPassword, accountType,
226 memberName, memberEmail, evening, day, other, memberAddress,
227 accountDate, memberBirthDate, accountRoles );
228
229 retVal = mapping.findForward("success");
230
231 } // end if ADMINCREATE
232
233 // Handle fall-through caused by invalid function values
234 else if ( retVal == null ) {
235 String msg = "invalid function == '" +function+ "'";
236 theLog.error( msg );
237 throw new ServletException( msg );
238 } // end fall-through
239
240 }
241 catch( ServletException x ) {
242 theLog.debug( x.getMessage() );
243 throw x;
244 }
245 catch( Throwable t ) {
246 theLog.error( t );
247 throw new ServletException( t );
248 }
249
250 // Remove the form bean if processing is complete
251 if ( retVal != null && mapping.getAttribute() != null ) {
252
253 if ("request".equals(mapping.getScope())) {
254 request.removeAttribute(mapping.getAttribute());
255 }
256 else {
257 HttpSession session = request.getSession();
258 session.removeAttribute(mapping.getAttribute());
259 }
260
261 }
262
263 return retVal;
264 } // End perform
265
266 } // SaveMemberInfoAction
267
268 /*
269 * $Log: SaveMemberInfoAction.java,v $
270 * Revision 1.11 2003/02/26 03:38:46 rphall
271 * Added copyright and GPL license
272 *
273 * Revision 1.10 2003/02/24 14:19:04 rphall
274 * Fixed workflow when accountName changes
275 *
276 * Revision 1.9 2003/02/24 13:29:54 rphall
277 * Added handling for when accountName changes
278 *
279 * Revision 1.8 2003/02/20 04:55:03 rphall
280 * Changes rippled through from MemberSnapshot, MemberInfoForm
281 *
282 * Revision 1.7 2003/02/19 03:22:56 rphall
283 * Fixed bug with null memberId during MemberEdit
284 *
285 * Revision 1.6 2003/02/18 04:28:26 rphall
286 * Major revision; working for MEMBEREDIT, ADMINEDIT, ADMINCREATE
287 *
288 * Revision 1.5 2003/02/15 04:31:42 rphall
289 * Changes connected to major revision of MemberBean
290 *
291 * Revision 1.4 2003/02/10 05:20:31 rphall
292 * Fixed bug when day/other phone is null; cleaned up debugging
293 *
294 */
295