Exemple #1
0
        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);
            }
        }
		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);
			}
		}
Exemple #3
0
        private Order PlaceOrderHelper(OrderRequisition requisition)
        {
            // get appropriate A# for this order
            var accNum = GetAccessionNumberForOrder(requisition);

            var patient           = this.PersistenceContext.Load <Patient>(requisition.Patient.PatientRef, EntityLoadFlags.Proxy);
            var orderingFacility  = this.PersistenceContext.Load <Facility>(requisition.OrderingFacility.FacilityRef, EntityLoadFlags.Proxy);
            var visit             = FindOrCreateVisit(requisition, patient, orderingFacility, accNum);
            var orderingPhysician = this.PersistenceContext.Load <ExternalPractitioner>(requisition.OrderingPractitioner.PractitionerRef, EntityLoadFlags.Proxy);
            var diagnosticService = this.PersistenceContext.Load <DiagnosticService>(requisition.DiagnosticService.DiagnosticServiceRef);
            var priority          = EnumUtils.GetEnumValue <OrderPriority>(requisition.Priority);


            var resultRecipients = CollectionUtils.Map(
                requisition.ResultRecipients ?? new List <ResultRecipientDetail>(),
                (ResultRecipientDetail s) => new ResultRecipient(
                    this.PersistenceContext.Load <ExternalPractitionerContactPoint>(s.ContactPoint.ContactPointRef, EntityLoadFlags.Proxy),
                    EnumUtils.GetEnumValue <ResultCommunicationMode>(s.PreferredCommunicationMode)));

            // generate set of procedures
            // create a temp map from procedure back to its requisition, this will be needed later
            var orderAssembler        = new OrderEntryAssembler();
            var mapProcToReq          = new Dictionary <Procedure, ProcedureRequisition>();
            var procedureNumberBroker = PersistenceContext.GetBroker <IProcedureNumberBroker>();
            var dicomUidBroker        = PersistenceContext.GetBroker <IDicomUidBroker>();
            var procedures            = CollectionUtils.Map(
                requisition.Procedures ?? new List <ProcedureRequisition>(),
                delegate(ProcedureRequisition req)
            {
                var rpt = this.PersistenceContext.Load <ProcedureType>(req.ProcedureType.ProcedureTypeRef);
                var rp  = new Procedure(rpt, procedureNumberBroker.GetNext(), dicomUidBroker.GetNewUid());
                mapProcToReq.Add(rp, req);

                // important to set this flag prior to creating the procedure steps, because it may affect
                // which procedure steps are created
                rp.DowntimeRecoveryMode = requisition.IsDowntimeOrder;
                return(rp);
            });


            // generate a new order with the default set of procedures
            var order = Order.NewOrder(
                new OrderCreationArgs(
                    Platform.Time,
                    this.CurrentUserStaff,
                    null,
                    accNum,
                    patient,
                    visit,
                    diagnosticService,
                    requisition.ReasonForStudy,
                    priority,
                    orderingFacility,
                    requisition.SchedulingRequestTime,
                    orderingPhysician,
                    resultRecipients,
                    procedures),
                procedureNumberBroker,
                dicomUidBroker);

            // note: need to lock the new order now, prior to creating the procedure steps
            // otherwise may get exceptions saying the Procedure is a transient object
            this.PersistenceContext.Lock(order, DirtyState.New);

            // create procedure steps and update from requisition
            foreach (var procedure in order.Procedures)
            {
                procedure.CreateProcedureSteps();
                if (mapProcToReq.ContainsKey(procedure))
                {
                    orderAssembler.UpdateProcedureFromRequisition(procedure, mapProcToReq[procedure], this.CurrentUserStaff, this.PersistenceContext);
                }
            }

            // add order notes
            if (requisition.Notes != null)
            {
                var noteAssembler = new OrderNoteAssembler();
                noteAssembler.SynchronizeOrderNotes(order, requisition.Notes, this.CurrentUserStaff, this.PersistenceContext);
            }

            // add attachments
            if (requisition.Attachments != null)
            {
                var attachmentAssembler = new OrderAttachmentAssembler();
                attachmentAssembler.Synchronize(order.Attachments, requisition.Attachments, this.CurrentUserStaff, this.PersistenceContext);
            }

            if (requisition.ExtendedProperties != null)
            {
                ExtendedPropertyUtils.Update(order.ExtendedProperties, requisition.ExtendedProperties);
            }

            return(order);
        }
		private Order PlaceOrderHelper(OrderRequisition requisition)
		{
			// get appropriate A# for this order
			var accNum = GetAccessionNumberForOrder(requisition);

			var patient = this.PersistenceContext.Load<Patient>(requisition.Patient.PatientRef, EntityLoadFlags.Proxy);
			var orderingFacility = this.PersistenceContext.Load<Facility>(requisition.OrderingFacility.FacilityRef, EntityLoadFlags.Proxy);
			var visit = FindOrCreateVisit(requisition, patient, orderingFacility, accNum);
			var orderingPhysician = this.PersistenceContext.Load<ExternalPractitioner>(requisition.OrderingPractitioner.PractitionerRef, EntityLoadFlags.Proxy);
			var diagnosticService = this.PersistenceContext.Load<DiagnosticService>(requisition.DiagnosticService.DiagnosticServiceRef);
			var priority = EnumUtils.GetEnumValue<OrderPriority>(requisition.Priority);


			var resultRecipients = CollectionUtils.Map(
				requisition.ResultRecipients ?? new List<ResultRecipientDetail>(),
				(ResultRecipientDetail s) => new ResultRecipient(
												this.PersistenceContext.Load<ExternalPractitionerContactPoint>(s.ContactPoint.ContactPointRef, EntityLoadFlags.Proxy),
												EnumUtils.GetEnumValue<ResultCommunicationMode>(s.PreferredCommunicationMode)));

			// generate set of procedures
			// create a temp map from procedure back to its requisition, this will be needed later
			var orderAssembler = new OrderEntryAssembler();
			var mapProcToReq = new Dictionary<Procedure, ProcedureRequisition>();
			var procedureNumberBroker = PersistenceContext.GetBroker<IProcedureNumberBroker>();
			var dicomUidBroker = PersistenceContext.GetBroker<IDicomUidBroker>();
			var procedures = CollectionUtils.Map(
				requisition.Procedures ?? new List<ProcedureRequisition>(),
				delegate(ProcedureRequisition req)
				{
					var rpt = this.PersistenceContext.Load<ProcedureType>(req.ProcedureType.ProcedureTypeRef);
					var rp = new Procedure(rpt, procedureNumberBroker.GetNext(), dicomUidBroker.GetNewUid());
					mapProcToReq.Add(rp, req);

					// important to set this flag prior to creating the procedure steps, because it may affect
					// which procedure steps are created
					rp.DowntimeRecoveryMode = requisition.IsDowntimeOrder;
					return rp;
				});


			// generate a new order with the default set of procedures
			var order = Order.NewOrder(
				new OrderCreationArgs(
					Platform.Time,
					this.CurrentUserStaff,
					null,
					accNum,
					patient,
					visit,
					diagnosticService,
					requisition.ReasonForStudy,
					priority,
					orderingFacility,
					requisition.SchedulingRequestTime,
					orderingPhysician,
					resultRecipients,
					procedures),
				procedureNumberBroker,
				dicomUidBroker);

			// note: need to lock the new order now, prior to creating the procedure steps
			// otherwise may get exceptions saying the Procedure is a transient object
			this.PersistenceContext.Lock(order, DirtyState.New);

			// create procedure steps and update from requisition
			foreach (var procedure in order.Procedures)
			{
				procedure.CreateProcedureSteps();
				if(mapProcToReq.ContainsKey(procedure))
				{
					orderAssembler.UpdateProcedureFromRequisition(procedure, mapProcToReq[procedure], this.CurrentUserStaff, this.PersistenceContext);
				}
			}

			// add order notes
			if (requisition.Notes != null)
			{
				var noteAssembler = new OrderNoteAssembler();
				noteAssembler.SynchronizeOrderNotes(order, requisition.Notes, this.CurrentUserStaff, this.PersistenceContext);
			}

			// add attachments
			if(requisition.Attachments != null)
			{
				var attachmentAssembler = new OrderAttachmentAssembler();
				attachmentAssembler.Synchronize(order.Attachments, requisition.Attachments, this.CurrentUserStaff, this.PersistenceContext);
			}

			if (requisition.ExtendedProperties != null)
			{
				ExtendedPropertyUtils.Update(order.ExtendedProperties, requisition.ExtendedProperties);
			}

			return order;
		}