public CompleteOrderDocumentationResponse CompleteOrderDocumentation(CompleteOrderDocumentationRequest request)
        {
            var order = this.PersistenceContext.Load <Order>(request.OrderRef);

            var interpretationSteps = new List <InterpretationStep>();

            foreach (var procedure in order.Procedures)
            {
                if (procedure.DocumentationProcedureStep != null && !procedure.DocumentationProcedureStep.IsTerminated)
                {
                    procedure.DocumentationProcedureStep.Complete();
                }

                // schedule the interpretation step if the procedure was performed
                // Note: this logic is probably UHN-specific... ideally this aspect of the workflow should be configurable,
                // because it may be desirable to scheduled the interpretation prior to completing the documentation
                if (procedure.IsPerformed)
                {
                    var interpretationStep = GetPendingInterpretationStep(procedure);
                    if (interpretationStep != null)
                    {
                        // bug #3037: schedule the interpretation for the performed time, which may be earlier than the current time
                        // in downtime mode
                        interpretationStep.Schedule(procedure.PerformedTime);
                        interpretationSteps.Add(interpretationStep);
                    }
                }
            }

            this.PersistenceContext.SynchState();

            var planAssembler = new ProcedurePlanAssembler();

            return(new CompleteOrderDocumentationResponse
            {
                ProcedurePlan = planAssembler.CreateProcedurePlanSummary(order, this.PersistenceContext),
                InterpretationStepRefs = CollectionUtils.Map <InterpretationStep, EntityRef>(interpretationSteps, step => step.GetRef())
            });
        }
        private bool Save(bool completeDocumentation)
        {
            // only do validation if they are completing the documentation, not if they are just saving a draft
            if (completeDocumentation)
            {
                if (this.HasValidationErrors)
                {
                    ShowValidation(true);
                    return(false);
                }
            }

            try
            {
                // allow extension pages to save data
                var veto = false;
                foreach (var page in _extensionPages)
                {
                    veto = veto || !page.Save(completeDocumentation);
                }
                if (veto)
                {
                    return(false);
                }

                _orderDetailsComponent.SaveData();
                _ppsComponent.SaveData();
                Platform.GetService <IModalityWorkflowService>(service =>
                {
                    var saveRequest = new SaveOrderDocumentationDataRequest(
                        _procedurePlan.OrderRef,
                        _orderExtendedProperties,
                        _orderNotes,
                        new List <ModalityPerformedProcedureStepDetail>(_ppsComponent.PerformedProcedureSteps),
                        _assignedRadiologist);

                    var saveResponse = service.SaveOrderDocumentationData(saveRequest);

                    if (completeDocumentation)
                    {
                        var completeRequest  = new CompleteOrderDocumentationRequest(saveResponse.ProcedurePlan.OrderRef);
                        var completeResponse = service.CompleteOrderDocumentation(completeRequest);

                        RefreshProcedurePlanSummary(completeResponse.ProcedurePlan);
                    }
                    else
                    {
                        RefreshProcedurePlanSummary(saveResponse.ProcedurePlan);
                    }
                });

                return(true);
            }
            catch (FaultException <ConcurrentModificationException> )
            {
                // bug #3469: we handle this exception explicitly, so we can instruct the user to close the workspace
                this.Host.ShowMessageBox(SR.MessageConcurrentModificationCloseWorkspace, MessageBoxActions.Ok);

                _saveEnabled = false;
                NotifyPropertyChanged("SaveEnabled");

                _completeEnabled = false;
                NotifyPropertyChanged("CompleteEnabled");
            }
            catch (Exception e)
            {
                ExceptionHandler.Report(e, this.Host.DesktopWindow);
            }

            return(false);
        }