Search in sources :

Example 1 with Person

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);
    }
}
Also used : SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) LuceneQuery(org.openmrs.api.db.hibernate.search.LuceneQuery) Person(org.openmrs.Person) Date(java.util.Date)

Example 2 with 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();
    }
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) CharArrayReader(java.io.CharArrayReader) ComplexData(org.openmrs.obs.ComplexData) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) File(java.io.File) Person(org.openmrs.Person) Date(java.util.Date) Location(org.openmrs.Location) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 3 with Person

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());
}
Also used : Obs(org.openmrs.Obs) Person(org.openmrs.Person) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 4 with Person

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());
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) Person(org.openmrs.Person) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 5 with Person

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());
}
Also used : Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Person (org.openmrs.Person)167 Test (org.junit.Test)140 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)107 PersonName (org.openmrs.PersonName)39 Date (java.util.Date)33 User (org.openmrs.User)33 Relationship (org.openmrs.Relationship)19 Obs (org.openmrs.Obs)16 Patient (org.openmrs.Patient)15 BindException (org.springframework.validation.BindException)15 Message (ca.uhn.hl7v2.model.Message)14 Concept (org.openmrs.Concept)14 Voidable (org.openmrs.Voidable)14 Errors (org.springframework.validation.Errors)14 ArrayList (java.util.ArrayList)10 Provider (org.openmrs.Provider)10 PersonMergeLog (org.openmrs.person.PersonMergeLog)9 RelationshipType (org.openmrs.RelationshipType)8 ORU_R01 (ca.uhn.hl7v2.model.v25.message.ORU_R01)7 NK1 (ca.uhn.hl7v2.model.v25.segment.NK1)7