Skip to content

Commit

Permalink
Update OrderValidator - TRUNK-4217
Browse files Browse the repository at this point in the history
Reverted changes to VisitServiceTest and ExistingVisitAssignmentHandlerTest
  • Loading branch information
vinkesh authored and wluyima committed Mar 26, 2014
1 parent 4be23f6 commit 7011338
Show file tree
Hide file tree
Showing 16 changed files with 466 additions and 135 deletions.
4 changes: 2 additions & 2 deletions api/src/main/java/org/openmrs/Order.java
Expand Up @@ -13,12 +13,12 @@
*/
package org.openmrs;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.order.OrderUtil;

import java.util.Date;

/**
* Dates should be interpreted as follows: If startDate is null then the order has been going on
* "since the beginning of time" Otherwise the order starts on startDate If discontinued is non-null
Expand Down
3 changes: 2 additions & 1 deletion api/src/main/java/org/openmrs/aop/RequiredDataAdvice.java
Expand Up @@ -166,8 +166,9 @@ else if (Reflect.isCollection(mainArgument) && isOpenmrsObjectCollection(mainArg

if (methodName.startsWith("void")) {
Voidable voidable = (Voidable) args[0];
Date dateVoided = voidable.getDateVoided() == null ? new Date() : voidable.getDateVoided();
String voidReason = (String) args[1];
recursivelyHandle(VoidHandler.class, voidable, voidReason);
recursivelyHandle(VoidHandler.class, voidable, Context.getAuthenticatedUser(), dateVoided, voidReason, null);

} else if (methodName.startsWith("unvoid")) {
Voidable voidable = (Voidable) args[0];
Expand Down
Expand Up @@ -13,9 +13,6 @@
*/
package org.openmrs.api.handler;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.openmrs.Encounter;
import org.openmrs.Order;
Expand All @@ -27,6 +24,9 @@
import org.openmrs.api.OrderService;
import org.openmrs.api.context.Context;

import java.util.Date;
import java.util.List;

/**
* This class deals with {@link Patient} objects when they are unvoided via the unvoid* method in an
* Openmrs Service. This handler is automatically called by the {@link RequiredDataAdvice} AOP
Expand Down
Expand Up @@ -13,20 +13,18 @@
*/
package org.openmrs.api.handler;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.openmrs.Encounter;
import org.openmrs.Order;
import org.openmrs.Patient;
import org.openmrs.User;
import org.openmrs.annotation.Handler;
import org.openmrs.aop.RequiredDataAdvice;
import org.openmrs.api.EncounterService;
import org.openmrs.api.OrderService;
import org.openmrs.api.context.Context;

import java.util.Date;
import java.util.List;

/**
* This class deals with {@link Patient} objects when they are voided via a void* method in an
* Openmrs Service. This handler is automatically called by the {@link RequiredDataAdvice} AOP
Expand Down Expand Up @@ -64,16 +62,5 @@ public void handle(Patient patient, User voidingUser, Date voidedDate, String vo
}
}
}
//void all the orders associated with this patient
OrderService os = Context.getOrderService();
List<Order> orders = os.getAllOrdersByPatient(patient);
if (CollectionUtils.isNotEmpty(orders)) {
for (Order order : orders) {
if (!order.isVoided()) {
order.setDateVoided(patient.getDateVoided());
os.voidOrder(order, voidReason);
}
}
}
}
}
16 changes: 8 additions & 8 deletions api/src/main/java/org/openmrs/api/impl/EncounterServiceImpl.java
Expand Up @@ -13,14 +13,6 @@
*/
package org.openmrs.api.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.openmrs.Cohort;
import org.openmrs.Encounter;
Expand Down Expand Up @@ -53,6 +45,14 @@
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
* Default implementation of the {@link EncounterService}
* <p>
Expand Down
46 changes: 44 additions & 2 deletions api/src/main/java/org/openmrs/validator/DrugOrderValidator.java
Expand Up @@ -15,6 +15,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.CareSetting;
import org.openmrs.DrugOrder;
import org.openmrs.annotation.Handler;
import org.springframework.validation.Errors;
Expand Down Expand Up @@ -51,6 +52,16 @@ public boolean supports(Class c) {
* @should fail validation if dosingType is null
* @should fail validation if order concept is null
* @should fail validation if drug concept is different from order concept
* @should fail validation if dose is null for SIMPLE dosingType
* @should fail validation if doseUnits is null for SIMPLE dosingType
* @should fail validation if route is null for SIMPLE dosingType
* @should fail validation if frequency is null for SIMPLE dosingType
* @should fail validation if instruction is null for FREE_TEXT dosingType
* @should fail validation if numberOfRefills is null for outpatient careSetting
* @should fail validation if quantityUnits is null for outpatient careSetting
* @should fail validation if quantity is null for outpatient careSetting
* @should fail validation if doseUnits is null when dose is present
* @should fail validation if quantityUnits is null when quantity is present
* @should pass validation if all fields are correct
*/
public void validate(Object obj, Errors errors) {
Expand All @@ -63,7 +74,6 @@ public void validate(Object obj, Errors errors) {
// for the following elements Order.hbm.xml says: not-null="true"
ValidationUtils.rejectIfEmpty(errors, "asNeeded", "error.null");
ValidationUtils.rejectIfEmpty(errors, "dosingType", "error.null");
//ValidationUtils.rejectIfEmpty(errors, "drug", "error.null");

if (order.getDrug() != null)
ValidationUtils.rejectIfEmpty(errors, "drug.concept", "error.null");
Expand All @@ -72,9 +82,41 @@ public void validate(Object obj, Errors errors) {
if (!(order.getDrug() == null) && !(order.getDrug().getConcept().equals(order.getConcept()))) {
errors.rejectValue("drug", "error.general");
errors.rejectValue("concept", "error.concept");

}
}
if (order.getDosingType() != null) {
if (order.getDosingType().equals(DrugOrder.DosingType.SIMPLE)) {
ValidationUtils.rejectIfEmpty(errors, "dose", "error.doseIsNullForDosingTypeSimple");
ValidationUtils.rejectIfEmpty(errors, "doseUnits", "error.doseUnitsIsNullForDosingTypeSimple");
ValidationUtils.rejectIfEmpty(errors, "route", "error.routeIsNullForDosingTypeSimple");
ValidationUtils.rejectIfEmpty(errors, "frequency", "error.frequencyIsNullForDosingTypeSimple");
} else if (order.getDosingType().equals(DrugOrder.DosingType.FREE_TEXT)) {
ValidationUtils.rejectIfEmpty(errors, "instructions", "error.instructionIsNullForDosingTypeFreeText");
} else {
ValidationUtils.rejectIfEmpty(errors, "dosingInstructions",
"error.dosingInstructionsIsNullForDosingTypeOther");
}
}
validateFieldsForOutpatientCareSettingType(order, errors);
validatePairedFields(order, errors);
}
}

private void validateFieldsForOutpatientCareSettingType(DrugOrder order, Errors errors) {
if (order.getCareSetting() != null
&& order.getCareSetting().getCareSettingType().equals(CareSetting.CareSettingType.OUTPATIENT)) {
ValidationUtils.rejectIfEmpty(errors, "quantity", "error.quantityIsNullForOutPatient");
ValidationUtils.rejectIfEmpty(errors, "quantityUnits", "error.quantityUnitsIsNullForOutPatient");
ValidationUtils.rejectIfEmpty(errors, "numRefills", "error.numRefillsIsNullForOutPatient");
}
}

private void validatePairedFields(DrugOrder order, Errors errors) {
if (order.getDose() != null) {
ValidationUtils.rejectIfEmpty(errors, "doseUnits", "error.doseUnitsRequiredWithDose");
}
if (order.getQuantity() != null) {
ValidationUtils.rejectIfEmpty(errors, "quantityUnits", "error.quantityUnitsRequiredWithQuantity");
}
}
}
50 changes: 32 additions & 18 deletions api/src/main/java/org/openmrs/validator/OrderValidator.java
Expand Up @@ -55,7 +55,11 @@ public boolean supports(Class c) {
* @should fail validation if voided is null
* @should fail validation if concept is null
* @should fail validation if patient is null
* @should fail validation if encounter is null
* @should fail validation if orderer is null
* @should fail validation if urgency is null
* @should fail validation if action is null
* @should fail validation if startDate is null
* @should fail validation if startDate after dateStopped
* @should fail validation if startDate after autoExpireDate
* @should pass validation if all fields are correct
Expand All @@ -65,31 +69,41 @@ public void validate(Object obj, Errors errors) {
if (order == null) {
errors.rejectValue("order", "error.general");
} else {
if (order.getEncounter() != null && order.getPatient() != null) {
if (!order.getEncounter().getPatient().equals(order.getPatient()))
errors.rejectValue("encounter", "Order.error.encounterPatientMismatch");
}

// for the following elements Order.hbm.xml says: not-null="true"
ValidationUtils.rejectIfEmpty(errors, "voided", "error.null");
ValidationUtils.rejectIfEmpty(errors, "concept", "Concept.noConceptSelected");
ValidationUtils.rejectIfEmpty(errors, "patient", "error.null");
ValidationUtils.rejectIfEmpty(errors, "encounter", "error.null");
ValidationUtils.rejectIfEmpty(errors, "orderer", "error.null");
ValidationUtils.rejectIfEmpty(errors, "urgency", "error.null");
ValidationUtils.rejectIfEmpty(errors, "startDate", "error.null");
ValidationUtils.rejectIfEmpty(errors, "action", "error.null");

Date startDate = order.getStartDate();
if (startDate != null) {
Date dateStopped = order.getDateStopped();
if (dateStopped != null && startDate.after(dateStopped)) {
errors.rejectValue("startDate", "Order.error.startDateAfterDiscontinuedDate");
errors.rejectValue("dateStopped", "Order.error.startDateAfterDiscontinuedDate");
}

Date autoExpireDate = order.getAutoExpireDate();
if (autoExpireDate != null && startDate.after(autoExpireDate)) {
errors.rejectValue("startDate", "Order.error.startDateAfterAutoExpireDate");
errors.rejectValue("autoExpireDate", "Order.error.startDateAfterAutoExpireDate");
}
validateSamePatientInOrderAndEncounter(order, errors);
validateStartDate(order, errors);
}
}

private void validateStartDate(Order order, Errors errors) {
Date startDate = order.getStartDate();
if (startDate != null) {
Date dateStopped = order.getDateStopped();
if (dateStopped != null && startDate.after(dateStopped)) {
errors.rejectValue("startDate", "Order.error.startDateAfterDiscontinuedDate");
errors.rejectValue("dateStopped", "Order.error.startDateAfterDiscontinuedDate");
}
Date autoExpireDate = order.getAutoExpireDate();
if (autoExpireDate != null && startDate.after(autoExpireDate)) {
errors.rejectValue("startDate", "Order.error.startDateAfterAutoExpireDate");
errors.rejectValue("autoExpireDate", "Order.error.startDateAfterAutoExpireDate");
}
}
}

private void validateSamePatientInOrderAndEncounter(Order order, Errors errors) {
if (order.getEncounter() != null && order.getPatient() != null) {
if (!order.getEncounter().getPatient().equals(order.getPatient()))
errors.rejectValue("encounter", "Order.error.encounterPatientMismatch");
}
}
}
40 changes: 20 additions & 20 deletions api/src/test/java/org/openmrs/OrderEntryIntegrationTest.java
Expand Up @@ -13,15 +13,6 @@
*/
package org.openmrs;

import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.openmrs.api.ConceptService;
import org.openmrs.api.EncounterService;
Expand All @@ -33,6 +24,15 @@
import org.openmrs.test.BaseContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
* Contains end to end tests for order entry operations i.g placing, discontinuing revising an order
*/
Expand Down Expand Up @@ -86,26 +86,24 @@ public void shouldPlaceADrugOrder() throws Exception {

//place drug order
DrugOrder order = new DrugOrder();
order.setEncounter(encounterService.getEncounter(3));
order.setPatient(patient);
order.setConcept(conceptService.getConcept(5497));
order.setConcept(conceptService.getConcept(88));
order.setCareSetting(careSetting);
order.setOrderer(Context.getProviderService().getProvider(1));
order.setStartDate(new Date());
order.setDrug(conceptService.getDrug(1));
order.setOrderType(orderService.getOrderTypeByName("Drug order"));
order.setEncounter(encounterService.getEncounter(3));
order.setDrug(conceptService.getDrug(3));
order.setDosingType(DrugOrder.DosingType.SIMPLE);
order.setDose(300.0);
Concept mgs = conceptService.getConcept(50);
order.setDoseUnits(mgs);
order.setDoseUnits(conceptService.getConcept(50));
order.setQuantity(20.0);
Concept tabs = conceptService.getConcept(51);
order.setQuantityUnits(tabs);
order.setQuantityUnits(conceptService.getConcept(51));
order.setDuration(20.0);
Concept days = conceptService.getConcept(1002);
order.setDurationUnits(days);
OrderFrequency onceDaily = orderService.getOrderFrequency(3000);
order.setFrequency(onceDaily);
order.setDurationUnits(conceptService.getConcept(1002));
order.setFrequency(orderService.getOrderFrequency(3000));
order.setRoute(conceptService.getConcept(22));
order.setNumRefills(10);

orderService.saveOrder(order, null);
List<Order> activeOrders = orderService.getActiveOrders(patient, orderService.getOrderTypeByName("Drug order"),
Expand All @@ -126,6 +124,7 @@ public void shouldPlaceATestOrder() throws Exception {
TestOrder order = new TestOrder();
order.setPatient(patient);
order.setOrderType(orderService.getOrderTypeByName("Test order"));
order.setEncounter(encounterService.getEncounter(3));
order.setConcept(conceptService.getConcept(5497));
order.setOrderer(providerService.getProvider(1));
order.setCareSetting(careSetting);
Expand Down Expand Up @@ -193,6 +192,7 @@ public void shouldReviseAnOrder() throws Exception {
assertTrue(originalActiveOrders.contains(originalOrder));

Order revisedOrder = originalOrder.cloneForRevision();
revisedOrder.setEncounter(encounterService.getEncounter(3));
revisedOrder.setInstructions("Take after a meal");
revisedOrder.setStartDate(new Date());
revisedOrder.setOrderer(providerService.getProvider(1));
Expand Down
36 changes: 18 additions & 18 deletions api/src/test/java/org/openmrs/api/EncounterServiceTest.java
Expand Up @@ -13,24 +13,6 @@
*/
package org.openmrs.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
Expand Down Expand Up @@ -59,6 +41,24 @@
import org.openmrs.test.Verifies;
import org.openmrs.util.OpenmrsConstants;

import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
* Tests all methods in the {@link EncounterService}
*/
Expand Down

0 comments on commit 7011338

Please sign in to comment.