package performa.form;

import java.util.Map;
import oneit.logging.*;
import oneit.objstore.StorageException;
import oneit.objstore.parser.BusinessObjectParser;
import oneit.security.SecUser;
import oneit.servlets.forms.*;
import oneit.servlets.process.*;
import oneit.utils.*;
import performa.orm.AdminUser;
import performa.utils.Utils;
 

public class ResetPasswordFP extends ORMProcessFormProcessor
{
    private static  LoggingArea  LOG    =   LoggingArea.createLoggingArea("ResetPasswordFP");
    
    
    @Override
    public SuccessfulResult processForm(ORMProcessState process, SubmissionDetails submission, Map params) throws BusinessException, StorageException
    {
        SecUser     user        =   (SecUser) process.getAttribute("SecUser");
        AdminUser   adminUser   =   user.getExtension(AdminUser.REFERENCE_AdminUser);
        
        if(adminUser != null)
        {
            LogMgr.log(LOG, LogLevel.PROCESSING1, "Inside ResetPasswordFP for reset pasword to ", user);
            
            adminUser.setForgotPasswordKey(null);
        }
        
        return Utils.processSuccessfulLogin(process, submission, params, user);
    }
    
    
    @Override
    public void validate(ORMProcessState process, SubmissionDetails submission, MultiException exceptions, Map params) throws StorageException
    {
        super.validate(process, submission, exceptions, params);
        
        SecUser user = (SecUser) process.getAttribute("SecUser");
        
        Debug.assertion(user != null, "No user found for rest password. Call from " + getClass().getName());
        
        BusinessObjectParser.assertFieldCondition(user.getPassword().length() >= 8, user, SecUser.FIELD_Password, exceptions, true, submission.getRequest());
    }
}