KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > validation > CreateUserValidator


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.validation;
22
23 import org.springframework.validation.Errors;
24 import org.springframework.validation.Validator;
25
26 import com.jaspersoft.jasperserver.api.metadata.user.domain.User;
27 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
28
29 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
30 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl;
31
32 import com.jaspersoft.jasperserver.api.metadata.user.domain.User;
33 import com.jaspersoft.jasperserver.api.metadata.user.service.UserAuthorityService;
34
35 import java.util.*;
36
37 public class CreateUserValidator extends CRUDUserValidator implements Validator {
38
39     //variable for userauth-service
40
protected UserAuthorityService userAuthService;
41
42     /*
43     * method to return authService object
44     * @param
45     * @return: Object
46     */

47     public UserAuthorityService getUserAuthService() {
48         return userAuthService;
49     }
50
51     /*
52     * method to return authService object
53     * @param
54     * @return: void
55     */

56     public void setUserAuthService(UserAuthorityService userAuthService) {
57         this.userAuthService = userAuthService;
58     }
59
60     public boolean supports(Class JavaDoc clazz) {
61         return User.class.isAssignableFrom(clazz);
62     }
63
64     public void validate(Object JavaDoc bean, Errors errors) {
65
66         super.validate(bean, errors);
67
68         User user = (User) bean;
69         ExecutionContext context = new ExecutionContextImpl();
70
71         if(duplicateUser(context, user.getUsername())) {
72             errors.rejectValue("username", null, "username already exists");
73         }
74     }
75
76     /*
77      * function to find if the user is already present or not
78      * @param:
79      * @return: boolean
80      */

81      private boolean duplicateUser(ExecutionContext context, String JavaDoc username) {
82
83          username = username.trim();
84          boolean duplicateUser = false;
85          User user = null;
86          String JavaDoc currUserName = null;
87          List users = userAuthService.getUsers(context, null);
88          ListIterator itr = users.listIterator();
89          while(itr.hasNext()) {
90              user = (User)itr.next();
91              currUserName = user.getUsername().trim();
92              if(currUserName.equalsIgnoreCase(username)) {
93                  duplicateUser = true;
94                  break;
95              }
96          }
97          return duplicateUser;
98      }
99
100 }
101
Popular Tags