// Essentially this is a copy of ClearCanvas.ImageViewer.StudyManagement.Core.DicomFilePublisher that allows us to move files // instead of leaving them orphaned in the original folder. // TODO: revisit this when ClearCanvas.ImageViewer.StudyManagement.Core.DicomFilePublisher gets an option for moving files public static void PublishLocal(ICollection<DicomFile> files) { if (files == null || files.Count == 0) return; var configuration = ClearCanvas.ImageViewer.Common.DicomServer.DicomServer.GetConfiguration(); 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.Move, FileImportBehaviourEnum.Move); // THIS IS THE CHANGE 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 Exception(message, ex); } }
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); } utility.PulseStudyWorkItems(); } catch (Exception ex) { var message = string.Format("Failed to import files"); throw new DicomFilePublishingException(message, ex); } }
public override bool OnReceiveRequest(Dicom.Network.DicomServer server, ServerAssociationParameters association, byte presentationID, DicomMessage message) { string studyInstanceUid; string seriesInstanceUid; DicomUid sopInstanceUid; bool ok = message.DataSet[DicomTags.SopInstanceUid].TryGetUid(0, out sopInstanceUid); if (ok) ok = message.DataSet[DicomTags.SeriesInstanceUid].TryGetString(0, out seriesInstanceUid); if (ok) ok = message.DataSet[DicomTags.StudyInstanceUid].TryGetString(0, out studyInstanceUid); if (!ok) { Platform.Log(LogLevel.Error, "Unable to retrieve UIDs from request message, sending failure status."); server.SendCStoreResponse(presentationID, message.MessageId, sopInstanceUid.UID, DicomStatuses.ProcessingFailure); return true; } if (_importContext == null) { LoadImportContext(association); } var importer = new ImportFilesUtility(_importContext); var result = importer.Import(message, BadFileBehaviourEnum.Ignore, FileImportBehaviourEnum.Save); if (result.Successful) { if (!String.IsNullOrEmpty(result.AccessionNumber)) Platform.Log(LogLevel.Info, "Received SOP Instance {0} from {1} to {2} (A#:{3} StudyUid:{4})", result.SopInstanceUid, association.CallingAE, association.CalledAE, result.AccessionNumber, result.StudyInstanceUid); else Platform.Log(LogLevel.Info, "Received SOP Instance {0} from {1} to {2} (StudyUid:{3})", result.SopInstanceUid, association.CallingAE, association.CalledAE, result.StudyInstanceUid); server.SendCStoreResponse(presentationID, message.MessageId, message.AffectedSopInstanceUid, result.DicomStatus); } else { if (result.DicomStatus == DicomStatuses.ProcessingFailure) Platform.Log(LogLevel.Error, "Failure importing sop: {0}", result.ErrorMessage); //OnReceiveError(message, result.ErrorMessage, association.CallingAE); server.SendCStoreResponse(presentationID, message.MessageId, message.AffectedSopInstanceUid, result.DicomStatus, result.ErrorMessage); } return true; }
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); } }
public bool CompleteStream(Dicom.Network.DicomServer server, ServerAssociationParameters assoc, byte presentationId, DicomMessage message) { DicomProcessingResult result; var importer = new ImportFilesUtility(_importContext); if (_rejectFile) { result = new DicomProcessingResult(); result.SetError(DicomStatuses.StorageStorageOutOfResources, string.Format("Import failed, disk space usage exceeded")); string studyInstanceUid = message.DataSet[DicomTags.StudyInstanceUid].GetString(0, string.Empty); WorkItem workItem; lock (_importContext.StudyWorkItemsSyncLock) _importContext.StudyWorkItems.TryGetValue(studyInstanceUid, out workItem); importer.InsertFailedWorkItemUid(workItem, message, result); _importContext.FatalError = true; importer.AuditFailure(result); Platform.Log(LogLevel.Warn, "Failure receiving sop, out of disk space: {0}", message.AffectedSopInstanceUid); server.SendCStoreResponse(presentationId, message.MessageId, message.AffectedSopInstanceUid, result.DicomStatus); return true; } try { if (_fileStream != null) { _fileStream.Flush(true); _fileStream.Close(); _fileStream.Dispose(); _fileStream = null; } // Convert to file to pass in the source filename var theFile = new DicomFile(message,_sourceFilename); result = importer.Import(theFile, BadFileBehaviourEnum.Delete, FileImportBehaviourEnum.Move); if (result.Successful) { if (!String.IsNullOrEmpty(result.AccessionNumber)) Platform.Log(LogLevel.Info, "Received SOP Instance {0} from {1} to {2} (A#:{3} StudyUid:{4})", result.SopInstanceUid, assoc.CallingAE, assoc.CalledAE, result.AccessionNumber, result.StudyInstanceUid); else Platform.Log(LogLevel.Info, "Received SOP Instance {0} from {1} to {2} (StudyUid:{3})", result.SopInstanceUid, assoc.CallingAE, assoc.CalledAE, result.StudyInstanceUid); } } catch (Exception e) { result = new DicomProcessingResult { DicomStatus = DicomStatuses.ProcessingFailure, ErrorMessage = e.Message }; } if (!result.Successful) { Platform.Log(LogLevel.Warn, "Failure importing sop: {0}", result.ErrorMessage); } CleanupFile(); server.SendCStoreResponse(presentationId, message.MessageId, message.AffectedSopInstanceUid, result.DicomStatus); return true; }
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); utility.PulseStudyWorkItems(); } catch (Exception ex) { var message = string.Format("Failed to import files"); throw new DicomFilePublishingException(message, ex); } }
public override bool OnReceiveRequest(ClearCanvas.Dicom.Network.DicomServer server, ServerAssociationParameters association, byte presentationID, DicomMessage message) { string studyInstanceUid; string seriesInstanceUid; DicomUid sopInstanceUid; bool ok = message.DataSet[DicomTags.SopInstanceUid].TryGetUid(0, out sopInstanceUid); if (ok) ok = message.DataSet[DicomTags.SeriesInstanceUid].TryGetString(0, out seriesInstanceUid); if (ok) ok = message.DataSet[DicomTags.StudyInstanceUid].TryGetString(0, out studyInstanceUid); if (!ok) { Platform.Log(LogLevel.Error, "Unable to retrieve UIDs from request message, sending failure status."); server.SendCStoreResponse(presentationID, message.MessageId, sopInstanceUid.UID, DicomStatuses.ProcessingFailure); return true; } if (_importContext == null) { _importContext = new DicomReceiveImportContext(association.CallingAE, GetRemoteHostName(association), StudyStore.GetConfiguration(), EventSource.CurrentProcess); // Publish new WorkItems as they're added to the context lock (_importContext.StudyWorkItemsSyncLock) { _importContext.StudyWorkItems.ItemAdded += delegate(object sender, DictionaryEventArgs<string,WorkItem> args) { Platform.GetService( (IWorkItemActivityMonitorService service) => service.Publish(new WorkItemPublishRequest { Item = WorkItemDataHelper .FromWorkItem( args.Item) })); var auditedInstances = new AuditedInstances(); var request = args.Item.Request as DicomReceiveRequest; if (request != null) { auditedInstances.AddInstance(request.Patient.PatientId, request.Patient.PatientsName, request.Study.StudyInstanceUid); } AuditHelper.LogReceivedInstances( association.CallingAE, GetRemoteHostName(association), auditedInstances, EventSource.CurrentProcess, EventResult.Success, EventReceiptAction.ActionUnknown); } ; _importContext.StudyWorkItems.ItemChanged += (sender, args) => Platform.GetService( (IWorkItemActivityMonitorService service) => service.Publish(new WorkItemPublishRequest {Item = WorkItemDataHelper.FromWorkItem(args.Item)})); } } var importer = new ImportFilesUtility(_importContext); var result = importer.Import(message,BadFileBehaviourEnum.Ignore, FileImportBehaviourEnum.Save); if (result.Successful) { if (!String.IsNullOrEmpty(result.AccessionNumber)) Platform.Log(LogLevel.Info, "Received SOP Instance {0} from {1} to {2} (A#:{3} StudyUid:{4})", result.SopInstanceUid, association.CallingAE, association.CalledAE, result.AccessionNumber, result.StudyInstanceUid); else Platform.Log(LogLevel.Info, "Received SOP Instance {0} from {1} to {2} (StudyUid:{3})", result.SopInstanceUid, association.CallingAE, association.CalledAE, result.StudyInstanceUid); server.SendCStoreResponse(presentationID, message.MessageId, message.AffectedSopInstanceUid, result.DicomStatus); } else { if (result.DicomStatus==DicomStatuses.ProcessingFailure) Platform.Log(LogLevel.Error, "Failure importing sop: {0}", result.ErrorMessage); //OnReceiveError(message, result.ErrorMessage, association.CallingAE); server.SendCStoreResponse(presentationID, message.MessageId, message.AffectedSopInstanceUid, result.DicomStatus, result.ErrorMessage); } return true; }
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); }
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); }