Search in sources :

Example 6 with PersonAttribute

use of org.openmrs.PersonAttribute in project openmrs-core by openmrs.

the class HibernatePersonDAO method deletePersonAndAttributes.

/**
 * Used by deletePerson, deletePatient, and deleteUser to remove all properties of a person
 * before deleting them.
 *
 * @param sessionFactory the session factory from which to pull the current session
 * @param person the person to delete
 */
public static void deletePersonAndAttributes(SessionFactory sessionFactory, Person person) {
    // delete properties and fields so hibernate can't complain
    for (PersonAddress address : person.getAddresses()) {
        if (address.getDateCreated() == null) {
            sessionFactory.getCurrentSession().evict(address);
        } else {
            sessionFactory.getCurrentSession().delete(address);
        }
    }
    person.setAddresses(null);
    for (PersonAttribute attribute : person.getAttributes()) {
        if (attribute.getDateCreated() == null) {
            sessionFactory.getCurrentSession().evict(attribute);
        } else {
            sessionFactory.getCurrentSession().delete(attribute);
        }
    }
    person.setAttributes(null);
    for (PersonName name : person.getNames()) {
        if (name.getDateCreated() == null) {
            sessionFactory.getCurrentSession().evict(name);
        } else {
            sessionFactory.getCurrentSession().delete(name);
        }
    }
    person.setNames(null);
    // finally, just tell hibernate to delete our object
    sessionFactory.getCurrentSession().delete(person);
}
Also used : PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) PersonAttribute(org.openmrs.PersonAttribute)

Example 7 with PersonAttribute

use of org.openmrs.PersonAttribute in project openmrs-core by openmrs.

the class PersonSaveHandler method handle.

/**
 * @see org.openmrs.api.handler.SaveHandler#handle(org.openmrs.OpenmrsObject, org.openmrs.User,
 *      java.util.Date, java.lang.String)
 */
@Override
public void handle(Person person, User creator, Date dateCreated, String other) {
    // address collection
    if (person.getAddresses() != null && !person.getAddresses().isEmpty()) {
        for (PersonAddress pAddress : person.getAddresses()) {
            if (pAddress.isBlank()) {
                person.removeAddress(pAddress);
                continue;
            }
            pAddress.setPerson(person);
        }
    }
    // name collection
    if (person.getNames() != null && !person.getNames().isEmpty()) {
        for (PersonName pName : person.getNames()) {
            pName.setPerson(person);
        }
    }
    // attribute collection
    if (person.getAttributes() != null && !person.getAttributes().isEmpty()) {
        for (PersonAttribute pAttr : person.getAttributes()) {
            pAttr.setPerson(person);
        }
    }
    // if the patient was marked as dead and reversed, drop the cause of death
    if (!person.getDead() && person.getCauseOfDeath() != null) {
        person.setCauseOfDeath(null);
    }
    // do the checks for voided attributes (also in PersonVoidHandler)
    if (person.getPersonVoided()) {
        if (!StringUtils.hasLength(person.getPersonVoidReason())) {
            throw new APIException("Person.voided.bit", new Object[] { person });
        }
        if (person.getPersonVoidedBy() == null) {
            person.setPersonVoidedBy(creator);
        }
        if (person.getPersonDateVoided() == null) {
            person.setPersonDateVoided(dateCreated);
        }
    } else {
        // voided is set to false
        person.setPersonVoidedBy(null);
        person.setPersonDateVoided(null);
        person.setPersonVoidReason(null);
    }
}
Also used : PersonName(org.openmrs.PersonName) APIException(org.openmrs.api.APIException) PersonAddress(org.openmrs.PersonAddress) PersonAttribute(org.openmrs.PersonAttribute)

Example 8 with PersonAttribute

use of org.openmrs.PersonAttribute in project openmrs-core by openmrs.

the class PatientServiceTest method mergePatients_shouldAuditCreatedAttributes.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldAuditCreatedAttributes() throws Exception {
    // retrieve preferred patient
    Patient preferred = patientService.getPatient(999);
    // retrieve notPreferredPatient and save it with a new attribute
    Patient notPreferred = patientService.getPatient(2);
    voidOrders(Collections.singleton(notPreferred));
    PersonAttribute attribute = new PersonAttribute(2);
    attribute.setValue("5089");
    attribute.setAttributeType(personService.getPersonAttributeType(1));
    notPreferred.addAttribute(attribute);
    patientService.savePatient(notPreferred);
    // merge the two patients and retrieve the audit object
    PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred);
    // find the UUID of the attribute that was added by the merge
    String addedAttributeUuid = null;
    preferred = patientService.getPatient(999);
    for (PersonAttribute a : preferred.getAttributes()) {
        if (a.getValue().equals(attribute.getValue())) {
            addedAttributeUuid = a.getUuid();
        }
    }
    Assert.assertNotNull("expected new attribute was not found in the preferred patient after the merge", addedAttributeUuid);
    Assert.assertTrue("person attribute creation not audited", isValueInList(addedAttributeUuid, audit.getPersonMergeLogData().getCreatedAttributes()));
}
Also used : Patient(org.openmrs.Patient) PersonMergeLog(org.openmrs.person.PersonMergeLog) PersonAttribute(org.openmrs.PersonAttribute) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 9 with PersonAttribute

use of org.openmrs.PersonAttribute in project openmrs-core by openmrs.

the class PatientDAOTest method getPatients_shouldFindPatientsEfficiently.

@Test
@Ignore("Designated for manual runs")
public void getPatients_shouldFindPatientsEfficiently() throws IOException, URISyntaxException {
    URL givenNamesIn = getClass().getResource("/org/openmrs/api/db/givenNames.csv");
    List<String> givenNames = FileUtils.readLines(new File(givenNamesIn.toURI()));
    URL familyNamesIn = getClass().getResource("/org/openmrs/api/db/familyNames.csv");
    List<String> familyNames = FileUtils.readLines(new File(familyNamesIn.toURI()));
    List<String> attributes = Arrays.asList("London", "Berlin", "Warsaw", "Paris", "Zurich", "Singapore");
    PatientIdentifierType idType = patientService.getPatientIdentifierTypeByName("Old Identification Number");
    PersonAttributeType attributeType = personService.getPersonAttributeTypeByName("Birthplace");
    attributeType.setSearchable(true);
    Context.getPersonService().savePersonAttributeType(attributeType);
    Location location = locationService.getLocation(1);
    // set the seed to have repeatable results
    Random random = new Random(100);
    List<String> generatedPatients = new ArrayList<>();
    for (int i = 0; i < 20000; i++) {
        int given = random.nextInt(givenNames.size());
        int family = random.nextInt(familyNames.size());
        int attribute = random.nextInt(attributes.size());
        generatedPatients.add((i + 1000) + " " + givenNames.get(given) + " " + familyNames.get(family) + " " + attributes.get(attribute));
        PersonName personName = new PersonName(givenNames.get(given), null, familyNames.get(family));
        Patient patient = new Patient();
        patient.setGender("m");
        patient.addIdentifier(new PatientIdentifier("" + (i + 1000), idType, location));
        patient.addName(personName);
        PersonAttribute personAttribute = new PersonAttribute();
        personAttribute.setAttributeType(attributeType);
        personAttribute.setValue(attributes.get(attribute));
        patient.addAttribute(personAttribute);
        patientService.savePatient(patient);
        if (i % 100 == 0) {
            System.out.println("Created " + i + " patients!");
            Context.flushSession();
            Context.clearSession();
        }
    }
    File file = File.createTempFile("generated-patients-", ".csv");
    FileUtils.writeLines(file, generatedPatients);
    System.out.println("Dumped generated patients to " + file.getAbsolutePath());
    long time = System.currentTimeMillis();
    updateSearchIndex();
    time = System.currentTimeMillis() - time;
    System.out.println("Indexing took " + time + " ms.");
    // get Lucene up to speed...
    patientService.getPatients("Aaaaa");
    time = System.currentTimeMillis();
    List<Patient> results = patientService.getPatients("Al");
    time = System.currentTimeMillis() - time;
    System.out.println("Starts with search for 'Al' name returned " + results.size() + " in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("Al", 0, 15);
    time = System.currentTimeMillis() - time;
    System.out.println("Starts with search for 'Al' name limited to 15 results returned in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("Al Dem");
    time = System.currentTimeMillis() - time;
    System.out.println("Starts with search for 'Al Dem' name returned " + results.size() + " in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("Al Dem", 0, 15);
    time = System.currentTimeMillis() - time;
    System.out.println("Starts with search for 'Al Dem' name limited to 15 results returned in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("Jack");
    time = System.currentTimeMillis() - time;
    System.out.println("Starts with search for 'Jack' name returned " + results.size() + " in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("Jack", 0, 15);
    time = System.currentTimeMillis() - time;
    System.out.println("Starts with search for 'Jack' name limited to 15 results returned in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("Jack Sehgal");
    time = System.currentTimeMillis() - time;
    System.out.println("Starts with search for 'Jack Sehgal' name returned " + results.size() + " in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("Jack Sehgal", 0, 15);
    time = System.currentTimeMillis() - time;
    System.out.println("Starts with search for 'Jack Sehgal' name limited to 15 results returned in " + time + " ms");
    Context.getAdministrationService().setGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_SEARCH_MATCH_MODE, OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_SEARCH_MATCH_ANYWHERE);
    time = System.currentTimeMillis();
    results = patientService.getPatients("aso");
    time = System.currentTimeMillis() - time;
    System.out.println("Anywhere search for 'aso' name returned " + results.size() + " in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("aso", 0, 15);
    time = System.currentTimeMillis() - time;
    System.out.println("Anywhere search for 'aso' name limited to 15 results returned in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("aso os");
    time = System.currentTimeMillis() - time;
    System.out.println("Anywhere search for 'aso os' name returned " + results.size() + " in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("aso os", 0, 15);
    time = System.currentTimeMillis() - time;
    System.out.println("Anywhere search for 'aso os' limited to 15 results returned in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("9243");
    time = System.currentTimeMillis() - time;
    System.out.println("Exact search for '9243' identifier returned " + results.size() + " in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("London");
    time = System.currentTimeMillis() - time;
    System.out.println("Exact search for 'London' attribute returned " + results.size() + " in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("London", 0, 15);
    time = System.currentTimeMillis() - time;
    System.out.println("Exact search for 'London' attribute limited to 15 results returned in " + time + " ms");
    Context.getAdministrationService().setGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PERSON_ATTRIBUTE_SEARCH_MATCH_MODE, OpenmrsConstants.GLOBAL_PROPERTY_PERSON_ATTRIBUTE_SEARCH_MATCH_ANYWHERE);
    time = System.currentTimeMillis();
    results = patientService.getPatients("uric");
    time = System.currentTimeMillis() - time;
    System.out.println("Anywhere search for 'uric' attribute returned " + results.size() + " in " + time + " ms");
    time = System.currentTimeMillis();
    results = patientService.getPatients("uric", 0, 15);
    time = System.currentTimeMillis() - time;
    System.out.println("Anywhere search for 'uric' attribute limited to 15 results returned in " + time + " ms");
}
Also used : PersonName(org.openmrs.PersonName) ArrayList(java.util.ArrayList) PersonAttributeType(org.openmrs.PersonAttributeType) Patient(org.openmrs.Patient) URL(java.net.URL) PatientIdentifier(org.openmrs.PatientIdentifier) PersonAttribute(org.openmrs.PersonAttribute) Random(java.util.Random) File(java.io.File) PatientIdentifierType(org.openmrs.PatientIdentifierType) Location(org.openmrs.Location) Ignore(org.junit.Ignore) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 10 with PersonAttribute

use of org.openmrs.PersonAttribute in project openmrs-module-mirebalais by PIH.

the class ZlEmrIdCardPrinter method getTelephoneNumber.

/**
 * @return the telephone number for the patient in the  format that it should be displayed on the id cards
 */
protected String getTelephoneNumber(Patient patient) {
    String phoneNumber = "";
    PersonAttributeType type = MetadataUtils.existing(PersonAttributeType.class, HaitiPersonAttributeTypes.TELEPHONE_NUMBER.uuid());
    PersonAttribute attr = patient.getAttribute(type);
    if (attr != null) {
        phoneNumber = StringUtils.defaultIfEmpty(attr.getValue(), "");
    }
    return phoneNumber;
}
Also used : PersonAttributeType(org.openmrs.PersonAttributeType) PersonAttribute(org.openmrs.PersonAttribute)

Aggregations

PersonAttribute (org.openmrs.PersonAttribute)20 Test (org.junit.Test)12 Patient (org.openmrs.Patient)9 PersonName (org.openmrs.PersonName)7 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)5 Date (java.util.Date)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 PersonAddress (org.openmrs.PersonAddress)3 PersonAttributeType (org.openmrs.PersonAttributeType)3 PatientAndMatchQuality (org.openmrs.module.registrationcore.api.search.PatientAndMatchQuality)3 BaseModuleContextSensitiveTest (org.openmrs.test.BaseModuleContextSensitiveTest)3 ArrayList (java.util.ArrayList)2 PersonMergeLog (org.openmrs.person.PersonMergeLog)2 DLD (ca.uhn.hl7v2.model.v25.datatype.DLD)1 IS (ca.uhn.hl7v2.model.v25.datatype.IS)1 File (java.io.File)1 URL (java.net.URL)1 Random (java.util.Random)1 Criteria (org.hibernate.Criteria)1 Ignore (org.junit.Ignore)1