/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package systole.persistence.brokersDB;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.PatientSyncBroker;
import systole.synchronization.remote.entities.PatientRemote;
/**
*
* @author Juan Manuel
*/
public class PatientSyncBrokerDB extends BrokerDB implements PatientSyncBroker {
public void savePatientRemote(PatientRemote patientRemote) throws ExceptionDAO {
try {
this.logger.logDebug("saving remote patient");
Session currentSession = FacadeDB.getInstance().getCurrentSession();
currentSession.save(patientRemote);
this.logger.logDebug("remote patient saved");
} catch (HibernateException e) {
this.logger.logError("error on save patient remote, msg: " + e.getMessage());
throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
}
}
public List<PatientRemote> getPatientsToUpdate() throws ExceptionDAO {
throw new UnsupportedOperationException("Not supported yet.");
}
public List<Patient> getPatientsToUpload() throws ExceptionDAO {
try {
this.logger.logDebug("getting patients to upload");
Session currentSession = FacadeDB.getInstance().getCurrentSession();
String hql = "select p from PatientRemote pr right join pr.patient p where pr=null ";
Query query = currentSession.createQuery(hql);
@SuppressWarnings("unchecked")
List<Patient> list = query.list();
this.logger.logDebug("getting patients to upload");
return list;
} catch (HibernateException e) {
this.logger.logError("error on get patients to upload, msg: " + e.getMessage());
throw new ExceptionDAO("No se pudieron obtener los pacientes", e.fillInStackTrace());
}
}
public int getLastRemotePatientIdSynchronized() throws ExceptionDAO {
try {
this.logger.logDebug("getting last remote patient sync");
Session currentSession = FacadeDB.getInstance().getCurrentSession();
Criteria criteria = currentSession.createCriteria(
PatientRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
PatientRemote patientRemote = (PatientRemote) criteria.uniqueResult();
this.logger.logDebug("get successfully");
return (patientRemote != null ? patientRemote.getRemoteId() : -1);
} catch (HibernateException e) {
this.logger.logError("error on get last remote patient synchronized, msg: " + e.getMessage());
throw new ExceptionDAO("No se pudieron obtener los pacientes", e.fillInStackTrace());
}
}
public PatientRemote getPatientRemoteByPatient(Patient patient) throws ExceptionDAO {
try {
this.logger.logDebug("getting patient remote by local patient");
Session currentSession = FacadeDB.getInstance().getCurrentSession();
Criteria criteria = currentSession.createCriteria(
PatientRemote.class).add(Restrictions.eq("patient", patient)).setMaxResults(1);
PatientRemote sportRemote = (PatientRemote) criteria.uniqueResult();
this.logger.logDebug("get successfully");
return sportRemote;
} catch (HibernateException e) {
this.logger.logError("error on get remote patient by patient, msg: " + e.getMessage());
throw new ExceptionDAO("No se pudieron obtener los pacientes", e.fillInStackTrace());
}
}
public Patient getPatientByRemoteId(int remoteId) throws ExceptionDAO {
try {
this.logger.logDebug("getting patient by remote id");
Session currentSession = FacadeDB.getInstance().getCurrentSession();
Criteria criteria = currentSession.createCriteria(
PatientRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
PatientRemote patientRemote = (PatientRemote) criteria.uniqueResult();
this.logger.logDebug("get successfully");
return (patientRemote != null ? patientRemote.getPatient() : null);
} catch (HibernateException e) {
this.logger.logError("error on get patient by remote id, msg: " + e.getMessage());
throw new ExceptionDAO("No se pudieron obtener los pacientes", e.fillInStackTrace());
}
}
}