private int SendAnnotationsInFolderToDataService(string folderName, string dataServiceUrl)
        {
            if (string.IsNullOrEmpty(folderName) || string.IsNullOrEmpty(dataServiceUrl))
            {
                Platform.Log(LogLevel.Error, "Trying to send queued annotations from an invalid folder ({0}) or to an invalid data service ({1})", folderName, dataServiceUrl);
                return 0;
            }

            var folderPath = System.IO.Path.Combine(AimSettings.Default.AnnotationQueuesFolder, folderName);
            if (!System.IO.Directory.Exists(folderPath))
                return 0;

            var sentCounter = 0;
            try
            {
                var annotationFiles = System.IO.Directory.GetFiles(folderPath, "*.xml", System.IO.SearchOption.TopDirectoryOnly);

                const int fileCountToSendAtOnce = 10;
                for (var i = 0; i < (annotationFiles.Length + fileCountToSendAtOnce) / fileCountToSendAtOnce; i++)
                {
                    var xmlAnnotations = new Dictionary<string, string>();
                    var readAnnotationFiles = new List<string>();
                    using (var xmlModel = new aim_dotnet.XmlModel())
                    {
                        var nextBatchMax = Math.Min((i + 1)*fileCountToSendAtOnce, annotationFiles.Length);
                        for (var j = i*fileCountToSendAtOnce; j < nextBatchMax; j++)
                        {
                            var annotationPathName = System.IO.Path.Combine(folderPath, annotationFiles[j]);
                            try
                            {
                                var annotations = xmlModel.ReadAnnotationsFromFile(annotationPathName);
                                try
                                {
                                    foreach (var annotation in annotations)
                                    {
                                        xmlAnnotations.Add(annotation.UniqueIdentifier, xmlModel.WriteAnnotationToXmlString(annotation));
                                    }
                                    readAnnotationFiles.Add(annotationPathName);
                                }
                                catch (Exception ex)
                                {
                                    Platform.Log(LogLevel.Error, ex, "Failed to convert annotation to XML string. File: {0}", annotationPathName);
                                    xmlAnnotations.Clear();
                                }
                                finally
                                {
                                    annotations.Clear();
                                }
                            }
                            catch (Exception ex)
                            {
                                Platform.Log(LogLevel.Error, ex, "Failed to read annotation file from queue: {0}", annotationPathName);
                            }
                        }
                    }

                    if (xmlAnnotations.Count > 0)
                    {
                        try
                        {
                            AIMTCGAService.AIMTCGASubmit.sendAIMTCGAAnnotation(new List<string>(xmlAnnotations.Values).ToArray());
                            sentCounter += xmlAnnotations.Count;
                            Debug.Assert(readAnnotationFiles.Count <= xmlAnnotations.Count, "There are more files to delete than read annoations");
                            xmlAnnotations.Clear();
                            foreach (var readAnnotationFile in readAnnotationFiles)
                            {
                                try
                                {
                                    System.IO.File.Delete(readAnnotationFile);
                                }
                                catch (Exception ex)
                                {
                                    Platform.Log(LogLevel.Error, ex, "Failed to delete sent annoation file ({0}). The file will be send again.", readAnnotationFile);
                                }
                            }
                            readAnnotationFiles.Clear();
                        }
                        catch (Exception ex)
                        {
                            Platform.Log(LogLevel.Debug, ex, "Failed to send annotations to the AIM Data Serice ({0}).", dataServiceUrl);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Platform.Log(LogLevel.Error, ex, "Failed to send files from {0} to server {1}", folderPath, dataServiceUrl);
            }

            return sentCounter;
        }
        private int SendAnnotationsInFolderToDataService(string folderName, string dataServiceUrl)
        {
            if (string.IsNullOrEmpty(folderName) || string.IsNullOrEmpty(dataServiceUrl))
            {
                Platform.Log(LogLevel.Error, "Trying to send queued annotations from an invalid folder ({0}) or to an invalid data service ({1})", folderName, dataServiceUrl);
                return(0);
            }

            var folderPath = System.IO.Path.Combine(AimSettings.Default.AnnotationQueuesFolder, folderName);

            if (!System.IO.Directory.Exists(folderPath))
            {
                return(0);
            }

            var sentCounter = 0;

            try
            {
                var annotationFiles = System.IO.Directory.GetFiles(folderPath, "*.xml", System.IO.SearchOption.TopDirectoryOnly);

                const int fileCountToSendAtOnce = 10;
                for (var i = 0; i < (annotationFiles.Length + fileCountToSendAtOnce) / fileCountToSendAtOnce; i++)
                {
                    var xmlAnnotations      = new Dictionary <string, string>();
                    var readAnnotationFiles = new List <string>();
                    using (var xmlModel = new aim_dotnet.XmlModel())
                    {
                        var nextBatchMax = Math.Min((i + 1) * fileCountToSendAtOnce, annotationFiles.Length);
                        for (var j = i * fileCountToSendAtOnce; j < nextBatchMax; j++)
                        {
                            var annotationPathName = System.IO.Path.Combine(folderPath, annotationFiles[j]);
                            try
                            {
                                var annotations = xmlModel.ReadAnnotationsFromFile(annotationPathName);
                                try
                                {
                                    foreach (var annotation in annotations)
                                    {
                                        xmlAnnotations.Add(annotation.UniqueIdentifier, xmlModel.WriteAnnotationToXmlString(annotation));
                                    }
                                    readAnnotationFiles.Add(annotationPathName);
                                }
                                catch (Exception ex)
                                {
                                    Platform.Log(LogLevel.Error, ex, "Failed to convert annotation to XML string. File: {0}", annotationPathName);
                                    xmlAnnotations.Clear();
                                }
                                finally
                                {
                                    annotations.Clear();
                                }
                            }
                            catch (Exception ex)
                            {
                                Platform.Log(LogLevel.Error, ex, "Failed to read annotation file from queue: {0}", annotationPathName);
                            }
                        }
                    }

                    if (xmlAnnotations.Count > 0)
                    {
                        try
                        {
                            AIMTCGAService.AIMTCGASubmit.sendAIMTCGAAnnotation(new List <string>(xmlAnnotations.Values).ToArray());
                            sentCounter += xmlAnnotations.Count;
                            Debug.Assert(readAnnotationFiles.Count <= xmlAnnotations.Count, "There are more files to delete than read annoations");
                            xmlAnnotations.Clear();
                            foreach (var readAnnotationFile in readAnnotationFiles)
                            {
                                try
                                {
                                    System.IO.File.Delete(readAnnotationFile);
                                }
                                catch (Exception ex)
                                {
                                    Platform.Log(LogLevel.Error, ex, "Failed to delete sent annoation file ({0}). The file will be send again.", readAnnotationFile);
                                }
                            }
                            readAnnotationFiles.Clear();
                        }
                        catch (Exception ex)
                        {
                            Platform.Log(LogLevel.Debug, ex, "Failed to send annotations to the AIM Data Serice ({0}).", dataServiceUrl);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Platform.Log(LogLevel.Error, ex, "Failed to send files from {0} to server {1}", folderPath, dataServiceUrl);
            }

            return(sentCounter);
        }