KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > reg > ProfileAction


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.reg;
22
23 import org.apache.struts.action.Action;
24 import org.apache.struts.action.ActionMapping;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.DynaActionForm;
27 import org.apache.struts.action.ActionForward;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import com.methodhead.aikp.AikpAction;
32 import com.methodhead.aikp.AutoIntKeyPersistable;
33 import com.methodhead.aikp.IntKey;
34 import com.methodhead.util.OperationContext;
35 import com.methodhead.sitecontext.SiteContext;
36 import com.methodhead.util.StrutsUtil;
37 import com.methodhead.event.Event;
38 import com.methodhead.auth.AuthUser;
39 import com.methodhead.auth.AuthUtil;
40 import com.methodhead.auth.AuthAction;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import org.apache.commons.lang.StringUtils;
44
45 public class ProfileAction
46 extends
47   AuthAction {
48
49   // constructors /////////////////////////////////////////////////////////////
50

51   // constants ////////////////////////////////////////////////////////////////
52

53   // classes //////////////////////////////////////////////////////////////////
54

55   // methods //////////////////////////////////////////////////////////////////
56

57   protected void populateForm(
58     DynaActionForm form,
59     AutoIntKeyPersistable persistable ) {
60
61     User user = ( User )persistable;
62
63     form.set( "id", "" + user.getInt( "id" ) );
64     form.set( "firstname", user.getContact().getString( "firstname" ) );
65     form.set( "middlename", user.getContact().getString( "middlename" ) );
66     form.set( "lastname", user.getContact().getString( "lastname" ) );
67     form.set( "company", user.getContact().getString( "company" ) );
68     form.set( "address1", user.getContact().getString( "address1" ) );
69     form.set( "address2", user.getContact().getString( "address2" ) );
70     form.set( "city", user.getContact().getString( "city" ) );
71     form.set( "state", user.getContact().getString( "state" ) );
72     form.set( "zip", user.getContact().getString( "zip" ) );
73     form.set( "country", user.getContact().getString( "country" ) );
74     form.set( "phone", user.getContact().getString( "phone" ) );
75     form.set( "fax", user.getContact().getString( "fax" ) );
76     form.set( "email", user.getContact().getString( "email" ) );
77     form.set( "url", user.getContact().getString( "url" ) );
78     form.set( "password", "" );
79     form.set( "verifypassword", "" );
80   }
81
82   protected void populatePersistable(
83     AutoIntKeyPersistable persistable,
84     DynaActionForm form ) {
85
86     User user = ( User )persistable;
87
88     user.getContact().setString(
89       "firstname", ( String JavaDoc )form.get( "firstname" ) );
90     user.getContact().setString(
91       "middlename", ( String JavaDoc )form.get( "middlename" ) );
92     user.getContact().setString(
93       "lastname", ( String JavaDoc )form.get( "lastname" ) );
94     user.getContact().setString(
95       "company", ( String JavaDoc )form.get( "company" ) );
96     user.getContact().setString(
97       "address1", ( String JavaDoc )form.get( "address1" ) );
98     user.getContact().setString(
99       "address2", ( String JavaDoc )form.get( "address2" ) );
100     user.getContact().setString(
101       "city", ( String JavaDoc )form.get( "city" ) );
102     user.getContact().setString(
103       "state", ( String JavaDoc )form.get( "state" ) );
104     user.getContact().setString(
105       "zip", ( String JavaDoc )form.get( "zip" ) );
106     user.getContact().setString(
107       "country", ( String JavaDoc )form.get( "country" ) );
108     user.getContact().setString(
109       "phone", ( String JavaDoc )form.get( "phone" ) );
110     user.getContact().setString(
111       "fax", ( String JavaDoc )form.get( "fax" ) );
112     user.getContact().setString(
113       "email", ( String JavaDoc )form.get( "email" ) );
114     user.getContact().setString(
115       "url", ( String JavaDoc )form.get( "url" ) );
116   }
117
118   protected ActionForward doProfileForm(
119     OperationContext op,
120     Object JavaDoc policy ) {
121
122     populateForm( op.form, ( User )op.user );
123
124     return op.mapping.findForward( "form" );
125   }
126
127   protected ActionForward doProfile(
128     OperationContext op,
129     Object JavaDoc policy ) {
130
131     if ( !StringUtils.isBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
132       return op.mapping.findForward( "cancelled" );
133
134     User user = ( User )op.user;
135
136     populatePersistable( user, op.form );
137
138     user.save();
139
140     StrutsUtil.addMessage(
141       op.request, "reg.profile.profileupdated", null, null, null );
142
143     Event.log(
144       SiteContext.getDefaultContext(),
145       user.getLogin(),
146       "reg",
147       "Updated profile." );
148
149     return new ActionForward( op.mapping.getInput() );
150   }
151
152   protected ActionForward doPasswordForm(
153     OperationContext op,
154     Object JavaDoc policy ) {
155
156     op.form.set( "oldpassword", "" );
157     op.form.set( "password", "" );
158     op.form.set( "verifypassword", "" );
159
160     return op.mapping.findForward( "form" );
161   }
162
163   protected ActionForward doPassword(
164     OperationContext op,
165     Object JavaDoc policy ) {
166
167     if ( !StringUtils.isBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
168       return op.mapping.findForward( "cancelled" );
169
170     User user = ( User )op.user;
171
172     user.setString( "password", ( String JavaDoc )op.form.get( "password" ) );
173     user.save();
174
175     Event.log(
176       SiteContext.getDefaultContext(),
177       user.getLogin(),
178       "reg",
179       "Updated password." );
180
181     StrutsUtil.addMessage(
182       op.request, "reg.profile.passwordupdated", null, null, null );
183
184     return op.mapping.findForward( "success" );
185   }
186
187   public ActionForward doExecute(
188     ActionMapping mapping,
189     ActionForm form,
190     HttpServletRequest JavaDoc request,
191     HttpServletResponse JavaDoc response ) {
192
193     //
194
// get some things we'll need
195
//
196
DynaActionForm dynaForm = ( DynaActionForm )form;
197     Object JavaDoc policy = StrutsUtil.getPolicy( mapping );
198     AuthUser user = AuthUtil.getUser( request );
199
200     OperationContext op =
201       new OperationContext( mapping, dynaForm, request, response, user );
202
203     //
204
// execute the appopriate method
205
//
206
if ( mapping.getPath().equals( "/profileForm" ) ) {
207       return doProfileForm( op, policy );
208     }
209     if ( mapping.getPath().equals( "/profile" ) ) {
210       return doProfile( op, policy );
211     }
212     if ( mapping.getPath().equals( "/passwordForm" ) ) {
213       return doPasswordForm( op, policy );
214     }
215     if ( mapping.getPath().equals( "/password" ) ) {
216       return doPassword( op, policy );
217     }
218
219     throw new RuntimeException JavaDoc(
220       "Unexpected mapping path \"" + mapping.getPath() + "\"." );
221   }
222
223   // properties ///////////////////////////////////////////////////////////////
224

225   // attributes ///////////////////////////////////////////////////////////////
226
}
227
Popular Tags