public static StatsServer[] GetServers(int serviceId)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive
                | DemandAccount.IsAdmin);
            if (accountCheck < 0) return null;

            StatisticsServer stats = new StatisticsServer();
            ServiceProviderProxy.Init(stats, serviceId);
            return stats.GetServers();
        }
        public static StatsSite GetSite(int itemId)
        {
            // load meta item
            StatsSite item = (StatsSite)PackageController.GetPackageItem(itemId);
            if (item == null)
                return null;

            // load item from service
            StatisticsServer stats = new StatisticsServer();
            ServiceProviderProxy.Init(stats, item.ServiceId);
            StatsSite site = stats.GetSite(item.SiteId);

            if (site == null)
                return null;

            site.Id = item.Id;
            site.Name = item.Name;
            site.ServiceId = item.ServiceId;
            site.PackageId = item.PackageId;
            site.SiteId = item.SiteId;

            // update statistics URL
            if (!String.IsNullOrEmpty(site.StatisticsUrl))
            {
				// load space owner
				UserInfo user = PackageController.GetPackageOwner(item.PackageId);
				if (user != null)
				{
                    site.StatisticsUrl = Utils.ReplaceStringVariable(site.StatisticsUrl, "username",
                        HttpUtility.UrlEncode(user.Username));
					site.StatisticsUrl = Utils.ReplaceStringVariable(site.StatisticsUrl, "password",
                        HttpUtility.UrlEncode(user.Password));
				}
            }

            return site;
        }
 public static StatisticsServer GetStatisticsServer(int serviceId)
 {
     StatisticsServer stats = new StatisticsServer();
     ServiceProviderProxy.Init(stats, serviceId);
     return stats;
 }
        public void ImportItem(int packageId, int itemTypeId, Type itemType,
			ResourceGroupInfo group, string itemName)
        {
            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);
            if (serviceId == 0)
                return;

            StatisticsServer stats = new StatisticsServer();
            ServiceProviderProxy.Init(stats, serviceId);

            if (itemType == typeof(StatsSite))
            {
                // import statistics site
                StatsSite site = new StatsSite();
                site.ServiceId = serviceId;
                site.PackageId = packageId;
                site.Name = itemName;
                site.GroupName = group.GroupName;

                // load site id
                site.SiteId = stats.GetSiteId(itemName);

                PackageController.AddPackageItem(site);
            }
        }
        public List<string> GetImportableItems(int packageId, int itemTypeId, Type itemType, ResourceGroupInfo group)
        {
            List<string> items = new List<string>();

            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);
            if (serviceId == 0)
                return items;

            // Mail provider
            StatisticsServer stats = new StatisticsServer();
            ServiceProviderProxy.Init(stats, serviceId);

            if (itemType == typeof(StatsSite))
                items.AddRange(stats.GetSites());

            return items;
        }
        public static int DeleteSite(int itemId)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);
            if (accountCheck < 0) return accountCheck;


            // load original meta item
            StatsSite origItem = (StatsSite)PackageController.GetPackageItem(itemId);
            if (origItem == null)
                return BusinessErrorCodes.ERROR_STATS_PACKAGE_ITEM_NOT_FOUND;

            // place log record
            TaskManager.StartTask("STATS_SITE", "DELETE", origItem.Name);
            TaskManager.ItemId = origItem.Id;

            try
            {
                // get service
                StatisticsServer stats = new StatisticsServer();
                ServiceProviderProxy.Init(stats, origItem.ServiceId);

                // delete service item
                stats.DeleteSite(origItem.SiteId);

                // delete meta item
                PackageController.DeletePackageItem(origItem.Id);

                return 0;
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static int UpdateSite(StatsSite item)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
            if (accountCheck < 0) return accountCheck;

            // load original meta item
            StatsSite origItem = (StatsSite)PackageController.GetPackageItem(item.Id);
            if (origItem == null)
                return BusinessErrorCodes.ERROR_STATS_PACKAGE_ITEM_NOT_FOUND;

            // check package
            int packageCheck = SecurityContext.CheckPackage(origItem.PackageId, DemandPackage.IsActive);
            if (packageCheck < 0) return packageCheck;

            // update statistics site
            item.Name = origItem.Name;
            item.SiteId = origItem.SiteId;

            // place log record
            TaskManager.StartTask("STATS_SITE", "UPDATE", origItem.Name);
            TaskManager.ItemId = origItem.Id;

            try
            {
                StatisticsServer stats = new StatisticsServer();
                ServiceProviderProxy.Init(stats, origItem.ServiceId);
                stats.UpdateSite(item);

                // update service item
                PackageController.UpdatePackageItem(item);

                return 0;
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static int AddSite(StatsSite item)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
            if (accountCheck < 0) return accountCheck;

            // check package
            int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
            if (packageCheck < 0) return packageCheck;

            // check quota
            QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.STATS_SITES);
            if (quota.QuotaExhausted)
                return BusinessErrorCodes.ERROR_STATS_RESOURCE_QUOTA_LIMIT;

            // check if stats resource is available
            int serviceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.Statistics);
            if (serviceId == 0)
                return BusinessErrorCodes.ERROR_STATS_RESOURCE_UNAVAILABLE;

            // check package items
            if (PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(StatsSite)) != null)
                return BusinessErrorCodes.ERROR_STATS_PACKAGE_ITEM_EXISTS;

            // place log record
            TaskManager.StartTask("STATS_SITE", "ADD", item.Name);

            try
            {
                // load web site
                WebSite siteItem = (WebSite)PackageController.GetPackageItemByName(item.PackageId,
                    item.Name, typeof(WebSite));

                if (siteItem == null)
                    return BusinessErrorCodes.ERROR_WEB_SITE_SERVICE_UNAVAILABLE;

                // get service web site
                WebServer web = new WebServer();
                ServiceProviderProxy.Init(web, siteItem.ServiceId);
                WebSite site = web.GetSite(siteItem.SiteId);

                List<DomainInfo> pointers = WebServerController.GetWebSitePointers(siteItem.Id);
                List<string> aliases = new List<string>();

                foreach(DomainInfo pointer in pointers)
                    aliases.Add(pointer.DomainName);

                StatisticsServer stats = new StatisticsServer();
                ServiceProviderProxy.Init(stats, serviceId);
                string siteNumber = (site.IIs7) ? site[WebSite.IIS7_SITE_ID] : siteItem.SiteId.Replace("/", "");
                string logsCommonFolder = site.LogsPath;
                string logsFolder = Path.Combine(logsCommonFolder, siteNumber);

				// get service settings
				StringDictionary settings = ServerController.GetServiceSettings(serviceId);
				if (Utils.ParseBool(settings["BuildUncLogsPath"], false))
				{
					logsFolder = FilesController.ConvertToUncPath(siteItem.ServiceId, logsFolder);
				}

                item.LogDirectory = logsFolder;
                item.DomainAliases = aliases.ToArray();

                // install statistics
                item.SiteId = stats.AddSite(item);

                // save statistics item
                item.ServiceId = serviceId;
                int itemId = PackageController.AddPackageItem(item);

                TaskManager.ItemId = itemId;

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