KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > user > validator > ValidUserValidator


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.faces.user.validator;
6
7 import javax.faces.component.UIComponent;
8 import javax.faces.context.FacesContext;
9 import javax.faces.validator.Validator;
10 import javax.faces.validator.ValidatorException;
11
12 import org.exoplatform.container.PortalContainer ;
13 import org.exoplatform.services.organization.OrganizationService ;
14 import org.exoplatform.faces.application.ExoFacesMessage;
15 import org.exoplatform.faces.core.component.UIInput;
16 /**
17  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
18  * @since Aug 13, 2004
19  * @version $Id: EmptyStringValidator.java,v 1.4 2004/08/19 14:53:44 tuan08 Exp $
20  */

21 public class ValidUserValidator implements Validator {
22   public void validate(FacesContext context, UIComponent component,
23                        Object JavaDoc value) throws ValidatorException {
24     String JavaDoc s = (String JavaDoc) value ;
25     if(s == null || s.length() == 0) {
26       UIInput uiInput = (UIInput) component ;
27       Object JavaDoc[] args = {uiInput.getName(), s} ;
28       throw new ValidatorException(new ExoFacesMessage("#{ValidUserValidator.msg.empty-input}", args)) ;
29     }
30     PortalContainer container = PortalContainer.getInstance() ;
31     OrganizationService service =
32       (OrganizationService) container.getComponentInstanceOfType(OrganizationService.class) ;
33     try {
34       if(service.findUserByName(s) == null) {
35         UIInput uiInput = (UIInput) component ;
36         Object JavaDoc[] args = {uiInput.getName(), s} ;
37         throw new ValidatorException(new ExoFacesMessage("#{ValidUserValidator.msg.invalid-username}", args)) ;
38       }
39     } catch(Exception JavaDoc ex) {
40       throw new ValidatorException(new ExoFacesMessage(ex.getMessage())) ;
41     }
42   }
43 }
Popular Tags