/*
 * Created on 27. 7. 2017 15:48:48
 *
 */
package com.jh.radegast.security;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.jh.common.jpa.AbstractHibernateRepository;

/**
 * The Class AppUserRepository.
 *
 * @author Jan Hrabal
 */
@Repository
public class AppUserRepository extends AbstractHibernateRepository {

    /**
     * Find by email and deleted false.
     *
     * @param email the email
     * @return the app user
     */
    public AppUser findByEmailAndDeletedFalse(String email) {
    	Criteria c = criteria(AppUser.class);
        c.add(Restrictions.ilike("email", email, MatchMode.EXACT));
    	c.add(Restrictions.or(Restrictions.eq("deleted", Boolean.FALSE), Restrictions.isNull("deleted")));
    	return (AppUser) c.uniqueResult();
    }

	/**
	 * Find by email.
	 *
	 * @param email the email
	 * @return the app user
	 */
	public AppUser findByEmail(String email) {
    	Criteria c = criteria(AppUser.class);
    	c.add(Restrictions.ilike("email", email, MatchMode.EXACT));
    	return (AppUser) c.uniqueResult();
	}
    
	
	public AppUser findById(long id) {
    	Criteria c = criteria(AppUser.class);
    	c.add(Restrictions.idEq(id));
    	return (AppUser) c.uniqueResult();
	}
	
	
	public void savePasswordResetRequest(PasswordResetRequest request) {
		save(request);
	}
	
	
	public PasswordResetRequest fetchPasswordResetRequest(String token) {
    	Criteria c = criteria(PasswordResetRequest.class);
        c.add(Restrictions.eq("hash", token));
    	return (PasswordResetRequest) c.uniqueResult();
	}
	
	
}
