public virtual void ExtendEligibleGroupsLife()
        {
            ServicesSearchServiceClient searchServiceClient = new ServicesSearchServiceClient(false);
            FilterCriteria            filterCriteria        = this.GetEligibleGroupsFilter();
            int                       totalFound            = 0;
            Dictionary <string, bool> containers            = null;

            if ((Helper.CurrentTask.get_Targets() == null ? false : Helper.CurrentTask.get_Targets().Count > 0))
            {
                containers = Helper.CurrentTask.get_Targets().ToDictionary <SchedulingTarget, string, bool>((SchedulingTarget target) => target.get_Target(), (SchedulingTarget target) => false);
            }
            SearchFilter searchFilter1 = new SearchFilter();

            searchFilter1.set_ExtensionDataCriteria(filterCriteria);
            searchFilter1.set_ProviderCriteria(new FilterCriteria());
            SearchFilter searchFilter = searchFilter1;
            List <IdentityStoreObject> groupsToExtend = searchServiceClient.SearchEx(Helper.CurrentTask.get_IdentityStoreId(), 2, ref totalFound, searchFilter, containers, string.Empty, 1, -1, 20000, this.GetAttributesToLoad(), false);

            try
            {
                List <IdentityStoreObject> groupsOld          = DeepCopyExtensionMethods.DeepCopy <List <IdentityStoreObject> >(groupsToExtend);
                List <string> reducedGroupsToNotify           = new List <string>();
                List <IdentityStoreObject> groupsToUpdate     = this.PrepareGroupsForExtensions(groupsToExtend, ref reducedGroupsToNotify);
                ServicesGroupServiceClient groupServiceClient = new ServicesGroupServiceClient(false);
                List <IdentityStoreObject> groupsToUpdate1    = this.CloneObjectsForUpdate(new List <string>()
                {
                    "XGroupExpirationPolicy",
                    "XGroupExpirationDate"
                }, groupsToUpdate, groupsOld);
                if ((groupsToUpdate1 == null ? false : groupsToUpdate1.Any <IdentityStoreObject>()))
                {
                    string       compressedString = DataCompressionHelper.CompressObjects <List <IdentityStoreObject> >(groupsToUpdate1);
                    ActionResult result           = groupServiceClient.UpdateManyWithCompression(Helper.CurrentTask.get_IdentityStoreId(), compressedString, typeof(IdentityStoreObject).FullName);
                    this.LogResults(result, "ExtendEligibleGroupsLife");
                }
                if (reducedGroupsToNotify.Count > 0)
                {
                    groupServiceClient.SendGlmNotification(Helper.CurrentTask.get_IdentityStoreId(), 37, reducedGroupsToNotify);
                }
            }
            catch (Exception exception)
            {
                Exception Ex = exception;
                LogExtension.LogException(GroupsProcessor.logger, string.Format("An error ocurred while expiring groups: {0}", Ex.Message), Ex);
            }
        }
        public virtual void ExpireTheGroups(List <IdentityStoreObject> groups)
        {
            List <IdentityStoreObject> groupsForExtension = new List <IdentityStoreObject>();
            List <string> strs = new List <string>();
            List <string> groupsToNotifyForExtension        = new List <string>();
            List <IdentityStoreObject> groupsToNotifyUpdate = new List <IdentityStoreObject>();
            List <IdentityStoreObject> groupsOld            = DeepCopyExtensionMethods.DeepCopy <List <IdentityStoreObject> >(groups);

            foreach (IdentityStoreObject group in groups)
            {
                if (!this.IsGroup(group))
                {
                    GroupsProcessor.logger.DebugFormat("ExpireTheGroups. Object {0} is not a group.", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()).get_Value() ?? string.Empty);
                }
                else if (this.IsSystemGroup(group))
                {
                    GroupsProcessor.logger.DebugFormat("ExpireTheGroups. Object {0} is a system group.", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()).get_Value() ?? string.Empty);
                }
                else if (this.IsGroupInExcludedContainer(group))
                {
                    GroupsProcessor.logger.DebugFormat("ExpireTheGroups. Object {0} is in excluded containers.", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()).get_Value() ?? string.Empty);
                }
                else if (!this.ShouldExpireSecurityGroup(group))
                {
                    GroupsProcessor.logger.DebugFormat("ExpireTheGroups. Object {0} is a security group.", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()).get_Value() ?? string.Empty);
                }
                else if (this.ExtendLifeForGUS(group))
                {
                    GroupsProcessor.logger.DebugFormat("ExpireTheGroups. Extending life of object {0}", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()));
                    groupsForExtension.Add(group);
                    groupsToNotifyForExtension.Add(group.get_ObjectIdFromIdentityStore());
                }
                else if (this.PrepareGroupForLifeExtension(group))
                {
                    GroupsProcessor.logger.DebugFormat("ExpireTheGroups. Extending life of object {0}", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()));
                    groupsForExtension.Add(group);
                }
                else if (!this.HasReceivedNotificationInLast7Days(group))
                {
                    strs.Add(group.get_ObjectIdFromIdentityStore());
                    groupsToNotifyUpdate.Add(group);
                }
                else
                {
                    GroupsProcessor.logger.DebugFormat("ExpireTheGroups. Object {0} has received notification within last 7 days. Not expiring.", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()).get_Value() ?? string.Empty);
                }
            }
            ServicesGroupServiceClient groupServiceClient = new ServicesGroupServiceClient(false);

            if (groupsForExtension.Count > 0)
            {
                List <IdentityStoreObject> groupsToUpdate = this.CloneObjectsForUpdate(new List <string>()
                {
                    "XGroupExpirationDate"
                }, groupsForExtension, groupsOld);
                string       compressedString = DataCompressionHelper.CompressObjects <List <IdentityStoreObject> >(groupsToUpdate);
                ActionResult result           = groupServiceClient.UpdateManyWithCompression(Helper.CurrentTask.get_IdentityStoreId(), compressedString, typeof(IdentityStoreObject).FullName);
                this.LogResults(result, "ExtendGroupsLife");
            }
            if (strs.Count > 0)
            {
                ActionResult result = groupServiceClient.Expire(Helper.CurrentTask.get_IdentityStoreId(), strs);
                this.LogResults(result, "ExpireGroups");
                this.GetExcludedNestedGroups((
                                                 from grp in groups
                                                 where strs.Contains(grp.get_ObjectIdFromIdentityStore())
                                                 select grp).ToList <IdentityStoreObject>(), strs);
                result = groupServiceClient.SendGlmNotification(Helper.CurrentTask.get_IdentityStoreId(), 28, strs);
                this.LogResults(result, "ExpireGroups-Notifications");
                groupsToNotifyUpdate.ForEach((IdentityStoreObject g) => this.SetAttributeValue("IMGLastSentExpireNotificationDate", DateTime.Now.Date.ToString("yyyy MMMM dd HH:mm:ss"), g.get_AttributesBusinessObject()));
                List <IdentityStoreObject> groupsToUpdate = this.CloneObjectsForUpdate(new List <string>()
                {
                    "IMGLastSentExpireNotificationDate"
                }, groupsToNotifyUpdate, null);
                groupsToUpdate.ForEach((IdentityStoreObject g) => g.set_StopNotification(true));
                string compressedString = DataCompressionHelper.CompressObjects <List <IdentityStoreObject> >(groupsToUpdate);
                result = groupServiceClient.UpdateManyWithCompression(Helper.CurrentTask.get_IdentityStoreId(), compressedString, typeof(IdentityStoreObject).FullName);
                this.LogResults(result, "UpdateExpireGroupsNotificationData");
            }
            if (groupsToNotifyForExtension.Count > 0)
            {
                ActionResult result = groupServiceClient.SendGlmNotification(Helper.CurrentTask.get_IdentityStoreId(), 35, groupsToNotifyForExtension);
                this.LogResults(result, "ExtendedGroups-Notifications");
            }
        }
        public virtual void DeleteExpiredGroups(List <IdentityStoreObject> groups)
        {
            LogExtension.EnterMethod(GroupsProcessor.logger, MethodBase.GetCurrentMethod(), new object[0]);
            List <string> groupsToNotify = new List <string>();
            List <IdentityStoreObject> groupsToDelete = new List <IdentityStoreObject>();

            try
            {
                foreach (IdentityStoreObject group in groups)
                {
                    try
                    {
                        if (!this.IsGroup(group))
                        {
                            GroupsProcessor.logger.DebugFormat("DeleteExpiredGroups. Object {0} is not a group.", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()).get_Value() ?? string.Empty);
                            continue;
                        }
                        else if (this.IsSystemGroup(group))
                        {
                            GroupsProcessor.logger.DebugFormat("DeleteExpiredGroups. Object {0} is a system group.", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()).get_Value() ?? string.Empty);
                            continue;
                        }
                        else if (this.IsGroupInExcludedContainer(group))
                        {
                            GroupsProcessor.logger.DebugFormat("DeleteExpiredGroups. Object {0} is in excluded containers.", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()).get_Value() ?? string.Empty);
                            continue;
                        }
                        else if (this.ShouldExpireSecurityGroup(group))
                        {
                            string   expirationDateString = this.GetAttributeValue("XGroupExpirationDate", group.get_AttributesBusinessObject()).get_Value();
                            DateTime expirationDate       = Helper.ParseDateTime(expirationDateString);
                            if (expirationDate.Date == DateTime.MinValue.Date)
                            {
                                continue;
                            }
                            else if (DateTime.Now.Date.Subtract(expirationDate.Date).Days >= Helper.AppConfiguration.get_DeletionDaysAfterExpiry())
                            {
                                this.SetAttributeValue("IMGIsDeleted", "true", group.get_AttributesBusinessObject());
                                string updatedDisplayName = this.GetUpdatedDisplayName(group);
                                group.set_ObjectDisplayName(updatedDisplayName);
                                this.SetAttributeValue(Helper.KnownProviderAttributes.get_DisplayName(), updatedDisplayName, group.get_AttributesBusinessObject());
                                groupsToNotify.Add(group.get_ObjectIdFromIdentityStore());
                                groupsToDelete.Add(group);
                                GroupsProcessor.logger.InfoFormat("Group Deleted: {0}", group.get_ObjectDisplayName());
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            GroupsProcessor.logger.DebugFormat("DeleteExpiredGroups. Object {0} is a security group.", this.GetAttributeValue(Helper.KnownProviderAttributes.get_Name(), group.get_AttributesBusinessObject()).get_Value() ?? string.Empty);
                            continue;
                        }
                    }
                    catch (Exception exception)
                    {
                        Exception ex = exception;
                        GroupsProcessor.logger.Error(string.Format("An Error occured while performing deletion operation on group: {0} Reason: {1}", this.GetAttributeValue(Helper.KnownProviderAttributes.get_DistinguishedName(), group.get_AttributesBusinessObject()).get_Value() ?? string.Empty, ex.Message), ex);
                    }
                }
                ServicesGroupServiceClient groupServiceClient = new ServicesGroupServiceClient(false);
                if (groupsToDelete.Count > 0)
                {
                    groupsToDelete.ForEach((IdentityStoreObject g) => this.SetAttributeValue("IMGLastSentExpireNotificationDate", DateTime.Now.Date.ToString("yyyy MMMM dd HH:mm:ss"), g.get_AttributesBusinessObject()));
                    groupsToDelete.ForEach((IdentityStoreObject g) => this.SetAttributeValue("IMGIsDeleted", "TRUE", g.get_AttributesBusinessObject()));
                    List <IdentityStoreObject> groupsToUpdate = this.CloneObjectsForUpdate(new List <string>()
                    {
                        "IMGLastSentExpireNotificationDate",
                        "IMGIsDeleted",
                        Helper.KnownProviderAttributes.get_DisplayName()
                    }, groupsToDelete, null);
                    string       compressedString = DataCompressionHelper.CompressObjects <List <IdentityStoreObject> >(groupsToUpdate);
                    ActionResult result           = groupServiceClient.UpdateManyWithCompression(Helper.CurrentTask.get_IdentityStoreId(), compressedString, typeof(IdentityStoreObject).FullName);
                    this.LogResults(result, "DeleteExpiredGroups");
                }
                if (groupsToNotify.Count > 0)
                {
                    ActionResult result = groupServiceClient.SendGlmNotification(Helper.CurrentTask.get_IdentityStoreId(), 29, groupsToNotify);
                    this.LogResults(result, "DeleteExpiredGroups-Notifications");
                }
                LogExtension.ExitMethod(GroupsProcessor.logger, MethodBase.GetCurrentMethod(), new object[0]);
            }
            catch (Exception exception1)
            {
                Exception Ex = exception1;
                LogExtension.LogException(GroupsProcessor.logger, string.Format("An error ocurred while deleting 30 days old expired groups: {0}", Ex.Message), Ex);
            }
        }
        public virtual void NotifyTheExpiringGroups(List <IdentityStoreObject> expiringGroups)
        {
            List <string> groupsToNotifyOneDay              = new List <string>();
            List <string> groupsToNotifySevenDays           = new List <string>();
            List <string> groupsToNotifyThirtyDays          = new List <string>();
            List <IdentityStoreObject> groupsToNotifyUpdate = new List <IdentityStoreObject>();

            foreach (IdentityStoreObject group in expiringGroups)
            {
                if (group != null)
                {
                    if (this.IsGroup(group))
                    {
                        IdentityStoreObject parentGroup = this.GetParentGroup(expiringGroups, group);
                        if (parentGroup != null)
                        {
                            if (this.IsGroupInExcludedContainer(parentGroup))
                            {
                                continue;
                            }
                        }
                        else if (this.IsGroupInExcludedContainer(group))
                        {
                            continue;
                        }
                        if (this.ShouldExpireSecurityGroup(group))
                        {
                            if ((this.ExtendedGroups == null || this.ExtendedGroups.Count <= 0 ? true : !this.ExtendedGroups.Contains(group.get_ObjectIdFromIdentityStore())))
                            {
                                int dueDays = 0;
                                if (this.IsNotificationDueOneDay(group, out dueDays))
                                {
                                    groupsToNotifyOneDay.Add(group.get_ObjectIdFromIdentityStore());
                                    groupsToNotifyUpdate.Add(group);
                                }
                                dueDays = 0;
                                if (this.IsNotificationDueSevenDays(group, out dueDays))
                                {
                                    groupsToNotifySevenDays.Add(group.get_ObjectIdFromIdentityStore());
                                    groupsToNotifyUpdate.Add(group);
                                }
                                dueDays = 0;
                                if (this.IsNotificationDueThirtyDays(group, out dueDays))
                                {
                                    groupsToNotifyThirtyDays.Add(group.get_ObjectIdFromIdentityStore());
                                    groupsToNotifyUpdate.Add(group);
                                }
                            }
                        }
                    }
                }
            }
            if ((groupsToNotifyOneDay.Count > 0 || groupsToNotifySevenDays.Count > 0 ? true : groupsToNotifyThirtyDays.Count > 0))
            {
                ServicesGroupServiceClient groupServiceClient = new ServicesGroupServiceClient(false);
                ActionResult result = null;
                if (groupsToNotifyOneDay.Count > 0)
                {
                    result = groupServiceClient.SendGlmNotification(Helper.CurrentTask.get_IdentityStoreId(), 38, groupsToNotifyOneDay);
                    this.LogResults(result, "NotifyTheExpiringGroups-OneDay-Notifications");
                }
                if (groupsToNotifySevenDays.Count > 0)
                {
                    result = groupServiceClient.SendGlmNotification(Helper.CurrentTask.get_IdentityStoreId(), 39, groupsToNotifySevenDays);
                    this.LogResults(result, "NotifyTheExpiringGroups-SevenDays-Notifications");
                }
                if (groupsToNotifyThirtyDays.Count > 0)
                {
                    result = groupServiceClient.SendGlmNotification(Helper.CurrentTask.get_IdentityStoreId(), 40, groupsToNotifyThirtyDays);
                    this.LogResults(result, "NotifyTheExpiringGroups-ThirtyDays-Notifications");
                }
                groupsToNotifyUpdate.ForEach((IdentityStoreObject g) => this.SetAttributeValue("IMGLastSentExpireNotificationDate", DateTime.Now.Date.ToString("yyyy MMMM dd HH:mm:ss"), g.get_AttributesBusinessObject()));
                List <IdentityStoreObject> groupsToUpdate = this.CloneObjectsForUpdate(new List <string>()
                {
                    "IMGLastSentExpireNotificationDate"
                }, groupsToNotifyUpdate, null);
                groupsToUpdate.ForEach((IdentityStoreObject g) => g.set_StopNotification(true));
                string compressedString = DataCompressionHelper.CompressObjects <List <IdentityStoreObject> >(groupsToUpdate);
                result = groupServiceClient.UpdateManyWithCompression(Helper.CurrentTask.get_IdentityStoreId(), compressedString, typeof(IdentityStoreObject).FullName);
                this.LogResults(result, "NotifyTheExpiringGroups-AfterNotificationsUpdate");
            }
        }