Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Xml;
- using System.Xml.XPath;
- using EPiServer.Core;
- using System.Configuration;
- using Vitrolife.ContentImport.Entities;
- using System.Text;
- using EPiServer.SpecializedProperties;
- namespace ClassName.ContentImport {
- /// <summary>
- /// Handling import of content into Episerver
- /// </summary>
- public class Manager {
- private const string SRC_TYPE_RSS = "rss";
- private HttpServerUtility m_httpServerUtil = null;
- private List<ContentSetting> m_listContentSettings = null;
- private List<Status> m_listStatusMsg = new List<Status>();
- public List<Status> StatusMsg {
- get {
- return m_listStatusMsg;
- }
- }
- /// <summary>
- /// It seams like both the RSS feeds has the same namespaces
- /// </summary>
- /// <param name="nt"></param>
- /// <returns></returns>
- private XmlNamespaceManager GetNamespaceManager(XmlNameTable nt) {
- XmlNamespaceManager nsMan = new XmlNamespaceManager(nt);
- nsMan.AddNamespace("atom", "http://www.w3.org/2005/Atom");
- nsMan.AddNamespace("obs", "http://www.cision.com/");
- nsMan.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");
- nsMan.AddNamespace("default", "http://purl.org/rss/1.0/"); // obs; man MÅSTE ge även default namespace ett namn för att XPath uttrycken skall fungera
- return nsMan;
- }
- public Manager() {
- }
- /// <summary>
- /// For now Im using this just to clean up the scrambled html tags <>
- /// Dont know if this is the way to do it
- /// Depends, maybe we want to remove all html if its ugly ?
- /// </summary>
- private HttpServerUtility ServerUtil {
- get {
- if (m_httpServerUtil == null) {
- m_httpServerUtil = HttpContext.Current.Server;
- }
- return m_httpServerUtil;
- }
- }
- /// <summary>
- /// Extracts the settings
- /// </summary>
- private List<ContentSetting> ContentSettings {
- get {
- if (m_listContentSettings == null) {
- XPathDocument xpDoc = new XPathDocument(System.Configuration.ConfigurationManager.AppSettings["ContentImportConfigFile"]);
- XPathNavigator xpNav = xpDoc.CreateNavigator();
- m_listContentSettings = new List<ContentSetting>();
- XPathNodeIterator xpNodeItItems = xpNav.Select("/config/import-items/item");
- while (xpNodeItItems.MoveNext()) {
- XPathNavigator xpNavItem = xpNodeItItems.Current;
- string strSrcUrl = xpNavItem.GetAttribute("src-url", string.Empty);
- string strSrcType = xpNavItem.GetAttribute("src-type", string.Empty);
- string strSrcDetailsUrl = xpNavItem.GetAttribute("src-details-url", string.Empty);
- string strEpiDestID = xpNavItem.GetAttribute("epi-dest-id", string.Empty);
- string strEpiDestLang = xpNavItem.GetAttribute("epi-dest-lang", string.Empty);
- int intEpiDestID = -1;
- if (int.TryParse(strEpiDestID, out intEpiDestID)) {
- m_listContentSettings.Add(new ContentSetting(strSrcUrl, strSrcType, strSrcDetailsUrl, intEpiDestID, strEpiDestLang));
- }
- }
- }
- return m_listContentSettings;
- }
- }
- //private string CreateFileContent(string strSrcUrl)
- //{
- // // obs:files alla children som har "MAIN"
- // // RelatedLinks är en LinkCollection
- // XPathDocument xpDoc;
- // try {
- // // Suppose this could go wrong
- // xpDoc = new XPathDocument(strSrcUrl);
- // }
- // catch {
- // return string.Empty; // Dont know if there is any point try to do anything with this case for now
- // }
- // XPathNavigator xpNav = xpDoc.CreateNavigator();
- // XmlNamespaceManager nsMan = GetNamespaceManager(xpNav.NameTable);
- // return string.Empty;
- //}
- private string ExtractStringValue(XPathNavigator xpNav) {
- if(xpNav != null) {
- return xpNav.Value;
- }
- return string.Empty;
- }
- //private string CreateMainContent(string strSrcUrl) {
- private void CreateMainContent(string strSrcUrl, ref string strContent, ref LinkItemCollection linkNewColl) {
- XPathDocument xpDoc;
- try {
- // Suppose this could go wrong
- xpDoc = new XPathDocument(strSrcUrl);
- }
- catch {
- //return string.Empty; // Dont know if there is any point try to do anything with this case for now
- return;
- }
- XPathNavigator xpNav = xpDoc.CreateNavigator();
- XmlNamespaceManager nsMan = GetNamespaceManager(xpNav.NameTable);
- string[] arrLines = xpNav.SelectSingleNode("/rss/channel/item/content:encoded", nsMan).InnerXml.Split(new string[] { "\n\r" }, StringSplitOptions.RemoveEmptyEntries);
- StringBuilder sbContent = new StringBuilder();
- // probably the simplest way to do this. Less complex than using regexp
- foreach (string strLine in arrLines) {
- if (!string.IsNullOrWhiteSpace(strLine)) {
- sbContent.AppendFormat("<p>{0}</p>", strLine);
- }
- }
- strContent = sbContent.ToString();
- //return sbContent.ToString();
- // now collect the files, only those with MAIN
- XPathNodeIterator xpNodeItFileItems = xpNav.Select("/rss/channel/item/obs:files/obs:file[obs:filetype='MAIN']", nsMan);
- //newLinks = new LinkItemCollection();
- if(xpNodeItFileItems.Count>0) {
- linkNewColl = new LinkItemCollection();
- while(xpNodeItFileItems.MoveNext()) {
- string strFileDesc = ExtractStringValue(xpNodeItFileItems.Current.SelectSingleNode("obs:filedescription", nsMan));
- string strFileUrl = ExtractStringValue(xpNodeItFileItems.Current.SelectSingleNode("obs:fileurl", nsMan));
- LinkItem liTemp = new LinkItem();
- liTemp.Href = strFileUrl;
- liTemp.Text = strFileDesc;
- liTemp.Title = strFileDesc;
- linkNewColl.Add(liTemp);
- }
- }
- }
- //private LinkItemCollection CreateFileContent() {
- //}
- /// <summary>
- /// Find year container, if not found, create it
- /// </summary>
- /// <param name="prDestRoot"></param>
- /// <param name="langSel"></param>
- /// <param name="dtCreated"></param>
- /// <returns></returns>
- private PageReference GetYearEpiContainer(PageReference prDestRoot, LanguageSelector langSel, DateTime dtCreated) {
- const string PT_CONTAINER = "[General] Container";
- EPiServer.Core.PageDataCollection pdcYears = EPiServer.DataFactory.Instance.GetChildren(prDestRoot, langSel);
- PageReference prYear = null;
- string strYearRequested = dtCreated.ToString("yyyy");
- // try to find year requested
- foreach (PageData pd in pdcYears) {
- if (pd.PageName == strYearRequested) {
- prYear = pd.PageLink;
- break;
- }
- }
- // Could'nt find it. Create it.
- if (prYear == null) {
- PageData pdPageNewYear = EPiServer.DataFactory.Instance.GetDefaultPageData(prDestRoot, PT_CONTAINER, langSel).CreateWritableClone();
- pdPageNewYear.PageName = strYearRequested;
- prYear = EPiServer.DataFactory.Instance.Save(pdPageNewYear, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
- }
- return prYear;
- }
- /// <summary>
- /// Handling creating and populating the Epi page
- /// </summary>
- //private bool CreateEpiPage(PageReference prDestRoot, LanguageSelector langSel, ContentSetting cntSetting, string strID, string strTitle, string strDescription, string strPubDate) {
- private bool CreateEpiPage(PageReference prDestRoot, LanguageSelector langSel, ContentSetting cntSetting, string strID, string strTitle, string strDescription, string strPubDate) {
- const string PT_PRESS_REL_NAME = "[Corporate] Press release";
- const string PROP_NAME_SRC_REF = "SrcRef";
- DateTime dtCreated = DateTime.MinValue;
- if (!DateTime.TryParse(strPubDate, out dtCreated)) {
- dtCreated = DateTime.Now; // Should have something, I suppose ...
- }
- PageReference prYear = GetYearEpiContainer(prDestRoot, langSel, dtCreated);
- EPiServer.Core.PageDataCollection pdcPages = EPiServer.DataFactory.Instance.GetChildren(prYear, langSel);
- // Try to find the "SrcRef" == strID, if found : dont create a new page. Bail out.
- foreach (PageData pd in pdcPages) {
- if (pd[PROP_NAME_SRC_REF] as string == strID) {
- // already there, bail out !
- return false;
- }
- }
- // create a new page with the data
- // get the data from this url
- //string strMainBody = CreateMainContent(string.Format(cntSetting.SrcDetailsUrl, strID));
- LinkItemCollection linksFromFeed = null;
- string strMainBody = string.Empty;
- CreateMainContent(string.Format(cntSetting.SrcDetailsUrl, strID), ref strMainBody, ref linksFromFeed);
- PageData pdPageNew = EPiServer.DataFactory.Instance.GetDefaultPageData(prYear, PT_PRESS_REL_NAME, langSel).CreateWritableClone();
- pdPageNew.SetValue("PageCreated", dtCreated);
- pdPageNew.Property[PROP_NAME_SRC_REF].Value = strID;
- pdPageNew.PageName = strTitle;
- pdPageNew.SetValue("MainIntro", ServerUtil.HtmlDecode(strDescription)); // clean up the scramble tags ?
- pdPageNew.SetValue("MainBody", strMainBody);
- if(linksFromFeed != null && linksFromFeed.Count > 0) {
- LinkItemCollection linksCurrent = pdPageNew["RelatedLinks"] as LinkItemCollection ?? new LinkItemCollection();
- linksCurrent.AddRange(linksFromFeed);
- pdPageNew.SetValue("RelatedLinks", linksCurrent);
- }
- EPiServer.DataFactory.Instance.Save(pdPageNew, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
- return true;
- }
- /// <summary>
- /// Collects data from the ContentSettings, via urls
- /// Creates
- /// </summary>
- public void CreateContent() {
- foreach (ContentSetting cntItem in ContentSettings) {
- XPathDocument xpDoc;
- try {
- // Suppose this could go wrong
- xpDoc = new XPathDocument(cntItem.SrcUrl);
- }
- catch(Exception err) {
- StatusMsg.Add(new Status(true, err.Message, cntItem));
- continue;
- }
- XPathNavigator xpNav = xpDoc.CreateNavigator();
- XmlNamespaceManager nsMan = GetNamespaceManager(xpNav.NameTable);
- int intTotResCount = 0;
- int intImpResCount = 0;
- // This way its pretty easy to add more ways to import content into the site
- if (cntItem.SrcType == SRC_TYPE_RSS) {
- XPathNodeIterator xpNodeItItems = xpNav.Select("/rss/channel/item");
- PageReference prDestRoot = new PageReference(cntItem.EpiDestID);
- LanguageSelector langSel = new LanguageSelector(cntItem.EpiDestLang);
- while (xpNodeItItems.MoveNext()) {
- XPathNavigator xpNavItem = xpNodeItItems.Current;
- string strID = xpNavItem.SelectSingleNode("@obs:id", nsMan).InnerXml;
- string strTitle = xpNavItem.SelectSingleNode("title").InnerXml;
- string strDescription = xpNavItem.SelectSingleNode("description").InnerXml;
- string strPubDate = xpNavItem.SelectSingleNode("pubDate").InnerXml;
- //if (CreateEpiPage(prDestRoot, langSel, cntItem, strID, strTitle, strDescription, strPubDate)) {
- if(CreateEpiPage(prDestRoot, langSel, cntItem, strID, strTitle, strDescription, strPubDate)) {
- intImpResCount++;
- }
- intTotResCount++;
- }
- }
- // This was ok. Give the calling code a chance to do something with the result
- StatusMsg.Add(new Status(false, string.Format("ImpCount:{0}, TotCount:{1};", intImpResCount, intTotResCount), cntItem));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement