private void UpdateProceduresHelper(Order order, IEnumerable<ProcedureRequisition> procedureReqs, ModifyOrderRequest request) { // do not update the procedures if the order is completed if (order.IsTerminated) return; var assembler = new OrderEntryAssembler(); // if any procedure is in downtime recovery mode, assume the entire order is a "downtime order" var isDowntime = CollectionUtils.Contains(order.Procedures, p => p.DowntimeRecoveryMode); // separate the list into additions and updates var existingReqs = new List<ProcedureRequisition>(); var addedReqs = new List<ProcedureRequisition>(); foreach (var req in procedureReqs) { if (CollectionUtils.Contains(order.Procedures, x => req.ProcedureNumber == x.Number)) { existingReqs.Add(req); } else { addedReqs.Add(req); } } // process the additions first, so that we don't accidentally cancel an order (if all its procedures are cancelled momentarily) var procedureNumberBroker = PersistenceContext.GetBroker<IProcedureNumberBroker>(); var dicomUidBroker = PersistenceContext.GetBroker<IDicomUidBroker>(); foreach (var req in addedReqs) { var requestedType = this.PersistenceContext.Load<ProcedureType>(req.ProcedureType.ProcedureTypeRef); // create a new procedure for this requisition var procedure = new Procedure(requestedType, procedureNumberBroker.GetNext(), dicomUidBroker.GetNewUid()) { DowntimeRecoveryMode = isDowntime }; order.AddProcedure(procedure); // note: need to lock the new procedure now, prior to creating the procedure steps // otherwise may get exceptions saying the Procedure is a transient object this.PersistenceContext.Lock(procedure, DirtyState.New); // create the procedure steps procedure.CreateProcedureSteps(); // apply the requisition information to the actual procedure assembler.UpdateProcedureFromRequisition(procedure, req, this.CurrentUserStaff, this.PersistenceContext); LogicalHL7Event.ProcedureCreated.EnqueueEvents(procedure); } // process updates foreach (var req in existingReqs) { var requestedType = this.PersistenceContext.Load<ProcedureType>(req.ProcedureType.ProcedureTypeRef); var procedure = CollectionUtils.SelectFirst(order.Procedures, x => req.ProcedureNumber == x.Number); // validate that the type has not changed if (!procedure.Type.Equals(requestedType)) throw new RequestValidationException("Order modification must not modify the type of a requested procedure."); // If the procedure is already terminated, just move on to the next one since procedures cannot be "un-terminated". if (procedure.IsTerminated) continue; // apply the requisition information to the actual procedure assembler.UpdateProcedureFromRequisition(procedure, req, this.CurrentUserStaff, this.PersistenceContext); (req.Cancelled ? LogicalHL7Event.ProcedureCancelled : LogicalHL7Event.ProcedureModified).EnqueueEvents(procedure); } }
public ModifyOrderResponse ModifyOrder(ModifyOrderRequest request) { Platform.CheckForNullReference(request, "request"); Platform.CheckMemberIsSet(request.Requisition, "Requisition"); Platform.CheckMemberIsSet(request.Requisition.OrderRef, "OrderRef"); var order = this.PersistenceContext.Load<Order>(request.Requisition.OrderRef); var assembler = new OrderEntryAssembler(); assembler.UpdateOrderFromRequisition(order, request.Requisition, this.CurrentUserStaff, this.PersistenceContext); UpdateProceduresHelper(order, request.Requisition.Procedures, request); ValidateVisitsExist(order); this.PersistenceContext.SynchState(); var orderAssembler = new OrderAssembler(); return new ModifyOrderResponse(orderAssembler.CreateOrderSummary(order, this.PersistenceContext)); }