Пример #1
0
        public List<String> Import(ImportSiteMessage message)
        {
            Data.Hash importHash = Data.Hash.New(message.ImportHash);
            Data.Guid subscriptionId = Data.Guid.New(message.SubscriptionId);
            Boolean deleteExisting = message.DeleteExisting;

            List<String> status = new List<String>();

            byte[] data;

            AddStatus(importHash, status, "Site import started at " + UtcDateTime.Now.ToString());
            CmsSubscription subscription = SubscriptionManager.GetSubscription(subscriptionId);
            CmsTheme defaultTheme = ThemeManager.Instance.GetDefaultBySite(subscriptionId);

            //Check if the import-template already exists
            CmsTemplate template = TemplateManager.Instance.GetTemplate(subscriptionId, "import-template");
            if (template == null)
            {
                template = new CmsTemplate();
                template.Content = "{content}";
                template.Name = "import-template";
                template.IsGlobalTemplateType = false;
                template.LastSaved = UtcDateTime.Now;
                template.SubscriptionGuid = subscriptionId.Value;
                template.Theme = defaultTheme;

                TemplateManager.Instance.Save(template);
                AddStatus(importHash, status, "Successfully created and associated import template to deafult theme");
            }

            IDictionary<ImportType, IList<ImportedItem>> items = this.GetImportedItems(importHash);

            //Check if we need to delete the existing site
            if (deleteExisting)
            {
                //Erase all of the existing data
                SubscriptionManager.Erase(subscription.Guid, false, false);
                SessionProvider.Instance.Close();
                SessionProvider.Instance.GetOpenSession();

                //Setup the default
                SiteHelper.Configure(subscription.Guid);
            }

            //First, import all of the images
            IList<ImportedItem> images = items[ImportType.Image];
            foreach (ImportedItem image in images)
            {
                CmsUrl uri = new CmsUrl(image.Uri);
                data = SimpleWebClient.GetResponse(uri.ToUri());

                ImageManager.Instance.AddImage(subscriptionId, StorageClientConst.RootFolder, uri.Path, image.ContentType, data);
                AddStatus(importHash, status, "Successfully imported image: " + uri.ToString() + " (" + image.ContentType + ")");
            }

            //Create the sitemap and then add the page itself
            CmsSitePath root = CmsSiteMap.Instance.GetPath(subscriptionId, CmsSiteMap.RootPath);

            Dictionary<CmsUrl, int> cssUses = new Dictionary<CmsUrl, int>();
            Dictionary<CmsUrl, int> jsUses = new Dictionary<CmsUrl, int>();

            IList<ImportedItem> pages = NormalizeImport(items[ImportType.Page]);
            foreach (ImportedItem page in pages)
            {
                try
                {
                    CmsUrl uri = new CmsUrl(page.Uri);
                    CmsUrlWalker walker = new CmsUrlWalker(uri);

                    while (walker.Next())
                    {
                        String parent = walker.GetParentPath();
                        String current = walker.GetIndividualPath();
                        String fullpath = CmsSiteMap.PathCombine(parent, current);
                        int depth = walker.Depth;

                        if (!walker.IsLast)
                        {
                            //Check if the current path exists, if not, create it
                            if (!CmsSiteMap.Instance.Exists(subscriptionId, fullpath))
                                CmsSiteMap.Instance.AddChildDirectory(subscriptionId, parent, current);
                        }
                    }

                    String pageName = walker.GetIndividualPath();
                    CmsPage newpage = GetPage(template.Name, subscription.Culture, pageName, message.ReplacePhoneNumbers, page, cssUses, jsUses);
                    newpage.SubscriptionId = subscriptionId.Value;

                    //Add the page to the cms system
                    PageManager.Instance.AddNewPage(walker.GetParentPath(), pageName, newpage);
                    AddStatus(importHash, status, "Successfully imported page " + page.Uri);
                }
                catch (Exception ex)
                {
                    if (!ex.Message.Contains("404"))
                        Logging.Database.Write("import-site-manager", "Failed to import page: " + page.Uri + ", cause:" + ex.Message + ", stack:" + ex.StackTrace);
                    AddStatus(importHash, status, "Failed to import page " + page.Uri + ", Reason:" + ex.Message);
                }
            }

            //Import the css
            IList<ImportedItem> css = items[ImportType.Css];

            int sortOrder = 0;
            CssManager cssManager = new CssManager(null);
            cssManager.SubscriptionId = subscriptionId;
            foreach (ImportedItem item in css)
            {
                CmsUrl uri = new CmsUrl(item.Uri);
                data = SimpleWebClient.GetResponse(uri.ToUri());

                cssManager.Save(defaultTheme.ThemeGuid, uri.Path, data, true, 0);
                cssManager.UpdateSortInfo(defaultTheme, uri.Path, sortOrder++);
                AddStatus(importHash, status, "Successfully imported css file: " + uri.ToString());
            }

            //Import the javascript
            IList<ImportedItem> js = items[ImportType.Javascript];

            sortOrder = 0;
            JavascriptManager jsManager = new JavascriptManager(null);
            jsManager.SubscriptionId = subscriptionId;
            foreach (ImportedItem item in js)
            {
                CmsUrl uri = new CmsUrl(item.Uri);
                data = SimpleWebClient.GetResponse(uri.ToUri());

                jsManager.Save(defaultTheme.ThemeGuid, uri.Path, data, true, 0);
                jsManager.UpdateSortInfo(defaultTheme, uri.Path, sortOrder++);
                AddStatus(importHash, status, "Successfully imported javascript file: " + uri.ToString());
            }

            //Import any documents
            IList<ImportedItem> documents = items[ImportType.Document];
            foreach (ImportedItem item in documents)
            {
                CmsUrl uri = new CmsUrl(item.Uri);

                ContentFileUploadImpl handler = new ContentFileUploadImpl();
                String filename = uri.Path;
                if (ContentFileUploadImpl.IsValidFileType(filename))
                {
                    data = SimpleWebClient.GetResponse(uri.ToUri());

                    handler.Save(subscriptionId, data, uri.Path, true);
                }
            }

            AddStatus(importHash, status, "Site import completed successfully at " + UtcDateTime.Now.ToString());

            ImportedItemDao dao = new ImportedItemDao();
            dao.DeleteAllByImportHash(importHash);

            return status;
        }
 public void PerformErrorCheck()
 {
     //Check if this site hash is already in the database, if so, remove it
     ImportedItemDao dao = new ImportedItemDao();
     dao.DeleteAllByImportHash(Data.Hash.New(this.siteHash));
 }