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.

moving exceptions to separate classes. Changing call to use overloaded method without throwable

adding license to files
  • Loading branch information
samshuster committed Oct 26, 2016
1 parent c016290 commit b33cae6
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 58 deletions.
57 changes: 34 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 @@ -49,6 +49,17 @@
import org.openmrs.api.OrderService;
import org.openmrs.api.context.Context;
import org.openmrs.api.db.OrderDAO;
import org.openmrs.api.order.exceptions.CannotDeleteOrderPropertyInUseException;
import org.openmrs.api.order.exceptions.CannotDiscontinueAlreadyDiscontinuedOrderException;
import org.openmrs.api.order.exceptions.CannotDiscontinueOrderWithActionException;
import org.openmrs.api.order.exceptions.CannotEditAlreadyExistingOrderException;
import org.openmrs.api.order.exceptions.CannotEditOrderPropertyInUseException;
import org.openmrs.api.order.exceptions.CannotStopRetrospectiveDiscontinuedOrderException;
import org.openmrs.api.order.exceptions.CannotUnvoidOrderException;
import org.openmrs.api.order.exceptions.EditedOrderDoesNotMatchPreviousException;
import org.openmrs.api.order.exceptions.InvalidOrderException;
import org.openmrs.api.order.exceptions.PreviousOrderRequiredException;
import org.openmrs.api.order.exceptions.UnchangeableOrderPropertyException;
import org.openmrs.order.OrderUtil;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.OpenmrsUtil;
Expand Down Expand Up @@ -120,7 +131,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 CannotEditAlreadyExistingOrderException();
}
if (order.getDateActivated() == null) {
order.setDateActivated(new Date());
Expand All @@ -139,7 +150,7 @@ private Order saveOrder(Order order, OrderContext orderContext, boolean isRetros
}

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

Order previousOrder = order.getPreviousOrder();
Expand All @@ -162,7 +173,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 InvalidOrderException("Order.type.cannot.determine");
}

order.setOrderType(orderType);
Expand All @@ -173,19 +184,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 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 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 PreviousOrderRequiredException();
}
stopOrder(previousOrder, aMomentBefore(order.getDateActivated()), isRetrospective);
} else if (DISCONTINUE == order.getAction()) {
Expand All @@ -199,29 +210,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 UnchangeableOrderPropertyException.withProperty("patient");
} else if (!rowData[1].equals(previousOrder.getCareSetting().getCareSettingId())) {
throw new APIException("Order.cannot.change.careSetting");
throw UnchangeableOrderPropertyException.withProperty("careSetting");
} else if (!rowData[2].equals(previousOrder.getConcept().getConceptId())) {
throw new APIException("Order.cannot.change.concept");
throw 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 UnchangeableOrderPropertyException.withProperty("drug");
} else if (previousDrug != null && !OpenmrsUtil.nullSafeEquals(rowData[3], previousDrug.getDrugId())) {
throw new APIException("Order.cannot.change.drug");
throw 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 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 EditedOrderDoesNotMatchPreviousException("Order.type.does.not.match");
} else if (!order.getCareSetting().equals(previousOrder.getCareSetting())) {
throw new APIException("Order.care.setting.does.not.match");
throw new EditedOrderDoesNotMatchPreviousException("Order.care.setting.does.not.match");
} else if (!getActualType(order).equals(getActualType(previousOrder))) {
throw new APIException("Order.class.does.not.match");
throw new EditedOrderDoesNotMatchPreviousException("Order.class.does.not.match");
}
}

Expand Down Expand Up @@ -450,7 +461,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 CannotUnvoidOrderException(action);
}
stopOrder(previousOrder, aMomentBefore(order.getDateActivated()), false);
}
Expand Down Expand Up @@ -739,16 +750,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 CannotDiscontinueOrderWithActionException();
}

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

setProperty(orderToStop, "dateStopped", discontinueDate);
Expand All @@ -762,7 +773,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 CannotEditOrderPropertyInUseException.withProperty("frequency");
}
}

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

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

dao.purgeOrderFrequency(orderFrequency);
Expand Down Expand Up @@ -881,7 +892,7 @@ public OrderType saveOrderType(OrderType orderType) {
@Override
public void purgeOrderType(OrderType orderType) {
if (dao.isOrderTypeInUse(orderType)) {
throw new APIException("Order.type.cannot.delete");
throw CannotDeleteOrderPropertyInUseException.withProperty("type");
}
dao.purgeOrderType(orderType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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.order.exceptions;

import org.openmrs.api.APIException;

/**
* Thrown when trying to delete an order property that is still in use.
*/
public 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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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.order.exceptions;

import org.openmrs.api.APIException;

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

public static final long serialVersionUID = 22121214L;

public CannotDiscontinueAlreadyDiscontinuedOrderException() {
super("Order.stopped.cannot.discontinued");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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.order.exceptions;

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

import org.openmrs.api.APIException;

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

public static final long serialVersionUID = 22121215L;

public CannotDiscontinueOrderWithActionException() {
super("Order.action.cannot.discontinued", new Object[] { DISCONTINUE });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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.order.exceptions;

import org.openmrs.api.APIException;

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

public static final long serialVersionUID = 22121213L;

public CannotEditAlreadyExistingOrderException() {
super("Order.cannot.edit.existing");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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.order.exceptions;

import org.openmrs.api.APIException;

/**
* Thrown when trying to edit an order property which is still in use.
*/
public 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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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.order.exceptions;

import org.openmrs.api.APIException;

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

public static final long serialVersionUID = 22221215L;

public CannotStopRetrospectiveDiscontinuedOrderException() {
super("Order.retrospective.stopped.cannot.discontinued");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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.order.exceptions;

import org.openmrs.api.APIException;

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

public static final long serialVersionUID = 22121315L;

public CannotUnvoidOrderException(String action) {
super("Order.action.cannot.unvoid", new Object[] { action });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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.order.exceptions;

import org.openmrs.api.APIException;

/**
* Thrown when the new edited order contains modified properties that must be the same as previous order.
*/
public 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);
}
}

0 comments on commit b33cae6

Please sign in to comment.