Skip to content

Commit

Permalink
Add OrderTypeValidator - TRUNK-4311
Browse files Browse the repository at this point in the history
Follow up
  • Loading branch information
jkondrat authored and wluyima committed Mar 10, 2014
1 parent 7ab5b33 commit 2a72379
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 1 deletion.
14 changes: 13 additions & 1 deletion api/src/main/java/org/openmrs/api/OrderService.java
Expand Up @@ -24,10 +24,11 @@
import org.openmrs.OrderFrequency;
import org.openmrs.Patient;
import org.openmrs.User;
import org.openmrs.Provider;
import org.openmrs.OrderType;
import org.openmrs.annotation.Authorized;
import org.openmrs.api.db.OrderDAO;
import org.openmrs.util.PrivilegeConstants;
import org.openmrs.Provider;

/**
* Contains methods pertaining to creating/deleting/voiding Orders
Expand Down Expand Up @@ -286,6 +287,17 @@ public <Ord extends Order> List<Ord> getActiveOrders(Patient patient, Class<Ord>
@Authorized(PrivilegeConstants.VIEW_CARE_SETTINGS)
public List<CareSetting> getCareSettings(boolean includeRetired);

/**
* Gets OrderType that matches the specified name
*
* @param orderTypeName the name to match against
* @return OrderType
* @since 1.10
* @should return the order type that matches the specified name
*/
@Authorized(PrivilegeConstants.VIEW_ORDER_TYPES)
public OrderType getOrderTypeByName(String orderTypeName);

/**
* Gets OrderFrequency that matches the specified orderFrequencyId
*
Expand Down
6 changes: 6 additions & 0 deletions api/src/main/java/org/openmrs/api/db/OrderDAO.java
Expand Up @@ -24,6 +24,7 @@
import org.openmrs.OrderFrequency;
import org.openmrs.Patient;
import org.openmrs.User;
import org.openmrs.OrderType;

/**
* Order-related database functions
Expand Down Expand Up @@ -116,6 +117,11 @@ public <Ord extends Order> List<Ord> getActiveOrders(Patient patient, Class<Ord>
*/
public List<CareSetting> getCareSettings(boolean includeRetired);

/**
* @See OrderService#getOrderTypeByName
*/
public OrderType getOrderTypeByName(String orderTypeName);

/**
* @See OrderService#getOrderFrequency
*/
Expand Down
Expand Up @@ -42,6 +42,7 @@
import org.openmrs.OrderFrequency;
import org.openmrs.Patient;
import org.openmrs.User;
import org.openmrs.OrderType;
import org.openmrs.api.APIException;
import org.openmrs.api.db.DAOException;
import org.openmrs.api.db.OrderDAO;
Expand Down Expand Up @@ -278,6 +279,16 @@ public List<CareSetting> getCareSettings(boolean includeRetired) {
return c.list();
}

/**
* @See OrderDAO#getOrderTypeByName
*/
@Override
public OrderType getOrderTypeByName(String orderTypeName) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(OrderType.class);
criteria.add(Restrictions.eq("name", orderTypeName));
return (OrderType) criteria.uniqueResult();
}

/**
* @See OrderDAO#getOrderFrequency
*/
Expand Down
8 changes: 8 additions & 0 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Expand Up @@ -364,6 +364,14 @@ public List<CareSetting> getCareSettings(boolean includeRetired) {
return dao.getCareSettings(includeRetired);
}

/**
* @see OrderService#getOrderTypeByName(String)
*/
@Override
public OrderType getOrderTypeByName(String orderTypeName) {
return dao.getOrderTypeByName(orderTypeName);
}

/**
* @see OrderService#getOrderFrequency(Integer)
*/
Expand Down
62 changes: 62 additions & 0 deletions api/src/main/java/org/openmrs/validator/OrderTypeValidator.java
@@ -0,0 +1,62 @@
package org.openmrs.validator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.OrderType;
import org.openmrs.annotation.Handler;
import org.openmrs.api.context.Context;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
* Validates the {@link OrderType} class.
*
* @since 1.10
*/
@Handler(supports = { OrderType.class })
public class OrderTypeValidator implements Validator {

// Log for this class
protected final Log log = LogFactory.getLog(getClass());

/**
* Determines if the command object being submitted is a valid type
*
* @see org.springframework.validation.Validator#supports(java.lang.Class)
*/
@SuppressWarnings("rawtypes")
public boolean supports(Class c) {
return OrderType.class.isAssignableFrom(c);
}

/**
* Checks theobject for any inconsistencies/errors
*
* @see org.springframework.validation.Validator#validate(java.lang.Object,
* org.springframework.validation.Errors)
* @should fail if the orderType object is null
* @should fail if name is null
* @should fail if name is empty
* @should fail if name is whitespace
* @should fail if name is a duplicate
* @should pass if all fields are correct
*/
public void validate(Object obj, Errors errors) {
if (obj == null || !(obj instanceof OrderType)) {
throw new IllegalArgumentException("The parameter obj should not be null and must be of type" + OrderType.class);
} else {
OrderType orderType = (OrderType) obj;

String name = orderType.getName();
if (!StringUtils.hasText(name)) {
errors.rejectValue("name", "error.name");
}

OrderType ot = Context.getOrderService().getOrderTypeByName(name);
if (ot != null && !orderType.equals(ot)) {
errors.rejectValue("name", "OrderType.duplicate.name", "Duplicate order type name: " + name);
}
}
}
}
@@ -0,0 +1,91 @@
package org.openmrs.validator;

import org.junit.Assert;
import org.junit.Test;
import org.openmrs.OrderType;
import org.openmrs.test.BaseContextSensitiveTest;
import org.openmrs.test.Verifies;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

/**
* Contains tests methods for the {@link OrderTypeValidator}
*/
public class OrderTypeValidatorTest extends BaseContextSensitiveTest {

/**
* @see {@link OrderTypeValidator#validate(Object,Errors)}
*/
@Test(expected = IllegalArgumentException.class)
@Verifies(value = "should fail if the orderType object is null", method = "validate(Object,Errors)")
public void validate_shouldFailIfTheOrderTypeObjectIsNull() throws Exception {
Errors errors = new BindException(new OrderType(), "orderType");
new OrderTypeValidator().validate(null, errors);
}

/**
* @see {@link OrderTypeValidator#validate(Object,Errors)}
*/
@Test
@Verifies(value = "should fail if name is null", method = "validate(Object,Errors)")
public void validate_shouldFailIfNameIsNull() throws Exception {
OrderType orderType = new OrderType();
Errors errors = new BindException(orderType, "orderType");
new OrderTypeValidator().validate(orderType, errors);
Assert.assertEquals(true, errors.hasFieldErrors("name"));
}

/**
* @see {@link OrderTypeValidator#validate(Object,Errors)}
*/
@Test
@Verifies(value = "should fail if name is empty", method = "validate(Object,Errors)")
public void validate_shouldFailIfNameIsEmpty() throws Exception {
OrderType orderType = new OrderType();
orderType.setName("");
Errors errors = new BindException(orderType, "orderType");
new OrderTypeValidator().validate(orderType, errors);
Assert.assertEquals(true, errors.hasFieldErrors("name"));
}

/**
* @see {@link OrderTypeValidator#validate(Object,Errors)}
*/
@Test
@Verifies(value = "should fail if name is white space", method = "validate(Object,Errors)")
public void validate_shouldFailIfNameIsWhiteSpace() throws Exception {
OrderType orderType = new OrderType();
orderType.setName(" ");
Errors errors = new BindException(orderType, "orderType");
new OrderTypeValidator().validate(orderType, errors);
Assert.assertEquals(true, errors.hasFieldErrors("name"));
}

/**
* @see {@link OrderTypeValidator#validate(Object,Errors)}
*/
@Test
@Verifies(value = "should fail if name is a duplicate", method = "validate(Object,Errors)")
public void validate_shouldFailIfNameIsADuplicate() throws Exception {
OrderType orderType = new OrderType();
orderType.setName("Drug order");
Errors errors = new BindException(orderType, "orderType");
new OrderTypeValidator().validate(orderType, errors);
Assert.assertEquals(true, errors.hasFieldErrors("name"));
}

/**
* @see {@link OrderTypeValidator#validate(Object,Errors)}
*/
@Test
@Verifies(value = "should pass if all fields are correct", method = "validate(Object,Errors)")
public void validate_shouldPassIfAllFieldsAreCorrect() throws Exception {
OrderType orderType = new OrderType();
orderType.setName("unique name");
Errors errors = new BindException(orderType, "orderType");
new OrderTypeValidator().validate(orderType, errors);

Assert.assertFalse(errors.hasErrors());
}

}
Expand Up @@ -182,6 +182,7 @@
<order_frequency order_frequency_id="1" concept_id="113" creator="1" date_created="2008-08-15 13:52:53.0" retired="false" uuid="28090760-7c38-11e3-baa7-0800200c9a66" />
<order_frequency order_frequency_id="2" concept_id="3" creator="1" date_created="2008-08-15 13:52:53.0" retired="false" uuid="38090760-7c38-11e3-baa7-0800200c9a66" />
<order_frequency order_frequency_id="3" concept_id="4" creator="1" date_created="2008-08-15 13:52:53.0" retired="true" retire_reason="Some Retire Reason" retired_by="1" date_retired="2009-08-15 13:52:53.0" uuid="48090760-7c38-11e3-baa7-0800200c9a66" />
<order_type order_type_id="1" name="Drug order" description="Order of medications for the patient" java_class_name="org.openmrs.DrugOrder" date_created="2008-08-15 13:49:47.0" retired="false" uuid="84ce45a8-5e7c-48f7-a581-ca1d17d63a66" creator="1" />
<drug_order order_id="1" drug_inventory_id="3" dose="325.0" dose_units="50" as_needed="false" frequency="1" dosing_type="SIMPLE"/>
<drug_order order_id="2" drug_inventory_id="2" dose="1.0" dose_units="51" as_needed="false" frequency="1" dosing_type="SIMPLE"/>
<drug_order order_id="3" drug_inventory_id="2" dose="2.0" dose_units="51" as_needed="false" frequency="1" dosing_type="SIMPLE"/>
Expand Down Expand Up @@ -342,4 +343,5 @@
<visit visit_id="5" patient_id="6" visit_type_id="1" date_started="2005-01-01 00:00:00.0" creator="1" date_created="2005-01-01 00:00:00.0" voided="0" uuid="8cfda6ae-6b78-11e0-93c3-18a905e044dc" />
<visit visit_id="6" patient_id="2" visit_type_id="1" date_started="2005-01-01 00:00:00.0" creator="1" date_created="2005-01-01 00:00:00.0" voided="1" voided_by="1" date_voided="2005-01-01 00:00:00.0" void_reason="no reason" uuid="e1428fea-6b78-11e0-93c3-18a905e044dc" />
<visit_attribute_type visit_attribute_type_id="1" name="Audit Date" datatype="org.openmrs.customdatatype.datatype.DateDatatype" min_occurs="0" max_occurs="1" creator="1" date_created="2005-01-01 00:00:00.0" retired="0" uuid="8770f6d6-7673-11e0-8f03-001e378eb67e" />

</dataset>
1 change: 1 addition & 0 deletions webapp/src/main/webapp/WEB-INF/messages.properties
Expand Up @@ -1248,6 +1248,7 @@ OrderType.deleted=Order Type deleted
OrderType.list.empty=Currently, there are no order types defined
OrderType.cannot.delete=Cannot delete Order Type. Database constraints will be violated.
OrderType.select=You must select at least one order type.
OrderType.duplicate.name=Duplicate order type name

Order.header=Orders
Order.manage=Manage Orders
Expand Down

0 comments on commit 2a72379

Please sign in to comment.