Skip to content

Commit

Permalink
TRUNK-4109: Get observations by accession number
Browse files Browse the repository at this point in the history
org.openmrs.api.ObsService#getObservations and
org.openmrs.api.ObsService#getObservationCount are now overloaded to support
specification of accession number.
If specified, only observations (or their count) with matching accession
number are returned.

See https://issues.openmrs.org/browse/TRUNK-4109

Unit tests for org.openmrs.api.ObsService#getObservations and
org.openmrs.api.ObsService#getObservationCount now use the new
(and most general) methods.
  • Loading branch information
vencik committed Jun 24, 2014
1 parent 8d7d25b commit 05e0e30
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 75 deletions.
71 changes: 68 additions & 3 deletions api/src/main/java/org/openmrs/api/ObsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,39 @@ public interface ObsService extends OpenmrsService {
* @param includeVoidedObs true/false whether to also include the voided obs (required)
* @return list of Observations that match all of the criteria given in the arguments
* @throws APIException
*/
@Authorized(PrivilegeConstants.VIEW_OBS)
public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, List<String> sort,
Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs)
throws APIException;

/**
* @see org.openmrs.api.ObsService#getObservations(java.util.List, java.util.List,
* java.util.List, java.util.List, java.util.List, java.util.List, java.util.List,
* java.lang.Integer, java.lang.Integer, java.util.Date, java.util.Date, boolean)
*
* This method works exactly the same; it only adds accession number search criteria.
* It effectively surpasses the above method; the old one is however kept for backward
* compatibility reasons.
*
* @param whom List<Person> to restrict obs to (optional)
* @param encounters List<Encounter> to restrict obs to (optional)
* @param questions List<Concept> to restrict the obs to (optional)
* @param answers List<Concept> to restrict the valueCoded to (optional)
* @param personTypes List<PERSON_TYPE> objects to restrict this to. Only used if
* <code>whom</code> is an empty list (optional)
* @param locations The org.openmrs.Location objects to restrict to (optional)
* @param sort list of column names to sort on (obsId, obsDatetime, etc) if null, defaults to
* obsDatetime (optional)
* @param mostRecentN restrict the number of obs returned to this size (optional)
* @param obsGroupId the Obs.getObsGroupId() to this integer (optional)
* @param fromDate the earliest Obs date to get (optional)
* @param toDate the latest Obs date to get (optional)
* @param includeVoidedObs true/false whether to also include the voided obs (required)
* @param accessionNumber accession number (optional)
* @return list of Observations that match all of the criteria given in the arguments
* @throws APIException
* @should compare dates using lte and gte
* @should get all obs assigned to given encounters
* @should get all obs with question concept in given questions parameter
Expand All @@ -391,12 +424,13 @@ public interface ObsService extends OpenmrsService {
* @should return obs whose groupId is given obsGroupId
* @should not include voided obs
* @should include voided obs if includeVoidedObs is true
* @should only return observations with matching accession number
*/
@Authorized(PrivilegeConstants.VIEW_OBS)
public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, List<String> sort,
Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs)
throws APIException;
Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs,
String accessionNumber) throws APIException;

/**
* This method fetches the count of observations according to the criteria in the given
Expand Down Expand Up @@ -426,6 +460,36 @@ public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters,
* @param includeVoidedObs true/false whether to also include the voided obs (required)
* @return list of Observations that match all of the criteria given in the arguments
* @throws APIException
*/
@Authorized(PrivilegeConstants.VIEW_OBS)
public Integer getObservationCount(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, Integer obsGroupId,
Date fromDate, Date toDate, boolean includeVoidedObs) throws APIException;

/**
* @see org.openmrs.api.ObsService#getObservationCount(java.util.List, java.util.List,
* java.util.List, java.util.List, java.util.List, java.util.List, java.lang.Integer,
* java.util.Date, java.util.Date, boolean)
*
* This method works exactly the same; it only adds accession number search criteria.
* It effectively surpasses the above method; the old one is however kept for backward
* compatibility reasons.
*
* @param whom List<Person> to restrict obs to (optional)
* @param encounters List<Encounter> to restrict obs to (optional)
* @param questions List<Concept> to restrict the obs to (optional)
* @param answers List<Concept> to restrict the valueCoded to (optional)
* @param personTypes List<PERSON_TYPE> objects to restrict this to. Only used if
* <code>whom</code> is an empty list (optional)
* @param locations The org.openmrs.Location objects to restrict to (optional) obsDatetime
* (optional)
* @param obsGroupId the Obs.getObsGroupId() to this integer (optional)
* @param fromDate the earliest Obs date to get (optional)
* @param toDate the latest Obs date to get (optional)
* @param includeVoidedObs true/false whether to also include the voided obs (required)
* @param accessionNumber accession number (optional)
* @return list of Observations that match all of the criteria given in the arguments
* @throws APIException
* @should compare dates using lte and gte
* @should get the count of all obs assigned to given encounters
* @should get the count of all obs with question concept in given questions parameter
Expand All @@ -437,11 +501,12 @@ public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters,
* @should return the count of obs whose groupId is given obsGroupId
* @should not include count of voided obs
* @should include count of voided obs if includeVoidedObs is true
* @should return count of obs with matching accession number
*/
@Authorized(PrivilegeConstants.VIEW_OBS)
public Integer getObservationCount(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, Integer obsGroupId,
Date fromDate, Date toDate, boolean includeVoidedObs) throws APIException;
Date fromDate, Date toDate, boolean includeVoidedObs, String accessionNumber) throws APIException;

/**
* This method searches the obs table based on the given <code>searchString</code>.
Expand Down
13 changes: 7 additions & 6 deletions api/src/main/java/org/openmrs/api/db/ObsDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,24 @@ public interface ObsDAO {
/**
* @see org.openmrs.api.ObsService#getObservations(java.util.List, java.util.List,
* java.util.List, java.util.List, java.util.List, java.util.List, java.util.List,
* java.lang.Integer, java.lang.Integer, java.util.Date, java.util.Date, boolean)
* java.lang.Integer, java.lang.Integer, java.util.Date, java.util.Date, boolean,
* java.lang.String)
*/
public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, List<String> sort,
Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs)
throws DAOException;
Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs,
String accessionNumber) throws DAOException;

/**
* @see org.openmrs.api.ObsService#getObservationCount(java.util.List, java.util.List,
* java.util.List, java.util.List, java.util.List, java.util.List, java.lang.Integer,
* java.util.Date, java.util.Date, boolean)
* java.util.Date, java.util.Date, boolean, java.lang.String)
* @see ObsService#getObservationCount(org.openmrs.ConceptName, boolean)
*/
public Long getObservationCount(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, Integer obsGroupId,
Date fromDate, Date toDate, List<ConceptName> valueCodedNameAnswers, boolean includeVoidedObs)
throws DAOException;
Date fromDate, Date toDate, List<ConceptName> valueCodedNameAnswers, boolean includeVoidedObs,
String accessionNumber) throws DAOException;

/**
* Auto generated method comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,31 @@ public Obs saveObs(Obs obs) throws DAOException {

/**
* @see org.openmrs.api.db.ObsDAO#getObservations(List, List, List, List, List, List, List,
* Integer, Integer, Date, Date, boolean)
* Integer, Integer, Date, Date, boolean, String)
*/
@SuppressWarnings("unchecked")
public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, List<String> sortList,
Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs)
throws DAOException {
Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs,
String accessionNumber) throws DAOException {

Criteria criteria = createGetObservationsCriteria(whom, encounters, questions, answers, personTypes, locations,
sortList, mostRecentN, obsGroupId, fromDate, toDate, null, includeVoidedObs);
sortList, mostRecentN, obsGroupId, fromDate, toDate, null, includeVoidedObs, accessionNumber);

return criteria.list();
}

/**
* @see org.openmrs.api.db.ObsDAO#getObservationCount(java.util.List, java.util.List,
* java.util.List, java.util.List, java.util.List, java.util.List, java.lang.Integer,
* java.util.Date, java.util.Date, boolean)
* java.util.Date, java.util.Date, boolean, String)
*/
public Long getObservationCount(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, Integer obsGroupId,
Date fromDate, Date toDate, List<ConceptName> valueCodedNameAnswers, boolean includeVoidedObs)
throws DAOException {
Date fromDate, Date toDate, List<ConceptName> valueCodedNameAnswers, boolean includeVoidedObs,
String accessionNumber) throws DAOException {
Criteria criteria = createGetObservationsCriteria(whom, encounters, questions, answers, personTypes, locations,
null, null, obsGroupId, fromDate, toDate, valueCodedNameAnswers, includeVoidedObs);
null, null, obsGroupId, fromDate, toDate, valueCodedNameAnswers, includeVoidedObs, accessionNumber);
criteria.setProjection(Projections.rowCount());
return (Long) criteria.list().get(0);
}
Expand All @@ -186,12 +186,13 @@ public Long getObservationCount(List<Person> whom, List<Encounter> encounters, L
* @param fromDate
* @param toDate
* @param includeVoidedObs
* @param accessionNumber
* @return
*/
private Criteria createGetObservationsCriteria(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, List<String> sortList,
Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate, List<ConceptName> valueCodedNameAnswers,
boolean includeVoidedObs) {
boolean includeVoidedObs, String accessionNumber) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Obs.class, "obs");

if (CollectionUtils.isNotEmpty(whom)) {
Expand Down Expand Up @@ -260,6 +261,11 @@ private Criteria createGetObservationsCriteria(List<Person> whom, List<Encounter
if (!includeVoidedObs) {
criteria.add(Restrictions.eq("voided", false));
}

if (accessionNumber != null) {
criteria.add(Restrictions.eq("accessionNumber", accessionNumber));
}

return criteria;
}

Expand Down
41 changes: 38 additions & 3 deletions api/src/main/java/org/openmrs/api/impl/ObsServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,29 @@ public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters,
}

return dao.getObservations(whom, encounters, questions, answers, personTypes, locations, sort, mostRecentN,
obsGroupId, fromDate, toDate, includeVoidedObs);
obsGroupId, fromDate, toDate, includeVoidedObs, null);
}

/**
* @see org.openmrs.api.ObsService#getObservations(java.util.List, java.util.List,
* java.util.List, java.util.List, List, List, java.util.List, java.lang.Integer,
* java.lang.Integer, java.util.Date, java.util.Date, boolean, java.lang.String)
*/
@Transactional(readOnly = true)
public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, List<String> sort,
Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs,
String accessionNumber) throws APIException {

if (sort == null) {
sort = new Vector<String>();
}
if (sort.isEmpty()) {
sort.add("obsDatetime");
}

return dao.getObservations(whom, encounters, questions, answers, personTypes, locations, sort, mostRecentN,
obsGroupId, fromDate, toDate, includeVoidedObs, accessionNumber);
}

/**
Expand All @@ -333,7 +355,20 @@ public Integer getObservationCount(List<Person> whom, List<Encounter> encounters
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, Integer obsGroupId,
Date fromDate, Date toDate, boolean includeVoidedObs) throws APIException {
return OpenmrsUtil.convertToInteger(dao.getObservationCount(whom, encounters, questions, answers, personTypes,
locations, obsGroupId, fromDate, toDate, null, includeVoidedObs));
locations, obsGroupId, fromDate, toDate, null, includeVoidedObs, null));
}

/**
* @see org.openmrs.api.ObsService#getObservationCount(java.util.List, java.util.List,
* java.util.List, java.util.List, java.util.List, java.util.List, java.lang.Integer,
* java.util.Date, java.util.Date, boolean, java.lang.String)
*/
@Transactional(readOnly = true)
public Integer getObservationCount(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, Integer obsGroupId,
Date fromDate, Date toDate, boolean includeVoidedObs, String accessionNumber) throws APIException {
return OpenmrsUtil.convertToInteger(dao.getObservationCount(whom, encounters, questions, answers, personTypes,
locations, obsGroupId, fromDate, toDate, null, includeVoidedObs, accessionNumber));
}

/**
Expand Down Expand Up @@ -875,7 +910,7 @@ public void registerHandler(String key, String handlerClass) throws APIException
@Transactional(readOnly = true)
public Integer getObservationCount(List<ConceptName> conceptNames, boolean includeVoided) {
return OpenmrsUtil.convertToInteger(dao.getObservationCount(null, null, null, null, null, null, null, null, null,
conceptNames, true));
conceptNames, true, null));
}

/**
Expand Down

0 comments on commit 05e0e30

Please sign in to comment.