Search in sources :

Example 6 with UserService

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

the class UpdateProviderRetiredStatesBasedOnAssociatedUserAccounts method execute.

@Override
public void execute() {
    UserService userService = Context.getUserService();
    ProviderService providerService = Context.getProviderService();
    Set<Person> personsToIncludeWhenRetiringProviderAccts = new HashSet<Person>();
    Set<Person> personsToExcludeWhenRetiringProviderAccts = new HashSet<Person>();
    for (User user : userService.getUsers(null, null, true)) {
        // the user account has been retired over a month ago
        if (user.isRetired() && user.getDateRetired() != null && user.getDateRetired().before(new DateTime().minusMonths(1).toDate())) {
            personsToIncludeWhenRetiringProviderAccts.add(user.getPerson());
        } else {
            personsToExcludeWhenRetiringProviderAccts.add(user.getPerson());
        /* if (!user.isRetired()) {
                    personsToMakeSureHaveActiveProviderAcct.add(user.getPerson());
                }*/
        }
    }
    personsToIncludeWhenRetiringProviderAccts.removeAll(personsToExcludeWhenRetiringProviderAccts);
    // retire all provider accts associated with persons with retired user accts
    for (Person person : personsToIncludeWhenRetiringProviderAccts) {
        for (Provider provider : providerService.getProvidersByPerson(person)) {
            if (!provider.isRetired()) {
                providerService.retireProvider(provider, "user account retired over 1 month ago");
            }
        }
    }
/*   // check all persons to include and make sure, if they have provider accts, that at least one is unretired
        for (Person person : personsToMakeSureHaveActiveProviderAcct) {

            Collection<Provider> providers = providerService.getProvidersByPerson(person);

            if (providers.size() == 1 && providers.iterator().next().isRetired()) {
                providerService.unretireProvider(providers.iterator().next());
            }
            else if (providers.size() > 1) {

                Provider mostRecentlyRetiredProvider = null;
                Boolean noUnretiredProviders = true;
                Iterator<Provider> i = providers.iterator();

                while (i.hasNext()) {
                    Provider provider = i.next();
                    if (!provider.isRetired()) {
                        noUnretiredProviders = false;
                        break;
                    }
                    else if (mostRecentlyRetiredProvider == null || mostRecentlyRetiredProvider.getDateRetired().before(provider.getDateRetired())) {
                        mostRecentlyRetiredProvider = provider;
                    }
                }

                if (noUnretiredProviders) {
                    providerService.unretireProvider(mostRecentlyRetiredProvider);
                }
            }
        }*/
}
Also used : ProviderService(org.openmrs.api.ProviderService) User(org.openmrs.User) UserService(org.openmrs.api.UserService) Person(org.openmrs.Person) DateTime(org.joda.time.DateTime) HashSet(java.util.HashSet) Provider(org.openmrs.Provider)

Example 7 with UserService

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

the class PatientServiceImpl method changeUserAssociations.

/**
 * Change user associations for notPreferred to preferred person.
 *
 * @param preferred
 * @param notPreferred
 * @param mergedData a patient merge audit data object to update
 * @see PatientServiceImpl#mergePatients(Patient, Patient)
 */
private void changeUserAssociations(Patient preferred, Person notPreferred, PersonMergeLogData mergedData) {
    UserService userService = Context.getUserService();
    List<User> users = userService.getUsersByPerson(notPreferred, true);
    for (User user : users) {
        user.setPerson(preferred);
        User persisted = userService.saveUser(user);
        if (mergedData != null) {
            mergedData.addMovedUser(persisted.getUuid());
        }
    }
}
Also used : User(org.openmrs.User) UserService(org.openmrs.api.UserService)

Example 8 with UserService

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

the class ContextTest method becomeUser_shouldChangeLocaleWhenBecomeAnotherUser.

/**
 * @see Context#becomeUser(String)
 */
@Test
public void becomeUser_shouldChangeLocaleWhenBecomeAnotherUser() {
    UserService userService = Context.getUserService();
    User user = new User(new Person());
    user.addName(new PersonName("givenName", "middleName", "familyName"));
    user.getPerson().setGender("M");
    user.setUserProperty(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE, "pt_BR");
    userService.createUser(user, "TestPass123");
    Context.becomeUser(user.getSystemId());
    Locale locale = Context.getLocale();
    Assert.assertEquals("pt", locale.getLanguage());
    Assert.assertEquals("BR", locale.getCountry());
    Context.logout();
}
Also used : Locale(java.util.Locale) PersonName(org.openmrs.PersonName) User(org.openmrs.User) UserService(org.openmrs.api.UserService) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 9 with UserService

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

the class RoleEditor method setAsText.

/**
 * @should set using name
 * @should set using uuid
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    UserService es = Context.getUserService();
    if (StringUtils.hasText(text)) {
        try {
            Role r = es.getRole(text);
            setValue(r);
            // when a role is not found, no exception is generated. throw one to execute the catch block
            if (r == null) {
                throw new Exception();
            }
        } catch (Exception ex) {
            Role r = es.getRoleByUuid(text);
            setValue(r);
            if (r == null) {
                log.error("Error setting text: " + text, ex);
                throw new IllegalArgumentException("Role not found: " + ex.getMessage());
            }
        }
    } else {
        setValue(null);
    }
}
Also used : Role(org.openmrs.Role) UserService(org.openmrs.api.UserService)

Example 10 with UserService

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

the class PrivilegeEditor method setAsText.

/**
 * @should set using name
 * @should set using uuid
 */
@Override
public void setAsText(String text) throws IllegalArgumentException {
    UserService es = Context.getUserService();
    if (StringUtils.hasText(text)) {
        try {
            Privilege p = es.getPrivilege(text);
            setValue(p);
            // when a privilege is not found, no exception is generated. throw one to execute the catch block
            if (p == null) {
                throw new Exception();
            }
        } catch (Exception ex) {
            Privilege p = es.getPrivilegeByUuid(text);
            setValue(p);
            if (p == null) {
                log.error("Error setting text: " + text, ex);
                throw new IllegalArgumentException("Privilege not found: " + ex.getMessage());
            }
        }
    } else {
        setValue(null);
    }
}
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