Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EA-201 - Add additional method to AccountService to find accounts by … #241

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.openmrs.module.emrapi.account;

import lombok.Data;

/**
* Represents criteria for searching for Accounts
*/
@Data
public class AccountSearchCriteria {

private String nameOrIdentifier;

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public interface AccountService {
*/
List<AccountDomainWrapper> getAllAccounts();

/**
* @should get all unique accounts that match the given criteria
*/
List<AccountDomainWrapper> getAccounts(AccountSearchCriteria criteria);

/**
* Save the account details to the database
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openmrs.module.emrapi.account;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.openmrs.Person;
import org.openmrs.Privilege;
import org.openmrs.Provider;
Expand All @@ -12,10 +14,12 @@
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.emrapi.EmrApiConstants;
import org.openmrs.module.emrapi.EmrApiProperties;
import org.openmrs.module.emrapi.db.EmrApiDAO;
import org.openmrs.module.emrapi.domainwrapper.DomainWrapperFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -33,6 +37,8 @@ public class AccountServiceImpl extends BaseOpenmrsService implements AccountSer

private EmrApiProperties emrApiProperties;

private EmrApiDAO emrApiDAO;

/**
* @param userService the userService to set
*/
Expand Down Expand Up @@ -62,47 +68,66 @@ public void setEmrApiProperties(EmrApiProperties emrApiProperties) {
this.emrApiProperties = emrApiProperties;
}

public void setEmrApiDAO(EmrApiDAO emrApiDAO) {
this.emrApiDAO = emrApiDAO;
}

/**
* @see org.openmrs.module.emrapi.account.AccountService#getAllAccounts()
*/
@Override
@Transactional(readOnly = true)
public List<AccountDomainWrapper> getAllAccounts() {
return getAccounts(new AccountSearchCriteria());
}

Map<Person, AccountDomainWrapper> byPerson = new LinkedHashMap<Person, AccountDomainWrapper>();
@Override
public List<AccountDomainWrapper> getAccounts(AccountSearchCriteria criteria) {
Map<Person, AccountDomainWrapper> byPerson = new LinkedHashMap<>();
List<User> users;
List<Provider> providers;
if (StringUtils.isNotBlank(criteria.getNameOrIdentifier())) {
Map<String, Object> searchParams = new HashMap<>();
searchParams.put("search", "%" + criteria.getNameOrIdentifier() + "%");
users = emrApiDAO.executeHqlFromResource("hql/user_search.hql", searchParams, User.class);
providers = emrApiDAO.executeHqlFromResource("hql/provider_search.hql", searchParams, Provider.class);
}
else {
users = userService.getAllUsers();
providers = providerService.getAllProviders();
}

for (User user : userService.getAllUsers()) {
for (User user : users) {
//exclude daemon user
if (EmrApiConstants.DAEMON_USER_UUID.equals(user.getUuid()))
if (EmrApiConstants.DAEMON_USER_UUID.equals(user.getUuid())) {
continue;

if (!user.getPerson().isPersonVoided()) {
byPerson.put(user.getPerson(), domainWrapperFactory.newAccountDomainWrapper(user.getPerson()));
}
Person person = user.getPerson();
if (BooleanUtils.isNotTrue(person.getPersonVoided())) {
byPerson.put(person, domainWrapperFactory.newAccountDomainWrapper(person));
}
}

for (Provider provider : providerService.getAllProviders()) {
for (Provider provider : providers) {

// skip the baked-in unknown provider
if (provider.equals(emrApiProperties.getUnknownProvider())) {
continue;
}

if (provider.getPerson() == null)
throw new APIException("Providers not associated to a person are not supported");
Person person = provider.getPerson();

AccountDomainWrapper account = byPerson.get(provider.getPerson());
if (account == null && !provider.getPerson().isPersonVoided()) {
byPerson.put(provider.getPerson(), domainWrapperFactory.newAccountDomainWrapper(provider.getPerson()));
if (person == null) {
throw new APIException("Providers not associated to a person are not supported");
}
}

List<AccountDomainWrapper> accounts = new ArrayList<AccountDomainWrapper>();
for (AccountDomainWrapper account : byPerson.values()) {
accounts.add(account);
AccountDomainWrapper account = byPerson.get(person);
if (account == null && BooleanUtils.isNotTrue(person.getPersonVoided())) {
byPerson.put(person, domainWrapperFactory.newAccountDomainWrapper(person));
}
}

return accounts;
return new ArrayList<>(byPerson.values());
}

/**
Expand Down
20 changes: 20 additions & 0 deletions api/src/main/resources/hql/provider_search.hql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
select
pr
from
Provider as pr
left join pr.person as p
left join p.names as pn
where (p.voided is null or p.voided = false)
and (pn.voided is null or pn.voided = false)
and (
:search is null or
pr.identifier like :search or
pn.prefix like :search or
pn.givenName like :search or
pn.middleName like :search or
pn.familyNamePrefix like :search or
pn.familyName like :search or
pn.familyName2 like :search or
pn.familyNameSuffix like :search or
pn.degree like :search
)
22 changes: 22 additions & 0 deletions api/src/main/resources/hql/user_search.hql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
select
u
from
User as u
left join u.person as p
left join p.names as pn
where (p.voided is null or p.voided = false)
and (pn.voided is null or pn.voided = false)
and (
:search is null or
u.username like :search or
u.systemId like :search or
u.email like :search or
pn.prefix like :search or
pn.givenName like :search or
pn.middleName like :search or
pn.familyNamePrefix like :search or
pn.familyName like :search or
pn.familyName2 like :search or
pn.familyNameSuffix like :search or
pn.degree like :search
)
1 change: 1 addition & 0 deletions api/src/main/resources/moduleApplicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<property name="providerService" ref="providerService"/>
<property name="domainWrapperFactory" ref="domainWrapperFactory"/>
<property name="emrApiProperties" ref="emrApiProperties"/>
<property name="emrApiDAO" ref="emrApiDAOImpl"/>
</bean>
</property>
<property name="preInterceptors">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import java.util.Locale;
import java.util.Set;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

public class AccountComponentTest extends EmrApiContextSensitiveTest {

@Autowired
Expand All @@ -51,6 +54,7 @@ public class AccountComponentTest extends EmrApiContextSensitiveTest {

@Before
public void beforeAllTests() throws Exception {
executeDataSet("baseTestDataset.xml");
executeDataSet("accountComponentTestDataset.xml");
}

Expand Down Expand Up @@ -248,4 +252,36 @@ public void shouldHandlePersonWithoutUser() {

}

/**
* @see AccountService#getAccounts(AccountSearchCriteria) ()
*/
@Test
public void shouldGetAccountsThatMatchSearchCriteria() throws Exception {
// user 501: username = bruno, systemId = 2-6, retired = true, person=501, provider 1001, identifier = 123, retired = false
// user 502: username = butch, systemId = 3-4, retired = false, person=502
// provider 1, person=1, identifier = Test, retired = false
// provider 5000, person=5000, name = John Smith, set as unknown provider
// provider 5001, person=5001, name = Mary Smith, no user

// Should include all accounts except for the unknown provider
AccountSearchCriteria criteria = new AccountSearchCriteria();
List<AccountDomainWrapper> allAccounts = accountService.getAccounts(criteria);
assertThat(allAccounts.size(), equalTo(4));

criteria.setNameOrIdentifier("bruno");
allAccounts = accountService.getAccounts(criteria);
assertThat(allAccounts.size(), equalTo(1));

criteria.setNameOrIdentifier("Smith");
allAccounts = accountService.getAccounts(criteria);
assertThat(allAccounts.size(), equalTo(1));

criteria.setNameOrIdentifier("3");
allAccounts = accountService.getAccounts(criteria);
assertThat(allAccounts.size(), equalTo(2));

criteria.setNameOrIdentifier("Bob");
allAccounts = accountService.getAccounts(criteria);
assertThat(allAccounts.size(), equalTo(0));
}
}
9 changes: 9 additions & 0 deletions api/src/test/resources/baseTestDataset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@
retired = "false"
/>

<person person_id="5000" gender="M" birthdate="1948-01-01 00:00:00.0" birthdate_estimated="0" dead="false" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="343a8d95-6b1d-11ef-9f7f-0242ac120002"/>
<person_name person_name_id="5000" preferred="true" person_id="5000" given_name="John" family_name="Smith" creator="1" date_created="2005-09-22 00:00:00.0" voided="false" void_reason="" uuid="8f70ed2a-6b1d-11ef-9f7f-0242ac120002"/>
<provider provider_id="5000" person_id="5000" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" uuid="a6135c13-6b1c-11ef-9f7f-0242ac120002"/>

<person person_id="5001" gender="F" birthdate="1952-01-01 00:00:00.0" birthdate_estimated="0" dead="false" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="0670f680-6b1f-11ef-9f7f-0242ac120002"/>
<person_name person_name_id="5001" preferred="true" person_id="5001" given_name="Mary" family_name="Smith" creator="1" date_created="2005-09-22 00:00:00.0" voided="false" void_reason="" uuid="0fc40bbe-6b1f-11ef-9f7f-0242ac120002"/>
<provider provider_id="5001" person_id="5001" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" uuid="15864a30-6b1f-11ef-9f7f-0242ac120002"/>


<metadatamapping_metadata_term_mapping metadata_term_mapping_id = "3245" metadata_source_id = "2134"
creator = "1" date_created="2008-08-18 12:38:58.0" uuid="cf25712e-f193-48d5-a85a-5fd8d0244c71"
retired = "false" code = "emr.unknownProvider" name = "Unknown Provider"
metadata_uuid = "a6135c13-6b1c-11ef-9f7f-0242ac120002" metadata_class = "org.openmrs.Provider"
/>

<metadatamapping_metadata_term_mapping metadata_term_mapping_id = "3246" metadata_source_id = "2134"
Expand Down
Loading