Skip to content

Commit

Permalink
TRUNK-4696: added specific exception classes for the Order Service
Browse files Browse the repository at this point in the history
Modifying some Order Exception definitions to be more in line with the messages.properties. Fixing test to map to these new definitions.
  • Loading branch information
samshuster committed Oct 23, 2016
1 parent f5ef7a7 commit f7b79fd
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 58 deletions.
170 changes: 170 additions & 0 deletions api/src/main/java/org/openmrs/api/OrderExceptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* 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.api;

import static org.openmrs.Order.Action.DISCONTINUE;

/**
* Represents specific error conditions that are part of OrderService.
* The specific error classes define only a default constructor if they are meant to be used with only a single error message.
* In other cases, there should be static factory methods that can be used to create the error class with the specific error message needed.
*/
public class OrderExceptions {

public static final long serialVersionUID = 22121212L;

/**
* Thrown when the OrderAlreadyExists.
*/
public static class CannotEditAlreadyExistingOrderException extends APIException {

public static final long serialVersionUID = 22121213L;

public CannotEditAlreadyExistingOrderException() {
super("Order.cannot.edit.existing");
}
}

/**
* Thrown when the Order has already been discontinued.
*/
public static class CannotDiscontinueAlreadyDiscontinuedOrderException extends APIException {

public static final long serialVersionUID = 22121214L;

public CannotDiscontinueAlreadyDiscontinuedOrderException() {
super("Order.stopped.cannot.discontinued");
}
}

/**
* Thrown when trying to stop a retrospective discontinued order.
*/
public static class CannotStopRetrospectiveDiscontinuedOrderException extends APIException {

public static final long serialVersionUID = 22221215L;

public CannotStopRetrospectiveDiscontinuedOrderException() {
super("Order.retrospective.stopped.cannot.discontinued");
}
}

/**
* Thrown with trying to discontinue an order with a specific action that cannot be discontinued.
*/
public static class CannotDiscontinueOrderWithActionException extends APIException {

public static final long serialVersionUID = 22121215L;

public CannotDiscontinueOrderWithActionException() {
super("Order.action.cannot.discontinued", new Object[] { DISCONTINUE }, null);
}
}

/**
* Thrown when an attempt to unvoid an order fails.
*/
public static class CannotUnvoidOrderException extends APIException {

public static final long serialVersionUID = 22121315L;

public CannotUnvoidOrderException(String action) {
super("Order.action.cannot.unvoid", new Object[] { action });
}
}

/**
* Thrown when a previous order is required.
*/
public static class PreviousOrderRequiredException extends APIException {

public static final long serialVersionUID = 22121216L;

public PreviousOrderRequiredException() {
super("Order.previous.required");
}
}

/**
* Thrown when trying to change unchangeable order properties.
*/
public static class UnchangeableOrderPropertyException extends APIException {

public static final long serialVersionUID = 22121217L;

private UnchangeableOrderPropertyException(String message) {
super(message);
}

public static UnchangeableOrderPropertyException withProperty(String property) {
return new UnchangeableOrderPropertyException(String.format("Order.cannot.change.%s", property));
}
}

/**
* Thrown when the new edited order contains modified properties that must be the same as previous order.
*/
public static class EditedOrderDoesNotMatchPreviousException extends APIException {

public static final long serialVersionUID = 22121218L;

public EditedOrderDoesNotMatchPreviousException(String message) {
super(message);
}

public EditedOrderDoesNotMatchPreviousException(String message, Object[] params) {
super(message, params);
}
}

/**
* Thrown when the order is invalid.
*/
public static class InvalidOrderException extends APIException {

public static final long serialVersionUID = 22121219L;

public InvalidOrderException(String message) {
super(message);
}
}

/**
* Thrown when trying to edit an order property which is still in use.
*/
public static class CannotEditOrderPropertyInUseException extends APIException {

public static final long serialVersionUID = 22121220L;

private CannotEditOrderPropertyInUseException(String message) {
super(message);
}

public static CannotEditOrderPropertyInUseException withProperty(String property) {
return new CannotEditOrderPropertyInUseException(String.format("Order.%s.cannot.edit", property));
}
}

/**
* Thrown when trying to delete an order property that is still in use.
*/
public static class CannotDeleteOrderPropertyInUseException extends APIException {

public static final long serialVersionUID = 22121221L;

private CannotDeleteOrderPropertyInUseException(String message) {
super(message);
}

public static CannotDeleteOrderPropertyInUseException withProperty(String property) {
return new CannotDeleteOrderPropertyInUseException(String.format("Order.%s.cannot.delete", property));
}
}
}
47 changes: 24 additions & 23 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.openmrs.api.AmbiguousOrderException;
import org.openmrs.api.GlobalPropertyListener;
import org.openmrs.api.OrderContext;
import org.openmrs.api.OrderExceptions;
import org.openmrs.api.OrderNumberGenerator;
import org.openmrs.api.OrderService;
import org.openmrs.api.context.Context;
Expand Down Expand Up @@ -120,7 +121,7 @@ public synchronized Order saveRetrospectiveOrder(Order order, OrderContext order

private Order saveOrder(Order order, OrderContext orderContext, boolean isRetrospective) {
if (order.getOrderId() != null) {
throw new APIException("Order.cannot.edit.existing");
throw new OrderExceptions.CannotEditAlreadyExistingOrderException();
}
if (order.getDateActivated() == null) {
order.setDateActivated(new Date());
Expand All @@ -139,7 +140,7 @@ private Order saveOrder(Order order, OrderContext orderContext, boolean isRetros
}

if (concept == null) {
throw new APIException("Order.concept.required");
throw new OrderExceptions.InvalidOrderException("Order.concept.required");
}

Order previousOrder = order.getPreviousOrder();
Expand All @@ -162,7 +163,7 @@ private Order saveOrder(Order order, OrderContext orderContext, boolean isRetros

//this order's order type should match that of the previous
if (orderType == null || (previousOrder != null && !orderType.equals(previousOrder.getOrderType()))) {
throw new APIException("Order.type.cannot.determine");
throw new OrderExceptions.InvalidOrderException("Order.type.cannot.determine");
}

order.setOrderType(orderType);
Expand All @@ -173,19 +174,19 @@ private Order saveOrder(Order order, OrderContext orderContext, boolean isRetros
careSetting = orderContext.getCareSetting();
}
if (careSetting == null || (previousOrder != null && !careSetting.equals(previousOrder.getCareSetting()))) {
throw new APIException("Order.care.cannot.determine");
throw new OrderExceptions.InvalidOrderException("Order.care.cannot.determine");
}
order.setCareSetting(careSetting);
}

if (!order.getOrderType().getJavaClass().isAssignableFrom(order.getClass())) {
throw new APIException("Order.type.class.does.not.match",
throw new OrderExceptions.EditedOrderDoesNotMatchPreviousException("Order.type.class.does.not.match",
new Object[] { order.getOrderType().getJavaClass(), order.getClass().getName() });
}

if (REVISE == order.getAction()) {
if (previousOrder == null) {
throw new APIException("Order.previous.required");
throw new OrderExceptions.PreviousOrderRequiredException();
}
stopOrder(previousOrder, aMomentBefore(order.getDateActivated()), isRetrospective);
} else if (DISCONTINUE == order.getAction()) {
Expand All @@ -199,29 +200,29 @@ private Order saveOrder(Order order, OrderContext orderContext, boolean isRetros
List<Object[]> rows = dao.getOrderFromDatabase(previousOrder, isPreviousDrugOrder);
Object[] rowData = rows.get(0);
if (!rowData[0].equals(previousOrder.getPatient().getPatientId())) {
throw new APIException("Order.cannot.change.patient");
throw OrderExceptions.UnchangeableOrderPropertyException.withProperty("patient");
} else if (!rowData[1].equals(previousOrder.getCareSetting().getCareSettingId())) {
throw new APIException("Order.cannot.change.careSetting");
throw OrderExceptions.UnchangeableOrderPropertyException.withProperty("careSetting");
} else if (!rowData[2].equals(previousOrder.getConcept().getConceptId())) {
throw new APIException("Order.cannot.change.concept");
throw OrderExceptions.UnchangeableOrderPropertyException.withProperty("concept");
} else if (isPreviousDrugOrder) {
Drug previousDrug = ((DrugOrder) previousOrder).getDrug();
if (previousDrug == null && rowData[3] != null) {
throw new APIException("Order.cannot.change.drug");
throw OrderExceptions.UnchangeableOrderPropertyException.withProperty("drug");
} else if (previousDrug != null && !OpenmrsUtil.nullSafeEquals(rowData[3], previousDrug.getDrugId())) {
throw new APIException("Order.cannot.change.drug");
throw OrderExceptions.UnchangeableOrderPropertyException.withProperty("drug");
}
}

//concept should be the same as on previous order, same applies to drug for drug orders
if (!order.hasSameOrderableAs(previousOrder)) {
throw new APIException("The orderable of the previous order and the new one order don't match");
throw new OrderExceptions.EditedOrderDoesNotMatchPreviousException("The orderable of the previous order and the new one order don't match");
} else if (!order.getOrderType().equals(previousOrder.getOrderType())) {
throw new APIException("Order.type.does.not.match");
throw new OrderExceptions.EditedOrderDoesNotMatchPreviousException("Order.type.does.not.match");
} else if (!order.getCareSetting().equals(previousOrder.getCareSetting())) {
throw new APIException("Order.care.setting.does.not.match");
throw new OrderExceptions.EditedOrderDoesNotMatchPreviousException("Order.care.setting.does.not.match");
} else if (!getActualType(order).equals(getActualType(previousOrder))) {
throw new APIException("Order.class.does.not.match");
throw new OrderExceptions.EditedOrderDoesNotMatchPreviousException("Order.class.does.not.match");
}
}

Expand Down Expand Up @@ -450,7 +451,7 @@ public Order unvoidOrder(Order order) throws APIException {
if (previousOrder != null && isDiscontinueOrReviseOrder(order)) {
if (!previousOrder.isActive()) {
final String action = DISCONTINUE == order.getAction() ? "discontinuation" : "revision";
throw new APIException("Order.action.cannot.unvoid", new Object[] { action });
throw new OrderExceptions.CannotUnvoidOrderException(action);
}
stopOrder(previousOrder, aMomentBefore(order.getDateActivated()), false);
}
Expand Down Expand Up @@ -739,16 +740,16 @@ private void stopOrder(Order orderToStop, Date discontinueDate, boolean isRetros
throw new IllegalArgumentException("Discontinue date cannot be in the future");
}
if (DISCONTINUE == orderToStop.getAction()) {
throw new APIException("Order.action.cannot.discontinued", new Object[] { DISCONTINUE });
throw new OrderExceptions.CannotDiscontinueOrderWithActionException();
}

if (isRetrospective && orderToStop.getDateStopped() != null) {
throw new APIException("Order.retrospective.stopped.cannot.discontinued");
throw new OrderExceptions.CannotStopRetrospectiveDiscontinuedOrderException();
}
if (!isRetrospective && !orderToStop.isActive()) {
throw new APIException("Order.stopped.cannot.discontinued");
throw new OrderExceptions.CannotDiscontinueAlreadyDiscontinuedOrderException();
} else if (isRetrospective && !orderToStop.isActive(discontinueDate)) {
throw new APIException("Order.retrospective.stopped.cannot.discontinued");
throw new OrderExceptions.CannotStopRetrospectiveDiscontinuedOrderException();
}

setProperty(orderToStop, "dateStopped", discontinueDate);
Expand All @@ -762,7 +763,7 @@ private void stopOrder(Order orderToStop, Date discontinueDate, boolean isRetros
public OrderFrequency saveOrderFrequency(OrderFrequency orderFrequency) throws APIException {
if (orderFrequency.getOrderFrequencyId() != null) {
if (dao.isOrderFrequencyInUse(orderFrequency)) {
throw new APIException("Order.frequency.cannot.edit");
throw OrderExceptions.CannotEditOrderPropertyInUseException.withProperty("frequency");
}
}

Expand Down Expand Up @@ -793,7 +794,7 @@ public OrderFrequency unretireOrderFrequency(OrderFrequency orderFrequency) {
public void purgeOrderFrequency(OrderFrequency orderFrequency) {

if (dao.isOrderFrequencyInUse(orderFrequency)) {
throw new APIException("Order.frequency.cannot.delete");
throw OrderExceptions.CannotDeleteOrderPropertyInUseException.withProperty("frequency");
}

dao.purgeOrderFrequency(orderFrequency);
Expand Down Expand Up @@ -881,7 +882,7 @@ public OrderType saveOrderType(OrderType orderType) {
@Override
public void purgeOrderType(OrderType orderType) {
if (dao.isOrderTypeInUse(orderType)) {
throw new APIException("Order.type.cannot.delete");
throw OrderExceptions.CannotDeleteOrderPropertyInUseException.withProperty("type");
}
dao.purgeOrderType(orderType);
}
Expand Down

0 comments on commit f7b79fd

Please sign in to comment.