private void ProcessResultSet(Dictionary <string, List <Item> > set, EmailUtility emailUtility)
 {
     foreach (var recipient in set.Keys)
     {
         try
         {
             emailUtility.SendNotificationEmail(set[recipient], recipient);
         }
         catch (Exception exc)
         {
             Logger.Error(string.Format("Sending notification to {0} failed", recipient), exc, this);
         }
     }
 }
        //        The entry point of the agent
        public void Run(Item[] itemArray, CommandItem commandItem, ScheduleItem scheduledItem)
        {
            Logger.Info(string.Format("Started on {0}", DateTime.Now), this);
            //          Association between users and items:key - user email, value - items to include in notification
            Dictionary<string, List<Item>> recipientItemsDictionary = new Dictionary<string, List<Item>>();
            try
            {
                EmailUtility emailUtility = new EmailUtility();
                var allWorkflows = WorkflowHelper.GetAllWorkflows();
                foreach (var workflow in allWorkflows)
                {
                    var worklowStates = WorkflowHelper.GetNonFinalStatesByTemplate(workflow, StateTemplateName);
                    foreach (var state in worklowStates)
                    {
                        string timeFrameString = WorkflowHelper.GetStateFieldValue(state, TimeFiledName);
                        if (string.IsNullOrEmpty(timeFrameString))
                        {
                            Logger.Warn(
                                string.Format("{0} field isn't specified for state {1}", TimeFiledName,
                                              state.DisplayName), this);
                            continue;
                        }

                        TimeSpan timeFrame = DateUtil.ParseTimeSpan(timeFrameString, TimeSpan.Zero);
                        if (timeFrame == TimeSpan.Zero)
                        {
                            Logger.Warn(
                                string.Format("{0} field isn't well formatted for state {1}", TimeFiledName,
                                              state.DisplayName), this);
                            continue;
                        }

                        Field recipientsField = WorkflowHelper.GetStateField(state, RecipientsFieldName);
                        if (recipientsField == null || string.IsNullOrEmpty(recipientsField.Value))
                        {
                            Logger.Warn(
                                string.Format("{0} field isn't specified for state {1}", RecipientsFieldName,
                                              state.DisplayName), this);
                            continue;
                        }

                        List<string> recepients = GetEmailsForUsersAndRoles(recipientsField);
                        if (recepients.Count == 0)
                        {
                            Logger.Warn(
                                string.Format("There's no users with valid email addresses to notify for state {0}",
                                              state.DisplayName), this);
                            continue;
                        }

                        var itemsInState = WorkflowHelper.GetItemsInState(workflow, state.StateID);
                        foreach (var item in  itemsInState)
                        {
                            if (WorkflowHelper.IsTimeLimitExceeded(item, workflow, timeFrame))
                            {
                                AddToResultSet(item, recipientItemsDictionary, recepients.ToArray());
                            }
                        }
                    }
                }

                ProcessResultSet(recipientItemsDictionary, emailUtility);
            }
            catch (Exception exc)
            {
                Logger.Error("Unspecified error ocurred", exc, this);
            }
            finally
            {
                Logger.Info("Stopped", this);
            }
        }
 private void ProcessResultSet(Dictionary<string, List<Item>> set, EmailUtility emailUtility)
 {
     foreach (var recipient in set.Keys)
     {
         try
         {
             emailUtility.SendNotificationEmail(set[recipient], recipient);
         }
         catch (Exception exc)
         {
             Logger.Error(string.Format("Sending notification to {0} failed", recipient), exc, this);
         }
     }
 }
//        The entry point of the agent
        public void Run(Item[] itemArray, CommandItem commandItem, ScheduleItem scheduledItem)
        {
            Logger.Info(string.Format("Started on {0}", DateTime.Now), this);
//          Association between users and items:key - user email, value - items to include in notification
            Dictionary <string, List <Item> > recipientItemsDictionary = new Dictionary <string, List <Item> >();

            try
            {
                EmailUtility emailUtility = new EmailUtility();
                var          allWorkflows = WorkflowHelper.GetAllWorkflows();
                foreach (var workflow in allWorkflows)
                {
                    var worklowStates = WorkflowHelper.GetNonFinalStatesByTemplate(workflow, StateTemplateName);
                    foreach (var state in worklowStates)
                    {
                        string timeFrameString = WorkflowHelper.GetStateFieldValue(state, TimeFiledName);
                        if (string.IsNullOrEmpty(timeFrameString))
                        {
                            Logger.Warn(
                                string.Format("{0} field isn't specified for state {1}", TimeFiledName,
                                              state.DisplayName), this);
                            continue;
                        }

                        TimeSpan timeFrame = DateUtil.ParseTimeSpan(timeFrameString, TimeSpan.Zero);
                        if (timeFrame == TimeSpan.Zero)
                        {
                            Logger.Warn(
                                string.Format("{0} field isn't well formatted for state {1}", TimeFiledName,
                                              state.DisplayName), this);
                            continue;
                        }

                        Field recipientsField = WorkflowHelper.GetStateField(state, RecipientsFieldName);
                        if (recipientsField == null || string.IsNullOrEmpty(recipientsField.Value))
                        {
                            Logger.Warn(
                                string.Format("{0} field isn't specified for state {1}", RecipientsFieldName,
                                              state.DisplayName), this);
                            continue;
                        }

                        List <string> recepients = GetEmailsForUsersAndRoles(recipientsField);
                        if (recepients.Count == 0)
                        {
                            Logger.Warn(
                                string.Format("There's no users with valid email addresses to notify for state {0}",
                                              state.DisplayName), this);
                            continue;
                        }

                        var itemsInState = WorkflowHelper.GetItemsInState(workflow, state.StateID);
                        foreach (var item in  itemsInState)
                        {
                            if (WorkflowHelper.IsTimeLimitExceeded(item, workflow, timeFrame))
                            {
                                AddToResultSet(item, recipientItemsDictionary, recepients.ToArray());
                            }
                        }
                    }
                }

                ProcessResultSet(recipientItemsDictionary, emailUtility);
            }
            catch (Exception exc)
            {
                Logger.Error("Unspecified error ocurred", exc, this);
            }
            finally
            {
                Logger.Info("Stopped", this);
            }
        }