/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package salaselo.session;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import salaselo.entities.Usuario;
import salaselo.util.HibernateUtil;

/**
 *
 * @author maldoror
 */
public class UserSession extends GenericSession {

    private Usuario owner;

    public UserSession(Usuario owner) {
        this.owner = owner;
    }

    @Override
    public boolean isAdmin() {
        return false;
    }

    @Override
    public Usuario getSessionOwner() {
        return owner;
    }

    public static UserSession getInstance(String rut,String password) {
        try{
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction tx = session.beginTransaction();
            List<Usuario> usr = session.createQuery("from Usuario where rut='" +
                    rut +"' and password='" + password + "'").list();
            session.close();
            if(!usr.isEmpty()){
                return new UserSession(usr.get(0));
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
