Search in sources :

Example 36 with PersonName

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

the class PersonServiceImpl method setPreferredPersonName.

private void setPreferredPersonName(Person person) {
    PersonName preferredName = null;
    PersonName possiblePreferredName = person.getPersonName();
    if (possiblePreferredName != null && possiblePreferredName.getPreferred() && !possiblePreferredName.getVoided()) {
        preferredName = possiblePreferredName;
    }
    for (PersonName name : person.getNames()) {
        if (preferredName == null && !name.getVoided()) {
            name.setPreferred(true);
            preferredName = name;
            continue;
        }
        if (!name.equals(preferredName)) {
            name.setPreferred(false);
        }
    }
}
Also used : PersonName(org.openmrs.PersonName)

Example 37 with PersonName

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

the class MigrationHelper method importUsers.

/**
 * Takes XML like: <something> <user date_changed="2001-03-06 08:46:53.0"
 * date_created="2001-03-06 08:46:53.0" username="hamish@mit.edu" first_name="Hamish"
 * last_name="Fraser" user_id="2001"/> </something> Returns the number of users added
 */
public static int importUsers(Document document) throws ParseException {
    int ret = 0;
    Random rand = new Random();
    UserService us = Context.getUserService();
    List<Node> toAdd = new ArrayList<>();
    findNodesNamed(document, "user", toAdd);
    for (Node node : toAdd) {
        Element e = (Element) node;
        String username = e.getAttribute("username");
        if (username == null || username.length() == 0) {
            throw new IllegalArgumentException("each <user /> element must define a user_name attribute");
        }
        if (us.getUserByUsername(username) != null) {
            continue;
        }
        User user = new User();
        user.setPerson(new Person());
        PersonName pn = new PersonName(e.getAttribute("first_name"), "", e.getAttribute("last_name"));
        user.addName(pn);
        user.setUsername(username);
        user.setDateCreated(parseDate(e.getAttribute("date_created")));
        user.setDateChanged(parseDate(e.getAttribute("date_changed")));
        // Generate a temporary password: 8-12 random characters
        String pass;
        {
            int length = rand.nextInt(4) + 8;
            char[] password = new char[length];
            for (int x = 0; x < length; x++) {
                int randDecimalAsciiVal = rand.nextInt(93) + 33;
                password[x] = (char) randDecimalAsciiVal;
            }
            pass = new String(password);
        }
        us.createUser(user, pass);
        ++ret;
    }
    return ret;
}
Also used : PersonName(org.openmrs.PersonName) User(org.openmrs.User) Random(java.util.Random) UserService(org.openmrs.api.UserService) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Person(org.openmrs.Person)

Example 38 with PersonName

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

the class MigrationHelper method importRelationships.

/**
 * Takes a list of Strings of the format RELATIONSHIP:&lt;user last name&gt;,&lt;user first
 * name&gt;,&lt;relationship type name&gt;,&lt;patient identifier type name&gt;,&lt;identifier&gt; so if user hfraser
 * if the cardiologist of the patient with patient_id 8039 in PIH's old emr, then:
 * RELATIONSHIP:hfraser,Cardiologist,HIV-EMRV1,8039 (the "RELATIONSHIP:" is not actually
 * necessary. Anything before and including the first : will be dropped If autoCreateUsers is
 * true, and no user exists with the given username, one will be created. If autoAddRole is
 * true, then whenever a user is auto-created, if a role exists with the same name as
 * relationshipType.name, then the user will be added to that role
 */
public static int importRelationships(Collection<String> relationships, boolean autoCreateUsers, boolean autoAddRole) {
    PatientService ps = Context.getPatientService();
    UserService us = Context.getUserService();
    PersonService personService = Context.getPersonService();
    List<Relationship> relsToAdd = new ArrayList<>();
    Random rand = new Random();
    for (String s : relationships) {
        if (s.contains(":")) {
            s = s.substring(s.indexOf(":") + 1);
        }
        String[] ss = s.split(",");
        if (ss.length < 5) {
            throw new IllegalArgumentException("The line '" + s + "' is in the wrong format");
        }
        String userLastName = ss[0];
        String userFirstName = ss[1];
        String username = (userFirstName + userLastName).replaceAll(" ", "");
        String relationshipType = ss[2];
        String identifierType = ss[3];
        String identifier = ss[4];
        User user = null;
        {
            // first try looking for non-voided users
            List<User> users = us.getUsersByName(userFirstName, userLastName, false);
            if (users.size() == 1) {
                user = users.get(0);
            } else if (users.size() > 1) {
                throw new IllegalArgumentException("Found " + users.size() + " users named '" + userLastName + ", " + userFirstName + "'");
            }
        }
        if (user == null) {
            // next try looking for voided users
            List<User> users = us.getUsersByName(userFirstName, userLastName, false);
            if (users.size() == 1) {
                user = users.get(0);
            } else if (users.size() > 1) {
                throw new IllegalArgumentException("Found " + users.size() + " voided users named '" + userLastName + ", " + userFirstName + "'");
            }
        }
        if (user == null && autoCreateUsers) {
            user = new User();
            user.setPerson(new Person());
            PersonName pn = new PersonName(userFirstName, "", userLastName);
            user.addName(pn);
            user.setUsername(username);
            // Generate a temporary password: 8-12 random characters
            String pass;
            {
                int length = rand.nextInt(4) + 8;
                char[] password = new char[length];
                for (int x = 0; x < length; x++) {
                    int randDecimalAsciiVal = rand.nextInt(93) + 33;
                    password[x] = (char) randDecimalAsciiVal;
                }
                pass = new String(password);
            }
            if (autoAddRole) {
                Role role = us.getRole(relationshipType);
                if (role != null) {
                    user.addRole(role);
                }
            }
            us.createUser(user, pass);
        }
        if (user == null) {
            throw new IllegalArgumentException("Can't find user '" + userLastName + ", " + userFirstName + "'");
        }
        Person person = personService.getPerson(user.getUserId());
        RelationshipType relationship = personService.getRelationshipTypeByName(relationshipType);
        PatientIdentifierType pit = ps.getPatientIdentifierTypeByName(identifierType);
        List<PatientIdentifier> found = ps.getPatientIdentifiers(identifier, Collections.singletonList(pit), null, null, null);
        if (found.size() != 1) {
            throw new IllegalArgumentException("Found " + found.size() + " patients with identifier '" + identifier + "' of type " + identifierType);
        }
        Person relative = personService.getPerson(found.get(0).getPatient().getPatientId());
        Relationship rel = new Relationship();
        rel.setPersonA(person);
        rel.setRelationshipType(relationship);
        rel.setPersonB(relative);
        relsToAdd.add(rel);
    }
    int addedSoFar = 0;
    for (Relationship rel : relsToAdd) {
        personService.saveRelationship(rel);
        ++addedSoFar;
    }
    return addedSoFar;
}
Also used : PersonName(org.openmrs.PersonName) User(org.openmrs.User) UserService(org.openmrs.api.UserService) PersonService(org.openmrs.api.PersonService) ArrayList(java.util.ArrayList) RelationshipType(org.openmrs.RelationshipType) PatientIdentifier(org.openmrs.PatientIdentifier) Role(org.openmrs.Role) Random(java.util.Random) PatientService(org.openmrs.api.PatientService) Relationship(org.openmrs.Relationship) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) Person(org.openmrs.Person) PatientIdentifierType(org.openmrs.PatientIdentifierType)

Example 39 with PersonName

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

the class PatientServiceTest method mergePatients_shouldAuditCreatedNames.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldAuditCreatedNames() throws Exception {
    // retrieve preferred patient
    Patient preferred = patientService.getPatient(999);
    // retrieve notPreferredPatient and save it with an added name
    Patient notPreferred = patientService.getPatient(2);
    voidOrders(Collections.singleton(notPreferred));
    PersonName name = new PersonName("first1234", "middle", "last1234");
    notPreferred.addName(name);
    patientService.savePatient(notPreferred);
    // merge the two patients and retrieve the audit object
    PersonMergeLog audit = mergeAndRetrieveAudit(preferred, notPreferred);
    // find the UUID of the name that was added by the merge
    String addedNameUuid = null;
    preferred = patientService.getPatient(999);
    for (PersonName n : preferred.getNames()) {
        if (n.getFullName().equals(name.getFullName())) {
            addedNameUuid = n.getUuid();
        }
    }
    Assert.assertNotNull("expected new name was not found in the preferred patient after the merge", addedNameUuid);
    Assert.assertTrue("person name creation not audited", isValueInList(addedNameUuid, audit.getPersonMergeLogData().getCreatedNames()));
}
Also used : PersonName(org.openmrs.PersonName) Patient(org.openmrs.Patient) PersonMergeLog(org.openmrs.person.PersonMergeLog) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 40 with PersonName

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

the class PatientServiceTest method savePatient_shouldNotSetAVoidedNameOrAddressOrIdentifierAsPreferred.

/**
 * @see PatientService#savePatient(Patient)
 */
@Test
public void savePatient_shouldNotSetAVoidedNameOrAddressOrIdentifierAsPreferred() throws Exception {
    Patient patient = new Patient();
    patient.setGender("M");
    PatientIdentifier identifier = new PatientIdentifier("QWERTY", patientService.getPatientIdentifierType(2), locationService.getLocation(1));
    PatientIdentifier preferredIdentifier = new PatientIdentifier("QWERTY2", patientService.getPatientIdentifierType(2), locationService.getLocation(1));
    preferredIdentifier.setPreferred(true);
    preferredIdentifier.setVoided(true);
    patient.addIdentifier(identifier);
    patient.addIdentifier(preferredIdentifier);
    PersonName name = new PersonName("givenName", "middleName", "familyName");
    PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
    preferredName.setPreferred(true);
    preferredName.setVoided(true);
    patient.addName(name);
    patient.addName(preferredName);
    PersonAddress address = new PersonAddress();
    address.setAddress1("some address");
    PersonAddress preferredAddress = new PersonAddress();
    preferredAddress.setAddress1("another address");
    preferredAddress.setPreferred(true);
    preferredAddress.setVoided(true);
    patient.addAddress(address);
    patient.addAddress(preferredAddress);
    patientService.savePatient(patient);
    Assert.assertFalse(preferredIdentifier.getPreferred());
    Assert.assertFalse(preferredName.getPreferred());
    Assert.assertFalse(preferredAddress.getPreferred());
    Assert.assertTrue(identifier.getPreferred());
    Assert.assertTrue(name.getPreferred());
    Assert.assertTrue(address.getPreferred());
}
Also used : PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) Patient(org.openmrs.Patient) PatientIdentifier(org.openmrs.PatientIdentifier) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Aggregations

PersonName (org.openmrs.PersonName)115 Test (org.junit.Test)86 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)57 Patient (org.openmrs.Patient)46 Person (org.openmrs.Person)41 Date (java.util.Date)29 PatientIdentifier (org.openmrs.PatientIdentifier)24 PersonAddress (org.openmrs.PersonAddress)20 User (org.openmrs.User)19 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)17 Location (org.openmrs.Location)13 PatientIdentifierType (org.openmrs.PatientIdentifierType)13 ArrayList (java.util.ArrayList)9 PihCoreContextSensitiveTest (org.openmrs.module.pihcore.PihCoreContextSensitiveTest)8 PersonMergeLog (org.openmrs.person.PersonMergeLog)8 PersonAttribute (org.openmrs.PersonAttribute)7 PatientAndMatchQuality (org.openmrs.module.registrationcore.api.search.PatientAndMatchQuality)7 BindException (org.springframework.validation.BindException)7 Provider (org.openmrs.Provider)6 Errors (org.springframework.validation.Errors)6