public static BrokerExchangeInfo Parse(string xml)
 {
     var doc = new XmlDocument();
     doc.LoadXml(xml);
     var nodes = doc.SelectNodes("objects/object");
     var configuration = new BrokerExchangeInfo{Files = new List<FileItem>()};
     if (nodes != null)
         foreach (XmlNode node in nodes)
         {
             var fi = new FileItem();
             configuration.Files.Add(fi);
             foreach (XmlNode childNode in node.ChildNodes)
             {
                 switch (childNode.Name)
                 {
                     case "id":
                         fi.Id = string.IsNullOrEmpty(childNode.InnerText) ? new int?() : int.Parse(childNode.InnerText);
                         break;
                     case "file":
                         fi.FilePath = string.IsNullOrEmpty(childNode.InnerText) ? string.Empty : childNode.InnerText;
                         break;
                     case "updated-at":
                         fi.UpdatedAt = string.IsNullOrEmpty(childNode.InnerText) ? new DateTime?() : DateTime.SpecifyKind(DateTime.Parse(childNode.InnerText), DateTimeKind.Utc);
                         break;
                     case "last-received-at":
                         fi.LastReceivedAt = string.IsNullOrEmpty(childNode.InnerText) ? new DateTime?() : DateTime.SpecifyKind(DateTime.Parse(childNode.InnerText), DateTimeKind.Utc);
                         break;
                     case "enabled":
                         fi.Enabled = string.IsNullOrEmpty(childNode.InnerText) ? false : bool.Parse(childNode.InnerText);
                         break;
                     case "upload-destinations":
                         var destNodes = childNode.SelectNodes("upload-destination");
                         fi.Exchanges = ParseDestinations(destNodes);
                         break;
                     default:
                         break;
                 }
             }
         }
     return configuration;
 }
        public void SendRequestUpdate()
        {
            var autoResetEvent = new AutoResetEvent(false);

            var brokerExchangeInfo = new BrokerExchangeInfo{Files = new List<FileItem>()};
            brokerExchangeInfo.Files.Add(new FileItem
                                             {
                                                 Enabled = true,
                                                 Exchanges = new List<ExchangeItem> { new ExchangeItem { Enabled = true, ExchangeName = "Stubhub", LastUploadAt = null, LastUploadFailed = false }
                                                                                       ,new ExchangeItem { Enabled = true, ExchangeName = "Event Inventory", LastUploadAt = null, LastUploadFailed = false }
                                                                                       ,new ExchangeItem { Enabled = true, ExchangeName = "Vivid", LastUploadAt = null, LastUploadFailed = false }},
                                                 FilePath = @"C:\Users\Drew Gainor\Desktop\Stubhub.txt",
                                                 Id = 899,
                                                 LastReceivedAt = null,
                                                 MarkedForDelete = false,
                                                 UpdatedAt = Convert.ToDateTime("2011-07-25T22:10:07Z")
                                             });

            brokerExchangeInfo.Files.Add(new FileItem
                                             {
                                                 Enabled = true,
                                                 Exchanges = null,
                                                 FilePath = @"C:\ticket_export\MyTickets.csv",
                                                 Id = null,
                                                 LastReceivedAt = null,
                                                 MarkedForDelete = false,
                                                 UpdatedAt = Convert.ToDateTime("2012-02-22T03:29:04Z")

                                             });

            brokerExchangeInfo.Files.Add(new FileItem
                                            {
                                                Enabled = true,
                                                Exchanges = null,
                                                FilePath = TestFullFilePath,
                                                Id = null,
                                                LastReceivedAt = null,
                                                MarkedForDelete = false,
                                                UpdatedAt = null

                                            });

            var req = new ServerConfigUpdateReq(ApiToken) { ExchangeInfo = brokerExchangeInfo };
            ServerConfigResp configResp = null;
            ClientServices.SendRequest<ServerConfigResp>(req, (r) =>
                                                                  {
                                                                      Assert.IsNotNull(r);
                                                                      configResp = r;
                                                                      autoResetEvent.Set();
                                                                  },
                                                         ProgressStatus, (e) =>
                                                                             {
                                                                                 ExceptionHander(e);
                                                                                 autoResetEvent.Set();
                                                                             });

            Assert.IsTrue(autoResetEvent.WaitOne(10000), "Asycnc Method finished");
            Assert.IsNotNull(configResp);
            Assert.IsTrue(configResp.ExchangeInfo.Files.Any());
        }
        private bool TryGetUpdateServerBrokerExchangeInfo(ref BrokerExchangeInfo exchangeInfo)
        {
            if (EventService.FileEventQueue.IsEmpty)
                return true;

            var serverResp = ClientServices.GetServerConfig(new ServerConfigReq(ApiToken));
            if (exchangeInfo != null && exchangeInfo.Files != null && exchangeInfo.Files.Count > 0)
            {
                foreach (var fItem in serverResp.ExchangeInfo.Files)
                {
                    var f2 = exchangeInfo.Files.FirstOrDefault(f => f.Id == fItem.Id);
                    if (f2 == null) continue;
                    fItem.NotificationUploadKey = f2.NotificationUploadKey;
                    fItem.FileUploadkey = f2.FileUploadkey;
                    fItem.FileCheckSum = f2.FileCheckSum;
                }
            }

            var msg = string.Empty;
            var refreshList = false;
            var exhangeFiles = serverResp.ExchangeInfo.Files;
            while (!EventService.FileEventQueue.IsEmpty)
            {
                FileEventItem item;
                if (!EventService.FileEventQueue.TryDequeue(out item)) continue;
                switch (item.ApiTokenEventType)
                {
                    case CrudEventType.Create:
                        if (!exhangeFiles.Exists(f => f.FilePath.ToLower().Equals(item.FileItem.FilePath.ToLower())))
                        {
                            exhangeFiles.Add(item.FileItem);
                            msg = string.Format("{0}{1}File Added:\n{2}", msg, string.IsNullOrEmpty(msg) ? string.Empty : "\n", item.FileItem.FilePath);
                        }
                        break;
                    case CrudEventType.Delete:
                        var deleteItem = exhangeFiles.FirstOrDefault(f => f.Id != null && f.Id == item.FileItem.Id);
                        if (deleteItem != null)
                        {
                            deleteItem.MarkedForDelete = true;
                            msg = string.Format("{0}{1}File Removed:\n{2}", msg,
                                string.IsNullOrEmpty(msg) ? string.Empty : "\n", item.FileItem.FilePath);
                        }

                        break;
                    case CrudEventType.Update:
                        var updateItem = exhangeFiles.FirstOrDefault(f => f.Id != null && f.Id == item.FileItem.Id);
                        if (updateItem != null)
                        {
                            updateItem.Enabled = item.FileItem.Enabled;
                            updateItem.FilePath = item.FileItem.FilePath;
                            msg = string.Format("{0}{1}File Updated:\n{2}", msg,
                                string.IsNullOrEmpty(msg) ? string.Empty : "\n", item.FileItem.FilePath);
                        }
                        break;
                    default:
                        refreshList = true;
                        break;
                }
            }

            if (!string.IsNullOrWhiteSpace(msg))
            {
                serverResp =
                    ClientServices.UpdateServerConfig(new ServerConfigUpdateReq(ApiToken) { ExchangeInfo = serverResp.ExchangeInfo });
                refreshList = true;
            }

            if (refreshList)
            {
                if (string.IsNullOrWhiteSpace(msg))
                    msg = "<NoBallon>Server Config info has beed refreshed";

                EventService.SendMessage(serverResp.ExchangeInfo.Files, MessageType.FileChangedAlert, msg);
            }
            exchangeInfo = serverResp.ExchangeInfo;
            return true;
        }