/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import entities.Korisnik;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.annotation.FacesConfig;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import util.HibernateUtil;

/**
 *
 * @author jelic
 */
@Named(value = "loginManagedBean")
@RequestScoped
@FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class LoginManagedBean {

    /**
     * Creates a new instance of LoginManagedBean
     */
    public LoginManagedBean() {
    }
    
    private String kor_ime="";
    private String lozinka;

    public String getKor_ime() {
        return kor_ime;
    }

    public void setKor_ime(String kor_ime) {
        this.kor_ime = kor_ime;
    }

    public String getLozinka() {
        return lozinka;
    }

    public void setLozinka(String lozinka) {
        this.lozinka = lozinka;
    }
    
    public void login(){
        SessionFactory sf = HibernateUtil.getSessionFactory();
        
        Session session = sf.openSession();
        session.beginTransaction();
        
        Criteria cr = session.createCriteria(Korisnik.class);
        Korisnik k = (Korisnik)cr.add(Restrictions.eq("kor_ime", kor_ime)).add(Restrictions.eq("lozinka", lozinka)).uniqueResult();
        
        session.getTransaction().commit();
        session.close();
        if(k!=null){
            System.out.println("Uspesan login");
        }
        else{
            System.out.println("Losi podaci");
        }
    }
    
    public String provera(){
        if(kor_ime.length()>2){
            return "OK";
        }
        return "NOT OK";
    }
    
}
