//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.Reader.Components;

namespace CommunityServer.Data
{
	/// <summary>
	/// Summary description for FeedReaderSqlDataProvider.
	/// </summary>
	public class FeedReaderSqlDataProvider : FeedReaderDataProvider
	{
		#region SettingsID 
		protected int GetSettingsID()
		{
			return  sqlHelper.GetSettingsID();
		}

		protected SqlParameter SettingsIDParameter()
		{
			SqlParameter p = new SqlParameter("@SettingsID",SqlDbType.Int);
			p.Value = GetSettingsID();
			return p;
		}
		#endregion

		#region Member variables
		
		protected string databaseOwner	= "dbo";	// overwrite in web.config
		string connectionString = null;
		protected ProviderHelper sqlHelper = null;
		
		#endregion


		#region Constructor(s)

		public FeedReaderSqlDataProvider(string databaseOwner, string connectionString)
		{
			// Read the connection string for this provider
			//
			this.connectionString = connectionString;

			// Read the database owner name for this provider
			//
			databaseOwner = databaseOwner;

			sqlHelper = ProviderHelper.Instance();
		
		}

		#endregion

		#region Methods

		public override ArrayList GetAllFeeds(int minutes)
		{
			using (SqlConnection myConnection = GetSqlConnection())
			{
				string sql = minutes > 0 ? ".cs_Feed_GetAll_ModifiedSince" : ".cs_Feed_GetAll";

				SqlCommand myCommand = new SqlCommand(databaseOwner + sql, myConnection);
				ArrayList feeds = new ArrayList();

				// Mark the Command as a SPROC
				myCommand.CommandType = CommandType.StoredProcedure;

                if(minutes > 0)
                    myCommand.Parameters.Add("@Minutes",SqlDbType.Int).Value = minutes;

                myCommand.Parameters.Add(this.SettingsIDParameter());

				// Execute the command
				myConnection.Open();
				SqlDataReader dr = myCommand.ExecuteReader();

				while(dr.Read())
					feeds.Add(PopulateFeedFromIDataReader(dr));
				

				dr.Close();
				myConnection.Close();

				// return the collection of IDs
				return feeds;				
			}
		}

		

		public override void UpdateFeed(int feedId, string url, string title, string link, string language, string generator, DateTime subscribeDate, DateTime lastUpdateDate, DateTime lastModified, string eTag, int feedStateId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Feed_Update", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@FeedId", SqlDbType.Int).Value = feedId;
				myCommand.Parameters.Add("@Url", SqlDbType.NVarChar).Value = url;
				myCommand.Parameters.Add("@Title", SqlDbType.NVarChar).Value = title;
				myCommand.Parameters.Add("@Link", SqlDbType.NVarChar).Value = link;
				myCommand.Parameters.Add("@Language", SqlDbType.NVarChar).Value = language;
				myCommand.Parameters.Add("@Generator", SqlDbType.NVarChar).Value = generator;
				myCommand.Parameters.Add("@SubscribeDate", SqlDbType.DateTime).Value = subscribeDate;
				myCommand.Parameters.Add("@LastUpdateDate", SqlDbType.DateTime).Value = lastUpdateDate;
                myCommand.Parameters.Add("@LastModified", SqlDbType.DateTime).Value = lastModified;
                myCommand.Parameters.Add("@ETag", SqlDbType.NVarChar).Value = eTag;
				myCommand.Parameters.Add("@FeedStateId", SqlDbType.Int).Value = feedStateId;

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				myConnection.Close();

			}
		}

		
		public override void UpdateFeedStatus(int feedId, FeedState state)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Feed_UpdateFeedStatus", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				
				myCommand.Parameters.Add("@FeedId", SqlDbType.Int).Value = feedId;
				myCommand.Parameters.Add("@FeedStateId", SqlDbType.Int).Value = state;

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				myConnection.Close();
			}
		}
		
			

		public override void UpdateFeedPosts(int feedId, string feedItemList)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_FeedPost_UpdatePosts", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@FeedId", SqlDbType.Int).Value = feedId;
				myCommand.Parameters.Add("@FeedItemList", SqlDbType.NText).Value = feedItemList;

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				
				myConnection.Close();
			}
		}

		public override ArrayList GetUsers()
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Feed_GetUsers", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				ArrayList arrayList = new ArrayList();

				// Set parameters
				//
				myCommand.Parameters.Add(this.SettingsIDParameter());

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{
					// Get the results
					//
					while(dr.Read())
						arrayList.Add(CommonDataProvider.cs_PopulateUserFromIDataReader(dr));

					dr.Close();
				}

				myConnection.Close();

				return arrayList;
			}
		}
		
		public override ArrayList GetFolders(int userId, int parentFolderId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Folder_GetFolders", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				ArrayList arrayList = new ArrayList();

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;

				// If 0 is passed, leave ParentFolderId null.
				if (parentFolderId != 0)
					myCommand.Parameters.Add("@ParentFolderId", SqlDbType.Int).Value = parentFolderId;

                myCommand.Parameters.Add(this.SettingsIDParameter());

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					while(dr.Read())
						arrayList.Add( PopulateFolderFromIDataReader(dr));

					dr.Close();
				}

				myConnection.Close();

				return arrayList;
			}
		
		}

		public override ArrayList GetFeeds(int userId, int folderId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Folder_GetFeeds", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				ArrayList arrayList = new ArrayList();

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;

				if (folderId != 0)
					myCommand.Parameters.Add("@FolderId", SqlDbType.Int).Value = folderId;

                myCommand.Parameters.Add(this.SettingsIDParameter());

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					while(dr.Read())
						arrayList.Add( PopulateFullFeedFromIDataReader(dr));

					dr.Close();
				}

				myConnection.Close();

				return arrayList;
			}
		}

		
		public override ArrayList GetFeedPosts(int feedId, int userId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Feed_GetFeedPosts", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				ArrayList arrayList = new ArrayList();

				// Set parameters
				//
				myCommand.Parameters.Add("@FeedId", SqlDbType.Int).Value = feedId;
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;

                myCommand.Parameters.Add(this.SettingsIDParameter());

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					while(dr.Read())
						arrayList.Add( PopulateFeedPostFromIDataReader(dr, DateTime.Now));

					dr.Close();
				}

				myConnection.Close();

				return arrayList;
			}
		}
		
			



		public override FeedPost GetPost(int feedPostId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_FeedPost_GetPost", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				FeedPost feedPost = new FeedPost();

				// Set parameters
				//
				myCommand.Parameters.Add("@FeedPostId", SqlDbType.Int).Value = feedPostId;

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					if (dr.Read())
						feedPost = PopulateFeedPostFromIDataReader(dr, DateTime.Now);

					dr.Close();
				}

				myConnection.Close();

				return feedPost;
			}
		}
		
			
		public override void MarkPostRead(int userId, int feedId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_UserReadPost_MarkPostRead", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
				myCommand.Parameters.Add("@FeedPostId", SqlDbType.Int).Value = feedId;

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				myConnection.Close();
			}
		}
		

		public override void RenameFolder(int folderId, string newFolderName)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Folder_RenameFolder", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@FolderId", SqlDbType.Int).Value = folderId;
				myCommand.Parameters.Add("@FolderName", SqlDbType.NVarChar).Value = newFolderName;

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				myConnection.Close();

			}
		}
		
		
		public override int AddFolder(int userId, string folderName, int parentFolderId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Folder_AddFolder", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
				myCommand.Parameters.Add("@FolderName", SqlDbType.NVarChar).Value = folderName;
				myCommand.Parameters.Add(this.SettingsIDParameter());

				SqlParameter folderIdParam = new SqlParameter("@FolderId", SqlDbType.Int);
				folderIdParam.Direction = ParameterDirection.Output;

				myCommand.Parameters.Add(folderIdParam);

				if (parentFolderId != 0)
					myCommand.Parameters.Add("@ParentFolderId", SqlDbType.Int).Value = parentFolderId;

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				int folderId = (int) myCommand.Parameters["@FolderId"].Value;

				myConnection.Close();

				return folderId;

			}
		}
			


		public override int AddFeed(int userId, int folderId, string url)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Feed_AddFeed", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
				myCommand.Parameters.Add(this.SettingsIDParameter());

				if (folderId != 0)
					myCommand.Parameters.Add("@FolderId", SqlDbType.Int).Value = folderId;

				myCommand.Parameters.Add("@Url", SqlDbType.NVarChar).Value = url;

				SqlParameter feedIdParam = new SqlParameter("@FeedId", SqlDbType.Int);
				feedIdParam.Direction = ParameterDirection.Output;


				myCommand.Parameters.Add(feedIdParam);

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				int feedId = (int) myCommand.Parameters["@FeedId"].Value;

				myConnection.Close();

				return feedId;
			}
		}
		

		public override void AddAggregatedFeed(int userId, int feedId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Roller_AddFeed", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
				myCommand.Parameters.Add("@FeedId", SqlDbType.Int).Value = feedId;

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();


				myConnection.Close();

			}
		}
		
	
		public override void DeleteFolder(int folderId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Folder_DeleteFolder", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@FolderId", SqlDbType.Int).Value = folderId;

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				myConnection.Close();
			}
		}
		

		public override void DeleteFeed(int userId, int feedId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_UserFeed_DeleteFeed", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
				myCommand.Parameters.Add("@FeedId", SqlDbType.Int).Value = feedId;
                myCommand.Parameters.Add(this.SettingsIDParameter());

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				myConnection.Close();
			}
		}

		
		public override void MoveFeed(int folderFeedId, int newFolderId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Feed_MoveFeed", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@FolderFeedId", SqlDbType.Int).Value = folderFeedId;

				if (newFolderId != 0)
					myCommand.Parameters.Add("@NewFolderId", SqlDbType.Int).Value = newFolderId;

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				myConnection.Close();

			}
		}

		
		public override void MoveFolder(int folderId, int newParentFolderId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Folder_MoveFolder", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;

				// Set parameters
				//
				myCommand.Parameters.Add("@FolderId", SqlDbType.Int).Value = folderId;

				if (newParentFolderId != 0)
					myCommand.Parameters.Add("@NewParentFolderId", SqlDbType.Int).Value = newParentFolderId;

				// Execute the command
				//
				myConnection.Open();
				myCommand.ExecuteNonQuery();

				myConnection.Close();
			}
		}

		
		public override ArrayList GetOpmlFeedsFolders(int userId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_User_GetOpmlFeedsFolders", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				ArrayList arrayList = new ArrayList();

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					while(dr.Read())
						arrayList.Add( PopulateOpmlOutlineFromIDataReader(dr));

					dr.Close();
				}

				myConnection.Close();

				return arrayList;
			}
		}

		
		public override Feed GetFeedProperties(int feedId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Feed_GetProperties", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				Feed feed = new Feed();

				// Set parameters
				//
				myCommand.Parameters.Add("@FeedId", SqlDbType.Int).Value = feedId;

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					while(dr.Read())
						feed = PopulateFeedPropertiesFromIDataReader(dr);

					dr.Close();
				}

				myConnection.Close();

				return feed;
			}
		}


		public override FeedPostSet GetRollerPosts(int userID, int pageIndex, int pageSize)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Roller_GetPosts", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				FeedPostSet posts = new FeedPostSet();

				// Set parameters
				//
				myCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = userID;
                myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = pageIndex;
                myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = pageSize;
                SqlParameter records = new SqlParameter("@TotalRecords", SqlDbType.Int);
                records.Direction = ParameterDirection.Output;
                myCommand.Parameters.Add(records);


				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					while(dr.Read())
						posts.Add( PopulateRollerFeedPostFromIDataReader(dr, DateTime.Now));

					dr.Close();
				}

				myConnection.Close();

                posts.TotalRecords = (int)myCommand.Parameters["@TotalRecords"].Value;

				return posts;
			}
		}
		

		
		public override ArrayList GetRollerFeeds(int userId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Roller_GetFeeds", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				ArrayList arrayList = new ArrayList();

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					while(dr.Read())
						arrayList.Add( PopulateBriefFeedFromIDataReader(dr));

					dr.Close();
				}

				myConnection.Close();

				return arrayList;
			}
		}
		
			
		public override ArrayList GetUnaggregatedFeeds(int userId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Roller_GetUnaggregatedFeeds", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				ArrayList arrayList = new ArrayList();

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					while(dr.Read())
						arrayList.Add( PopulateFeedFromIDataReader(dr));

					dr.Close();
				}

				myConnection.Close();

				return arrayList;
			}
		}


		public override FeedPost GetPostFullDetails(int feedId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_FeedPost_GetPostFullDetails", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				FeedPost feedPost = new FeedPost();

				// Set parameters
				//
				myCommand.Parameters.Add("@FeedId", SqlDbType.Int).Value = feedId;

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					while(dr.Read())
						feedPost = PopulateFeedPostFromIDataReader(dr, DateTime.Now);

					dr.Close();
				}

				myConnection.Close();

				return feedPost;
			}
		}
		

		public override ArrayList GetFolderSummary(int userId, int folderId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Folder_GetSummary", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				ArrayList results = new ArrayList();

				// Set parameters
				//
				myCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;

				if (folderId != 0)
					myCommand.Parameters.Add("@FolderId", SqlDbType.Int).Value = folderId;

                myCommand.Parameters.Add(this.SettingsIDParameter());

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{
					ArrayList mostActive = new ArrayList();
					ArrayList mostRecent = new ArrayList();
					ArrayList summaryPosts = new ArrayList();

					
					// Get the first set of results.
					while (dr.Read())
					{
						mostActive.Add(PopulateFeedPostFromIDataReader(dr, DateTime.Now));
					}
					results.Add(mostActive);

					dr.NextResult();

					// Now, the second set of results.
					int i = 0;
					while (dr.Read())
					{
						FeedPost currentPost = PopulateFeedPostFromIDataReader(dr, DateTime.Now);

						if (i < 10)
						{
							mostRecent.Add(currentPost);
						}
						summaryPosts.Add(currentPost);
						i++;
					}
					results.Add(mostRecent);
					results.Add(summaryPosts);

					dr.Close();
				}

				myConnection.Close();

				return results;
			}
		}
		
			
		public override Feed GetFeed(int feedId)
		{
			// Create Instance of Connection and Command Object
			//
			using( SqlConnection myConnection = GetSqlConnection() ) 
			{
				SqlCommand myCommand = new SqlCommand(databaseOwner + ".cs_Feed_GetFeed", myConnection);
				myCommand.CommandType = CommandType.StoredProcedure;
				Feed feed = new Feed();

				// Set parameters
				//
				myCommand.Parameters.Add("@FeedId", SqlDbType.Int).Value = feedId;

				// Execute the command
				//
				myConnection.Open();
				using(SqlDataReader dr = myCommand.ExecuteReader())
				{

					// Get the results
					//
					while(dr.Read())
						feed = PopulateFeedFromIDataReader(dr);

					dr.Close();
				}

				myConnection.Close();

				return feed;
			}
		}


		#endregion

		#region Properties

		public string ConnectionString
		{
			get { return connectionString; }	
			set { connectionString = value; }
		}

		#endregion

		#region Helpers

		protected SqlConnection GetSqlConnection () 
		{

			try 
			{
				return new SqlConnection(ConnectionString);
			} 
			catch 
			{
				throw new CSException(CSExceptionType.DataProvider, "SQL Connection String is invalid.");
			}

		}

		#endregion

	}
}
