Search in sources :

Example 1 with UserService

use of org.openmrs.api.UserService in project openmrs-core by openmrs.

the class PersonVoidHandler method handle.

/**
 * Sets all personVoid* attributes to the given parameters.
 *
 * @see org.openmrs.api.handler.RequiredDataHandler#handle(org.openmrs.OpenmrsObject,
 *      org.openmrs.User, java.util.Date, java.lang.String)
 * @should set the personVoided bit
 * @should set the personVoidReason
 * @should set personVoidedBy
 * @should not set personVoidedBy if non null
 * @should set personDateVoided
 * @should not set personDateVoided if non null
 * @should not set the personVoidReason if already personVoided
 * @should retire users
 */
@Override
public void handle(Person person, User voidingUser, Date voidedDate, String voidReason) {
    // skip over all work if the object is already voided
    if (!person.getPersonVoided()) {
        if (person.getPersonId() != null) {
            // Skip if person is not persisted
            UserService us = Context.getUserService();
            for (User user : us.getUsersByPerson(person, false)) {
                us.retireUser(user, voidReason);
            }
        }
        person.setPersonVoided(true);
        person.setPersonVoidReason(voidReason);
        if (person.getPersonVoidedBy() == null) {
            person.setPersonVoidedBy(voidingUser);
        }
        if (person.getPersonDateVoided() == null) {
            person.setPersonDateVoided(voidedDate);
        }
    }
}
Also used : User(org.openmrs.User) UserService(org.openmrs.api.UserService)

Example 2 with UserService

use of org.openmrs.api.UserService 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 3 with UserService

use of org.openmrs.api.UserService 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 4 with UserService

use of org.openmrs.api.UserService in project openmrs-core by openmrs.

the class ContextDAOTest method authenticate_shouldThrowAContextAuthenticationExceptionIfUsernameIsAnEmptyString.

@Test(expected = ContextAuthenticationException.class)
public void authenticate_shouldThrowAContextAuthenticationExceptionIfUsernameIsAnEmptyString() {
    // update a user with a username that is an empty string for this test
    UserService us = Context.getUserService();
    User u = us.getUser(1);
    u.setUsername("");
    u.getPerson().setGender("M");
    us.saveUser(u);
    dao.authenticate("", "password");
}
Also used : User(org.openmrs.User) UserService(org.openmrs.api.UserService) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with UserService

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

the class MirebalaisHospitalActivator method removeOldPrivileges.

/*  private void migratePaperRecordLocation(PaperRecordProperties paperRecordProperties) {

        Context.getAdministrationService().executeSQL("update patient_identifier set location_id = (select location_id from location where uuid='"+
                Locations.MIREBALAIS_HOSPITAL.uuid() + "')" +
                "where identifier_type = (select patient_identifier_type_id from patient_identifier_type where uuid = '" +
                paperRecordProperties.getPaperRecordIdentifierType().getUuid() + "')" +
                "and location_id = (select location_id from location where uuid='" +
                Locations.MIREBALAIS_CDI_PARENT.uuid() + "')", false);

        Context.getAdministrationService().executeSQL("update paperrecord_paper_record set record_location = (select location_id from location where uuid='" +
                Locations.MIREBALAIS_HOSPITAL.uuid() + "')" +
                "where record_location = (select location_id from location where uuid='" +
                Locations.MIREBALAIS_CDI_PARENT.uuid() + "')", false);

    }*/
private void removeOldPrivileges() {
    UserService userService = Context.getUserService();
    Privilege privilege = userService.getPrivilege("App: appointmentschedulingui.scheduleAdmin");
    if (privilege != null) {
        userService.purgePrivilege(privilege);
    }
}
Also used : UserService(org.openmrs.api.UserService) Privilege(org.openmrs.Privilege)

Aggregations

UserService (org.openmrs.api.UserService)10 User (org.openmrs.User)7 Person (org.openmrs.Person)4 PersonName (org.openmrs.PersonName)3 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 Test (org.junit.Test)2 Privilege (org.openmrs.Privilege)2 Role (org.openmrs.Role)2 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)2 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 DateTime (org.joda.time.DateTime)1 PatientIdentifier (org.openmrs.PatientIdentifier)1 PatientIdentifierType (org.openmrs.PatientIdentifierType)1 Provider (org.openmrs.Provider)1 Relationship (org.openmrs.Relationship)1 RelationshipType (org.openmrs.RelationshipType)1 PatientService (org.openmrs.api.PatientService)1