/*
 * 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 controler;

import db.HibernateUtil;
import entiteti.Brod;
import entiteti.Korisnik;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author jelic
 */

@ManagedBean
@ViewScoped
public class Ajax {
    private  List<Brod> brodovi = new ArrayList<Brod>();
    
    public void dohvatiBrodove(){
        SessionFactory sessionF = HibernateUtil.getSessionFactory();
        Session session = sessionF.openSession();
        session.beginTransaction();
        
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession s = (HttpSession) fc.getExternalContext().getSession(false);
        
        Korisnik ulogovan = (Korisnik) s.getAttribute("user");
        
        Criteria query = session.createCriteria(Brod.class);
        brodovi = query.add(Restrictions.eq("kapetan", ulogovan.getUsername())).list();
        session.getTransaction().commit();
        session.close();
   }

    public List<Brod> getBrodovi() {
        return brodovi;
    }

    public void setBrodovi(List<Brod> brodovi) {
        this.brodovi = brodovi;
    }
    
    private String selektovanBrod;

    public String getSelektovanBrod() {
        return selektovanBrod;
    }

    public void setSelektovanBrod(String selektovanBrod) {
        this.selektovanBrod = selektovanBrod;
    }
    
    public void change(){
        System.out.println("Ajax");
    }
    
}
