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 is 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.
@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 if(orderContext != null){ nextOrderNumber = orderContext.getAttribute("pregeneratedOrderNumber"); } //User didn't specify one, generate it if(nextOrderNumber == null){ ....... } return nextOrderNumber; } }
public class LoginServlet extends HttpServlet { protected void doGet(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...... } }
public class OrderServlet extends HttpServlet { protected void doGet(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("concepttId"))); order.setOrderer(Context.getProviderService().getProvider(request.getParameter("providerId"))); order.setEncounter(Context.getEncounterService().getEncounter(request.getParameter("encounterId"))); OrderContext orderContext = request.getSession().getAttribute("orderContext"); orderContext.setAttribute("pregeneratedOrderNumber", request.getParameter("orderNumber")); try{ orderService.saveOrder(order, orderContext); }finally{ orderContext.removeAttribute("pregeneratedOrderNumber"); } //Proceed... } }
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.