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

package salaselo.util;

import org.hibernate.Session;
import org.hibernate.Transaction;
import salaselo.entities.Asignacion;


/**
 *
 * @author maldoror
 */
public class Persistor {
    public static boolean persistAsignacion(Asignacion asignacionToPersist){
        try{
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction tx = session.beginTransaction();
            if(!session.createQuery("from Asignacion where dia='" +
                    CalendarioUtil.dateToString(asignacionToPersist.getDia()) + "'" +
                    " and bloque='" + asignacionToPersist.getBloque() +
                    "' and sala.salaID="+asignacionToPersist.getSala().getSalaID()).list().isEmpty())
                return false;
            session.saveOrUpdate(asignacionToPersist);
            session.flush();
            tx.commit();
            return true;
        }catch(Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

}
