/*
 * $Id: ForgotPasswordForm.java,v 1.9 2005/02/20 23:43:52 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: ForgotPasswordForm.java,v $
 * Revision 1.9  2005/02/20 23:43:52  marbobo
 * + Unicode-Anpassung
 *
 * Revision 1.8  2004/08/08 01:40:49  marbobo
 * + Refactorings und Behandlung von Fehlermeldungen
 *
 * Revision 1.7  2004/07/20 08:53:52  jonyp
 * added copyright info + cvs log
 *
 *
 */
package vkc.view.form;

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.entities.VKCUser;
import vkc.db.serializer.VKCUserSerializer;

import vkc.util.StringUtils;
import vkc.util.TraceLogger;
import vkc.util.VKCStringUtils;

import vkc.view.WebShare;

import javax.servlet.http.HttpServletRequest;


public final class ForgotPasswordForm extends ActionForm {
    private static Object wrongAuthenthication = new Object();
    private String email 	= "";
    private String username	= "";

    public void reset( ActionMapping mapping, HttpServletRequest request ) {
        email = "";
        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.send.password" ) != null ) {
            try {
                if ( getUsername().length() == 0 ) {
                    errors.add( "username", new ActionError( "vkc.administration.error.username.required" ) );
                }

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

                VKCUser user = null;

                try {
                    user = VKCUserSerializer.retrieveUser( VKCStringUtils.filterUnicodeChars( getUsername() ) );
                } catch ( Exception ex ) {
                    servlet.log( "Could not retrieve User from DB:\n" + ex.toString() );
                }

                if ( user == null || !user.getEmail().equalsIgnoreCase( getEmail() ) ) {
                    //Falscheingaben 5 sec verzögern, wegen Ausspähung von Benutzer-Accounts
                    synchronized ( wrongAuthenthication ) {
                        try {
                            Thread.sleep( 5000 );
                        } catch ( InterruptedException iex ) {
                        }

    					errors.add( "username.email.mismatch", new ActionError(	"vkc.administration.error.username.email.mismatch" ) );
                    }
                } 
            } catch ( Exception e ) {
                String errorId = TraceLogger.createErrorId();
                WebShare.getTraceLogger().logStackTrace( e, errorId );
                errors.add( "error", new ActionError( "error.general", errorId ) );
            }
        }

        return errors;
    }

    public String getEmail() {
        return email;
    }

    public String getUsername() {
        return username;
    }

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

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