/**
*
*/
package systole.persistence.brokersDB;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import systole.domain.persons.Patient;
import systole.domain.persons.Person;
import systole.domain.persons.identityCard.IdentityCard;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.PatientBroker;
import systole.view.messages.ErrorMessages;
/**
* @author jmj
*
*/
public class PatientBrokerDB extends BrokerDB implements PatientBroker {
/**
*
*
*
*/
public PatientBrokerDB() {
super();
}
/* (non-Javadoc)
* @see systole.persistence.brokersInterface.PatientBroker#getAllPatient()
*/
@Override
public List<Patient> getAllPatient() throws ExceptionDAO {
try {
this.logger.logDebug("Getting all patients");
Session currentSession = FacadeDB.getInstance().getCurrentSession();
List<Patient> list = currentSession.createQuery("from Patient p order by upper(p.surname)").list();
this.logger.logDebug("get patients successfully");
return list;
} catch (HibernateException e) {
this.logger.logError("error on get patients, msg: " + e.getMessage());
throw new ExceptionDAO("No se pudieron obtener los pacientes", e.fillInStackTrace());
}
}
/* (non-Javadoc)
* @see systole.persistence.brokersInterface.PatientBroker#getPatientById(long)
*/
@Override
public Patient getPatientById(Integer id) throws ExceptionDAO {
try {
this.logger.logDebug("Getting patient");
Session currentSession = FacadeDB.getInstance().getCurrentSession();
Patient instance = (Patient) currentSession.get(Patient.class, id);
this.logger.logDebug("get patient successfully");
return instance;
} catch (HibernateException e) {
this.logger.logError("error on get patient by id, msg: " + e.getMessage());
throw new ExceptionDAO("No se pudo obtener el Paciente", e.fillInStackTrace());
}
}
/* (non-Javadoc)
* @see systole.persistence.brokersInterface.PatientBroker#insert(systole.domain.persons.Patient)
*/
@Override
public void insert(Patient patient) throws ExceptionDAO {
try {
this.logger.logDebug("saving patient");
Session currentSession = FacadeDB.getInstance().getCurrentSession();
currentSession.save(patient);
this.logger.logDebug("save patient successfully");
} catch (HibernateException e) {
this.logger.logError("error on save patient, msg: " + e.getMessage());
throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
}
}
/* (non-Javadoc)
* @see systole.persistence.brokersInterface.PatientBroker#update(systole.domain.persons.Patient)
*/
@Override
public void update(Patient patient) throws ExceptionDAO {
try {
this.logger.logDebug("update patient");
Session currentSession = FacadeDB.getInstance().getCurrentSession();
currentSession.update(patient);
this.logger.logDebug("update patient successfully");
} catch (HibernateException e) {
this.logger.logError("error on update patient, msg: " + e.getMessage());
throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
}
}
@SuppressWarnings("unchecked")
public boolean existCardIdentity(IdentityCard identityCard, Integer id) throws ExceptionDAO {
try {
this.logger.logDebug("exist identity card");
Session currentSession = FacadeDB.getInstance().getCurrentSession();
Criteria criteria = currentSession.createCriteria(Person.class).
add((id == null) ? Restrictions.eq("identityCard", identityCard)
: Restrictions.and(Restrictions.eq("identityCard", identityCard), Restrictions.ne("id", id)));
List list = criteria.list();
this.logger.logDebug("exist identity card successfully");
return !list.isEmpty();
} catch (HibernateException e) {
this.logger.logError("error on exist identity card, msg: " + e.getMessage());
throw new ExceptionDAO("No se pudo verificar el Tipo y Número de Documento", e.fillInStackTrace());
}
}
}