/* * This work is licensed under the Creative Commons Attribution 2.5 License. * To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ * or send a letter to Creative Commons, 543 Howard Street, 5th Floor, * San Francisco, California, 94105, USA. * * Original developer: David Betz * */ using System; using System.IO; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; using System.Text; using SD.LLBLGen.Pro.ORMSupportClasses; using SD.LLBLGen.Pro.DQE.SqlServer; using DataFeedFrameworkDAL.EntityClasses; using DataFeedFrameworkDAL.FactoryClasses; using DataFeedFrameworkDAL.CollectionClasses; using DataFeedFrameworkDAL.HelperClasses; using DataFeedFrameworkDAL; using Rss; using Atom.Core; using System.Security; namespace FeedBuilder { public static class FeedGenerator { public static String GetRssFeedText(Int32 id, Uri link) { if (FeedSecurity.CheckAccessById(id)) { return FeedGenerator.GetRssFeedTextById(id, link); } else { throw new SecurityException("This feed only allows access by SecretId"); } } public static String GetRssFeedText(String title, Uri link) { if (FeedSecurity.CheckAccessByTitle(title)) { return FeedGenerator.GetRssFeedTextById(FeedGenerator.GetFeedIdByTitle(title), link); } else { throw new SecurityException("This feed only allows access by SecretId"); } } public static String GetRssFeedText(Guid secretId, Uri link) { return FeedGenerator.GetRssFeedTextById(FeedGenerator.GetFeedIdBySecretId(secretId), link); } private static String GetRssFeedTextById(Int32 id, Uri target) { RssFeed feed = FeedGenerator.GenerateFeed(id, target); String feedText; using (MemoryStream stream = new MemoryStream()) { feed.Version = RssVersion.RSS20; feed.Write(stream); byte[] buffer = stream.GetBuffer(); feedText = Encoding.UTF8.GetString(buffer); } return feedText.Substring(0, feedText.LastIndexOf("") + "".Length); } private static Int32 GetFeedIdBySecretId(Guid secretId) { FeedCreationCollection collection = new FeedCreationCollection(); IPredicateExpression filter = new PredicateExpression(); filter.Add(FeedCreationFields.FeedGuid == secretId.ToString()); collection.GetMulti(filter); if (collection.Count < 1) { throw new FeedGenerationException("Invalid title."); } return collection[0].FeedCreationId; } private static Int32 GetFeedIdByTitle(String title) { FeedCreationCollection collection = new FeedCreationCollection(); IPredicateExpression filter = new PredicateExpression(); filter.Add(FeedCreationFields.FeedCreationTitle == title); collection.GetMulti(filter); if (collection.Count < 1) { throw new FeedGenerationException("Invalid title."); } return collection[0].FeedCreationId; } private static RssFeed GenerateFeed(Int32 feedId, Uri actualLink) { FeedCreationEntity feed = new FeedCreationEntity(feedId); return GenerateFeed(feedId, actualLink, feed.FeedCreationTitle, feed.FeedCreationDescription, feed.FeedCreationStatement, feed.FeedCreationDatabase); } private static RssFeed GenerateFeed(Int32 feedId, Uri actualLink, String title, String description, String statement, String database) { RssFeed feed = new RssFeed(); if (description.Length < 1) { description = " "; } if (title.Length < 1) { throw new FeedGenerationException("Title is required."); } if (statement.Length < 1) { throw new FeedGenerationException("SQL Statement is required."); } RssChannel channel = new RssChannel(); channel.Title = title; channel.Description = description; channel.Link = actualLink; using (SqlConnection connection = new SqlConnection(ConfigurationFacade.ApplicationSettings("DataFeedFrameworkDAL.ConnectionString"))) { connection.Open(); if (!String.IsNullOrEmpty(database)) { connection.ChangeDatabase(database); } using (SqlCommand command = new SqlCommand(statement, connection)) { command.CommandType = CommandType.Text; SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { RssItem item = new RssItem(); Int32 id = 0; if (reader["Id"] != null) { if (Int32.TryParse(reader["Id"].ToString(), out id)) { } } item.Title = (String)reader["Title"]; if (reader["Description"] is System.String) { item.Description = (String)reader["Description"]; } String template = String.Empty; try { template = reader["LinkTemplate"].ToString(); } catch { } item.Link = GetEntryLink(id, title, description, template); channel.Items.Add(item); } } else { RssItem item = new RssItem(); item.Title = "No entries"; item.Link = actualLink; item.Description = String.Empty; channel.Items.Add(item); } } feed.Channels.Add(channel); } return feed; } private static Uri GetEntryLink(Int32 id, String title, String description, String template) { if (!String.IsNullOrEmpty(template)) { String parsedLink = TemplateParser.ParseLink(template, id, title, description); if (parsedLink.StartsWith("http://") || parsedLink.StartsWith("https://")) { return new Uri(parsedLink); } else { return new Uri(ConfigurationFacade.ApplicationSettings("SiteRoot") + parsedLink); } } else if (id > 0) { return new Uri(TemplateParser.ParseLink(ConfigurationFacade.ApplicationSettings("SiteRoot") + FeedConfiguration.DefaultEntryLinkTemplate, id, title, description)); } else { return new Uri(""); } } } }