private void PackageThemes(Data.Guid siteGuid, IList<SitePackageTheme> packageThemes, IPackageStatusNotifier notifier)
        {
            IList<CmsTheme> themes = ThemeManager.Instance.GetAllBySite(siteGuid);
            foreach (CmsTheme theme in themes)
            {

                SitePackageTheme packageTheme = new SitePackageTheme();

                IList<JavascriptFile> jsFiles = JavascriptManager.Instance.List(theme);
                foreach (JavascriptFile file in jsFiles)
                {
                    JavascriptFile temp = JavascriptManager.Instance.Get(theme, file.FullName);
                    file.Content = temp.Content;
                }

                IList<CssFile> cssFiles = CssManager.Instance.List(theme);
                foreach (CssFile file in cssFiles)
                {
                    CssFile temp = CssManager.Instance.Get(theme, file.FullName);
                    file.Content = temp.Content;
                }

                //Create a snapshot of the images
                IStorageClient client = StorageHelper.GetStorageClient();
                String imageDirectory = SiteHelper.GetStorageKey(SiteHelper.ImagesContainerKey, siteGuid.Value);

                DoNotify(notifier, "Creating Theme Image Snapshots (please wait...)");
                packageTheme.Header = theme.Header;
                packageTheme.Footer = theme.Footer;
                packageTheme.Templates = TemplateManager.Instance.GetTemplates(theme);
                packageTheme.Images = client.CreateSnapshot(imageDirectory, theme.ThemeGuid);
                packageTheme.Javascript = jsFiles;
                packageTheme.Css = cssFiles;
                packageTheme.Theme = theme;

                packageThemes.Add(packageTheme);
            }
        }
        private void SaveImagesToDatabase(IPackageStatusNotifier notifier, Data.Guid siteGuid, String container, String directory)
        {
            IStorageClient client = StorageHelper.GetStorageClient();

            DoNotify(notifier, "Saving the image metadata to the database", true);
            IList<StorageFile> images = client.List(container, directory);

            ImageManager.SaveImagesToDatabase(siteGuid, client, container, directory, images, null);
        }
 private void DoNotify(IPackageStatusNotifier notifier, String eventName, bool doNotStep)
 {
     if (notifier != null)
     {
         if (doNotStep)
             notifier.OnNotify(this.guid, eventName, currentStepCount, maxSteps);
         else
             DoNotify(notifier, eventName);
     }
 }
 private void DoNotify(IPackageStatusNotifier notifier, String eventName)
 {
     if (notifier != null)
         notifier.OnNotify(this.guid, eventName, currentStepCount++,maxSteps);
 }
        private void DeployThemes(SitePackage sitepackage, Data.Guid guid, IPackageStatusNotifier notifier)
        {
            //Deploy the themes into the site
            foreach (SitePackageTheme themeWrapper in sitepackage.Themes)
            {
                CmsTheme theme = themeWrapper.Theme;

                Boolean isEnabled = theme.IsEnabled;
                theme = ThemeManager.Instance.Add(guid, theme.Name, theme.Description);

                theme.IsEnabled = isEnabled;
                theme.Header = themeWrapper.Header;
                theme.Footer = themeWrapper.Footer;
                ThemeManager.Instance.Save(theme);

                //Save all of the templates for this theme
                foreach (CmsTemplate template in themeWrapper.Templates)
                {
                    CmsTemplate newTemplate = new CmsTemplate();
                    newTemplate.IsGlobalTemplateType = template.IsGlobalTemplateType;
                    newTemplate.Name = template.Name;
                    newTemplate.SubscriptionGuid = guid.Value;
                    newTemplate.Theme = theme;
                    newTemplate.Content = template.Content;
                    newTemplate.LastSaved = template.LastSaved;

                    TemplateManager.Instance.Save(newTemplate);
                }

                //Save the javascript files for this theme
                foreach (JavascriptFile js in themeWrapper.Javascript)
                {
                    JavascriptManager.Instance.Save(guid, theme, js);
                }

                //Save the css files for this theme
                foreach (CssFile css in themeWrapper.Css)
                {
                    CssManager.Instance.Save(guid, theme, css);
                }

                //Save all the images for this theme
                DoNotify(notifier, "Copying Theme Images to Site (please wait...) ");
                String copyFromImageContainer = SiteHelper.GetStorageKey(SiteHelper.ImagesContainerKey, sitepackage.OriginalSiteGuid.Value);
                String copyToImageContainer = SiteHelper.GetStorageKey(SiteHelper.ImagesContainerKey, guid.Value);

                IStorageClient client = StorageHelper.GetStorageClient();
                client.CopyFromSnapshots(themeWrapper.Images, copyFromImageContainer, copyToImageContainer, theme.ThemeGuid, Permissions.Public);

                SaveImagesToDatabase(notifier, guid, copyToImageContainer, theme.ThemeGuid);
            }
        }
        private void DeployPages(SitePackage sitepackage, Data.Guid guid, IPackageStatusNotifier notifier)
        {
            //Deploy the sitemap
            foreach (CmsSitePath path in sitepackage.SiteMapPaths)
            {
                path.Id = 0;
                path.SubscriptionGuid = guid.Value;

                CmsSiteMap.Instance.Save(path);
            }

            //Deploy the pages into the site
            foreach (SitePackagePage pageWrapper in sitepackage.Pages)
            {
                CmsPage page = pageWrapper.Page;

                page.Id = 0;
                page.SubscriptionId = guid.Value;
                page.IsApproved = false;
                page.Guid = System.Guid.NewGuid().ToString();
                PageManager.Instance.Save(page);

                //Save the javascript files for this theme
                foreach (JavascriptFile js in pageWrapper.Javascript)
                {
                    JavascriptManager.Instance.Save(guid, page, js);
                }

                //Save the css files for this theme
                foreach (CssFile css in pageWrapper.Css)
                {
                    CssManager.Instance.Save(guid, page, css);
                }
            }

            DoNotify(notifier, "Copying Page Images to Site (please wait...) ");

            //Save all the page-level images for this site
            String copyFromImageContainer = SiteHelper.GetStorageKey(SiteHelper.ImagesContainerKey, sitepackage.OriginalSiteGuid.Value);
            String copyToImageContainer = SiteHelper.GetStorageKey(SiteHelper.ImagesContainerKey, guid.Value);

            IStorageClient client = StorageHelper.GetStorageClient();
            client.CopyFromSnapshots(sitepackage.PageImages, copyFromImageContainer, copyToImageContainer, StorageClientConst.RootFolder, Permissions.Public);

            //Save the page images to the database
            SaveImagesToDatabase(notifier, guid, copyToImageContainer, StorageClientConst.RootFolder);
        }
        public void DeployToSubscription(Data.Guid userGuid, Data.Guid siteGuid, Data.Guid packageGuid, IPackageStatusNotifier notifier)
        {
            maxSteps = DeployUserSiteSteps;

            if (!IsPackageValidForUser(userGuid, packageGuid))
                throw new ArgumentException("The specified package is not valid for the currently logged in user and may not be applied.");

            Package package = GetPackage(packageGuid);

            DoNotify(notifier, "Reading Package From Archive");
            //Deploy the package into the demo site
            IStorageClient client = StorageHelper.GetStorageClient();
            byte[] zipped = client.Open(PackageContainer, PackageDirectory, package.Guid + PackageExtension);

            Compression.ZipHandler zip = new Compression.ZipHandler(zipped);
            byte[] serialized = zip.Decompress()[0].Data;

            SitePackage sitepackage = Serializer.ToObject<SitePackage>(serialized);
            Data.Guid guid = siteGuid;

            if (package.PackageType == PackageTypes.Site)
            {
                DoNotify(notifier, "Erasing existing site data");
                SubscriptionManager.Erase(siteGuid);
            }

            DoNotify(notifier, "Deploying Package Themes To Site");
            DeployThemes(sitepackage, guid, notifier);
            if (package.PackageType == PackageTypes.Site)
            {
                DoNotify(notifier, "Deploying Package Pages To Site");
                DeployPages(sitepackage, guid, notifier);

                DoNotify(notifier, "Deploying Package Content Types To Site");
                DeployContentTypes(sitepackage, guid);

                DoNotify(notifier, "Deploying Package Content To Site");
                DeployContent(sitepackage, guid);
            }
        }
        public void DeployDemoPackage(Data.Guid packageGuid, IPackageStatusNotifier notifier)
        {
            DoNotify(notifier, "Deploying Demo Package");

            PackageDao dao = new PackageDao();
            Package package = dao.FindByPackageGuid(packageGuid);
            if (package != null)
            {
                //Get the owner's subscription for this package
                CmsSubscription owner = SubscriptionManager.GetSubscription(package.OwnerSubscriptionId);
                DoNotify(notifier, "Creating Demo Account");

                //Check if our demo account exists
                MembershipUserWrapper wrapper = MembershipUtil.FindByUsername(MembershipUtil.DemoAccountUsername);
                if (!wrapper.IsValid())
                    wrapper = MembershipUtil.CreateDemoAccount();

                //Find a free subdomain name
                String subdomain = "demo-" + owner.Subdomain;
                Boolean isAvailable = SubscriptionManager.IsSubdomainAvailable(subdomain);
                int i = 1;
                while (!isAvailable)
                {
                    i++;
                    subdomain = "demo" + i.ToString() + "-" + owner.Subdomain;
                    isAvailable = SubscriptionManager.IsSubdomainAvailable(subdomain);
                }

                //Create a new subscription for the demo account
                CmsSubscription subscription = new CmsSubscription();
                subscription.Guid = package.Guid;
                subscription.Created = UtcDateTime.Now;
                subscription.Subdomain = subdomain;
                subscription.StagingDomain = subscription.Subdomain + GooeyConfigManager.DefaultCmsDomain;
                subscription.SubscriptionPlan = SubscriptionManager.GetSubscriptionPlan(SubscriptionPlans.Demo);
                subscription.PrimaryUser= wrapper.UserInfo;
                subscription.IsDemo = true;
                subscription.IsCampaignEnabled = true;
                subscription.Expires = DateTime.MaxValue;
                subscription.Culture = GooeyConfigManager.DefaultCulture;
                SubscriptionManager.Create(wrapper, subscription);

                DoNotify(notifier, "Reading Package From Archive");
                //Deploy the package into the demo site
                IStorageClient client = StorageHelper.GetStorageClient();
                byte [] zipped = client.Open(PackageContainer, PackageDirectory, package.Guid + PackageExtension);

                Compression.ZipHandler zip = new Compression.ZipHandler(zipped);
                byte [] serialized = zip.Decompress()[0].Data;

                SitePackage sitepackage = Serializer.ToObject<SitePackage>(serialized);

                Data.Guid guid = Data.Guid.New(subscription.Guid);

                DoNotify(notifier, "Deploying Package Themes To Site");
                DeployThemes(sitepackage, guid, notifier);

                DoNotify(notifier, "Deploying Package Pages To Site");
                DeployPages(sitepackage, guid, notifier);

                DoNotify(notifier, "Deploying Package Content Types To Site");
                DeployContentTypes(sitepackage, guid);

                DoNotify(notifier, "Deploying Package Content To Site");
                DeployContent(sitepackage, guid);
            }
        }
        public Data.Guid CreatePackage(Package package, IPackageStatusNotifier notifier)
        {
            PackageDao dao = new PackageDao();

            this.guid = package.Guid;
            String siteGuid = package.OwnerSubscriptionId;

            SitePackage sitepackage = new SitePackage();
            IList<SitePackageTheme> packageThemes = new List<SitePackageTheme>();
            IList<SitePackagePage> packagePages = new List<SitePackagePage>();
            IList<SiteContentType> packageContentTypes = new List<SiteContentType>();
            IList<SiteContent> packageContent = new List<SiteContent>();
            IList<CmsSitePath> sitePaths = new List<CmsSitePath>();

            DoNotify(notifier, "Packaging Themes");
            PackageThemes(siteGuid, packageThemes, notifier);

            DoNotify(notifier, "Packaging Pages");
            PackagePages(siteGuid, packagePages);

            DoNotify(notifier, "Packaging Content Types");
            PackageContentTypes(siteGuid, packageContentTypes);

            DoNotify(notifier, "Packaging Content");
            PackageContent(siteGuid, packageContent);

            IStorageClient client = StorageHelper.GetStorageClient();
            String imageDirectory = SiteHelper.GetStorageKey(SiteHelper.ImagesContainerKey, siteGuid);

            DoNotify(notifier, "Creating Page Image Snapshots (please wait)");
            sitepackage.PageImages = client.CreateSnapshot(imageDirectory, StorageClientConst.RootFolder);
            sitepackage.Themes = packageThemes;
            sitepackage.SiteMapPaths = CmsSiteMap.Instance.GetAllPaths(siteGuid);
            sitepackage.Pages = packagePages;
            sitepackage.ContentTypes = packageContentTypes;
            sitepackage.SiteContent = packageContent;
            sitepackage.OriginalSiteGuid = siteGuid;

            DoNotify(notifier, "Creating Package Archive");
            byte[] data = null;
            using (MemoryStream outputstream = new MemoryStream())
            {
                //Serialize the object and then compress the serialized object
                data = Serializer.ToByteArray<SitePackage>(sitepackage);
                using (ZipFile zip = new ZipFile())
                {
                    zip.AddEntry("sitepackage.bin", data);
                    zip.Save(outputstream);
                }
                data = null;

                //Write the serialized data to the container
                data = outputstream.ToArray();
            }

            DoNotify(notifier, "Saving Package Archive");
            client = StorageHelper.GetStorageClient();
            client.Save(PackageContainer,PackageDirectory, package.Guid + PackageExtension, data, Permissions.Private);

            return package.Guid;
        }