/*
 * LoginValidator.java
 * 
 * Created on 2007.10.15., 21:11:08
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.nir.nmail.validators;

import hu.nir.nmail.dbm.DataManager;
import hu.nir.nmail.ejb.stateless.NmailUserLoginManager;
import hu.nir.nmail.spring.beans.LoginData;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author attila
 */
public class LoginValidator implements Validator {
    
    private DataManager dataManager;
    
    public boolean supports(Class clazz) {
        return clazz.equals(LoginData.class);
    }

    public void validate(Object target, Errors errors) {
        LoginData loginData = (LoginData) target;
        
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "loginName", "required.loginName");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "required.password");
        NmailUserLoginManager nmailUserLoginManager = dataManager.getNmailUserLoginManager();
        Long userId = nmailUserLoginManager.getUserId(loginData.getLoginName(), loginData.getPassword());
        if (userId == null) {
            errors.reject("error.login");
        }
    }

    public DataManager getDataManager() {
        return dataManager;
    }

    public void setDataManager(DataManager dataManager) {
        this.dataManager = dataManager;
    }

}
