/// <summary> /// This method returns the result of a search. It returns the exact number of records as determined /// by the RecsPerPage input parameter. /// </summary> /// <param name="ToSearch">Specifies what, specifically, to search. Users can opt to search /// for posts (PostsSearch) or search for post authors (PostsBySearch).</param> /// <param name="SearchWhat">Instructs how to interpret the search query. Values can be: SearchAllWords, /// SearchAnyWord, or SearchExactPhrase.</param> /// <param name="ForumToSearch">Indicates the ID of the Forum to search. To search all forums, /// specify a value of -1 here.</param> /// <param name="SearchTerms">Specifies the user's typed-in search query.</param> /// <param name="Page">Indicates what page of the search results to display.</param> /// <param name="RecsPerPage">Determines how many records per page to display.</param> /// <returns>A PostCollection containing RecsPerPage Posts (perhaps less).</returns> /// <remarks>Page is indexed starting at 1. That is, to get the first page of search results /// pass a value of 1 as the Page parameter.</remarks> public static PostCollection PerformSearch(ToSearchEnum ToSearch, SearchWhatEnum SearchWhat, int ForumToSearch, String SearchTerms, int Page, int RecsPerPage) { // Create Instance of the IDataProviderBase IDataProviderBase dp = DataProvider.Instance(); // ensure that Page > 0 and RecsPerPage > 0 if (RecsPerPage <= 0) RecsPerPage = 10; if (Page <= 0) Page = 1; return dp.GetSearchResults(ToSearch, SearchWhat, ForumToSearch, SearchTerms, Page, RecsPerPage, (UlterSystems.PortalLib.BusinessObjects.Person.RequestUser().ID).ToString()); }
/// <summary> /// This method returns the result of a search. It returns the exact number of records as determined /// by the RecsPerPage input parameter. /// </summary> /// <param name="ToSearch">Specifies what, specifically, to search. Users can opt to search /// for posts (PostsSearch) or search for post authors (PostsBySearch).</param> /// <param name="SearchWhat">Instructs how to interpret the search query. Values can be: SearchAllWords, /// SearchAnyWord, or SearchExactPhrase.</param> /// <param name="ForumToSearch">Indicates the ID of the Forum to search. To search all forums, /// specify a value of -1 here.</param> /// <param name="SearchTerms">Specifies the user's typed-in search query.</param> /// <param name="Page">Indicates what page of the search results to display.</param> /// <param name="RecsPerPage">Determines how many records per page to display.</param> /// <returns>A PostCollection containing RecsPerPage Posts (perhaps less).</returns> /// <remarks>Page is indexed starting at 1. That is, to get the first page of search results /// pass a value of 1 as the Page parameter.</remarks> public static PostCollection PerformSearch(ToSearchEnum ToSearch, SearchWhatEnum SearchWhat, int ForumToSearch, String SearchTerms, int Page, int RecsPerPage) { // Create Instance of the IDataProviderBase IDataProviderBase dp = DataProvider.Instance(); // ensure that Page > 0 and RecsPerPage > 0 if (RecsPerPage <= 0) { RecsPerPage = 10; } if (Page <= 0) { Page = 1; } return(dp.GetSearchResults(ToSearch, SearchWhat, ForumToSearch, SearchTerms, Page, RecsPerPage, HttpContext.Current.User.Identity.Name)); }
/*********************************************************************************/ /************************ SEARCH FUNCTIONS *********************** * These functions are used to perform searching. * ***************************************************************/ /// <summary> /// Performs a search, returning a PostCollection object with appropriate posts. /// </summary> /// <param name="ToSearch">Specifies what to search, specifically. Must be set to a valid /// ToSearchEnum value, which supports two possible values: PostsSearch and PostsBySearch.</param> /// <param name="SearchWhat">A SearchWhatEnum value, this parameter specifies what to search. /// Acceptable values are: SearchAllWords, SearchAnyWord, and SearchExactPhrase.</param> /// <param name="ForumToSearch">Specifies what forum to search. To search all forums, pass in a /// value of 0.</param> /// <param name="SearchTerms">Specifies the terms to search on.</param> /// <param name="Page">Specifies what page of the search results to display.</param> /// <param name="RecsPerPage">Specifies how many records per page to show on the search /// results.</param> /// <returns>A PostCollection object, containing the posts to display for the particular page /// of the search results.</returns> public PostCollection GetSearchResults(ToSearchEnum ToSearch, SearchWhatEnum SearchWhat, int ForumToSearch, String SearchTerms, int Page, int RecsPerPage, string username) { // return all of the forums and their total and daily posts // first, though, we've got to put our search phrase in the right order String strColumnName = ""; String strWhereClause = " WHERE ("; String [] aTerms = null; // Are we searching for a particular user? if (ToSearch == ToSearchEnum .PostsSearch) { strColumnName = "Body"; // depending on the search style, our WHERE clause will differ switch(SearchWhat) { case SearchWhatEnum.SearchExactPhrase: // easy, we want to search for the exact search term strWhereClause += strColumnName + " LIKE '%" + SearchTerms + "%' "; break; case SearchWhatEnum.SearchAllWords: // allrighty, we want to find rows where each word is found // split up the search term string into an array aTerms = SearchTerms.Split(new char[]{' '}); // now, loop through the aTerms array strWhereClause += strColumnName + " LIKE '%" + String.Join("%' AND " + strColumnName + " LIKE '%", aTerms) + "%'"; break; case SearchWhatEnum.SearchAnyWord: // allrighty, we want to find rows where each word is found // split up the search term string into an array aTerms = SearchTerms.Split(new char[]{' '}); // now, loop through the aTerms array strWhereClause += strColumnName + " LIKE '%" + String.Join("%' OR " + strColumnName + " LIKE '%", aTerms) + "%'"; break; } strWhereClause += ")"; } else if (ToSearch == ToSearchEnum.PostsBySearch) { strColumnName = "UserName"; strWhereClause += strColumnName + " = '" + SearchTerms + "') "; } // see if we need to add a restriction on the ForumID if (ForumToSearch > 0) strWhereClause += " AND P.ForumID = " + ForumToSearch.ToString() + " "; // Create Instance of Connection and Command Object SqlConnection myConnection = new SqlConnection(Globals.DatabaseConnectionString); SqlCommand myCommand = new SqlCommand("dbo.forums_GetSearchResults", myConnection); // Mark the Command as a SPROC myCommand.CommandType = CommandType.StoredProcedure; SqlParameter parameterPage = new SqlParameter("@Page", SqlDbType.Int, 4); parameterPage.Value = Page; myCommand.Parameters.Add(parameterPage); SqlParameter parameterRecsPerPage = new SqlParameter("@RecsPerPage", SqlDbType.Int, 4); parameterRecsPerPage.Value = RecsPerPage; myCommand.Parameters.Add(parameterRecsPerPage); SqlParameter parameterSearchTerms = new SqlParameter("@SearchTerms", SqlDbType.NVarChar, 500); parameterSearchTerms.Value = strWhereClause; myCommand.Parameters.Add(parameterSearchTerms); if ( (username == null) || (username == String.Empty)) myCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = System.DBNull.Value; else myCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = int.Parse(username); //myCommand.Parameters.Add("@Username", SqlDbType.NVarChar, 50).Value = username; // Execute the command myConnection.Open(); SqlDataReader dr = myCommand.ExecuteReader(); // populate the Posts collection PostCollection posts = new PostCollection(); if (!dr.Read()) { dr.Close(); myConnection.Close(); // we have an empty result, return the empty post collection return posts; } else { // we have to populate our postcollection posts.TotalRecordCount = Convert.ToInt32(dr["MoreRecords"]); do { posts.Add(PopulatePostFromSqlDataReader(dr)); ((Post) posts[posts.Count - 1]).ForumName = Convert.ToString(dr["ForumName"]); } while (dr.Read()); dr.Close(); myConnection.Close(); return posts; } }