Advanced Example Usage Of OrderContext And OrderNumberGenerator

Let's say you have been asked to build a user interface for data clerks that only enter a single type of orders for patients in a specific care setting implying that when the user is logging into the system they choose an order type and a care setting, we know that the care setting and order type are always going to be the same for the duration of the user's http session. Let's also say that some of the orders they enter into the system already have pre-generated order numbers and you are required to let them enter those order numbers. How do you address this to simplify your code? This is when an OrderContext becomes relevant. You can initialize the care setting and order type upon user login, set them on the OrderContext object which you can cache in the http session. As for the pre-generated order numbers, you could write a form in the UI backed up by a Servlet or Controller that reads and sets the next order number as an order context attribute with a specific known key, along side it you also write an order number generator that knows how look up the next order number from the order context by its key. Lets have a code example to demonstrate this.

 

LoginServlet
public class LoginServlet extends HttpServlet {
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//process login....

		String careSettingId = request.getParameter("careSettingId");
		String orderorderTypeIdId = request.getParameter("orderTypeId");
		OrderService orderService = Context.getOrderService();
		OrderContext orderContext = new OrderContext();
		orderContext.setCareSetting(orderService.getCareSetting(careSettingId));		
		orderContext.setOrderType(orderService.getOrderType(orderTypeId));

		request.getSession().setAttribute("orderContext", orderContext);

		 //Proceed......
	}
}
OrderServlet
public class OrderServlet extends HttpServlet {
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		OrderService orderService = Context.getOrderService();
		Order order = new Order();
		order.setPatient(Context.getPatientService().getPatient(request.getParameter("patientId")));
		order.setConcept(Context.getConceptService().getConcept(request.getParameter("conceptId")));
		order.setOrderer(Context.getProviderService().getProvider(request.getParameter("providerId")));
		order.setEncounter(Context.getEncounterService().getEncounter(request.getParameter("encounterId")));
	    
        //Get the order context and pass it to the API when saving the order
		OrderContext orderContext = request.getSession().getAttribute("orderContext");
		//This will later be looked up by our order number generator
		orderContext.setAttribute("pregeneratedOrderNumber", request.getParameter("userEnteredOrderNumber"));

        try{ 
			orderService.saveOrder(order, orderContext);
		}finally{
			orderContext.removeAttribute("pregeneratedOrderNumber");
        }

		//Proceed...
	}
}
MyOrderNumberGenerator
@Component("myOrderNumberGenerator")
public class MyOrderNumberGenerator implements OrderNumberGenerator {
    
    @Override
    public String getNewOrderNumber(OrderContext orderContext) {
		String nextOrderNumber = null;
		//Use the pre-generated one entered by the user if any which was added to
		//the orderContext by the OrderServlet class below that handles the request
		if(orderContext != null){
			nextOrderNumber = orderContext.getAttribute("pregeneratedOrderNumber"); 
		}

		//User didn't specify one, generate it
        if(nextOrderNumber == null){
			.......
		}
			
		return nextOrderNumber;
    }
}

 

In the example code above, note that we never set the orderType and careSetting fields on the order object because the API will set them for us by copying them from the orderContext object passed in to the saveOrder method, and we are also able to allow the user to specify an order number to be assigned to the new order by using a combination of an OrderNumberGenerator and an OrderContext.

Of course there is more useful things you can do with the orderContext to pass around data, so where applicable use it and see what you can get away with.