/// <summary>
        /// Gets work fo all apps in a geven state after a fixed point in time.
        /// </summary>
        /// <param name="session">A secure Session.</param>
        /// <param name="formId">The form id.</param>
        /// <param name="state">The Workflow State.</param>
        /// <param name="transition">The Workflow Transision.</param>
        /// <param name="work">The work list to update.</param>
        private void GetWorkAfterDateTime(SecureSession session, string formId, WorkflowState state, WorkflowTransition transition, UnitOfWorkList<ApplicationWorkItem> work)
        {
            if (transition.ExitStateName == state.Name)
            {
                return;
            }

            ApplicationSearchFilter filter = new ApplicationSearchFilter
            {
                ApplicationStates = new List<string> { state.Name }
            };

            IEnumerable<Application> apps = null;

            if (transition.OnDateTimeCompare == ComparisonType.Value)
            {
                DateTime? pointInTime = transition.GetOnDateTimeTransitionDateTime();
                if (pointInTime != null && pointInTime <= DateTime.Now)
                {
                    apps = this.formServiceGateway.SearchApplications(session, formId, filter, null);
                }
            }

            if (transition.OnDateTimeCompare == ComparisonType.Field && !string.IsNullOrEmpty(transition.OnDateTimeField))
            {
                var offset = DateTime.Now;
                if (!string.IsNullOrEmpty(transition.OnDateTimeOffset))
                {
                    offset = offset.Subtract(transition.OnDateTimeOffset);
                }

                filter.ApplicationDataDates = new Dictionary<string, DateTimeRange>
                {
                    { transition.OnDateTimeField, new DateTimeRange() { To = offset } }
                };

                apps = this.formServiceGateway.SearchApplications(session, formId, filter);
            }

            if (apps != null)
            {
                work.AddRange(apps.Select(app => new ApplicationWorkItem
                {
                    Application = app,
                    Transition = transition
                }).Select(workItem => new UnitOfWork<ApplicationWorkItem>(workItem)));
            }
        }
        /// <summary>
        /// Gets work based on a known transition timespan.
        /// </summary>
        /// <param name="session">A secure Session.</param>
        /// <param name="formId">The form id.</param>
        /// <param name="state">The Workflow State.</param>
        /// <param name="transition">The Workflow Transision.</param>
        /// <param name="work">The work list to update.</param>
        private void GetWorkAfterTimespan(SecureSession session, string formId, WorkflowState state, WorkflowTransition transition, UnitOfWorkList<ApplicationWorkItem> work)
        {
            DateTime? lastTransitionDate = transition.GetKnownTransitionDateTime();
            if (lastTransitionDate != null)
            {
                ApplicationSearchFilter filter = new ApplicationSearchFilter
                {
                    ApplicationStates = new List<string> { state.Name },
                    StateTransitionDate = new DateTimeRange()
                    {
                        To = lastTransitionDate
                    }
                };

                // Pagination pagination = new Pagination() { PageIndex = 0, PageSize = 50 };
                Pagination pagination = null;

                IEnumerable<Application> apps = this.formServiceGateway.SearchApplications(session, formId, filter, pagination);
                work.AddRange(apps.Select(app => new ApplicationWorkItem
                {
                    Application = app,
                    Transition = transition
                }).Select(workItem => new UnitOfWork<ApplicationWorkItem>(workItem)));
            }
        }
        /// <summary>
        /// Gets work for all applicaiotns in the specified state.
        /// </summary>
        /// <param name="session">A secure Session.</param>
        /// <param name="formId">The form id.</param>
        /// <param name="state">The Workflow State.</param>
        /// <param name="transition">The Workflow Transision.</param>
        /// <param name="work">The work list to update.</param>
        private void GetAllEventsInState(SecureSession session, string formId, WorkflowState state, WorkflowTransition transition, UnitOfWorkList<ApplicationWorkItem> work)
        {
            ApplicationSearchFilter filter = new ApplicationSearchFilter
            {
                ApplicationStates = new List<string> { state.Name }
            };

            IEnumerable<Application> apps = this.formServiceGateway.SearchApplications(session, formId, filter);
            work.AddRange(apps.Select(app => new ApplicationWorkItem
            {
                Application = app,
                Transition = transition
            }).Select(workItem => new UnitOfWork<ApplicationWorkItem>(workItem)));
        }