KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Collections JavaDoc;
43 import org.apache.commons.lang.StringUtils;
44
45 public class UserAction
46 extends
47   AikpAction {
48
49   // constructors /////////////////////////////////////////////////////////////
50

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

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

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

57   protected AutoIntKeyPersistable createPersistable(
58     OperationContext op ) {
59     return new User();
60   }
61
62   protected void populateForm(
63     DynaActionForm form,
64     AutoIntKeyPersistable persistable ) {
65
66     User user = ( User )persistable;
67
68     form.set( "id", "" + user.getInt( "id" ) );
69     form.set( "firstname", user.getContact().getString( "firstname" ) );
70     form.set( "middlename", user.getContact().getString( "middlename" ) );
71     form.set( "lastname", user.getContact().getString( "lastname" ) );
72     form.set( "company", user.getContact().getString( "company" ) );
73     form.set( "address1", user.getContact().getString( "address1" ) );
74     form.set( "address2", user.getContact().getString( "address2" ) );
75     form.set( "city", user.getContact().getString( "city" ) );
76     form.set( "state", user.getContact().getString( "state" ) );
77     form.set( "zip", user.getContact().getString( "zip" ) );
78     form.set( "country", user.getContact().getString( "country" ) );
79     form.set( "phone", user.getContact().getString( "phone" ) );
80     form.set( "fax", user.getContact().getString( "fax" ) );
81     form.set( "email", user.getContact().getString( "email" ) );
82     form.set( "url", user.getContact().getString( "url" ) );
83     form.set( "password", "" );
84     form.set( "verifypassword", "" );
85   }
86
87   protected void populatePersistable(
88     AutoIntKeyPersistable persistable,
89     DynaActionForm form ) {
90
91     User user = ( User )persistable;
92
93     user.getContact().setString(
94       "firstname", ( String JavaDoc )form.get( "firstname" ) );
95     user.getContact().setString(
96       "middlename", ( String JavaDoc )form.get( "middlename" ) );
97     user.getContact().setString(
98       "lastname", ( String JavaDoc )form.get( "lastname" ) );
99     user.getContact().setString(
100       "company", ( String JavaDoc )form.get( "company" ) );
101     user.getContact().setString(
102       "address1", ( String JavaDoc )form.get( "address1" ) );
103     user.getContact().setString(
104       "address2", ( String JavaDoc )form.get( "address2" ) );
105     user.getContact().setString(
106       "city", ( String JavaDoc )form.get( "city" ) );
107     user.getContact().setString(
108       "state", ( String JavaDoc )form.get( "state" ) );
109     user.getContact().setString(
110       "zip", ( String JavaDoc )form.get( "zip" ) );
111     user.getContact().setString(
112       "country", ( String JavaDoc )form.get( "country" ) );
113     user.getContact().setString(
114       "phone", ( String JavaDoc )form.get( "phone" ) );
115     user.getContact().setString(
116       "fax", ( String JavaDoc )form.get( "fax" ) );
117     user.getContact().setString(
118       "email", ( String JavaDoc )form.get( "email" ) );
119     user.getContact().setString(
120       "url", ( String JavaDoc )form.get( "url" ) );
121
122     if ( !StringUtils.isBlank( ( String JavaDoc )form.get( "password" ) ) )
123       user.setString( "password", ( String JavaDoc )form.get( "password" ) );
124   }
125
126   protected ActionForward getForwardForSave(
127     OperationContext op,
128     Object JavaDoc policy ) {
129
130     return new ActionForward( "/user.do?action=list" );
131   }
132
133   protected ActionForward getForwardForDelete(
134     OperationContext op,
135     Object JavaDoc policy ) {
136
137     return new ActionForward( "/user.do?action=list" );
138   }
139
140   protected ActionForward doCancelSave(
141     OperationContext op,
142     Object JavaDoc policy ) {
143
144     return new ActionForward( "/user.do?action=list" );
145   }
146
147   protected ActionForward doList(
148     OperationContext op,
149     Object JavaDoc policy ) {
150
151     String JavaDoc msg = ( ( RegPolicy )policy ).isListUsersAuthorized( op );
152
153     if ( msg != null ) {
154       StrutsUtil.addMessage(
155         op.request, msg, null, null, null );
156       return op.mapping.findForward( "accessDenied" );
157     }
158
159     ActionForward forward = super.doList( op, policy );
160
161     List JavaDoc list = ( List JavaDoc )op.form.get( "list" );
162
163     //
164
// fully load each user
165
//
166
for ( Iterator JavaDoc iter = ( list ).iterator(); iter.hasNext(); ) {
167       User u = ( User )iter.next();
168       u.load( new IntKey( u.getInt( "id" ) ) );
169     }
170
171     //
172
// sort the users
173
//
174
Collections.sort( list );
175
176     return forward;
177   }
178
179   protected ActionForward doDelete(
180     OperationContext op,
181     Object JavaDoc policy ) {
182
183     String JavaDoc msg = ( ( RegPolicy )policy ).isDeleteUserAuthorized( op );
184
185     if ( msg != null ) {
186       StrutsUtil.addMessage(
187         op.request, msg, null, null, null );
188       return op.mapping.findForward( "accessDenied" );
189     }
190
191     String JavaDoc email =
192       StringUtils.defaultString(
193         op.request.getParameter( "email" ), "" + op.form.get( "id" ) );
194
195     ActionForward forward = super.doDelete( op, policy );
196
197     Event.log(
198       SiteContext.getDefaultContext(),
199       op.user.getLogin(),
200       email,
201       "User deleted." );
202
203     return forward;
204   }
205
206   protected ActionForward doSave(
207     OperationContext op,
208     Object JavaDoc policy ) {
209
210     String JavaDoc msg = ( ( RegPolicy )policy ).isSaveUserAuthorized( op );
211
212     if ( msg != null ) {
213       StrutsUtil.addMessage(
214         op.request, msg, null, null, null );
215       return op.mapping.findForward( "accessDenied" );
216     }
217
218     ActionForward forward = super.doSave( op, policy );
219
220     Event.log(
221       SiteContext.getDefaultContext(),
222       op.user.getLogin(), ( String JavaDoc )op.form.get( "email" ), "User updated." );
223
224     return forward;
225   }
226
227   protected ActionForward doSaveNew(
228     OperationContext op,
229     Object JavaDoc policy ) {
230
231     String JavaDoc msg = ( ( RegPolicy )policy ).isSaveNewUserAuthorized( op );
232
233     if ( msg != null ) {
234       StrutsUtil.addMessage(
235         op.request, msg, null, null, null );
236       return op.mapping.findForward( "accessDenied" );
237     }
238
239     ActionForward forward = super.doSaveNew( op, policy );
240
241     Event.log(
242       SiteContext.getDefaultContext(),
243       op.user.getLogin(), ( String JavaDoc )op.form.get( "email" ), "User created." );
244
245     return forward;
246   }
247
248   protected ActionForward doEdit(
249     OperationContext op,
250     Object JavaDoc policy ) {
251
252     String JavaDoc msg = ( ( RegPolicy )policy ).isEditUserAuthorized( op );
253
254     if ( msg != null ) {
255       StrutsUtil.addMessage(
256         op.request, msg, null, null, null );
257       return op.mapping.findForward( "accessDenied" );
258     }
259
260     return super.doEdit( op, policy );
261   }
262
263   protected ActionForward doNew(
264     OperationContext op,
265     Object JavaDoc policy ) {
266
267     String JavaDoc msg = ( ( RegPolicy )policy ).isNewUserAuthorized( op );
268
269     if ( msg != null ) {
270       StrutsUtil.addMessage(
271         op.request, msg, null, null, null );
272       return op.mapping.findForward( "accessDenied" );
273     }
274
275     return super.doNew( op, policy );
276   }
277
278   // properties ///////////////////////////////////////////////////////////////
279

280   // attributes ///////////////////////////////////////////////////////////////
281
}
282
Popular Tags