Skip to content

Commit

Permalink
Changing order number incrementing strategy from memory to database for:
Browse files Browse the repository at this point in the history
The API should assign order numbers to new orders - TRUNK-4163
  • Loading branch information
dkayiwa committed Dec 16, 2013
1 parent 985baae commit b66bc5c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
6 changes: 0 additions & 6 deletions api/src/main/java/org/openmrs/api/db/OrderDAO.java
Expand Up @@ -103,10 +103,4 @@ public <Ord extends Order> List<Ord> getOrders(Class<Ord> orderClassType, List<P
* Delete Obs that references an order
*/
public void deleteObsThatReference(Order order);

/**
* @return the highest orderId that has been persisted to the database
* @should return the highest order id
*/
public Integer getHighestOrderId();
}
Expand Up @@ -18,7 +18,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Expression;
import org.openmrs.Concept;
Expand Down Expand Up @@ -203,13 +202,4 @@ public void deleteObsThatReference(Order order) {
.executeUpdate();
}
}

/**
* @see org.openmrs.api.db.OrderDAO#getHighestOrderId()
*/
@Override
public Integer getHighestOrderId() {
Query query = sessionFactory.getCurrentSession().createSQLQuery("SELECT max(order_id) FROM orders");
return (Integer) query.uniqueResult();
}
}
25 changes: 17 additions & 8 deletions api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java
Expand Up @@ -27,6 +27,7 @@
import org.openmrs.DrugOrder;
import org.openmrs.Encounter;
import org.openmrs.EncounterType;
import org.openmrs.GlobalProperty;
import org.openmrs.Location;
import org.openmrs.Order;
import org.openmrs.OrderType;
Expand All @@ -42,6 +43,8 @@
import org.openmrs.order.DrugOrderSupport;
import org.openmrs.order.OrderUtil;
import org.openmrs.order.RegimenSuggestion;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.PrivilegeConstants;
import org.springframework.util.StringUtils;

/**
Expand All @@ -58,8 +61,6 @@ public class OrderServiceImpl extends BaseOpenmrsService implements OrderService

protected OrderDAO dao;

private static Integer orderNumberCounter = -1;

public OrderServiceImpl() {
}

Expand Down Expand Up @@ -628,12 +629,20 @@ public List<Order> getOrdersByEncounter(Encounter encounter) {
*/
@Override
public synchronized String getNewOrderNumber() {
if (orderNumberCounter < 0) {
// we've just started up, so we need to fetch this from the DAO
Integer temp = dao.getHighestOrderId();
orderNumberCounter = temp == null ? 0 : temp;
GlobalProperty globalProperty = Context.getAdministrationService().getGlobalPropertyObject(
OpenmrsConstants.GLOBAL_PROPERTY_NEXT_ORDER_NUMBER);
if (globalProperty == null) {
globalProperty = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_NEXT_ORDER_NUMBER, "1",
"The next order number available for assignment");
}
orderNumberCounter += 1;
return "ORD-" + orderNumberCounter;

String orderNumber = "ORD-" + globalProperty.getValue();

This comment has been minimized.

Copy link
@wluyima

wluyima Dec 17, 2013

Member

I think you need to check and catch null, blank, white spaces and ensure the value is a valid number. I know this unlikely to be the case but you never know.


globalProperty.setPropertyValue(String.valueOf(Long.parseLong(globalProperty.getPropertyValue()) + 1));
Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);

This comment has been minimized.

Copy link
@wluyima

wluyima Dec 17, 2013

Member

This needs to be a try finally clause

Context.getAdministrationService().saveGlobalProperty(globalProperty);
Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);

return orderNumber;
}
}
5 changes: 5 additions & 0 deletions api/src/main/java/org/openmrs/util/OpenmrsConstants.java
Expand Up @@ -1050,6 +1050,8 @@ public static final Collection<String> AUTO_ROLES() {

public static final String GP_CASE_SENSITIVE_NAMES_IN_CONCEPT_NAME_TABLE = "concept.caseSensitiveNamesInConceptNameTable";

public static final String GLOBAL_PROPERTY_NEXT_ORDER_NUMBER = "orderEntry.nextOrderNumber";

/**
* At OpenMRS startup these global properties/default values/descriptions are inserted into the
* database if they do not exist yet.
Expand Down Expand Up @@ -1475,6 +1477,9 @@ public static final List<GlobalProperty> CORE_GLOBAL_PROPERTIES() {
props.add(new GlobalProperty(GLOBAL_PROPERTY_USER_REQUIRE_EMAIL_AS_USERNAME, "false",
"Indicates whether a username must be a valid e-mail or not.", BooleanDatatype.class, null));

props.add(new GlobalProperty(GLOBAL_PROPERTY_NEXT_ORDER_NUMBER, "1",
"The next order number available for assignment"));

for (GlobalProperty gp : ModuleFactory.getGlobalProperties()) {
props.add(gp);
}
Expand Down

0 comments on commit b66bc5c

Please sign in to comment.