use of org.openmrs.Person in project openmrs-core by openmrs.
the class HibernatePatientDAO method insertPatientStubIfNeeded.
/**
* Inserts a row into the patient table This avoids hibernate's bunging of our
* person/patient/user inheritance
*
* @param patient
*/
private void insertPatientStubIfNeeded(Patient patient) {
boolean stubInsertNeeded = false;
if (patient.getPatientId() != null) {
// check if there is a row with a matching patient.patient_id
String sql = "SELECT 1 FROM patient WHERE patient_id = :patientId";
Query query = sessionFactory.getCurrentSession().createSQLQuery(sql);
query.setInteger("patientId", patient.getPatientId());
stubInsertNeeded = (query.uniqueResult() == null);
}
if (stubInsertNeeded) {
// If not yet persisted
if (patient.getCreator() == null) {
patient.setCreator(Context.getAuthenticatedUser());
}
// If not yet persisted
if (patient.getDateCreated() == null) {
patient.setDateCreated(new Date());
}
String insert = "INSERT INTO patient (patient_id, creator, voided, date_created) VALUES (:patientId, :creator, 0, :dateCreated)";
Query query = sessionFactory.getCurrentSession().createSQLQuery(insert);
query.setInteger("patientId", patient.getPatientId());
query.setInteger("creator", patient.getCreator().getUserId());
query.setDate("dateCreated", patient.getDateCreated());
query.executeUpdate();
// Without evicting person, you will get this error when promoting person to patient
// org.hibernate.NonUniqueObjectException: a different object with the same identifier
// value was already associated with the session: [org.openmrs.Patient#]
// see TRUNK-3728
Person person = (Person) sessionFactory.getCurrentSession().get(Person.class, patient.getPersonId());
sessionFactory.getCurrentSession().evict(person);
}
}
use of org.openmrs.Person in project openmrs-core by openmrs.
the class ObsServiceTest method saveObs_shouldCreateNewFileFromComplexDataForNewObs.
/**
* @see ObsService#saveObs(Obs,String)
*/
@Test
public void saveObs_shouldCreateNewFileFromComplexDataForNewObs() {
executeDataSet(COMPLEX_OBS_XML);
ObsService os = Context.getObsService();
ConceptService cs = Context.getConceptService();
AdministrationService as = Context.getAdministrationService();
// make sure the file isn't there to begin with
File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
File createdFile = new File(complexObsDir, "nameOfFile.txt");
if (createdFile.exists())
createdFile.delete();
// the complex data to put onto an obs that will be saved
Reader input = new CharArrayReader("This is a string to save to a file".toCharArray());
ComplexData complexData = new ComplexData("nameOfFile.txt", input);
// must fetch the concept instead of just new Concept(8473) because the attributes on concept are checked
// this is a concept mapped to the text handler
Concept questionConcept = cs.getConcept(8474);
Obs obsToSave = new Obs(new Person(1), questionConcept, new Date(), new Location(1));
obsToSave.setComplexData(complexData);
try {
os.saveObs(obsToSave, null);
// make sure the file appears now after the save
Assert.assertTrue(createdFile.exists());
} finally {
// we always have to delete this inside the same unit test because it is outside the
// database and hence can't be "rolled back" like everything else
createdFile.delete();
}
}
use of org.openmrs.Person in project openmrs-core by openmrs.
the class ObsServiceTest method getObservationsByPerson_shouldGetAllObservationsAssignedToGivenPerson.
/**
* @see ObsService#getObservationsByPerson(Person)
*/
@Test
public void getObservationsByPerson_shouldGetAllObservationsAssignedToGivenPerson() {
ObsService obsService = Context.getObsService();
List<Obs> obss = obsService.getObservationsByPerson(new Person(7));
Assert.assertEquals(9, obss.size());
Assert.assertEquals(16, obss.get(0).getObsId().intValue());
Assert.assertEquals(7, obss.get(8).getObsId().intValue());
}
use of org.openmrs.Person in project openmrs-core by openmrs.
the class ObsServiceTest method getObservationsByPersonAndConcept_shouldGetObservationsMatchingPersonAndQuestion.
/**
* @see ObsService#getObservationsByPersonAndConcept(Person,Concept)
*/
@Test
public void getObservationsByPersonAndConcept_shouldGetObservationsMatchingPersonAndQuestion() {
ObsService obsService = Context.getObsService();
List<Obs> obss = obsService.getObservationsByPersonAndConcept(new Person(7), new Concept(5089));
Assert.assertEquals(3, obss.size());
Assert.assertEquals(16, obss.get(0).getObsId().intValue());
Assert.assertEquals(10, obss.get(1).getObsId().intValue());
Assert.assertEquals(7, obss.get(2).getObsId().intValue());
}
use of org.openmrs.Person in project openmrs-core by openmrs.
the class PersonServiceTest method unvoidPerson_shouldUnvoidPatient.
/**
* @see PersonService#unvoidPerson(Person)
*/
@Test
public void unvoidPerson_shouldUnvoidPatient() throws Exception {
// given
Person person = personService.getPerson(2);
personService.voidPerson(person, "reason");
// when
personService.unvoidPerson(person);
// then
Assert.assertFalse(person.getVoided());
}
Aggregations