private static void GetOldestAndNewestItemDatesByProperty(Folder folder, PropertyDefinition[] propertyToSortBy, GetMailboxFolderStatistics.SetItemDate newestItemDate, GetMailboxFolderStatistics.SetItemDate oldestItemDate, GetMailboxFolderStatistics.SetItemDate newestDeletedItemDate, GetMailboxFolderStatistics.SetItemDate oldestDeletedItemDate)
 {
     using (QueryResult queryResult = folder.ItemQuery(ItemQueryType.None, null, GetMailboxFolderStatistics.sortColumns, propertyToSortBy))
     {
         if (queryResult.EstimatedRowCount < 1)
         {
             newestItemDate(null);
             oldestItemDate(null);
         }
         else
         {
             object[][] rows = queryResult.GetRows(1);
             if (GetMailboxFolderStatistics.PropertyExists(rows[0][0]))
             {
                 newestItemDate(new DateTime?((DateTime)((ExDateTime)rows[0][0])));
             }
             queryResult.SeekToOffset(SeekReference.BackwardFromEnd, -1);
             rows = queryResult.GetRows(1);
             if (GetMailboxFolderStatistics.PropertyExists(rows[0][0]))
             {
                 oldestItemDate(new DateTime?((DateTime)((ExDateTime)rows[0][0])));
             }
         }
     }
 }
 protected override void InternalProcessRecord()
 {
     TaskLogger.LogEnter();
     try
     {
         using (MailboxSession mailboxSession = this.OpenMailboxSession())
         {
             if (mailboxSession == null)
             {
                 base.WriteError(new MailboxFolderStatisticsException(this.Identity.ToString(), Strings.LogonFailure), ErrorCategory.ReadError, null);
             }
             else
             {
                 ElcFolderType   folderScope = (this.FolderScope != null) ? this.FolderScope.Value : ElcFolderType.All;
                 List <object[]> list        = this.PopulateFolderRows(mailboxSession, folderScope);
                 if (list == null || list.Count == 0)
                 {
                     base.WriteError(new MailboxFolderStatisticsException(this.Identity.ToString(), Strings.NoFoldersInMailbox), ErrorCategory.ReadError, null);
                 }
                 else
                 {
                     for (int i = 0; i < list.Count; i++)
                     {
                         object[] array = list[i];
                         if (GetMailboxFolderStatistics.PropertyExists(array[1]))
                         {
                             MailboxFolderConfiguration folderInformation = this.GetFolderInformation(i, list, mailboxSession, this.ConfigurationSession);
                             if (folderInformation != null)
                             {
                                 GetMailboxFolderStatistics.GetFolderSearchBacklinkNames(mailboxSession, (VersionedId)array[1], folderInformation);
                                 if (this.IncludeAnalysis)
                                 {
                                     GetMailboxFolderStatistics.GetAnalysis(mailboxSession, (VersionedId)array[1], folderInformation);
                                 }
                                 if (this.IncludeOldestAndNewestItems)
                                 {
                                     GetMailboxFolderStatistics.GetOldestAndNewestItem(mailboxSession, (VersionedId)array[1], folderInformation);
                                 }
                                 this.WriteResult(folderInformation);
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (StorageTransientException ex)
     {
         base.WriteError(new MailboxFolderStatisticsException(this.Identity.ToString(), ex.ToString()), ErrorCategory.ReadError, null);
     }
     finally
     {
         TaskLogger.LogExit();
     }
 }
 private static void GetFolderSearchBacklinkNames(MailboxSession mailboxSession, VersionedId folderId, MailboxFolderConfiguration folderInfo)
 {
     PropertyDefinition[] propsToReturn = new PropertyDefinition[]
     {
         FolderSchema.SearchBacklinkNames
     };
     using (Folder folder = Folder.Bind(mailboxSession, folderId, propsToReturn))
     {
         object obj = folder.TryGetProperty(FolderSchema.SearchBacklinkNames);
         if (GetMailboxFolderStatistics.PropertyExists(obj))
         {
             folderInfo.SearchFolders = (string[])obj;
         }
     }
 }
        private MailboxFolderConfiguration GetFolderInformation(int folderIndex, List <object[]> allFolderRows, MailboxSession mailboxSession, IConfigurationSession adConfigSession)
        {
            MailboxFolderConfiguration mailboxFolderConfiguration = null;

            object[] array = allFolderRows[folderIndex];
            if (GetMailboxFolderStatistics.PropertyExists(array[1]) && GetMailboxFolderStatistics.PropertyExists(array[15]))
            {
                mailboxFolderConfiguration = new MailboxFolderConfiguration();
                string folderPath = (string)array[15];
                mailboxFolderConfiguration.FolderPath = folderPath;
                mailboxFolderConfiguration.SetIdentity(new MailboxFolderId(this.Identity.ToString(), mailboxFolderConfiguration.FolderPath));
                mailboxFolderConfiguration.Date = (DateTime)((ExDateTime)array[10]);
                if (GetMailboxFolderStatistics.PropertyExists(array[0]))
                {
                    mailboxFolderConfiguration.Name = (string)array[0];
                }
                else
                {
                    this.WriteWarning(Strings.UnableToRetrieveFolderName(mailboxFolderConfiguration.FolderPath));
                }
                StoreObjectId objectId = ((VersionedId)array[1]).ObjectId;
                mailboxFolderConfiguration.FolderId = objectId.ToBase64String();
                long num = 0L;
                if (GetMailboxFolderStatistics.PropertyExists(array[6]))
                {
                    num = (long)array[6];
                }
                mailboxFolderConfiguration.FolderSize = ByteQuantifiedSize.FromBytes(checked ((ulong)num));
                if (GetMailboxFolderStatistics.PropertyExists(array[4]))
                {
                    mailboxFolderConfiguration.ItemsInFolder = (int)array[4];
                    mailboxFolderConfiguration.ItemsInFolderAndSubfolders = mailboxFolderConfiguration.ItemsInFolder;
                }
                if (GetMailboxFolderStatistics.PropertyExists(array[5]))
                {
                    mailboxFolderConfiguration.ItemsInFolder += (int)array[5];
                    mailboxFolderConfiguration.ItemsInFolderAndSubfolders += (int)array[5];
                }
                if (GetMailboxFolderStatistics.PropertyExists(array[11]))
                {
                    mailboxFolderConfiguration.DeletedItemsInFolder = (int)array[11];
                    mailboxFolderConfiguration.DeletedItemsInFolderAndSubfolders = mailboxFolderConfiguration.DeletedItemsInFolder;
                }
                if (GetMailboxFolderStatistics.PropertyExists(array[12]))
                {
                    mailboxFolderConfiguration.DeletedItemsInFolder += (int)array[12];
                    mailboxFolderConfiguration.DeletedItemsInFolderAndSubfolders += mailboxFolderConfiguration.DeletedItemsInFolder;
                }
                int num2 = -1;
                if (GetMailboxFolderStatistics.PropertyExists(array[3]))
                {
                    num2 = (int)array[3];
                }
                if (GetMailboxFolderStatistics.PropertyExists(array[2]) && (bool)array[2])
                {
                    bool flag  = false;
                    bool flag2 = false;
                    for (int i = folderIndex + 1; i < allFolderRows.Count; i++)
                    {
                        object[] array2 = allFolderRows[i];
                        if (GetMailboxFolderStatistics.PropertyExists(array2[15]) && GetMailboxFolderStatistics.PropertyExists(array2[3]))
                        {
                            int num3 = (int)array2[3];
                            if (num3 <= num2)
                            {
                                break;
                            }
                            if (GetMailboxFolderStatistics.PropertyExists(array2[6]))
                            {
                                long num4 = (long)array2[6];
                                if (num4 > 0L)
                                {
                                    num += num4;
                                }
                            }
                            else
                            {
                                flag2 = true;
                            }
                            if (GetMailboxFolderStatistics.PropertyExists(array2[4]))
                            {
                                mailboxFolderConfiguration.ItemsInFolderAndSubfolders += (int)array2[4];
                            }
                            else
                            {
                                flag = true;
                            }
                            if (GetMailboxFolderStatistics.PropertyExists(array2[5]))
                            {
                                mailboxFolderConfiguration.ItemsInFolderAndSubfolders += (int)array2[5];
                            }
                            else
                            {
                                flag = true;
                            }
                            if (GetMailboxFolderStatistics.PropertyExists(array2[11]))
                            {
                                mailboxFolderConfiguration.DeletedItemsInFolderAndSubfolders += (int)array2[11];
                            }
                            if (GetMailboxFolderStatistics.PropertyExists(array2[12]))
                            {
                                mailboxFolderConfiguration.DeletedItemsInFolderAndSubfolders += (int)array2[12];
                            }
                        }
                    }
                    if (flag)
                    {
                        this.WriteWarning(Strings.TotalFolderCount(mailboxFolderConfiguration.FolderPath));
                    }
                    if (flag2)
                    {
                        this.WriteWarning(Strings.TotalFolderSize(mailboxFolderConfiguration.FolderPath));
                    }
                }
                mailboxFolderConfiguration.FolderAndSubfolderSize = ByteQuantifiedSize.FromBytes(checked ((ulong)num));
                if (GetMailboxFolderStatistics.PropertyExists(array[7]))
                {
                    bool flag3 = ((int)array[7] & 1) != 0;
                    mailboxFolderConfiguration.FolderType = (flag3 ? ElcFolderType.ManagedCustomFolder.ToString() : null);
                }
                if (mailboxFolderConfiguration.FolderType == null)
                {
                    VersionedId       folderId          = (VersionedId)array[1];
                    DefaultFolderType defaultFolderType = mailboxSession.IsDefaultFolderType(folderId);
                    if (defaultFolderType == DefaultFolderType.None)
                    {
                        if (GetMailboxFolderStatistics.PropertyExists(array[9]))
                        {
                            StoreObjectId folderId2 = (StoreObjectId)array[9];
                            defaultFolderType = mailboxSession.IsDefaultFolderType(folderId2);
                        }
                        switch (defaultFolderType)
                        {
                        case DefaultFolderType.AdminAuditLogs:
                        case DefaultFolderType.Audits:
                            mailboxFolderConfiguration.FolderType = defaultFolderType.ToString();
                            break;

                        default:
                            mailboxFolderConfiguration.FolderType = Strings.UserCreatedFolder;
                            break;
                        }
                    }
                    else
                    {
                        mailboxFolderConfiguration.FolderType = defaultFolderType.ToString();
                    }
                }
                if (GetMailboxFolderStatistics.PropertyExists(array[8]))
                {
                    Guid             guid          = new Guid((string)array[8]);
                    ADObjectId       valueToSearch = new ADObjectId(guid.ToByteArray());
                    List <ELCFolder> list          = ELCTaskHelper.FindELCFolder(adConfigSession, valueToSearch, FindByType.FolderAdObjectId);
                    if (list == null || list.Count != 1)
                    {
                        this.WriteWarning(Strings.UnableToRetrieveManagedFolder);
                    }
                    else
                    {
                        mailboxFolderConfiguration.ManagedFolder = new ELCFolderIdParameter(list[0].Id);
                    }
                }
                IConfigurationSession configurationSession = null;
                if (GetMailboxFolderStatistics.PropertyExists(array[13]))
                {
                    byte[] array3 = (byte[])array[13];
                    if (array3 != null)
                    {
                        Guid tagGuid = new Guid(array3);
                        configurationSession = this.CreateConfigurationSession(mailboxSession);
                        List <RetentionPolicyTag> list2 = ELCTaskHelper.FindRetentionPolicyTag(configurationSession, tagGuid);
                        if (list2 == null || list2.Count != 1)
                        {
                            this.WriteWarning(Strings.UnableToRetrieveDeletePolicyTag);
                        }
                        else
                        {
                            mailboxFolderConfiguration.DeletePolicy = new RetentionPolicyTagIdParameter(list2[0].Id);
                        }
                    }
                }
                if (GetMailboxFolderStatistics.PropertyExists(array[14]))
                {
                    byte[] array4 = (byte[])array[14];
                    if (array4 != null)
                    {
                        Guid tagGuid2 = new Guid(array4);
                        if (configurationSession == null)
                        {
                            configurationSession = this.CreateConfigurationSession(mailboxSession);
                        }
                        List <RetentionPolicyTag> list3 = ELCTaskHelper.FindRetentionPolicyTag(configurationSession, tagGuid2);
                        if (list3 == null || list3.Count != 1)
                        {
                            this.WriteWarning(Strings.UnableToRetrieveArchivePolicyTag);
                        }
                        else
                        {
                            mailboxFolderConfiguration.ArchivePolicy = new RetentionPolicyTagIdParameter(list3[0].Id);
                        }
                    }
                }
            }
            return(mailboxFolderConfiguration);
        }