private void BindCRMStats(OrganizationStatistics orgStats)
		{
            lnkCRMUsers.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "crmusers",
                "SpaceID=" + PanelSecurity.PackageId);

		    crmUsersStats.QuotaUsedValue = orgStats.CreatedCRMUsers;
		    crmUsersStats.QuotaValue = orgStats.AllocatedCRMUsers;
		}
        private void BindEnterpriseStorageStats(OrganizationStatistics stats, OrganizationStatistics tenantStats)
        {
            enterpriseStorageSpaceStats.QuotaValue = stats.AllocatedEnterpriseStorageSpace;
            enterpriseStorageSpaceStats.QuotaUsedValue = stats.UsedEnterpriseStorageSpace;
            if (stats.AllocatedEnterpriseStorageSpace != -1) enterpriseStorageSpaceStats.QuotaAvailable = tenantStats.AllocatedEnterpriseStorageSpace - tenantStats.UsedEnterpriseStorageSpace;

            lnkEnterpriseStorageSpace.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "enterprisestorage_folders",
            "SpaceID=" + PanelSecurity.PackageId.ToString());

            enterpriseStorageFoldersStats.QuotaValue = stats.AllocatedEnterpriseStorageFolders;
            enterpriseStorageFoldersStats.QuotaUsedValue = stats.CreatedEnterpriseStorageFolders;
            if (stats.AllocatedEnterpriseStorageFolders != -1) enterpriseStorageFoldersStats.QuotaAvailable = tenantStats.AllocatedEnterpriseStorageFolders - tenantStats.CreatedEnterpriseStorageFolders;

            lnkEnterpriseStorageFolders.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "enterprisestorage_folders",
            "SpaceID=" + PanelSecurity.PackageId.ToString());
        }
        private void BindRemoteDesktopStats(OrganizationStatistics stats, OrganizationStatistics tenantStats)
        {
            rdsServers.QuotaValue = stats.AllocatedRdsServers;
            rdsServers.QuotaUsedValue = stats.CreatedRdsServers;
            if (stats.AllocatedRdsServers != -1)
            {
                rdsServers.QuotaAvailable = tenantStats.AllocatedRdsServers - tenantStats.CreatedRdsServers;
            }

            rdsCollections.QuotaValue = stats.AllocatedRdsCollections;
            rdsCollections.QuotaUsedValue = stats.CreatedRdsCollections;

            if (stats.AllocatedRdsCollections != -1)
            {
                rdsCollections.QuotaAvailable = tenantStats.AllocatedRdsCollections - tenantStats.CreatedRdsCollections;
            }                       

            rdsUsers.QuotaValue = stats.AllocatedRdsUsers;
            rdsUsers.QuotaUsedValue = stats.CreatedRdsUsers;

            if (stats.AllocatedRdsCollections != -1)
            {
                rdsUsers.QuotaAvailable = tenantStats.AllocatedRdsUsers - tenantStats.CreatedRdsUsers;
            }

            lnkRdsServers.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "rds_collections", "SpaceID=" + PanelSecurity.PackageId);
            lnkRdsCollections.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "rds_collections", "SpaceID=" + PanelSecurity.PackageId);
            lnkRdsUsers.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "rds_collections", "SpaceID=" + PanelSecurity.PackageId);
        }
        private void BindLyncStats(OrganizationStatistics stats, OrganizationStatistics tenantStats)
        {
            lyncUsersStats.QuotaValue = stats.AllocatedLyncUsers;
            lyncUsersStats.QuotaUsedValue = stats.CreatedLyncUsers;
            if (stats.AllocatedLyncUsers != -1) lyncUsersStats.QuotaAvailable = tenantStats.AllocatedLyncUsers - tenantStats.CreatedLyncUsers;

            lnkLyncUsers.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "lync_users",
            "SpaceID=" + PanelSecurity.PackageId.ToString());
        }
        private void BindBESStats(OrganizationStatistics stats, OrganizationStatistics tenantStats)
        {
            besUsersStats.QuotaValue = stats.AllocatedBlackBerryUsers;
            besUsersStats.QuotaUsedValue = stats.CreatedBlackBerryUsers;
            if (stats.AllocatedBlackBerryUsers != -1) besUsersStats.QuotaAvailable = tenantStats.AllocatedBlackBerryUsers - tenantStats.CreatedBlackBerryUsers;

            lnkBESUsers.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "blackberry_users",
            "SpaceID=" + PanelSecurity.PackageId.ToString());
        }
        private void BindCRM2013Stats(OrganizationStatistics stats, OrganizationStatistics tenantStats)
        {
            lnkProfessionalCRMUsers.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "crmusers",
                "SpaceID=" + PanelSecurity.PackageId);

            lnkBasicCRMUsers.NavigateUrl = lnkCRMUsers.NavigateUrl;
            lnkEssentialCRMUsers.NavigateUrl = lnkCRMUsers.NavigateUrl;

            lnkCRM2013DBSize.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "crm_storage_settings",
                "SpaceID=" + PanelSecurity.PackageId);

            crmProfessionalUsersStats.QuotaUsedValue = stats.CreatedProfessionalCRMUsers;
            crmProfessionalUsersStats.QuotaValue = stats.AllocatedProfessionalCRMUsers;

            crmBasicUsersStats.QuotaUsedValue = stats.CreatedBasicCRMUsers;
            crmBasicUsersStats.QuotaValue = stats.AllocatedBasicCRMUsers;

            crmEssentialUsersStats.QuotaUsedValue = stats.CreatedEssentialCRMUsers;
            crmEssentialUsersStats.QuotaValue = stats.AllocatedEssentialCRMUsers;

            crm2013DBSize.QuotaUsedValue = Convert.ToInt32(stats.UsedCRMDiskSpace > 0 ? stats.UsedCRMDiskSpace / (1024 * 1024) : -1);
            crm2013DBSize.QuotaValue = Convert.ToInt32(stats.AllocatedCRMDiskSpace > 0 ? stats.AllocatedCRMDiskSpace / (1024 * 1024) : -1);
        }
        private void BindCRMStats(OrganizationStatistics stats, OrganizationStatistics tenantStats)
        {
            lnkCRMUsers.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "crmusers",
                "SpaceID=" + PanelSecurity.PackageId);

            lnkLimitedCRMUsers.NavigateUrl = lnkCRMUsers.NavigateUrl;
            lnkESSCRMUsers.NavigateUrl = lnkCRMUsers.NavigateUrl;

            lnkCRMDBSize.NavigateUrl = EditUrl("ItemID", PanelRequest.ItemID.ToString(), "crm_storage_settings",
                "SpaceID=" + PanelSecurity.PackageId);

            crmUsersStats.QuotaUsedValue = stats.CreatedCRMUsers;
            crmUsersStats.QuotaValue = stats.AllocatedCRMUsers;

            //if (stats.AllocatedCRMUsers != -1) crmUsersStats.QuotaAvailable = tenantStats.AllocatedCRMUsers - tenantStats.CreatedCRMUsers;

            crmLimitedUsersStats.QuotaUsedValue = stats.CreatedLimitedCRMUsers;
            crmLimitedUsersStats.QuotaValue = stats.AllocatedLimitedCRMUsers;

            crmESSUsersStats.QuotaUsedValue = stats.CreatedESSCRMUsers;
            crmESSUsersStats.QuotaValue = stats.AllocatedESSCRMUsers;

            crmDBSize.QuotaUsedValue = Convert.ToInt32(stats.UsedCRMDiskSpace > 0 ? stats.UsedCRMDiskSpace / (1024 * 1024) : -1);
            crmDBSize.QuotaValue = Convert.ToInt32(stats.AllocatedCRMDiskSpace>0 ? stats.AllocatedCRMDiskSpace/(1024*1024) : -1);
        }
        public static OrganizationStatistics GetOrganizationStatistics(int itemId)
        {
            #region Demo Mode
            if (IsDemoMode)
            {
                OrganizationStatistics stats = new OrganizationStatistics();
                stats.AllocatedMailboxes = 4;
                stats.CreatedMailboxes = 3;
                stats.AllocatedContacts = 4;
                stats.CreatedContacts = 2;
                stats.AllocatedDistributionLists = 5;
                stats.CreatedDistributionLists = 1;
                stats.AllocatedPublicFolders = 40;
                stats.CreatedPublicFolders = 4;
                stats.AllocatedDomains = 5;
                stats.CreatedDomains = 2;
                stats.AllocatedDiskSpace = 200;
                stats.UsedDiskSpace = 70;
                stats.CreatedUsers = 5;
                stats.AllocatedUsers = 10;
                stats.CreatedSharePointSiteCollections = 1;
                stats.AllocatedSharePointSiteCollections = 5;
                return stats;
            }
            #endregion
            
            // place log record
            TaskManager.StartTask("ORGANIZATION", "GET_ORG_STATS");
            TaskManager.ItemId = itemId;

            try
            {
                Organization org = (Organization)PackageController.GetPackageItem(itemId);
                if (org == null)
                    return null;

                OrganizationStatistics stats = ObjectUtils.FillObjectFromDataReader<OrganizationStatistics>(
                    DataProvider.GetOrganizationStatistics(itemId));


                // disk space               
                // allocated quotas                               
                PackageContext cntx = PackageController.GetPackageContext(org.PackageId);                
                stats.AllocatedUsers = cntx.Quotas[Quotas.ORGANIZATION_USERS].QuotaAllocatedValue;
                stats.AllocatedDomains = cntx.Quotas[Quotas.ORGANIZATION_DOMAINS].QuotaAllocatedValue;
				
				if (cntx.Groups.ContainsKey(ResourceGroups.HostedSharePoint))
				{
					SharePointSiteCollectionListPaged sharePointStats = HostedSharePointServerController.GetSiteCollectionsPaged(org.PackageId, org.Id, string.Empty, string.Empty, string.Empty, 0, 0);
					stats.CreatedSharePointSiteCollections = sharePointStats.TotalRowCount;
					stats.AllocatedSharePointSiteCollections = cntx.Quotas[Quotas.HOSTED_SHAREPOINT_SITES].QuotaAllocatedValue;
				}


                if (cntx.Groups.ContainsKey(ResourceGroups.HostedCRM))
                {                                        
                    stats.CreatedCRMUsers = CRMController.GetCRMUsersCount(org.Id, string.Empty, string.Empty).Value;
                    stats.AllocatedCRMUsers = cntx.Quotas[Quotas.CRM_USERS].QuotaAllocatedValue;
                }

                if (cntx.Groups.ContainsKey(ResourceGroups.BlackBerry))
                {
                    stats.CreatedBlackBerryUsers = BlackBerryController.GetBlackBerryUsersCount(org.Id, string.Empty, string.Empty).Value;
                    stats.AllocatedBlackBerryUsers = cntx.Quotas[Quotas.BLACKBERRY_USERS].QuotaAllocatedValue;
                }

                if (cntx.Groups.ContainsKey(ResourceGroups.OCS))
                {
                    stats.CreatedOCSUsers = OCSController.GetOCSUsersCount(org.Id, string.Empty, string.Empty).Value;
                    stats.AllocatedOCSUsers = cntx.Quotas[Quotas.OCS_USERS].QuotaAllocatedValue;
                }


                return stats;
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
		public static OrganizationStatistics GetOrganizationStatistics(int itemId)
		{
			#region Demo Mode
			if (IsDemoMode)
			{
				OrganizationStatistics stats = new OrganizationStatistics();
				stats.AllocatedMailboxes = 10;
				stats.CreatedMailboxes = 4;
				stats.AllocatedContacts = 4;
				stats.CreatedContacts = 2;
				stats.AllocatedDistributionLists = 5;
				stats.CreatedDistributionLists = 1;
				stats.AllocatedPublicFolders = 40;
				stats.CreatedPublicFolders = 4;
				stats.AllocatedDomains = 5;
				stats.CreatedDomains = 2;
				stats.AllocatedDiskSpace = 200;
				stats.UsedDiskSpace = 70;
				return stats;
			}
			#endregion

			// place log record
			TaskManager.StartTask("EXCHANGE", "GET_ORG_STATS");
			TaskManager.ItemId = itemId;

			try
			{
				Organization org = (Organization)PackageController.GetPackageItem(itemId);
				if (org == null)
					return null;

				OrganizationStatistics stats = ObjectUtils.FillObjectFromDataReader<OrganizationStatistics>(
					DataProvider.GetExchangeOrganizationStatistics(itemId));
				
				// disk space
				stats.UsedDiskSpace = org.DiskSpace;

				// allocated quotas
				PackageContext cntx = PackageController.GetPackageContext(org.PackageId);
				stats.AllocatedMailboxes = cntx.Quotas[Quotas.EXCHANGE2007_MAILBOXES].QuotaAllocatedValue;
				stats.AllocatedContacts = cntx.Quotas[Quotas.EXCHANGE2007_CONTACTS].QuotaAllocatedValue;
				stats.AllocatedDistributionLists = cntx.Quotas[Quotas.EXCHANGE2007_DISTRIBUTIONLISTS].QuotaAllocatedValue;
				stats.AllocatedPublicFolders = cntx.Quotas[Quotas.EXCHANGE2007_PUBLICFOLDERS].QuotaAllocatedValue;				
				stats.AllocatedDiskSpace = cntx.Quotas[Quotas.EXCHANGE2007_DISKSPACE].QuotaAllocatedValue;

				return stats;
			}
			catch (Exception ex)
			{
				throw TaskManager.WriteError(ex);
			}
			finally
			{
				TaskManager.CompleteTask();
			}
		}
        private static OrganizationStatistics GetOrganizationStatisticsInternal(int itemId, bool byOrganization)
        {
            #region Demo Mode
            if (IsDemoMode)
            {
                OrganizationStatistics stats = new OrganizationStatistics();
                stats.AllocatedMailboxes = 10;
                stats.CreatedMailboxes = 4;
                stats.AllocatedContacts = 4;
                stats.CreatedContacts = 2;
                stats.AllocatedDistributionLists = 5;
                stats.CreatedDistributionLists = 1;
                stats.AllocatedPublicFolders = 40;
                stats.CreatedPublicFolders = 4;
                stats.AllocatedDomains = 5;
                stats.CreatedDomains = 2;
                stats.AllocatedDiskSpace = 200;
                stats.UsedDiskSpace = 70;
                return stats;
            }
            #endregion

            // place log record
            TaskManager.StartTask("EXCHANGE", "GET_ORG_STATS", itemId);

            try
            {
                Organization org = GetOrganization(itemId);
                if (org == null)
                    return null;

                // Log Extension
                LogExtension.WriteVariables(new {byOrganization});

                OrganizationStatistics stats = new OrganizationStatistics();

                if (byOrganization)
                {
                    OrganizationStatistics tempStats = ObjectUtils.FillObjectFromDataReader<OrganizationStatistics>(DataProvider.GetExchangeOrganizationStatistics(org.Id));

                    stats.CreatedMailboxes = tempStats.CreatedMailboxes;
                    stats.CreatedContacts = tempStats.CreatedContacts;
                    stats.CreatedDistributionLists = tempStats.CreatedDistributionLists;
                    stats.CreatedDomains = tempStats.CreatedDomains;
                    stats.CreatedPublicFolders = tempStats.CreatedPublicFolders;
                    stats.UsedDiskSpace = tempStats.UsedDiskSpace;
                    stats.UsedLitigationHoldSpace = tempStats.UsedLitigationHoldSpace;
                    stats.UsedArchingStorage = tempStats.UsedArchingStorage;

                    stats.CreatedSharedMailboxes = tempStats.CreatedSharedMailboxes;
                    stats.CreatedResourceMailboxes = tempStats.CreatedResourceMailboxes;
                }
                else
                {
                    UserInfo user = ObjectUtils.FillObjectFromDataReader<UserInfo>(DataProvider.GetUserByExchangeOrganizationIdInternally(org.Id));
                    List<PackageInfo> Packages = PackageController.GetPackages(user.UserId);

                    if ((Packages != null) & (Packages.Count > 0))
                    {
                        foreach (PackageInfo Package in Packages)
                        {
                            List<Organization> orgs = null;

                            orgs = GetExchangeOrganizations(Package.PackageId, false);

                            if ((orgs != null) & (orgs.Count > 0))
                            {
                                foreach (Organization o in orgs)
                                {
                                    // Log Extension
                                    LogExtension.WriteVariable("Nested Org", o.Name);
                                   
                                    OrganizationStatistics tempStats = ObjectUtils.FillObjectFromDataReader<OrganizationStatistics>(DataProvider.GetExchangeOrganizationStatistics(o.Id));

                                    stats.CreatedMailboxes += tempStats.CreatedMailboxes;
                                    stats.CreatedContacts += tempStats.CreatedContacts;
                                    stats.CreatedDistributionLists += tempStats.CreatedDistributionLists;
                                    stats.CreatedDomains += tempStats.CreatedDomains;
                                    stats.CreatedPublicFolders += tempStats.CreatedPublicFolders;
                                    stats.UsedDiskSpace += tempStats.UsedDiskSpace;
                                    stats.UsedLitigationHoldSpace += tempStats.UsedLitigationHoldSpace;
                                    stats.UsedArchingStorage += tempStats.UsedArchingStorage;

                                    stats.CreatedSharedMailboxes += tempStats.CreatedSharedMailboxes;
                                    stats.CreatedResourceMailboxes += tempStats.CreatedResourceMailboxes;
                                }
                            }
                        }
                    }
                }

                // disk space
                //stats.UsedDiskSpace = org.DiskSpace;


                // allocated quotas
                PackageContext cntx = PackageController.GetPackageContext(org.PackageId);
                stats.AllocatedMailboxes = cntx.Quotas[Quotas.EXCHANGE2007_MAILBOXES].QuotaAllocatedValue;
                stats.AllocatedContacts = cntx.Quotas[Quotas.EXCHANGE2007_CONTACTS].QuotaAllocatedValue;
                stats.AllocatedDistributionLists = cntx.Quotas[Quotas.EXCHANGE2007_DISTRIBUTIONLISTS].QuotaAllocatedValue;
                stats.AllocatedPublicFolders = cntx.Quotas[Quotas.EXCHANGE2007_PUBLICFOLDERS].QuotaAllocatedValue;
                stats.AllocatedDiskSpace = cntx.Quotas[Quotas.EXCHANGE2007_DISKSPACE].QuotaAllocatedValue;
                stats.AllocatedLitigationHoldSpace = cntx.Quotas[Quotas.EXCHANGE2007_RECOVERABLEITEMSSPACE].QuotaAllocatedValue;
                stats.AllocatedArchingStorage = cntx.Quotas[Quotas.EXCHANGE2013_ARCHIVINGSTORAGE].QuotaAllocatedValue;

                stats.AllocatedSharedMailboxes = cntx.Quotas[Quotas.EXCHANGE2013_SHAREDMAILBOXES].QuotaAllocatedValue;
                stats.AllocatedResourceMailboxes = cntx.Quotas[Quotas.EXCHANGE2013_RESOURCEMAILBOXES].QuotaAllocatedValue;

                return stats;
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }