KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > action > UserAction


1 package org.javabb.action;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.javabb.action.infra.BaseAction;
9 import org.javabb.infra.UserContext;
10 import org.javabb.infra.Utils;
11 import org.javabb.transaction.CategoryTransaction;
12 import org.javabb.transaction.UserTransaction;
13 import org.javabb.vh.Stats;
14 import org.javabb.vo.User;
15
16 /*
17  * Copyright 2004 JavaFree.org
18  *
19  * Licensed under the Apache License, Version 2.0 (the "License");
20  * you may not use this file except in compliance with the License.
21  * You may obtain a copy of the License at
22  *
23  * http://www.apache.org/licenses/LICENSE-2.0
24  *
25  * Unless required by applicable law or agreed to in writing, software
26  * distributed under the License is distributed on an "AS IS" BASIS,
27  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  * See the License for the specific language governing permissions and
29  * limitations under the License.
30  */

31
32 /**
33  * $Id: UserAction.java,v 1.35.2.1.2.2.2.5 2006/08/14 14:26:25 daltoncamargo Exp $
34  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org </a> <br>
35  * @author Ronald Tetsuo Miura <br>
36  * @author Lucas Teixeira - <a HREF="mailto:lucas@javabb.org">lucas@javabb.org </a> <br>
37  */

38
39 public class UserAction extends BaseAction {
40     /**
41      *
42      */

43     private static final long serialVersionUID = 1L;
44
45     /**
46      * User to be bind with Action
47      */

48     private User _user = new User();
49
50     /**
51      * {@link org.javabb.transaction.Transaction Transaction}object to perform operations with and
52      * for users.
53      */

54     private UserTransaction userTransaction;
55
56     private CategoryTransaction _categoryTransaction;
57     
58     /**
59      * {@link ArrayList List}which will be contains all users returned from a search.
60      */

61     private List JavaDoc lstUsersPortal = new ArrayList JavaDoc();
62
63     /**
64      * Point out if user decide to remain loged on or not. The accepted values are: <ul><li>0 -
65      * Not persitent login;</li><li>1 - Persistent login.</li></ul> The default value is 1.
66      */

67     private int cookie = 1;
68
69     // Variável para indicar no View que foram encontrados
70
// erros no Controller
71

72     /**
73      * ??? TODO Documentar...
74      */

75     private String JavaDoc invalidData;
76     
77     private String JavaDoc invalidCaptcha;
78
79     // Erros de registro/modificação de usuário
80

81     /**
82      * TODO Documentar...
83      */

84     private String JavaDoc userNameInUse;
85
86     /**
87      * If user email are be use
88      */

89     private String JavaDoc userEmailInUse;
90
91     /**
92      * Defines how to sort users.
93      */

94     private String JavaDoc sortBy;
95
96     /**
97      * Defines if sort is asc or desc
98      */

99     private String JavaDoc sortOrder;
100     
101     private Stats stats = new Stats();
102     
103     /**
104      * Field used on finding users
105      */

106     private String JavaDoc username;
107     
108     /**
109      * Field used on hash user
110      */

111     private String JavaDoc userHash;
112     
113     
114     private List JavaDoc userRanks = new ArrayList JavaDoc();
115     
116 // Upload Parameters
117
private File JavaDoc file;
118     private String JavaDoc contentType;
119     private String JavaDoc filename;
120
121     /**
122      * @param cookie The cookie to set.
123      */

124     public void setCookie(int cookie) {
125         this.cookie = cookie;
126     }
127
128     /**
129      * @param invalidData The invalidData to set.
130      */

131     public void setInvalidData(String JavaDoc invalidData) {
132         this.invalidData = invalidData;
133     }
134
135     /**
136      * @param userEmailInUse The userEmailInUse to set.
137      */

138     public void setUserEmailInUse(String JavaDoc userEmailInUse) {
139         this.userEmailInUse = userEmailInUse;
140     }
141
142     /**
143      * @param userNameInUse The userNameInUse to set.
144      */

145     public void setUserNameInUse(String JavaDoc userNameInUse) {
146         this.userNameInUse = userNameInUse;
147     }
148
149     /**
150      * @param sortBy The sortBy to set.
151      */

152     public void setSortBy(String JavaDoc sortBy) {
153         this.sortBy = sortBy;
154     }
155
156     /**
157      * @param sortOrder The sortOrder to set.
158      */

159     public void setSortOrder(String JavaDoc sortOrder) {
160         this.sortOrder = sortOrder;
161     }
162
163     // ####################################################################
164
// Dependencies
165
// ####################################################################
166

167     /**
168      * @param userTransaction The model to set.
169      */

170     public void setUserTransaction(UserTransaction userTransaction) {
171         this.userTransaction = userTransaction;
172     }
173
174     /**
175      * @param categoryTransaction
176      */

177     public void setCategoryTransaction(CategoryTransaction categoryTransaction) {
178         this._categoryTransaction = categoryTransaction;
179     }
180     
181     
182     
183     
184     // ####################################################################
185
// Actions
186
// ####################################################################
187

188     /**
189      * @return Action status
190      */

191     public String JavaDoc loadUsersPortal() {
192         stats.setTotalPostCount(new Integer JavaDoc((int) _categoryTransaction.countAllPosts()));
193         _user = userTransaction.getUser(_userId);
194         return SUCCESS;
195     }
196
197     /**
198      * @return Action status
199      */

200     public String JavaDoc deleteUsersPortal() {
201         userTransaction.deleteUser(_user);
202         setUrl("user_management.jbb");
203         return SUCCESS;
204     }
205
206     public String JavaDoc viewLegend(){
207         userRanks = userTransaction.getUserRanks();
208         return SUCCESS;
209     }
210     
211     /**
212      * @return Action status
213      */

214     public String JavaDoc loadEditUsersPortal() {
215         _user = UserContext.getContext().getUser();
216         _user = userTransaction.getUser(_user.getId());
217         return SUCCESS;
218     }
219
220     /**
221      * @return Action status
222      */

223     public String JavaDoc updateUsersPortal() {
224         if ((_user.getEmail() != null) && !"".equals(_user.getEmail())) {
225             //Verify if the email already be registered for other user
226
if (userTransaction.isEmailValid(_user.getEmail(), _user.getIdUser())) {
227                 //Throw Error
228
invalidData = "y";
229                 userEmailInUse = "y";
230                 return INPUT;
231             }
232         }
233
234         _user = userTransaction.updateProfileUser(_user, false);
235         
236         setUrl("show_edit_profile.jbb");
237
238         // Updating the changes of user on the session
239
UserContext.getContext().deauthenticate();
240         UserContext.getContext().setUser(_user);
241
242         return SUCCESS;
243     }
244
245     
246     public String JavaDoc updateUserByAdmin() {
247         // Obtain the logged user
248

249         if ((_user.getEmail() != null) && !"".equals(_user.getEmail())) {
250             //Verify if the email already be registered for other user
251
if (userTransaction.isEmailValid(_user.getEmail(), _user.getIdUser())) {
252                 //Throw Error
253
invalidData = "y";
254                 userEmailInUse = "y";
255                 return INPUT;
256             }
257         }
258
259         _user = userTransaction.updateProfileUser(_user, true);
260
261         return SUCCESS;
262     }
263     
264     
265     /**
266      * @return Action status
267      */

268     public String JavaDoc manageAdmRights() {
269         userTransaction.updateAmRights(_user);
270         return SUCCESS;
271     }
272
273     /**
274      * @return Action status
275      */

276     public String JavaDoc insertUsersPortal() throws Exception JavaDoc{
277         User u = userTransaction.findUserByUserName(_user.getUser());
278
279         
280         //Code captcha is invalid
281
if(!checkCaptcha()){
282             invalidData = "y";
283             invalidCaptcha = "y";
284             return INPUT;
285         }
286         
287         if (u != null) {
288             // Informa o tipo de erro
289
invalidData = "y";
290             userNameInUse = "y";
291
292             return INPUT;
293         }
294
295         if (userTransaction.isEmailValid(_user.getEmail())) {
296             // Informa o tipo de erro
297
invalidData = "y";
298             userEmailInUse = "y";
299             return INPUT;
300         }
301
302         _user.setWebsite(Utils.validateWebSite(_user.getWebsite()));
303         _user.setDataRegistro(new Date JavaDoc());
304         _user.setPasswordHash(Utils.encrypt(_user.getPasswordHash()));
305         _user.setAdmin(new Integer JavaDoc(0));
306         _user.setUser_posts(new Long JavaDoc(0));
307 // _user.setUserCode(Utils.getCodeUser(_user.getUser()));
308

309         userTransaction.createUser(_user);
310         setUrl("forum.jbb");
311
312         return SUCCESS;
313     }
314
315     /**
316      * @return Action status
317      */

318     public String JavaDoc verifyLogin() {
319         log.debug("Verify Login");
320
321         
322         if(_user.getAdmin() != null){
323             _user.setAdmin(null);
324         }
325         
326         // DI by Spring
327
// model = new UserTransaction();
328
_user.setPasswordHash(Utils.encrypt(_user.getPasswordHash()));
329         _user = userTransaction.verifyLogin(_user.getUser(), _user.getPasswordHash());
330
331         if (_user == null) {
332             // Login inválido, vai inserir o erro na tela
333
invalidData = "invalidData";
334
335             return LOGIN;
336         }
337
338         // Login OK, bean do usuário vai pra sessão
339
UserContext.getContext().setUser(_user);
340         setSessionAttribute("jbbRemoveCookie", "0");
341
342         // Limpa a variável, pois o login é válido
343
invalidData = null;
344
345         // Usuário optou por permanecer logado
346
// será inserido um cookie para isso
347
if (cookie == 1) {
348             /*
349              * JbbCookie jbbCookie = new JbbCookie(); jbbCookie.addCookie(usersPortal,
350              * ServletActionContext.getResponse());
351              * jbbCookie.getCookie(ServletActionContext.getRequest());
352              */

353         }
354
355         if (getSessionAttribute("jbbUrlBeforeLogin") != null) {
356             // Verifica se o usuário clicou em algum link
357
// e foi solicitado para que ele se logasse
358
return INPUT;
359         }
360
361         // Caso contrário, ele manda para o index do fórum
362
return SUCCESS;
363     }
364
365     /**
366      * @return Action status
367      */

368     public String JavaDoc logout() {
369         // Remove o bean do usuário da sessão
370
setSessionAttribute("jbbRemoveCookie", "1");
371
372         // TODO A linha acima não deveria ser
373
// insertSession("jbbRemoveCookie", String.valueOf(cookie));
374
UserContext.getContext().deauthenticate();
375         removeSessionAttribute("pages");
376         
377         setUrl("forum.jbb?javaBBId=" + System.currentTimeMillis());
378
379         return SUCCESS;
380     }
381
382     /**
383      * @return result
384      */

385     public String JavaDoc listUsersByWhere() {
386         lstUsersPortal = userTransaction.listUsersByWhere(_user, getPage());
387
388         // usado para exibir ou não a paginação
389
if (_user.getUser() != null && !_user.getUser().equals("")) {
390             setTemp(null);
391         } else {
392             setTemp("!");
393         }
394
395         return SUCCESS;
396     }
397     
398     /**
399      * @return result
400      */

401     public String JavaDoc findUserByUserName() {
402         User u = userTransaction.findUserByUserName(_user.getName());
403         if(u == null){
404             //se não encontrar nenhum user seto o id pra -1 para exibir a menssagem de user invalido
405
_user.setIdUser(new Long JavaDoc(-1));
406         }else{
407             userTransaction.sendSecurityCode(u);
408         }
409         return SUCCESS;
410     }
411     
412     public String JavaDoc verifyForgetPwd(){
413         userTransaction.verifyForgetPwd(_userId, userHash);
414         setUrl("forum.jbb");
415         return SUCCESS;
416     }
417
418     /**
419      * @return result
420      */

421     public String JavaDoc listAllRegisteredUsers() {
422         lstUsersPortal = userTransaction.listAllRegisteredUsers(getSortBy(),
423             getSortOrder(),
424             getPage());
425
426         return SUCCESS;
427     }
428     
429     public String JavaDoc listByUserName() {
430         if (username != null && !"".equals(username)){
431             lstUsersPortal = userTransaction.findUserListByUserName(username);
432         }
433         return SUCCESS;
434     }
435
436     public String JavaDoc uploadProfileImage() throws Exception JavaDoc{
437         log.debug(this.filename);
438         setUrl("show_edit_profile.jbb?");
439         return SUCCESS;
440     }
441     
442     // ####################################################################
443
// View objects accessors
444
// ####################################################################
445

446     /**
447      * @return Returns the lstUsersPortal.
448      */

449     public List JavaDoc getLstUsersPortal() {
450         return lstUsersPortal;
451     }
452
453     /**
454      * @return Returns the usersPortal.
455      */

456     public User getUser() {
457         return _user;
458     }
459
460     /**
461      * @return Returns the invalidData.
462      */

463     public String JavaDoc getInvalidData() {
464         return invalidData;
465     }
466
467     /**
468      * @return Returns the cookie.
469      */

470     public int getCookie() {
471         return cookie;
472     }
473
474     /**
475      * @return Returns the userEmailInUse.
476      */

477     public String JavaDoc getUserEmailInUse() {
478         return userEmailInUse;
479     }
480
481     /**
482      * @return Returns the userNameInUse.
483      */

484     public String JavaDoc getUserNameInUse() {
485         return userNameInUse;
486     }
487
488     /**
489      * @return Returns the sortBy.
490      */

491     public String JavaDoc getSortBy() {
492         return sortBy;
493     }
494
495     /**
496      * @return Returns the sortOrder.
497      */

498     public String JavaDoc getSortOrder() {
499         return sortOrder;
500     }
501
502     /**
503      * @return Returns the stats.
504      */

505     public Stats getStats() {
506         return stats;
507     }
508     /**
509      * @return Returns the username.
510      */

511     public String JavaDoc getUsername() {
512         return username;
513     }
514     /**
515      * @param username The username to set.
516      */

517     public void setUsername(String JavaDoc username) {
518         this.username = username;
519     }
520     /**
521      * @return Returns the userHash.
522      */

523     public String JavaDoc getUserHash() {
524         return userHash;
525     }
526     /**
527      * @param userHash The userHash to set.
528      */

529     public void setUserHash(String JavaDoc userHash) {
530         this.userHash = userHash;
531     }
532
533     public List JavaDoc getUserRanks() {
534         return userRanks;
535     }
536
537     public void setUserRanks(List JavaDoc userRanks) {
538         this.userRanks = userRanks;
539     }
540
541     public String JavaDoc getContentType() {
542         return contentType;
543     }
544
545     public void setContentType(String JavaDoc contentType) {
546         this.contentType = contentType;
547     }
548
549     public File JavaDoc getFile() {
550         return file;
551     }
552
553     public void setFile(File JavaDoc file) {
554         this.file = file;
555     }
556
557     public String JavaDoc getFilename() {
558         return filename;
559     }
560
561     public void setFilename(String JavaDoc filename) {
562         this.filename = filename;
563     }
564
565     public String JavaDoc getInvalidCaptcha() {
566         return invalidCaptcha;
567     }
568
569     public void setInvalidCaptcha(String JavaDoc invalidCaptcha) {
570         this.invalidCaptcha = invalidCaptcha;
571     }
572 }
Popular Tags