/*
 * $Id: RegisterNewUserForm.java,v 1.11 2005/02/20 23:34:33 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: RegisterNewUserForm.java,v $
 * Revision 1.11  2005/02/20 23:34:33  marbobo
 * + Bugfix
 *
 * Revision 1.10  2004/11/10 15:02:48  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.9  2004/08/08 01:40:49  marbobo
 * + Refactorings und Behandlung von Fehlermeldungen
 *
 * Revision 1.8  2004/07/20 08:54:33  jonyp
 * added copyright info + cvs log
 *
 *
 */
package vkc.view.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.misc.VKCLogManager;
import vkc.util.StringUtils;
import vkc.util.VKCStringUtils;

public final class RegisterNewUserForm extends ActionForm {
	private String  email          		= "";
	private String  firstname      		= "";
	private String  lastname       		= "";
	private String  username       		= "";

	public void reset( ActionMapping mapping, HttpServletRequest request ) {
		email       = "";
		firstname   = "";
		lastname    = "";
		username	= "";
	}

	/**
	 * Validiert die Eingabefelder und gibt gegebenfalls die Fehler zurück
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public ActionErrors validate( ActionMapping mapping, HttpServletRequest request ) {
		ActionErrors errors = new ActionErrors();		

        if ( request.getParameter( "action.create.user" ) != null ) {
    		if ( getUsername().length() == 0 ) {
    			errors.add( "username", new ActionError( "vkc.administration.error.username.required" ) );
    		}

    		if ( getFirstname().length() == 0 ) {
    			errors.add( "firstname", new ActionError( "vkc.administration.error.firstname.required" ) );
    		}

    		if ( getLastname().length() == 0 ) {
    			errors.add( "lastname", new ActionError( "vkc.administration.error.lastname.required" ) );
    		}

    		if ( getEmail().length() < 6 ) {
    			errors.add( "email", new ActionError( "vkc.administration.error.email.required" ) );
    		}
            
    		try {

    			if ( VKCUserSerializer.isUserExistent( VKCStringUtils.filterUnicodeChars( getUsername() ) ) ) {
    				errors.add( "username.exist", new ActionError( "vkc.administration.error.username.exist" ) );
    			}

    		} catch ( DBException e ) {
    			VKCLogManager.errorLog(this, "", e.getMessage(),e);
    		}
        }
		
		return errors;
	}

	public String getEmail() {
		return email;
	}

	public String getFirstname() {
		return firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public String getUsername() {
		return username;
	}

	public void setEmail( String s ) {
		email = StringUtils.normalize( s );
	}

	public void setFirstname( String s ) {
		firstname = StringUtils.normalize( s );
	}

	public void setLastname( String s ) {
		lastname = StringUtils.normalize( s );
	}

	public void setUsername( String s ) {
		username = StringUtils.normalize( s );
	}
}
