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);
}
}
}*/
}
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());
}
}
}
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();
}
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);
}
}
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);
}
}
Aggregations