SHARE
TWEET

Untitled

a guest Jul 5th, 2011 153 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Xml;
  6. using System.Xml.XPath;
  7.  
  8. using EPiServer.Core;
  9.  
  10. using System.Configuration;
  11. using Vitrolife.ContentImport.Entities;
  12. using System.Text;
  13. using EPiServer.SpecializedProperties;
  14.  
  15. namespace ClassName.ContentImport {
  16.     /// <summary>
  17.     /// Handling import of content into Episerver
  18.     /// </summary>
  19.     public class Manager {
  20.  
  21.         private const string SRC_TYPE_RSS = "rss";
  22.         private HttpServerUtility m_httpServerUtil = null;
  23.         private List<ContentSetting> m_listContentSettings = null;
  24.  
  25.         private List<Status> m_listStatusMsg = new List<Status>();
  26.         public List<Status> StatusMsg {
  27.             get {
  28.                 return m_listStatusMsg;
  29.             }
  30.         }
  31.  
  32.  
  33.         /// <summary>
  34.         /// It seams like both the RSS feeds has the same namespaces
  35.         /// </summary>
  36.         /// <param name="nt"></param>
  37.         /// <returns></returns>
  38.         private XmlNamespaceManager GetNamespaceManager(XmlNameTable nt) {
  39.             XmlNamespaceManager nsMan = new XmlNamespaceManager(nt);
  40.             nsMan.AddNamespace("atom", "http://www.w3.org/2005/Atom");
  41.             nsMan.AddNamespace("obs", "http://www.cision.com/");
  42.             nsMan.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");
  43.             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
  44.             return nsMan;
  45.         }
  46.  
  47.         public Manager() {
  48.        
  49.         }
  50.  
  51.         /// <summary>
  52.         /// For now Im using this just to clean up the scrambled html tags <>
  53.         /// Dont know if this is the way to do it
  54.         /// Depends, maybe we want to remove all html if its ugly ?
  55.         /// </summary>
  56.         private HttpServerUtility ServerUtil {
  57.             get {
  58.                 if (m_httpServerUtil == null) {
  59.                     m_httpServerUtil = HttpContext.Current.Server;
  60.                 }
  61.                 return m_httpServerUtil;
  62.             }
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Extracts the settings
  67.         /// </summary>
  68.         private List<ContentSetting> ContentSettings {
  69.             get {
  70.                 if (m_listContentSettings == null) {
  71.                     XPathDocument xpDoc = new XPathDocument(System.Configuration.ConfigurationManager.AppSettings["ContentImportConfigFile"]);
  72.                     XPathNavigator xpNav = xpDoc.CreateNavigator();
  73.  
  74.                     m_listContentSettings = new List<ContentSetting>();
  75.  
  76.                     XPathNodeIterator xpNodeItItems = xpNav.Select("/config/import-items/item");
  77.                     while (xpNodeItItems.MoveNext()) {
  78.                         XPathNavigator xpNavItem = xpNodeItItems.Current;
  79.                         string strSrcUrl = xpNavItem.GetAttribute("src-url", string.Empty);
  80.                         string strSrcType = xpNavItem.GetAttribute("src-type", string.Empty);
  81.                         string strSrcDetailsUrl = xpNavItem.GetAttribute("src-details-url", string.Empty);
  82.                         string strEpiDestID = xpNavItem.GetAttribute("epi-dest-id", string.Empty);
  83.                         string strEpiDestLang = xpNavItem.GetAttribute("epi-dest-lang", string.Empty);
  84.  
  85.                         int intEpiDestID = -1;
  86.  
  87.                         if (int.TryParse(strEpiDestID, out intEpiDestID)) {
  88.                             m_listContentSettings.Add(new ContentSetting(strSrcUrl, strSrcType, strSrcDetailsUrl, intEpiDestID, strEpiDestLang));
  89.                         }
  90.                     }
  91.                 }
  92.                 return m_listContentSettings;
  93.             }
  94.         }
  95.  
  96.         //private string CreateFileContent(string strSrcUrl)
  97.         //{
  98.  
  99.  
  100.         //   // obs:files alla children som har "MAIN"
  101.         //   // RelatedLinks är en LinkCollection
  102.  
  103.  
  104.         //   XPathDocument xpDoc;
  105.         //    try {
  106.         //        // Suppose this could go wrong
  107.         //        xpDoc = new XPathDocument(strSrcUrl);
  108.         //    }
  109.         //    catch {
  110.         //        return string.Empty; // Dont know if there is any point try to do anything with this case for now
  111.         //    }
  112.  
  113.         //    XPathNavigator xpNav = xpDoc.CreateNavigator();
  114.         //    XmlNamespaceManager nsMan = GetNamespaceManager(xpNav.NameTable);
  115.  
  116.         //    return string.Empty;
  117.         //}
  118.  
  119.  
  120.         private string ExtractStringValue(XPathNavigator xpNav) {
  121.             if(xpNav != null) {
  122.                 return xpNav.Value;
  123.             }
  124.             return string.Empty;
  125.         }
  126.  
  127.  
  128.         //private string CreateMainContent(string strSrcUrl) {
  129.         private void CreateMainContent(string strSrcUrl, ref string strContent, ref LinkItemCollection linkNewColl) {
  130.             XPathDocument xpDoc;
  131.             try {
  132.                 // Suppose this could go wrong
  133.                 xpDoc = new XPathDocument(strSrcUrl);
  134.             }
  135.             catch {
  136.                 //return string.Empty; // Dont know if there is any point try to do anything with this case for now
  137.                 return;
  138.             }
  139.  
  140.             XPathNavigator xpNav = xpDoc.CreateNavigator();
  141.             XmlNamespaceManager nsMan = GetNamespaceManager(xpNav.NameTable);
  142.  
  143.             string[] arrLines = xpNav.SelectSingleNode("/rss/channel/item/content:encoded", nsMan).InnerXml.Split(new string[] { "\n\r" }, StringSplitOptions.RemoveEmptyEntries);
  144.             StringBuilder sbContent = new StringBuilder();
  145.             // probably the simplest way to do this. Less complex than using regexp
  146.             foreach (string strLine in arrLines) {
  147.                 if (!string.IsNullOrWhiteSpace(strLine)) {
  148.                     sbContent.AppendFormat("<p>{0}</p>", strLine);
  149.                 }
  150.             }
  151.  
  152.             strContent = sbContent.ToString();
  153.             //return sbContent.ToString();
  154.  
  155.             // now collect the files, only those with MAIN
  156.             XPathNodeIterator xpNodeItFileItems = xpNav.Select("/rss/channel/item/obs:files/obs:file[obs:filetype='MAIN']", nsMan);
  157.  
  158.             //newLinks = new LinkItemCollection();
  159.  
  160.             if(xpNodeItFileItems.Count>0) {
  161.                 linkNewColl = new LinkItemCollection();
  162.                 while(xpNodeItFileItems.MoveNext()) {
  163.                     string strFileDesc = ExtractStringValue(xpNodeItFileItems.Current.SelectSingleNode("obs:filedescription", nsMan));
  164.                     string strFileUrl = ExtractStringValue(xpNodeItFileItems.Current.SelectSingleNode("obs:fileurl", nsMan));
  165.  
  166.                     LinkItem liTemp = new LinkItem();
  167.                     liTemp.Href = strFileUrl;
  168.                     liTemp.Text = strFileDesc;
  169.                     liTemp.Title = strFileDesc;
  170.                     linkNewColl.Add(liTemp);
  171.                 }
  172.             }
  173.         }
  174.  
  175.         //private LinkItemCollection CreateFileContent() {
  176.        
  177.         //}
  178.  
  179.         /// <summary>
  180.         /// Find year container, if not found, create it
  181.         /// </summary>
  182.         /// <param name="prDestRoot"></param>
  183.         /// <param name="langSel"></param>
  184.         /// <param name="dtCreated"></param>
  185.         /// <returns></returns>
  186.         private PageReference GetYearEpiContainer(PageReference prDestRoot, LanguageSelector langSel, DateTime dtCreated) {
  187.             const string PT_CONTAINER = "[General] Container";
  188.             EPiServer.Core.PageDataCollection pdcYears = EPiServer.DataFactory.Instance.GetChildren(prDestRoot, langSel);
  189.             PageReference prYear = null;
  190.            
  191.             string strYearRequested = dtCreated.ToString("yyyy");
  192.            
  193.             // try to find year requested
  194.             foreach (PageData pd in pdcYears) {
  195.                 if (pd.PageName == strYearRequested) {
  196.                     prYear = pd.PageLink;
  197.                     break;
  198.                 }
  199.             }
  200.  
  201.             // Could'nt find it. Create it.
  202.             if (prYear == null) {
  203.                 PageData pdPageNewYear = EPiServer.DataFactory.Instance.GetDefaultPageData(prDestRoot, PT_CONTAINER, langSel).CreateWritableClone();
  204.                 pdPageNewYear.PageName = strYearRequested;
  205.                 prYear = EPiServer.DataFactory.Instance.Save(pdPageNewYear, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
  206.             }
  207.  
  208.             return prYear;
  209.         }
  210.  
  211.  
  212.         /// <summary>
  213.         /// Handling creating and populating the Epi page
  214.         /// </summary>
  215.         //private bool CreateEpiPage(PageReference prDestRoot, LanguageSelector langSel, ContentSetting cntSetting, string strID, string strTitle, string strDescription, string strPubDate) {
  216.         private bool CreateEpiPage(PageReference prDestRoot, LanguageSelector langSel, ContentSetting cntSetting, string strID, string strTitle, string strDescription, string strPubDate) {
  217.             const string PT_PRESS_REL_NAME = "[Corporate] Press release";
  218.             const string PROP_NAME_SRC_REF = "SrcRef";
  219.  
  220.             DateTime dtCreated = DateTime.MinValue;
  221.             if (!DateTime.TryParse(strPubDate, out dtCreated)) {
  222.                 dtCreated = DateTime.Now; // Should have something, I suppose ...
  223.             }
  224.            
  225.             PageReference prYear = GetYearEpiContainer(prDestRoot, langSel, dtCreated);
  226.             EPiServer.Core.PageDataCollection pdcPages = EPiServer.DataFactory.Instance.GetChildren(prYear, langSel);
  227.  
  228.             // Try to find the "SrcRef" == strID, if found : dont create a new page. Bail out.
  229.             foreach (PageData pd in pdcPages) {
  230.                 if (pd[PROP_NAME_SRC_REF] as string == strID) {
  231.                     // already there, bail out !
  232.                     return false;
  233.                 }
  234.             }
  235.  
  236.             // create a new page with the data
  237.  
  238.             // get the data from this url
  239.             //string strMainBody = CreateMainContent(string.Format(cntSetting.SrcDetailsUrl, strID));
  240.             LinkItemCollection linksFromFeed = null;
  241.             string strMainBody = string.Empty;
  242.             CreateMainContent(string.Format(cntSetting.SrcDetailsUrl, strID), ref strMainBody, ref linksFromFeed);
  243.  
  244.             PageData pdPageNew = EPiServer.DataFactory.Instance.GetDefaultPageData(prYear, PT_PRESS_REL_NAME, langSel).CreateWritableClone();
  245.             pdPageNew.SetValue("PageCreated", dtCreated);
  246.             pdPageNew.Property[PROP_NAME_SRC_REF].Value = strID;
  247.             pdPageNew.PageName = strTitle;
  248.             pdPageNew.SetValue("MainIntro", ServerUtil.HtmlDecode(strDescription)); // clean up the scramble tags ?
  249.             pdPageNew.SetValue("MainBody", strMainBody);
  250.  
  251.             if(linksFromFeed != null && linksFromFeed.Count > 0) {
  252.                 LinkItemCollection linksCurrent = pdPageNew["RelatedLinks"] as LinkItemCollection ?? new LinkItemCollection();
  253.                 linksCurrent.AddRange(linksFromFeed);
  254.                 pdPageNew.SetValue("RelatedLinks", linksCurrent);
  255.             }
  256.  
  257.             EPiServer.DataFactory.Instance.Save(pdPageNew, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
  258.  
  259.             return true;
  260.         }
  261.  
  262.  
  263.         /// <summary>
  264.         /// Collects data from the ContentSettings, via urls
  265.         /// Creates
  266.         /// </summary>
  267.         public void CreateContent() {
  268.             foreach (ContentSetting cntItem in ContentSettings) {
  269.                 XPathDocument xpDoc;
  270.                 try {
  271.                     // Suppose this could go wrong
  272.                     xpDoc = new XPathDocument(cntItem.SrcUrl);
  273.                 }
  274.                 catch(Exception err) {
  275.                     StatusMsg.Add(new Status(true, err.Message, cntItem));
  276.                     continue;
  277.                 }
  278.  
  279.                 XPathNavigator xpNav = xpDoc.CreateNavigator();
  280.                 XmlNamespaceManager nsMan = GetNamespaceManager(xpNav.NameTable);
  281.  
  282.                 int intTotResCount = 0;
  283.                 int intImpResCount = 0;
  284.                
  285.                 // This way its pretty easy to add more ways to import content into the site
  286.                 if (cntItem.SrcType == SRC_TYPE_RSS) {
  287.                     XPathNodeIterator xpNodeItItems = xpNav.Select("/rss/channel/item");
  288.  
  289.                     PageReference prDestRoot = new PageReference(cntItem.EpiDestID);
  290.                     LanguageSelector langSel = new LanguageSelector(cntItem.EpiDestLang);
  291.                    
  292.                     while (xpNodeItItems.MoveNext()) {
  293.                         XPathNavigator xpNavItem = xpNodeItItems.Current;
  294.                         string strID = xpNavItem.SelectSingleNode("@obs:id", nsMan).InnerXml;
  295.                         string strTitle = xpNavItem.SelectSingleNode("title").InnerXml;
  296.                         string strDescription = xpNavItem.SelectSingleNode("description").InnerXml;
  297.                         string strPubDate = xpNavItem.SelectSingleNode("pubDate").InnerXml;
  298.  
  299.                         //if (CreateEpiPage(prDestRoot, langSel, cntItem, strID, strTitle, strDescription, strPubDate)) {
  300.                         if(CreateEpiPage(prDestRoot, langSel, cntItem, strID, strTitle, strDescription, strPubDate)) {
  301.                             intImpResCount++;
  302.                         }
  303.                         intTotResCount++;
  304.                     }
  305.                 }
  306.                 // This was ok. Give the calling code a chance to do something with the result
  307.                 StatusMsg.Add(new Status(false, string.Format("ImpCount:{0}, TotCount:{1};", intImpResCount, intTotResCount), cntItem));
  308.             }
  309.         }
  310.      
  311.       }
  312.     }
  313.  
  314. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top