Search in sources :

Example 71 with User

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

the class MigrationHelper method importRelationships.

/**
 * Takes a list of Strings of the format RELATIONSHIP:<user last name>,<user first
 * name>,<relationship type name>,<patient identifier type name>,<identifier> 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 72 with User

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

the class PatientServiceTest method mergePatients_shouldChangeUserRecordsOfNonPreferredPersonToPreferredPerson.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldChangeUserRecordsOfNonPreferredPersonToPreferredPerson() throws Exception {
    executeDataSet(USERS_WHO_ARE_PATIENTS_XML);
    Patient notPreferred = patientService.getPatient(2);
    voidOrders(Collections.singleton(notPreferred));
    Context.getPatientService().mergePatients(patientService.getPatient(6), notPreferred);
    User user = Context.getUserService().getUser(2);
    Assert.assertEquals(6, user.getPerson().getId().intValue());
}
Also used : User(org.openmrs.User) Patient(org.openmrs.Patient) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 73 with User

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

the class PatientServiceTest method getPatients_shouldSupportSimpleRegex.

/**
 * @see PatientService#getPatients(String, String, java.util.List, boolean)
 */
@Test
public void getPatients_shouldSupportSimpleRegex() throws Exception {
    Context.getAdministrationService().saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_REGEX, "^0*@SEARCH@([A-Z]+-[0-9])?$"));
    PatientIdentifier identifier = new PatientIdentifier("1234-4", new PatientIdentifierType(1), new Location(1));
    identifier.setCreator(new User(1));
    identifier.setDateCreated(new Date());
    Patient patient = Context.getPatientService().getPatient(2);
    patient.addIdentifier(identifier);
    Context.getPatientService().savePatient(patient);
    updateSearchIndex();
    assertEquals(1, Context.getPatientService().getPatients("1234-4").size());
}
Also used : User(org.openmrs.User) Patient(org.openmrs.Patient) PatientIdentifier(org.openmrs.PatientIdentifier) PatientIdentifierType(org.openmrs.PatientIdentifierType) Date(java.util.Date) GlobalProperty(org.openmrs.GlobalProperty) Location(org.openmrs.Location) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 74 with User

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

the class PatientServiceTest method shouldAllowGettingPatientsThatWereCreatedByUsersWhoArePatients.

/**
 * Make sure the api can handle having a User object that is also a patient and was previously
 * loaded via hibernate
 *
 * @throws Exception
 */
@Test
public void shouldAllowGettingPatientsThatWereCreatedByUsersWhoArePatients() throws Exception {
    executeDataSet(USERS_WHO_ARE_PATIENTS_XML);
    // we must fetch this person first, because this person is
    // the creator of the next. We need to make sure hibernate isn't
    // caching and returning different person objects when it shouldn't be
    Patient patient2 = patientService.getPatient(2);
    assertTrue("When getting a patient, it should be of the class patient, not: " + patient2.getClass(), patient2.getClass().equals(Patient.class));
    Patient patient3 = patientService.getPatient(3);
    assertTrue("When getting a patient, it should be of the class patient, not: " + patient3.getClass(), patient3.getClass().equals(Patient.class));
    User user2 = Context.getUserService().getUser(2);
    assertTrue("When getting a user, it should be of the class user, not: " + user2.getClass(), User.class.isAssignableFrom(user2.getClass()));
}
Also used : User(org.openmrs.User) Patient(org.openmrs.Patient) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 75 with User

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

the class DaemonTest method isDaemonUser_shouldReturnTrueForADaemonUser.

/**
 * @see Daemon#isDaemonUser(User user)
 */
@Test
public void isDaemonUser_shouldReturnTrueForADaemonUser() {
    User user = new User();
    user.setUuid(Daemon.DAEMON_USER_UUID);
    Assert.assertTrue(Daemon.isDaemonUser(user));
}
Also used : User(org.openmrs.User) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

User (org.openmrs.User)201 Test (org.junit.Test)150 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)132 Date (java.util.Date)38 Person (org.openmrs.Person)33 Encounter (org.openmrs.Encounter)21 Patient (org.openmrs.Patient)18 PersonName (org.openmrs.PersonName)17 Role (org.openmrs.Role)13 GlobalProperty (org.openmrs.GlobalProperty)11 Location (org.openmrs.Location)11 ArrayList (java.util.ArrayList)10 EncounterType (org.openmrs.EncounterType)10 Locale (java.util.Locale)7 UserService (org.openmrs.api.UserService)7 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)7 BindException (org.springframework.validation.BindException)7 Errors (org.springframework.validation.Errors)7 EncounterRole (org.openmrs.EncounterRole)6 PatientIdentifier (org.openmrs.PatientIdentifier)6