public PersonUpdate GetPersonUpdate(int PersonUpdateID)
        {
            var IwsRepo = new IwsRepository();
            var ds      = IwsRepo.GetPersonUpdate(PersonUpdateID);

            return(MapPersonUpdateDataSet(ds).FirstOrDefault());
        }
        public PersonUpdate BeginPersonUpdate(int PersonUpdateID)
        {
            var IwsRepo = new IwsRepository();

            if (CurrentUpdate != null && CurrentUpdate.Persons.Count > 0)
            {
                return(CurrentUpdate);
            }

            int UpdatePersonCallsPerHour = 75;

            Int32.TryParse(System.Configuration.ConfigurationManager.AppSettings["UpdatePersonCallsPerHour"], out UpdatePersonCallsPerHour);
            TimeSpan timeBetweenCalls = new TimeSpan(0, 0, 3600 / (UpdatePersonCallsPerHour <= 0 ? 75 : UpdatePersonCallsPerHour));

            var ds = IwsRepo.GetPersonUpdate(PersonUpdateID);

            CurrentUpdate = MapPersonUpdateDataSet(ds).FirstOrDefault();

            if (CurrentUpdate != null)
            {
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        DateTime lastCallTime = DateTime.UtcNow;
                        var pendingPersons    = CurrentUpdate.Persons.Where(x => x.IsPending).OrderBy(x => x.OrderBy).ToList();
                        log("Person Update ID " + CurrentUpdate.PersonUpdateID + " beginning.");
                        foreach (PersonUpdateDetail detail in pendingPersons)
                        {
                            ISBLibTest.ISBLibExtGCR IWS_Service = new ISBLibExtGCR(System.Configuration.ConfigurationManager.AppSettings["DOCM_IP"],
                                                                                   System.Configuration.ConfigurationManager.AppSettings["IDMS_IP"],
                                                                                   System.Configuration.ConfigurationManager.AppSettings["CMS_IP"],
                                                                                   System.Configuration.ConfigurationManager.AppSettings["EBTS_IP"]);

                            List <string> PersonData = new List <string>(); // empty; Person Update only.

                            log("Update Person: EmployeeID = " + detail.EmployeeID + ", PersonGUID :" + detail.PersonGUID);
                            foreach (BadgeRecord badge in detail.Badges)
                            {
                                List <string> BadgeStatusData = new List <string>();
                                BadgeStatusData.Add(badge.BadgeNumber);
                                BadgeStatusData.Add(badge.BadgeStatusCode == "ACTV" ? "Active" : "Revoked");
                                BadgeStatusData.Add(badge.ReasonForDeactivation);
                                BadgeStatusData.Add("");                    //Access Level (SIDA, Sterile): this can not be changed, so pass back their existing value if exists
                                BadgeStatusData.Add("");                    //LocalBadgeType:
                                BadgeStatusData.Add(badge.CorporationName); //EmployerName:
                                log("\tUpdate Badge Number: + " + badge.BadgeNumber + "," + badge.CorporationName + "," + badge.BadgeStatusCode + "," + badge.ReasonForDeactivation);
                                IWS_Service.UpdatePerson(detail.PersonGUID.ToString(), PersonData, BadgeStatusData, detail.TypeCode, badge == detail.Badges.Last());
                                if (badge == detail.Badges.Last())
                                {
                                    log("Sent to TSC");
                                }
                                if (PersonUpdate_CancelToken.IsCancellationRequested)
                                {
                                    // Cancel Web service was called.
                                    log("BeginPersonUpdate cancelled");
                                    PersonUpdate_CancelTokenSource = new CancellationTokenSource();
                                    PersonUpdate_CancelToken       = PersonUpdate_CancelTokenSource.Token;
                                    CurrentUpdate = null;
                                    break;
                                }
                            }

                            IwsRepo.MarkPersonUpdateDetailAsSent(detail.PersonUpdateID, detail.PersonID);
                            log("Update Person on PersonID " + detail.PersonID + " Complete");

                            DateTime finishTime   = DateTime.UtcNow;
                            DateTime nextCallTime = lastCallTime.Add(timeBetweenCalls);
                            if (finishTime < nextCallTime)
                            {
                                log("Sleeping for " + nextCallTime.Subtract(finishTime).TotalMilliseconds.ToString() + " milliseconds");
                                System.Threading.Thread.Sleep(Convert.ToInt32(nextCallTime.Subtract(finishTime).TotalMilliseconds));
                            }

                            lastCallTime = DateTime.UtcNow;
                        }
                        if (CurrentUpdate != null)
                        {
                            log("Person Update ID " + CurrentUpdate.PersonUpdateID + " complete.");
                        }
                        CurrentUpdate = null;
                    }
                    catch (Exception ex)
                    {
                        log(ex.ToString());
                    }
                });
            }
            return(CurrentUpdate);
        }