1 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 ; 30 import javax.servlet.http.HttpServletResponse ; 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 ; 42 import java.util.List ; 43 import org.apache.commons.lang.StringUtils; 44 45 public class ProfileAction 46 extends 47 AuthAction { 48 49 51 53 55 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 )form.get( "firstname" ) ); 90 user.getContact().setString( 91 "middlename", ( String )form.get( "middlename" ) ); 92 user.getContact().setString( 93 "lastname", ( String )form.get( "lastname" ) ); 94 user.getContact().setString( 95 "company", ( String )form.get( "company" ) ); 96 user.getContact().setString( 97 "address1", ( String )form.get( "address1" ) ); 98 user.getContact().setString( 99 "address2", ( String )form.get( "address2" ) ); 100 user.getContact().setString( 101 "city", ( String )form.get( "city" ) ); 102 user.getContact().setString( 103 "state", ( String )form.get( "state" ) ); 104 user.getContact().setString( 105 "zip", ( String )form.get( "zip" ) ); 106 user.getContact().setString( 107 "country", ( String )form.get( "country" ) ); 108 user.getContact().setString( 109 "phone", ( String )form.get( "phone" ) ); 110 user.getContact().setString( 111 "fax", ( String )form.get( "fax" ) ); 112 user.getContact().setString( 113 "email", ( String )form.get( "email" ) ); 114 user.getContact().setString( 115 "url", ( String )form.get( "url" ) ); 116 } 117 118 protected ActionForward doProfileForm( 119 OperationContext op, 120 Object 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 policy ) { 130 131 if ( !StringUtils.isBlank( ( String )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 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 policy ) { 166 167 if ( !StringUtils.isBlank( ( String )op.form.get( "cancel" ) ) ) 168 return op.mapping.findForward( "cancelled" ); 169 170 User user = ( User )op.user; 171 172 user.setString( "password", ( String )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 request, 191 HttpServletResponse response ) { 192 193 DynaActionForm dynaForm = ( DynaActionForm )form; 197 Object policy = StrutsUtil.getPolicy( mapping ); 198 AuthUser user = AuthUtil.getUser( request ); 199 200 OperationContext op = 201 new OperationContext( mapping, dynaForm, request, response, user ); 202 203 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 ( 220 "Unexpected mapping path \"" + mapping.getPath() + "\"." ); 221 } 222 223 225 } 227 | Popular Tags |