Skip to content

Commit

Permalink
TRUNK-5332: HibernateProviderDAO.getCountOfProviders handles more tha…
Browse files Browse the repository at this point in the history
…n one match.
  • Loading branch information
chinzou authored and mks-d committed Feb 12, 2018
1 parent af32ddc commit ef36406
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
Expand All @@ -25,6 +25,7 @@
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.openmrs.Person;
import org.openmrs.Provider;
import org.openmrs.ProviderAttribute;
Expand Down Expand Up @@ -63,6 +64,7 @@ private Session getSession() {
/**
* @see org.openmrs.api.db.ProviderDAO#saveProvider(org.openmrs.Provider)
*/
@Override
public Provider saveProvider(Provider provider) {
getSession().saveOrUpdate(provider);
return provider;
Expand All @@ -81,7 +83,7 @@ public void deleteProvider(Provider provider) {
*/
@Override
public Provider getProvider(Integer id) {
return (Provider) getSession().load(Provider.class, id);
return (Provider) getSession().get(Provider.class, id);
}

/**
Expand Down Expand Up @@ -116,7 +118,7 @@ public Collection<Provider> getProvidersByPerson(Person person, boolean includeR
*/
@Override
public ProviderAttribute getProviderAttribute(Integer providerAttributeID) {
return (ProviderAttribute) getSession().load(ProviderAttribute.class, providerAttributeID);
return (ProviderAttribute) getSession().get(ProviderAttribute.class, providerAttributeID);
}

/**
Expand Down Expand Up @@ -184,14 +186,15 @@ private Criteria prepareProviderCriteria(String name, boolean includeRetired) {
}

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Provider.class).createAlias("person", "p",
Criteria.LEFT_JOIN);
JoinType.LEFT_OUTER_JOIN);

if (!includeRetired) {
criteria.add(Restrictions.eq("retired", false));
}

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

criteria.createAlias("p.names", "personName", Criteria.LEFT_JOIN);
criteria.createAlias("p.names", "personName", JoinType.LEFT_OUTER_JOIN);

Disjunction or = Restrictions.disjunction();
or.add(Restrictions.ilike("identifier", name, getMatchMode()));
Expand Down Expand Up @@ -238,9 +241,8 @@ private Junction getNameSearchExpression(String name) {
*/
@Override
public Long getCountOfProviders(String name, boolean includeRetired) {
Criteria criteria = prepareProviderCriteria(name, includeRetired);
criteria.setProjection(Projections.countDistinct("providerId"));
return (Long) criteria.uniqueResult();
Criteria criteria = prepareProviderCriteria(name, includeRetired);
return (long) criteria.list().size();
}

/* (non-Javadoc)
Expand Down Expand Up @@ -274,7 +276,7 @@ private <T> T getByUuid(String uuid, Class<T> clazz) {
*/
@Override
public ProviderAttributeType getProviderAttributeType(Integer providerAttributeTypeId) {
return (ProviderAttributeType) getSession().load(ProviderAttributeType.class, providerAttributeTypeId);
return (ProviderAttributeType) getSession().get(ProviderAttributeType.class, providerAttributeTypeId);
}

/* (non-Javadoc)
Expand Down
4 changes: 1 addition & 3 deletions api/src/test/java/org/openmrs/api/ProviderServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@ public void getCountOfProviders_shouldFetchNumberOfProviderMatchingGivenQuery()
* @see ProviderService#getCountOfProviders(String)
*/
@Test
@Ignore
@Verifies(value = "should exclude retired providers", method = "getCountOfProviders(String)")
public void getCountOfProviders_shouldExcludeRetiredProviders() throws Exception {
assertEquals(2, service.getCountOfProviders("provider").intValue());
Expand All @@ -530,10 +529,9 @@ public void getCountOfProviders_shouldExcludeRetiredProviders() throws Exception
* @see ProviderService#getCountOfProviders(String,null)
*/
@Test
@Ignore
@Verifies(value = "should include retired providers if includeRetired is set to true", method = "getCountOfProviders(String,null)")
public void getCountOfProviders_shouldIncludeRetiredProvidersIfIncludeRetiredIsSetToTrue() throws Exception {
assertEquals(4, service.getCountOfProviders("provider").intValue());
assertEquals(4, service.getCountOfProviders("provider", true).intValue());
}

/**
Expand Down

0 comments on commit ef36406

Please sign in to comment.