public IAimObjectReference ReadXmlAnnotationFromString(AimVersion aimVersion, string xmlString)
        {
            IAimObjectReference aimObjectReference = null;

            switch (aimVersion)
            {
            case AimVersion.AimVersion3:
            {
                using (var aim3NativeHelper = new Aim3.Aim3NativeXmlHelper())
                {
                    aimObjectReference = aim3NativeHelper.ReadAnnotationFromString(xmlString);
                }
            }
            break;

            case AimVersion.AimVersion4:
            {
                using (var aim4NativeHelper = new Aim4.Aim4NativeXmlHelper())
                {
                    aimObjectReference = aim4NativeHelper.ReadAnnotationFromString(xmlString);
                }
            }
            break;

            default:
                Debug.Assert(false, "AimManager.ReadXmlAnnotationFromString: Unexpected AIM version");
                break;
            }

            return(aimObjectReference);
        }
        public Dictionary <string, string> WriteXmlAnnotationsToString(List <IAimObjectReference> aimAnnotationReferences)
        {
            Platform.CheckForNullReference(aimAnnotationReferences, "aimAnnotationReferences");

            var writtenAnnotations = new Dictionary <string, string>();

            var aim3Annotations = (from aimInstance in aimAnnotationReferences where aimInstance != null && aimInstance.AimVersion == AimVersion.AimVersion3 select((Aim3.Aim3ObjectReference)aimInstance).AimAnnotation).ToList();

            if (aim3Annotations.Any())
            {
                using (var aim3NativeHelper = new Aim3.Aim3NativeXmlHelper())
                {
                    foreach (var annotation in aim3Annotations)
                    {
                        writtenAnnotations.Add(annotation.UniqueIdentifier, aim3NativeHelper.WriteXmlAnnotationToString(annotation));
                    }
                }
            }

            var aim4Annotations = (from aimInstance in aimAnnotationReferences where aimInstance != null && aimInstance.AimVersion == AimVersion.AimVersion4 select((Aim4.Aim4ObjectReference)aimInstance).AnnotationCollection).ToList();

            if (aim4Annotations.Any())
            {
                using (var aim4NativeHelper = new Aim4.Aim4NativeXmlHelper())
                {
                    foreach (var annotation in aim4Annotations)
                    {
                        writtenAnnotations.Add((annotation.UniqueIdentifier ?? Aim4.AimNativeConverter.NewUid).Uid, aim4NativeHelper.WriteXmlAnnotationToString(annotation));
                    }
                }
            }

            var unknownAimVersion = aimAnnotationReferences.Where(aimInstance => aimInstance != null && aimInstance.AimVersion != AimVersion.AimVersion3 && aimInstance.AimVersion != AimVersion.AimVersion4).ToList();

            if (unknownAimVersion.Any())
            {
                Platform.Log(LogLevel.Error, "AimManager.WriteXmlAnnotationsToString: not all annotations can be saved to XML string [" + unknownAimVersion.Count + "].");
                throw new NotImplementedException("Cannot save some of the annotations to XML string [" + unknownAimVersion.Count + "]. Unknown version of AIM was encountered?");
            }

            return(writtenAnnotations);
        }
        /// <summary>
        /// Writes annotation to folder
        /// </summary>
        /// <param name="aimAnnotationReferences">List of annotations to write</param>
        /// <param name="folderPath">Complete folder path</param>
        /// <returns>List of complete file names that are written to the folder</returns>
        public string[] WriteXmlAnnotationsToFolder(List <IAimObjectReference> aimAnnotationReferences, string folderPath)
        {
            Platform.CheckForNullReference(aimAnnotationReferences, "aimAnnotationReferences");
            Platform.CheckForEmptyString(folderPath, "folderPath");

            string[] createdFiles = null;

            var aim3Annotations = (from aimInstance in aimAnnotationReferences where aimInstance != null && aimInstance.AimVersion == AimVersion.AimVersion3 select((Aim3.Aim3ObjectReference)aimInstance).AimAnnotation).ToList();

            if (aim3Annotations.Any())
            {
                using (var aim3NativeHelper = new Aim3.Aim3NativeXmlHelper())
                {
                    createdFiles = aim3NativeHelper.WriteXmlAnnotationsToFolder(aim3Annotations, folderPath);
                }
            }

            var aim4Annotations = (from aimInstance in aimAnnotationReferences where aimInstance != null && aimInstance.AimVersion == AimVersion.AimVersion4 select((Aim4.Aim4ObjectReference)aimInstance).AnnotationCollection).ToList();

            if (aim4Annotations.Any())
            {
                using (var aim4NativeHelper = new Aim4.Aim4NativeXmlHelper())
                {
                    createdFiles = aim4NativeHelper.WriteXmlAnnotationsToFolder(aim4Annotations, folderPath);
                }
            }

            var unknownAimVersion = aimAnnotationReferences.Where(aimInstance => aimInstance != null && aimInstance.AimVersion != AimVersion.AimVersion3 && aimInstance.AimVersion != AimVersion.AimVersion4).ToList();

            if (unknownAimVersion.Any())
            {
                Platform.Log(LogLevel.Error, "AimManager.WriteXmlAnnotationsToFolder: not all annotations can be saved to XML files [" + unknownAimVersion.Count + "].");
                throw new NotImplementedException("Cannot save some of the annotations to XML files [" + unknownAimVersion.Count + "]. Unknown version of AIM was encountered?");
            }

            return(createdFiles);
        }
        public static List <string> ConvertAnnotationsFromXmlToDicomFiles(AimVersion aimVersion, List <string> xmlAnnotationsFilePathNames, IBackgroundTaskContext context, out List <string> invalidFiles)
        {
            Platform.CheckForNullReference(xmlAnnotationsFilePathNames, "xmlAnnotationsFilePathNames");

            var convertedAnnotations = new List <string>();

            invalidFiles = new List <string>();

            switch (aimVersion)
            {
            case AimVersion.AimVersion3:
            {
                int cnt = 0;
                using (var aim3NativeXmlHelper = new Aim3.Aim3NativeXmlHelper())
                {
                    using (var aim3NativeDcmHelper = new Aim3.Aim3NativeDcmHelper())
                    {
                        foreach (string aimFile in xmlAnnotationsFilePathNames.Where(pathName => pathName != null))
                        {
                            // Read XML file
                            ReportTaskProgress(context, cnt, xmlAnnotationsFilePathNames.Count, String.Format("Reading file {0}", Path.GetFileName(aimFile)));
                            var annotations = aim3NativeXmlHelper.ReadAnnotationsFromFile(aimFile);

                            if (annotations.IsNullOrEmpty())
                            {
                                Platform.Log(LogLevel.Info, "No annotation is read from file {0}", Path.GetFileName(aimFile));
                                invalidFiles.Add(Path.GetFileName(aimFile));
                            }
                            else
                            {
                                // Write to temp file
                                ReportTaskProgress(context, cnt, xmlAnnotationsFilePathNames.Count,
                                                   String.Format("Read {0} annotations from file {1}", annotations.Count, Path.GetFileName(aimFile)));
                                int dcmFileCnt = 0;
                                foreach (var annotation in annotations)
                                {
                                    ReportTaskProgress(context, cnt, xmlAnnotationsFilePathNames.Count,
                                                       String.Format("Writing converted annotation to temporary file [{0} of {1}]", ++dcmFileCnt, annotations.Count));

                                    var tempFileName = aim3NativeDcmHelper.WriteAnnotationToTempFile(annotation);
                                    convertedAnnotations.Add(tempFileName);

                                    ReportTaskProgress(context, cnt, xmlAnnotationsFilePathNames.Count,
                                                       String.Format("Wrote converted annotation to file {0} [{1} of {2}]", tempFileName, dcmFileCnt, annotations.Count));
                                }
                            }
                            if (context != null && context.CancelRequested)
                            {
                                break;
                            }
                            cnt++;
                        }
                    }
                }
            }
            break;

            case AimVersion.AimVersion4:
            {
                int cnt = 0;
                using (var aim4NativeXmlHelper = new Aim4.Aim4NativeXmlHelper())
                {
                    using (var aim4NativeDcmHelper = new Aim4.Aim4NativeDcmHelper())
                    {
                        foreach (string aimFile in xmlAnnotationsFilePathNames.Where(pathName => pathName != null))
                        {
                            // Read XML file
                            ReportTaskProgress(context, cnt, xmlAnnotationsFilePathNames.Count, String.Format("Reading file {0}", Path.GetFileName(aimFile)));
                            var annotation = aim4NativeXmlHelper.ReadAnnotationFromFile(aimFile);

                            if (annotation == null)
                            {
                                Platform.Log(LogLevel.Info, "No annotation is read from file {0}", Path.GetFileName(aimFile));
                                invalidFiles.Add(Path.GetFileName(aimFile));
                            }
                            else
                            {
                                // Write to temp file
                                ReportTaskProgress(context, cnt, xmlAnnotationsFilePathNames.Count,
                                                   String.Format("Read annotation collection from file {0}", Path.GetFileName(aimFile)));

                                var tempFileName = aim4NativeDcmHelper.WriteAnnotationToTempFile(annotation);
                                convertedAnnotations.Add(tempFileName);

                                ReportTaskProgress(context, cnt, xmlAnnotationsFilePathNames.Count,
                                                   String.Format("Wrote converted annotation collection to file {0}", tempFileName));
                            }
                            if (context != null && context.CancelRequested)
                            {
                                break;
                            }
                            cnt++;
                        }
                    }
                }
            }
            break;

            default:
                Debug.Assert(false, "AimManager.ConvertAnnotationsFromXmlToDicomFiles: Unexpected AIM version");
                break;
            }

            return(convertedAnnotations);
        }
        /// <summary>
        /// Reads given AIM XML documents into memory
        /// </summary>
        /// <param name="aimVersion">Version of AIM given files are expected to be</param>
        /// <param name="filePaths">List of AIM XML documents on disk</param>
        /// <returns>Returns a dictionary of AIM file names mapped to the AIM XML document content</returns>
        public Dictionary <string, string> ReadXmlAnnotationsToMemory(AimVersion aimVersion, List <string> filePaths)
        {
            var uidToAnnotationDictionary = new Dictionary <string, string>();

            if (filePaths.IsNullOrEmpty())
            {
                return(uidToAnnotationDictionary);
            }

            switch (aimVersion)
            {
            case AimVersion.AimVersion3:
            {
                using (var aim3NativeHelper = new Aim3.Aim3NativeXmlHelper())
                {
                    foreach (var filePath in filePaths)
                    {
                        var annotations = aim3NativeHelper.ReadAnnotationsFromFile(filePath);
                        if (annotations != null)
                        {
                            Debug.Assert(annotations.Count < 2, "AIM 3 XML Annotation document cannot have more than one annotation");
                            foreach (var annotation in annotations.Where(ann => ann != null))
                            {
                                var strAnnotation = aim3NativeHelper.WriteXmlAnnotationToString(annotation);
                                if (!String.IsNullOrEmpty(strAnnotation))
                                {
                                    uidToAnnotationDictionary.Add(filePath, strAnnotation);
                                }
                            }
                        }
                    }
                }
            }
            break;

            case AimVersion.AimVersion4:
            {
                using (var aim4NativeHelper = new Aim4.Aim4NativeXmlHelper())
                {
                    foreach (var filePath in filePaths)
                    {
                        var annotation = aim4NativeHelper.ReadAnnotationFromFile(filePath);
                        if (annotation != null)
                        {
                            var strAnnotation = aim4NativeHelper.WriteXmlAnnotationToString(annotation);
                            if (!String.IsNullOrEmpty(strAnnotation))
                            {
                                uidToAnnotationDictionary.Add(filePath, strAnnotation);
                            }
                        }
                    }
                }
            }
            break;

            default:
                Debug.Assert(false, "AimManager.ReadXmlAnnotationsToMemory: Unexpected AIM version");
                break;
            }

            return(uidToAnnotationDictionary);
        }