Skip to content

Commit

Permalink
Merge pull request openmrs#2710 from dsurrao/TRUNK-5424
Browse files Browse the repository at this point in the history
TRUNK-5424: Added Order search criteria
  • Loading branch information
mogoodrich authored Sep 13, 2018
2 parents 3ddf773 + ae5bfe6 commit a994962
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/src/main/java/org/openmrs/api/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openmrs.Provider;
import org.openmrs.annotation.Authorized;
import org.openmrs.api.db.OrderDAO;
import org.openmrs.parameter.OrderSearchCriteria;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.PrivilegeConstants;

Expand Down Expand Up @@ -250,6 +251,18 @@ public interface OrderService extends OpenmrsService {
*/
@Authorized(PrivilegeConstants.GET_ORDERS)
public List<Order> getAllOrdersByPatient(Patient patient);

/**
* Get all orders that match a variety of (nullable) criteria contained in the parameter object.
* Each extra value for a parameter that is provided acts as an "and" and will reduce the number of results returned
*
* @param orderSearchCriteria the object containing search parameters
* @return a list of orders matching the search criteria
* @since 2.2
* @should get the order matching the search criteria
*/
@Authorized( { PrivilegeConstants.GET_ORDERS })
public List<Order> getOrders(OrderSearchCriteria orderSearchCriteria);

/**
* Unvoid order record. Reverse a previous call to {@link #voidOrder(Order, String)}
Expand Down
6 changes: 6 additions & 0 deletions api/src/main/java/org/openmrs/api/db/OrderDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openmrs.Patient;
import org.openmrs.User;
import org.openmrs.api.APIException;
import org.openmrs.parameter.OrderSearchCriteria;

/**
* Order-related database functions
Expand Down Expand Up @@ -71,6 +72,11 @@ public List<Order> getOrders(OrderType orderType, List<Patient> patients, List<C
*/
public List<Order> getOrders(Patient patient, CareSetting careSetting, List<OrderType> orderTypes,
boolean includeVoided, boolean includeDiscontinuationOrders);

/**
* @see org.openmrs.api.OrderService#getOrders(OrderSearchCriteria)
*/
public List<Order> getOrders(OrderSearchCriteria orderSearchCriteria);

/**
* @param uuid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Calendar;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
Expand Down Expand Up @@ -41,10 +42,13 @@
import org.openmrs.api.APIException;
import org.openmrs.api.db.DAOException;
import org.openmrs.api.db.OrderDAO;
import org.openmrs.parameter.OrderSearchCriteria;
import org.openmrs.util.OpenmrsConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.print.attribute.standard.RequestingUserName;

/**
* This class should not be used directly. This is just a common implementation of the OrderDAO that
* is used by the OrderService. This class is injected by spring into the desired OrderService
Expand Down Expand Up @@ -147,6 +151,54 @@ public List<Order> getOrders(OrderType orderType, List<Patient> patients, List<C

return crit.list();
}

/**
* @see org.openmrs.api.db.OrderDAO#getOrders(OrderSearchCriteria)
*/
@Override
public List<Order> getOrders(OrderSearchCriteria searchCriteria) {
Criteria crit = sessionFactory.getCurrentSession().createCriteria(Order.class);

if (searchCriteria.getPatient() != null && searchCriteria.getPatient().getPatientId() != null) {
crit.add(Restrictions.eq("patient", searchCriteria.getPatient()));
}
if (searchCriteria.getCareSetting() != null && searchCriteria.getCareSetting().getId() != null) {
crit.add(Restrictions.eq("careSetting", searchCriteria.getCareSetting()));
}
if (searchCriteria.getConcepts() != null && !searchCriteria.getConcepts().isEmpty()) {
crit.add(Restrictions.in("concept", searchCriteria.getConcepts()));
}
if (searchCriteria.getOrderTypes() != null && !searchCriteria.getOrderTypes().isEmpty()) {
crit.add(Restrictions.in("orderType", searchCriteria.getOrderTypes()));
}
if (searchCriteria.getActivatedOnOrBeforeDate() != null) {
// set the date's time to the last millisecond of the date
Calendar cal = Calendar.getInstance();
cal.setTime(searchCriteria.getActivatedOnOrBeforeDate());
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
crit.add(Restrictions.le("dateActivated", cal.getTime()));
}
if (searchCriteria.getActivatedOnOrAfterDate() != null) {
// set the date's time to 00:00:00.000
Calendar cal = Calendar.getInstance();
cal.setTime(searchCriteria.getActivatedOnOrAfterDate());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
crit.add(Restrictions.ge("dateActivated", cal.getTime()));
}
if (!searchCriteria.getIncludeVoided()) {
crit.add(Restrictions.eq("voided", false));
}

crit.addOrder(org.hibernate.criterion.Order.desc("dateActivated"));

return crit.list();
}

/**
* @see OrderDAO#getOrders(org.openmrs.Patient, org.openmrs.CareSetting, java.util.List,
Expand Down
6 changes: 6 additions & 0 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.openmrs.api.EditedOrderDoesNotMatchPreviousException;
import org.openmrs.api.OrderEntryException;
import org.openmrs.order.OrderUtil;
import org.openmrs.parameter.OrderSearchCriteria;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.OpenmrsUtil;
import org.slf4j.Logger;
Expand Down Expand Up @@ -532,6 +533,11 @@ public List<Order> getAllOrdersByPatient(Patient patient) {
return dao.getOrders(patient, null, null, true, true);
}

@Override
public List<Order> getOrders(OrderSearchCriteria orderSearchCriteria) {
return dao.getOrders(orderSearchCriteria);
}

/**
* @see org.openmrs.api.OrderService#getOrderByUuid(java.lang.String)
*/
Expand Down
106 changes: 106 additions & 0 deletions api/src/main/java/org/openmrs/parameter/OrderSearchCriteria.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.parameter;

import org.openmrs.CareSetting;
import org.openmrs.Concept;
import org.openmrs.OrderType;
import org.openmrs.Patient;

import java.util.Collection;
import java.util.Date;

/**
* The search parameter object for orders. A convenience interface for building
* instances is provided by {@link OrderSearchCriteriaBuilder}.
*
* @since 2.2
* @see OrderSearchCriteriaBuilder
*/
public class OrderSearchCriteria {

private final Patient patient;

private final CareSetting careSetting;

private final Collection<Concept> concepts;

private final Collection<OrderType> orderTypes;

/**
* Matches on dateActivated that is any time on this date or less
*/
private final Date activatedOnOrBeforeDate;

/**
* Matches on dateActivated that is any time on this date or more
*/
private final Date activatedOnOrAfterDate;

private final boolean includeVoided;

/**
* Instead of calling this constructor directly, it is recommended to use {@link OrderSearchCriteriaBuilder}.
* @param patient the patient the order is for
* @param careSetting the care setting to match on
* @param concepts the concepts defining the order must be in this collection
* @param orderTypes the order types to match on must be in this collection
* @param activatedOnOrBeforeDate orders must have dateActivated on or before this date
* @param activatedOnOrAfterDate orders must have dateActivated on or after this date
* @param includeVoided whether to include the voided orders or not
*/
public OrderSearchCriteria(Patient patient, CareSetting careSetting, Collection<Concept> concepts,
Collection<OrderType> orderTypes, Date activatedOnOrBeforeDate,
Date activatedOnOrAfterDate, boolean includeVoided) {
this.patient = patient;
this.careSetting = careSetting;
this.concepts = concepts;
this.orderTypes = orderTypes;
this.activatedOnOrBeforeDate = activatedOnOrBeforeDate;
this.activatedOnOrAfterDate = activatedOnOrAfterDate;
this.includeVoided = includeVoided;
}

/**
* @return the patient the order is for
*/
public Patient getPatient() { return patient; }

/**
* @return the care setting to match on
*/
public CareSetting getCareSetting() { return careSetting; }

/**
* @return the concepts defining the order must be in this collection
*/
public Collection<Concept> getConcepts() { return concepts; }

/**
* @return the order types to match on must be in this collection
*/
public Collection<OrderType> getOrderTypes() { return orderTypes; }

/**
* @return orders must have dateActivated on or before this date
*/
public Date getActivatedOnOrBeforeDate() { return activatedOnOrBeforeDate; }

/**
* @return orders must have dateActivated on or after this date
*/
public Date getActivatedOnOrAfterDate() { return activatedOnOrAfterDate; }

/**
* @return whether to include the voided orders or not
*/
public boolean getIncludeVoided() { return includeVoided; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.parameter;

import org.openmrs.CareSetting;
import org.openmrs.Concept;
import org.openmrs.OrderType;
import org.openmrs.Patient;

import java.util.Collection;
import java.util.Date;

/**
* A convenience builder for {@link OrderSearchCriteria}. Create a builder, set
* its properties to desired values and finally call {@link #createOrderSearchCriteria()}
* to create the actual search criteria instance.
* @see OrderSearchCriteria
*/
public class OrderSearchCriteriaBuilder {
private Patient patient;

private CareSetting careSetting;

private Collection<Concept> concepts;

private Collection<OrderType> orderTypes;

private Date activatedOnOrBeforeDate;

private Date activatedOnOrAfterDate;

private boolean includeVoided;

/**
* @param patient the patient the order is for
* @return this builder instance
*/
public OrderSearchCriteriaBuilder setPatient(Patient patient) {
this.patient = patient;
return (this);
}

/**
* @param careSetting the care setting to match on
* @return this builder instance
*/
public OrderSearchCriteriaBuilder setCareSetting(CareSetting careSetting) {
this.careSetting = careSetting;
return (this);
}

/**
* @param concepts the concepts defining the order must be in this collection
* @return this builder instance
*/
public OrderSearchCriteriaBuilder setConcepts(Collection<Concept> concepts) {
this.concepts = concepts;
return (this);
}

/**
* @param orderTypes the order types to match on must be in this collection
* @return this builder instance
*/
public OrderSearchCriteriaBuilder setOrderTypes(Collection<OrderType> orderTypes) {
this.orderTypes = orderTypes;
return (this);
}

/**
* @param activatedOnOrBeforeDate orders must have dateActivated on or before this date
* @return this builder instance
*/
public OrderSearchCriteriaBuilder setActivatedOnOrBeforeDate(Date activatedOnOrBeforeDate) {
this.activatedOnOrBeforeDate = activatedOnOrBeforeDate;
return (this);
}

/**
* @param activatedOnOrAfterDate orders must have dateActivated on or after this date
* @return this builder instance
*/
public OrderSearchCriteriaBuilder setActivatedOnOrAfterDate(Date activatedOnOrAfterDate) {
this.activatedOnOrAfterDate = activatedOnOrAfterDate;
return (this);
}

/**
* @param includeVoided whether to include the voided orders or not
* @return this builder instance
*/
public OrderSearchCriteriaBuilder setIncludeVoided(boolean includeVoided) {
this.includeVoided = includeVoided;
return (this);
}

/**
* Create an {@link OrderSearchCriteria} with the properties of this builder instance.
* @return a new search criteria instance
*/
public OrderSearchCriteria createOrderSearchCriteria() {
return new OrderSearchCriteria(patient, careSetting, concepts, orderTypes, activatedOnOrBeforeDate,
activatedOnOrAfterDate, includeVoided);
}
}

Loading

0 comments on commit a994962

Please sign in to comment.