
/*
 * Copyright (c) 2009-2010 DuraSpace. All rights reserved.
 */
package org.duracloud.account.annotation;

import java.text.MessageFormat;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder;

import org.duracloud.account.common.domain.DuracloudGroup;
import org.duracloud.account.db.error.UserAlreadyExistsException;
import org.duracloud.account.util.DuracloudUserService;
import org.duracloud.account.util.error.InvalidUsernameException;
import org.duracloud.account.util.error.ReservedPrefixException;
import org.duracloud.account.util.error.ReservedUsernameException;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author "Daniel Bernstein (dbernstein@duraspace.org)"
 *
 */

public class UsernameValidator implements ConstraintValidator<UsernameConstraint, String> {
	
	private static final String INVALID_USERNAME_MESSAGE =  
	    "The username \"{0}\" is invalid. Usernames must contain only lowercase letters, numbers, " +
	    "'-','_','@', '.', and start and end with a letter or number.";
	private static final String RESERVED_USERNAME_MESSAGE = "\"{0}\" is a reserved. Please choose another username.";
    private static final String RESERVED_PREFIX_MESSAGE = "Usernames may not be prefixed by \""+DuracloudGroup.PREFIX+"\". Please choose another username.";

    @Autowired(required=true)
	private DuracloudUserService duracloudUserService;

	/* (non-Javadoc)
	 * @see javax.validation.ConstraintValidator#isValid(java.lang.Object, javax.validation.ConstraintValidatorContext)
	 */
	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
	    try{
	        this.duracloudUserService.checkUsername(value);
	    }catch(InvalidUsernameException ex){
            context.disableDefaultConstraintViolation();
            String message;
            if (ex instanceof ReservedUsernameException) {
                message = RESERVED_USERNAME_MESSAGE;
            } else if (ex instanceof ReservedPrefixException) {
                message = RESERVED_PREFIX_MESSAGE;
            } else {
                message = INVALID_USERNAME_MESSAGE;
            }

            message = MessageFormat.format(message, value);
            ConstraintViolationBuilder cvb =
                context.buildConstraintViolationWithTemplate(message);
            cvb.addConstraintViolation();
            return false;
	    }catch(UserAlreadyExistsException ex){
	        return false;
	    }
	    
	    return true;
	}


    /* (non-Javadoc)
	 * @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
	 */
	@Override
	public void initialize(UsernameConstraint constraintAnnotation) {

	}

	public DuracloudUserService getUserService() {
		return duracloudUserService;
	}



	public void setUserService(DuracloudUserService duracloudUserService) {
		this.duracloudUserService = duracloudUserService;
	}
}

@@ -107,218 +107,8 @@
 n;%0A%0A
-import java.text.MessageFormat;%0A%0Aimport javax.validation.ConstraintValidator;%0Aimport javax.validation.ConstraintValidatorContext;%0Aimport javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder;%0A%0A
 impo
@@ -162,16 +162,16 @@
 dGroup;%0A
+
 import o
@@ -546,16 +546,225 @@
 wired;%0A%0A
+import javax.validation.ConstraintValidator;%0Aimport javax.validation.ConstraintValidatorContext;%0Aimport javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder;%0Aimport java.text.MessageFormat;%0A%0A
 /**%0A * @
@@ -1043,16 +1043,29 @@
 in only 
+%22 +%0A        %22
 lowercas
@@ -1088,18 +1088,8 @@
 rs, 
-%22 +%0A%09    %22
 '-',
@@ -1120,16 +1120,29 @@
 and end 
+%22 +%0A        %22
 with a l
@@ -1216,16 +1216,24 @@
 ESSAGE =
+%0A       
  %22%5C%22%7B0%7D%5C
@@ -1247,16 +1247,21 @@
 reserved
+ name
 . Please
@@ -1345,16 +1345,24 @@
 ESSAGE =
+%0A       
  %22Userna
@@ -1391,17 +1391,19 @@
 d by %5C%22%22
-+
+ + 
 Duraclou
@@ -1415,17 +1415,27 @@
 p.PREFIX
-+
+ +%0A        
 %22%5C%22. Ple

Fixes the reserved username message, updates a bit of formatting.