public void Process(NCrawler.Crawler crawler, NCrawler.PropertyBag propertyBag) { String uri = propertyBag.Step.Uri.ToString(); try { ImportedItem item = new ImportedItem(); item.ImportHash = this.siteHash; item.Guid = System.Guid.NewGuid().ToString(); item.SubscriptionId = this.siteGuid; item.ContentType = propertyBag.ContentType; item.ContentEncoding = propertyBag.ContentEncoding; item.Expires = UtcDateTime.Now.AddHours(6); item.Inserted = UtcDateTime.Now; item.Title = propertyBag.Title; item.Uri = propertyBag.Step.Uri.ToString(); ImportedItemDao dao = new ImportedItemDao(); using (Transaction tx = new Transaction()) { dao.Save<ImportedItem>(item); tx.Commit(); } } catch (Exception e) { Logging.Database.Write("import-error", "Unexpected exception importing url:" + uri + ", stack:" + e.StackTrace); } }
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 IDictionary<ImportType, IList<ImportedItem>> GetImportedItems(Data.Hash hash) { ImportedItemDao dao = new ImportedItemDao(); IList<ImportedItem> items = dao.FindbyHash(hash); IDictionary<ImportType,IList<ImportedItem>> results = new Dictionary<ImportType,IList<ImportedItem>>(); foreach (ImportedItem item in items) { ImportType type = GetImportItemType(item); if (!results.ContainsKey(type)) results[type] = new List<ImportedItem>(); results[type].Add(item); } //Make sure that each item type is accounted for foreach (String name in Enum.GetNames(typeof(ImportType))) { ImportType type = (ImportType)Enum.Parse(typeof(ImportType), name); if (!results.ContainsKey(type)) results[type] = new List<ImportedItem>(); } return results; }
public void AddToImportQueue(Data.Hash importHash, Data.Guid subscriptionId, String emailTo, Boolean replacePhoneNumber, IList<Data.Guid> removed, Boolean deleteExisting) { //First thing is to remove any pages which aren't being imported ImportedItemDao dao = new ImportedItemDao(); dao.RemoveImportedItems(removed); //Add the import hash to the queue for processing later ImportSiteMessage message = new ImportSiteMessage(); message.ImportHash = importHash.Value; message.SubscriptionId = subscriptionId.Value; message.DeleteExisting = deleteExisting; message.ReplacePhoneNumbers = replacePhoneNumber; message.CompletionEmail = emailTo; QueueManager queue = new QueueManager(QueueNames.ImportSiteQueue); queue.Put<ImportSiteMessage>(message); }
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)); }