Esempio n. 1
0
        /// <summary>
        /// Load all of the instances in a given <see cref="SeriesXml"/> file into the component for sending.
        /// </summary>
        /// <param name="seriesXml"></param>
        /// <param name="location"></param>
        /// <param name="patientsName"></param>
        /// <param name="patientId"></param>
        /// <param name="studyXml"></param>
        private void LoadSeriesFromSeriesXml(StudyXml studyXml, StudyLocation location, SeriesXml seriesXml,
                                             string patientsName, string patientId)
        {
            foreach (InstanceXml instanceXml in seriesXml)
            {
                var instance =
                    new StorageInstance(location.GetSopInstancePath(seriesXml.SeriesInstanceUid,
                                                                    instanceXml.SopInstanceUid));

                AddStorageInstance(instance);
                instance.SopClass = instanceXml.SopClass;
                instance.TransferSyntax = instanceXml.TransferSyntax;
                instance.SopInstanceUid = instanceXml.SopInstanceUid;
                instance.PatientId = patientId;
                instance.PatientsName = patientsName;
                instance.StudyInstanceUid = studyXml.StudyInstanceUid;
            }
        }
		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);
				}
			}
		}