示例#1
0
        public void PublishLocal(ICollection <DicomFile> files)
        {
            if (files == null || files.Count == 0)
            {
                return;
            }

            var configuration = GetServerConfiguration();
            var context       = new ImportStudyContext(configuration.AETitle, StudyStore.GetConfiguration(), EventSource.CurrentUser);

            var utility = new ImportFilesUtility(context);

            try
            {
                DicomProcessingResult failureResult = null;

                foreach (var file in files)
                {
                    var importResult = utility.Import(file, BadFileBehaviourEnum.Ignore, FileImportBehaviourEnum.Save);
                    if (importResult.DicomStatus != DicomStatuses.Success)
                    {
                        Platform.Log(LogLevel.Warn, "Unable to import published file: {0}", importResult.ErrorMessage);
                        failureResult = importResult;
                    }
                }

                if (failureResult != null)
                {
                    throw new ApplicationException(failureResult.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                var message = string.Format("Failed to import files");
                throw new DicomFilePublishingException(message, ex);
            }
        }
        private void RebuildStudyXml()
        {
            try
            {
                var studyXml = new StudyXml(Location.Study.StudyInstanceUid);

                DicomFile lastLoadedFile = null;

                FileProcessor.Process(Location.StudyFolder, "*.dcm", delegate(string file, out bool cancel)
                {
                    cancel = _cancelRequested;
                    try
                    {
                        var dicomFile = new DicomFile(file);
                        dicomFile.Load(DicomReadOptions.Default |
                                       DicomReadOptions.StorePixelDataReferences);

                        String sopInstanceUid = dicomFile.DataSet[DicomTags.SopInstanceUid].GetString(0, dicomFile.MediaStorageSopInstanceUid);
                        if (Path.GetFileNameWithoutExtension(file) == sopInstanceUid)
                        {
                            if (!studyXml.AddFile(dicomFile))
                            {
                                Platform.Log(LogLevel.Warn,
                                             "Importing file that was in the wrong study folder: {0}",
                                             file);
                                var context =
                                    new ImportStudyContext(
                                        dicomFile.SourceApplicationEntityTitle,
                                        StudyStore.GetConfiguration(), EventSource.CurrentProcess);
                                var importer = new ImportFilesUtility(context);
                                var result   = importer.Import(dicomFile,
                                                               BadFileBehaviourEnum.Delete,
                                                               FileImportBehaviourEnum.Move);
                                if (result.DicomStatus != DicomStatuses.Success)
                                {
                                    Platform.Log(LogLevel.Error,
                                                 "Unable to import file: {0}",
                                                 result.ErrorMessage);
                                    Failed         = true;
                                    FailureMessage = result.ErrorMessage;
                                }
                            }
                            else
                            {
                                lastLoadedFile = dicomFile;
                            }
                        }
                        else
                        {
                            Platform.Log(LogLevel.Info, "Ignoring duplicate file: {0}", file);
                        }
                    }
                    catch (Exception x)
                    {
                        Platform.Log(LogLevel.Error, x,
                                     "Failed to load file for reprocessing: {0}", file);
                        Failed         = true;
                        FailureMessage = x.Message;
                    }
                }, false);

                // This saves the study Xml to disk, and ensures the database is updated and the study is not marked as "deleted".
                // If a cancel was requested, don't save the file, and it will remain "as is"
                if (lastLoadedFile != null && !_cancelRequested)
                {
                    var p = new ProcessStudyUtility(Location)
                    {
                        IsReprocess = true
                    };

                    p.ProcessFile(lastLoadedFile, studyXml, null);
                }
            }
            catch (Exception x)
            {
                Platform.Log(LogLevel.Error, x, "Unexpected exception reindexing folder: {0}", Location.StudyFolder);
                Failed         = true;
                FailureMessage = x.Message;
            }

            if (_cancelRequested)
            {
                Platform.Log(LogLevel.Info, "Cancel requested while rebuilding Study XML in folder: {0}", Location.StudyFolder);
            }
            else
            {
                Platform.Log(LogLevel.Info, "Rebuilt Study XML for study: {0}", Location.Study.StudyInstanceUid);
            }
        }
        private void ReprocessFolder()
        {
            try
            {
                var studyXml = Location.LoadStudyXml();
                var fileList = new List <ProcessStudyUtility.ProcessorFile>();

                // This code will cleanup a folder and move images around to the proper location.
                // It in essence allows you to just copy a bunch of files into the filestore, and reindex will clean them up and organize them.
                FileProcessor.Process(Location.StudyFolder, "*.dcm", delegate(string file, out bool cancel)
                {
                    cancel = _cancelRequested;
                    try
                    {
                        var dicomFile = new DicomFile(file);

                        dicomFile.Load(DicomReadOptions.StorePixelDataReferences | DicomReadOptions.Default);
                        String studyInstanceUid = dicomFile.DataSet[DicomTags.StudyInstanceUid].GetString(0, string.Empty);

                        if (!Location.Study.StudyInstanceUid.Equals(studyInstanceUid))
                        {
                            Platform.Log(LogLevel.Warn,
                                         "Importing file that was in the wrong study folder: {0}",
                                         file);
                            var context =
                                new ImportStudyContext(
                                    dicomFile.SourceApplicationEntityTitle, StudyStore.GetConfiguration(), EventSource.CurrentProcess);
                            var importer = new ImportFilesUtility(context);
                            var result   = importer.Import(dicomFile, BadFileBehaviourEnum.Delete, FileImportBehaviourEnum.Move);
                            if (!result.DicomStatus.Equals(DicomStatuses.Success))
                            {
                                try
                                {
                                    Platform.Log(LogLevel.Error, "Unable to import file: {0}, deleting: {1}", result.ErrorMessage, file);
                                    FileUtils.Delete(file);
                                }
                                catch (Exception x)
                                {
                                    Platform.Log(LogLevel.Warn, x, "Unexpected exception deleting file: {0}", file);
                                    Failed         = true;
                                    FailureMessage = x.Message;
                                }
                            }
                        }
                        else
                        {
                            fileList.Add(new ProcessStudyUtility.ProcessorFile(dicomFile, null));

                            if (fileList.Count > 19)
                            {
                                var p = new ProcessStudyUtility(Location)
                                {
                                    IsReprocess = true
                                };

                                p.ProcessBatch(fileList, studyXml);

                                fileList.Clear();
                            }
                        }
                    }
                    catch (Exception x)
                    {
                        Platform.Log(LogLevel.Error, "Exception when reindexing {0} files, last file: {1}: {2}", fileList.Count, file, x.Message);
                        fileList.Clear();                                            // Clear out the failed entries
                        Failed         = true;
                        FailureMessage = x.Message;
                    }
                }, true);
                if (fileList.Count > 0)
                {
                    var p = new ProcessStudyUtility(Location)
                    {
                        IsReprocess = true
                    };

                    p.ProcessBatch(fileList, studyXml);

                    // Now apply Deletion rules

                    var ruleContext = new RulesEngineOptions
                    {
                        ApplyDeleteActions = true,
                        ApplyRouteActions  = false
                    };
                    RulesEngine.Create().ApplyStudyRules(p.StudyLocation.Study.ToStoreEntry(), ruleContext);
                }
            }
            catch (Exception x)
            {
                Platform.Log(LogLevel.Error, x, "Unexpected exception reindexing folder: {0}", Location.StudyFolder);
                Failed         = true;
                FailureMessage = x.Message;
            }

            if (_cancelRequested)
            {
                Platform.Log(LogLevel.Info, "Cancel requested while reprocessing folder: {0}", Location.StudyFolder);
            }
            else
            {
                Platform.Log(LogLevel.Info, "Completed reprocessing study folder: {0}", Location.Study.StudyInstanceUid);
            }
        }