Пример #1
0
		private void Process(DicomMessageBase message, FileImportBehaviourEnum fileImportBehaviour, WorkItem workItem, DicomProcessingResult result)
		{
			result.Initialize();

			// Use the command processor for rollback capabilities.
			using (var commandProcessor = new ViewerCommandProcessor(String.Format("Processing Sop Instance {0}", result.SopInstanceUid)))
			{
				try
				{
					var studyLocation = new StudyLocation(result.StudyInstanceUid);
					String destinationFile = studyLocation.GetSopInstancePath(result.SeriesInstanceUid, result.SopInstanceUid);

					DicomFile file = ConvertToDicomFile(message, destinationFile, _context.SourceAE);

					// Create the Study Folder, if need be
					commandProcessor.AddCommand(new CreateDirectoryCommand(studyLocation.StudyFolder));

					bool duplicateFile = false;
					string dupName = null;

					if (File.Exists(destinationFile))
					{
						// TODO (CR Jun 2012): Shouldn't the commands themselves make this decision at the time
						// the file is being saved? Otherwise, what happens if the same SOP were being saved 2x simultaneously.
						// I know the odds are low, but just pointing it out.
						duplicateFile = true;
						dupName = Guid.NewGuid().ToString() + ".dcm";
						destinationFile = Path.Combine(Path.GetDirectoryName(destinationFile) ?? string.Empty, dupName);
					}

					if (fileImportBehaviour == FileImportBehaviourEnum.Move)
					{
						commandProcessor.AddCommand(CommandFactory.CreateRenameFileCommand(file.Filename, destinationFile, true));
					}
					else if (fileImportBehaviour == FileImportBehaviourEnum.Copy)
					{
						commandProcessor.AddCommand(CommandFactory.CreateCopyFileCommand(file.Filename, destinationFile, true));
					}
					else if (fileImportBehaviour == FileImportBehaviourEnum.Save)
					{
						commandProcessor.AddCommand(CommandFactory.CreateSaveDicomFileCommand(destinationFile, file, true));
					}

					var insertWorkItemCommand = CreateWorkItemCommand(workItem, result, file, duplicateFile, dupName);
					commandProcessor.AddCommand(insertWorkItemCommand);

					if (commandProcessor.Execute())
					{
						result.DicomStatus = DicomStatuses.Success;
						IncrementTotalFiles(insertWorkItemCommand, result.StudyInstanceUid);
					}
					else
					{
						if (commandProcessor.FailureException is ChangeConflictException
						    || commandProcessor.FailureException is SqlCeLockTimeoutException)
							result.RetrySuggested = true; // Change conflict or lock timeout may work if we just retry

						Platform.Log(LogLevel.Warn, "Failure Importing file: {0}", file.Filename);
						string failureMessage = String.Format(
							"Failure processing message: {0}. Sending failure status.",
							commandProcessor.FailureReason);
						result.SetError(DicomStatuses.ProcessingFailure, failureMessage);

						// processor already rolled back
					}
				}
				catch (Exception e)
				{
					Platform.Log(LogLevel.Error, e, "Unexpected exception when {0}.  Rolling back operation.", commandProcessor.Description);
					commandProcessor.Rollback();
					result.SetError(result.DicomStatus ?? DicomStatuses.ProcessingFailure, e.Message);
				}
			}
		}
Пример #2
0
		private void Process(DicomMessageBase message, FileImportBehaviourEnum fileImportBehaviour, WorkItem workItem, DicomProcessingResult result)
        {
            result.Initialize();

            // Use the command processor for rollback capabilities.
            using (var commandProcessor = new ViewerCommandProcessor(String.Format("Processing Sop Instance {0}", result.SopInstanceUid)))
            {
                try
                {
                    var studyLocation = new StudyLocation(result.StudyInstanceUid);
                    String destinationFile = studyLocation.GetSopInstancePath(result.SeriesInstanceUid, result.SopInstanceUid);

                    DicomFile file = ConvertToDicomFile(message, destinationFile, _context.SourceAE);

                    // Create the Study Folder, if need be
                    commandProcessor.AddCommand(new CreateDirectoryCommand(studyLocation.StudyFolder));

                    bool duplicateFile = false;
                    string dupName = null;

                    if (File.Exists(destinationFile))
                    {
                        // TODO (CR Jun 2012): Shouldn't the commands themselves make this decision at the time
                        // the file is being saved? Otherwise, what happens if the same SOP were being saved 2x simultaneously.
                        // I know the odds are low, but just pointing it out.
                        duplicateFile = true;
                        dupName = Guid.NewGuid().ToString() + ".dcm";
                        destinationFile = Path.Combine(Path.GetDirectoryName(destinationFile), dupName);
                    }

                    if (fileImportBehaviour == FileImportBehaviourEnum.Move)
                    {
                        commandProcessor.AddCommand(new RenameFileCommand(file.Filename, destinationFile, true));
                    }
                    else if (fileImportBehaviour == FileImportBehaviourEnum.Copy)
                    {
                        commandProcessor.AddCommand(new CopyFileCommand(file.Filename, destinationFile, true));
                    }
                    else if (fileImportBehaviour == FileImportBehaviourEnum.Save)
                    {
                        commandProcessor.AddCommand(new SaveDicomFileCommand(destinationFile, file, true));
                    }

                    var insertWorkItemCommand = CreateWorkItemCommand(workItem, result, file, duplicateFile, dupName);
                    commandProcessor.AddCommand(insertWorkItemCommand);

                    if (commandProcessor.Execute())
                    {
                        result.DicomStatus = DicomStatuses.Success;
                        IncrementTotalFiles(insertWorkItemCommand, result.StudyInstanceUid);  
                    }
                    else
                    {
                        if (commandProcessor.FailureException is ChangeConflictException 
                            || commandProcessor.FailureException is SqlCeLockTimeoutException)
                                result.RetrySuggested = true; // Change conflict or lock timeout may work if we just retry

                        Platform.Log(LogLevel.Warn, "Failure Importing file: {0}", file.Filename);
                        string failureMessage = String.Format(
                            "Failure processing message: {0}. Sending failure status.",
                            commandProcessor.FailureReason);
                        result.SetError(DicomStatuses.ProcessingFailure, failureMessage);
                        
                        // processor already rolled back
                    }
                }
                catch (Exception e)
                {
                    Platform.Log(LogLevel.Error, e, "Unexpected exception when {0}.  Rolling back operation.", commandProcessor.Description);
                    commandProcessor.Rollback();
                    result.SetError(result.DicomStatus ?? DicomStatuses.ProcessingFailure, e.Message);
                }
            }
        }