/// <summary>
        /// Push clients
        /// </summary>
        public void PushClients()
        {
            Trace.TraceInformation("{0}: -- Starting PUSH of patients to CR --", this.m_context.JobId);

            List <Int32> unsyncedChildren = new List <int>();

            // Get the last sync to be completed
            using (SyncData dao = new SyncData())
            {
                // Last modified filter
                var      lastSync           = dao.GetLastSync();
                DateTime?lastModifiedFilter = lastSync == null ? null : (DateTime?)lastSync.StartTime;

                foreach (var id in dao.GetUnsyncedChildrenId())
                {
                    this.m_waitThread.QueueUserWorkItem(this.PushPatientsAsync, id);
                }
            }

            // Wait for the worker threads to finish
            this.m_waitThread.WaitOne();
            if (this.m_errorState)
            {
                throw new InvalidOperationException("Sync resulted in error state");
            }
        }
 /// <summary>
 /// Get the last sync context
 /// </summary>
 public static SynchronizationContext GetLastSync()
 {
     try
     {
         Trace.TraceInformation("Get last Synchrnoization Job Data");
         // Register
         using (var dao = new SyncData())
         {
             return(dao.GetLastSync());
         }
     }
     catch (Exception e)
     {
         Trace.TraceError(e.ToString());
         return(null);
     }
     finally
     {
     }
 }
        /// <summary>
        /// Pull clients
        /// </summary>
        public void PullClients()
        {
            Trace.TraceInformation("{0}: -- Starting PULL of patients from CR --", this.m_context.JobId);

            QBP_Q21 request = null;

            // Get the last sync to be completed
            using (SyncData dao = new SyncData())
            {
                // Last modified filter
                var      lastSync           = dao.GetLastSync();
                DateTime?lastModifiedFilter = lastSync == null ? null : (DateTime?)lastSync.StartTime;

                // Create a PDQ message
                if (lastModifiedFilter.HasValue)
                {
                    Trace.TraceInformation("{0}: Last sync was on {1}", this.m_context.JobId, lastModifiedFilter.Value
                                           );
                    //request = this.CreatePDQSearch(new KeyValuePair<string, string>("@PID.33", new TS(lastModifiedFilter.Value))) as QBP_Q21;
                    ////Trace.TraceInformation("{0}: Only PULL patients modified on {1:yyyy-MMM-dd}", this.m_context.JobId, new TS(this.m_context.StartTime.AddDays(-i), DatePrecision.Day).DateValue);
                    //this.m_waitThread.QueueUserWorkItem(this.PullPatientsAsync, request);

                    // Create a series of OR parameters representing days we're out of sync
                    for (int i = 0; i <= this.m_context.StartTime.Subtract(lastModifiedFilter.Value).TotalDays; i++)
                    {
                        request = this.CreatePDQSearch(new KeyValuePair <string, string>("@PID.33", new TS(this.m_context.StartTime.AddDays(-i), DatePrecision.Day)),
                                                       new KeyValuePair <string, string>("@PID.8", "M")) as QBP_Q21;
                        Trace.TraceInformation("{0}: Only PULL MALE patients modified on {1:yyyy-MMM-dd}", this.m_context.JobId, new TS(this.m_context.StartTime.AddDays(-i), DatePrecision.Day).DateValue);
                        this.m_waitThread.QueueUserWorkItem(this.PullPatientsAsync, request);
                        request = this.CreatePDQSearch(new KeyValuePair <string, string>("@PID.33", new TS(this.m_context.StartTime.AddDays(-i), DatePrecision.Day)),
                                                       new KeyValuePair <string, string>("@PID.8", "F")) as QBP_Q21;
                        Trace.TraceInformation("{0}: Only PULL FEMALE patients modified on {1:yyyy-MMM-dd}", this.m_context.JobId, new TS(this.m_context.StartTime.AddDays(-i), DatePrecision.Day).DateValue);
                        this.m_waitThread.QueueUserWorkItem(this.PullPatientsAsync, request);
                    }
                }
                else // No last modification date, we have to trick the CR into giving us a complete list
                {
                    for (int i = DateTime.Now.Year - 3; i <= DateTime.Now.Year; i++)
                    {
                        request = this.CreatePDQSearch(new KeyValuePair <String, String>("@PID.8", "F"), new KeyValuePair <String, String>("@PID.7", string.Format("{0:0000}", i))) as QBP_Q21;
                        this.m_waitThread.QueueUserWorkItem(this.PullPatientsAsync, request);
                        request = this.CreatePDQSearch(new KeyValuePair <String, String>("@PID.8", "M"), new KeyValuePair <String, String>("@PID.7", string.Format("{0:0000}", i))) as QBP_Q21;
                        this.m_waitThread.QueueUserWorkItem(this.PullPatientsAsync, request);
                    }
                }
            }

            // The wtp worker for query contiuation
            // This is when the response is not complete but there are more results waiting
            this.m_waitThread.WaitOne();
            if (this.m_errorState)
            {
                throw new InvalidOperationException("Sync resulted in error state");
            }

            // Work items
            while (this.m_workerItems.Count > 0)
            {
                this.m_waitThread.QueueUserWorkItem(this.ProcessPIDAsync, this.m_workerItems.Pop());
            }

            this.m_waitThread.WaitOne();

            if (this.m_errorState)
            {
                throw new InvalidOperationException("Sync resulted in error state");
            }
        }