Example #1
0
            public override void Execute()
            {
                List <string> studyUids = new List <string>();

                if (Result.QueryItems != null && Result.QueryItems.Count > 0)
                {
                    foreach (RetrieveQueryItem queryItem in Result.QueryItems)
                    {
                        if (queryItem.Study != null && !string.IsNullOrEmpty(queryItem.Study.StudyInstanceUid))
                        {
                            studyUids.Add(queryItem.Study.StudyInstanceUid);
                        }
                    }
                }

                if (studyUids.Count == 0)
                {
                    OnError("No Study Instance UID is specified");
                    OnCommandExecuted();
                    return;
                }

                if (this.IsCancelRequested())
                {
                    OnCancelRequested();
                    OnCommandExecuted();
                    return;
                }

                OnStatusChanged(RetrieveStatus.InProgress, "Querying for studies...");

                string url = null;

                try
                {
                    NBIARetrieveByStudyUIDs nbiaRetrieveByStudyUIDs = new NBIARetrieveByStudyUIDs();
                    url = nbiaRetrieveByStudyUIDs.retrieveStudyURL(studyUids.ToArray(), SearchSettings.Default.NBIADataServiceUrl);
                }
                catch (DataServiceUtil.GridServicerException ex)
                {
                    Platform.Log(LogLevel.Error, ex, "Failed to retrieve images from NBIA");
                }

                if (this.IsCancelRequested())
                {
                    OnCancelRequested();
                    OnCommandExecuted();
                    return;
                }

                if (string.IsNullOrEmpty(url))
                {
                    //Result.ProgressMessage = "No studies returned by NBIA.";
                    OnCommandCompleted("No studies returned by NBIA.");
                    OnCommandExecuted();
                    return;
                }

                string downloadedFilesFolder = this.DownloadQueryResults(url);

                if (this.IsCancelRequested())
                {
                    // TODO - delete downloaded files

                    OnCancelRequested();
                    OnCommandExecuted();
                    return;
                }

                if (string.IsNullOrEmpty(downloadedFilesFolder))
                {
                    // Status/Error should be reported by now

                    OnCommandExecuted();
                    return;
                }

                OnProgressUpdated("Importing images");

                try
                {
                    string[] files = Directory.GetFiles(downloadedFilesFolder, "*.dcm", SearchOption.AllDirectories);
                    if (files.Length > 0)
                    {
                        this.ImportDicomFiles(files);
                    }
                }
                catch (Exception ex)
                {
                    Platform.Log(LogLevel.Error, ex, "Error importing NBIA images");
                    OnError("Error importing NBIA images");
                    OnCommandExecuted();
                    return;
                }

                OnCommandCompleted("Done");
                OnCommandExecuted();
            }
        protected void RetrieveStudiesFromNBIA(List <string> studyUids)
        {
            string         errorMsg = null;
            BackgroundTask task     = new BackgroundTask(
                delegate(IBackgroundTaskContext context)
            {
                if (studyUids.Count == 0)
                {
                    context.Complete(null);
                    return;
                }

                try
                {
                    BackgroundTaskProgress progress = new BackgroundTaskProgress(0, 3, "Querying for available images");
                    context.ReportProgress(progress);
                    if (context.CancelRequested)
                    {
                        context.Cancel();
                        return;
                    }

                    NBIARetrieveByStudyUIDs nbiaRetrieveByStudyUIDs = new NBIARetrieveByStudyUIDs();
                    string url = nbiaRetrieveByStudyUIDs.retrieveStudyURL(studyUids.ToArray(), SearchSettings.Default.NBIADataServiceUrl);

                    if (!string.IsNullOrEmpty(url))
                    {
                        progress = new BackgroundTaskProgress(1, 3, "Retrieving images");
                        context.ReportProgress(progress);
                        if (context.CancelRequested)
                        {
                            context.Cancel();
                            return;
                        }

                        string downloadedFilesFolder = this.DownloadQueryResults(url, context);

                        if (!string.IsNullOrEmpty(downloadedFilesFolder))
                        {
                            progress = new BackgroundTaskProgress(2, 3, "Importing images");
                            context.ReportProgress(progress);
                            if (context.CancelRequested)
                            {
                                context.Cancel();
                                return;
                            }

                            string[] files = Directory.GetFiles(downloadedFilesFolder, "*.dcm", SearchOption.AllDirectories);
                            if (files.Length > 0)
                            {
                                this.ImportDicomFiles(files);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    errorMsg = ex.Message;
                    Platform.Log(LogLevel.Error, ex, "Failed to retrieve requested study(ies)");
                }

                context.Complete(null);
            }, true);

            ProgressDialog.Show(task, this.Context.DesktopWindow, true, ProgressBarStyle.Blocks);

            if (!string.IsNullOrEmpty(errorMsg))
            {
                this.Context.DesktopWindow.ShowMessageBox(errorMsg, MessageBoxActions.Ok);
            }
        }