示例#1
0
        public void SearchTestXmlTest()
        {
            Search search = new Search();
            XmlDocument xml = Serializer.SerializeToXml(search);

            Assert.IsNotNull(xml.SelectSingleNode("SEARCH"));

        }
示例#2
0
        /// <summary>
        /// Gets Recommendations from cache or db if not found in cache
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="readerCreator"></param>
        /// <param name="ignoreCache"></param>
        /// <returns></returns>
        public static Search CreateSearch(ICacheManager cache, IDnaDataReaderCreator readerCreator, int siteId, string searchString, string searchType, bool showApproved, bool showNormal, bool showSubmitted, int startIndex, int itemsPerPage, bool useFastSearch, bool ignoreCache)
        {
            var search = new Search();

            //string key = search.GetCacheKey(searchString, searchType, showApproved);
            //check for item in the cache first
            /*
            if (!ignoreCache)
            {
                //not ignoring cache
                search = (Search)cache.GetData(key);
                if (search != null)
                {
                    //check if still valid with db...
                    if (search.IsUpToDate(readerCreator))
                    {
                        return search;
                    }
                }
            }
            */

            if (searchType == "ARTICLE")
            {
                //create article search from db
                search = CreateArticleSearchFromDatabase(readerCreator, siteId, searchString, searchType, showApproved, showNormal, showSubmitted, startIndex, itemsPerPage, useFastSearch);
            }
            else //USER search
            {
                bool allowEmails = false;

                
                //create article search from db
                search = CreateUserSearchFromDatabase(readerCreator, siteId, searchString, searchType, allowEmails, startIndex, itemsPerPage);
            }

            //add to cache
            //cache.Add(key, search);

            return search;
        }
示例#3
0
 public Stream GetSearch(string siteName)
 {
     ISite site = Global.siteList.GetSite(siteName);
     var search = new Search();
     var querystring = QueryStringHelper.GetQueryParameterAsString("querystring", string.Empty);
     var showApproved = QueryStringHelper.GetQueryParameterAsInt("showapproved", 1);
     var showNormal = QueryStringHelper.GetQueryParameterAsInt("shownormal", 0);
     var showSubmitted = QueryStringHelper.GetQueryParameterAsInt("showsubmitted", 0);
     if (querystring != string.Empty)
     {
         try
         {
             search = Search.CreateSearch(cacheManager,
                                             readerCreator,
                                             site.SiteID,
                                             querystring,
                                             "USER",
                                             showApproved == 1 ? true : false,
                                             showNormal == 1 ? true : false,
                                             showSubmitted == 1 ? true : false,
                                             startIndex,
                                             itemsPerPage,
                                             false);
         }
         catch (ApiException ex)
         {
             throw new DnaWebProtocolException(ex);
         }
     }
     return GetOutputStream(search);
 }
示例#4
0
 public static Search CreateArticleSearch()
 {
     Search target = new Search()
     {
         RecentSearches = CreateRecentSearches(),
         SearchResults = new SearchResults()
         {
             SearchTerm = "dinosaur",
             SafeSearchTerm = "dinosaur",
             Type = "ARTICLE",
             Count = 1,
             More = 1,
             Skip = 0,
             ArticleResults = CreateArticleResults()
         },
         Functionality = new Functionality()
         {
             SearchArticles = new SearchArticles()
             {
                 Selected = 1,
                 ShowApproved = 1,
                 ShowNormal = 0,
                 ShowSubmitted = 0
             },
             SearchForums = "",
             SearchUsers = new SearchUsers()
             {
                 Selected = 0
             }
         }
     };
     return target;
 }
示例#5
0
        /// <summary>
        /// Creates the article search from db
        /// </summary>
        /// <param name="readerCreator"></param>
        /// <returns></returns>
        public static Search CreateUserSearchFromDatabase(IDnaDataReaderCreator readerCreator, int siteId, string searchString, string searchType, bool allowEmails, int startIndex, int itemsPerPage)
        {
            Search search = null;
            int count = 0;

            string query = GenerateANDSearchQuery(searchString);

            // fetch all the lovely intellectual property from the database
            using (IDnaDataReader reader = readerCreator.CreateDnaDataReader("SearchUsersByNameOrEmail"))
            {
                reader.AddParameter("NameOrEmail", query);

                if (allowEmails)
                {
                    reader.AddParameter("searchemails", 1);
                }
                else
                {
                    reader.AddParameter("searchemails", 0);
                }

                reader.AddParameter("SiteID", siteId);
                reader.AddParameter("skip", startIndex);
                reader.AddParameter("show", itemsPerPage);

                reader.Execute();

                // Make sure we got something back
                if (!reader.HasRows || !reader.Read())
                {
                    throw ApiException.GetError(ErrorType.NoResults);
                }
                else
                {
                    search = new Search();
                    search.SearchResults = new SearchResults();
                    search.SearchResults.SearchTerm = searchString;
                    search.SearchResults.Type = searchType;
                    search.SearchResults.StartIndex = startIndex;
                    search.SearchResults.ItemsPerPage = itemsPerPage;
                    search.SearchResults.Total = reader.GetInt32NullAsZero("Total") ;

                    search.SearchResults.UserResults = new List<UserElement>();
                    do
                    {   UserElement user = new UserElement() { user = User.CreateUserFromReader(reader)} ;
                        search.SearchResults.UserResults.Add(user);
                        count++;

                    } while (reader.Read());
                }
                search.SearchResults.Count = count;
            }
            return search;
        }
示例#6
0
        /// <summary>
        /// Creates the article search from db
        /// </summary>
        /// <param name="readerCreator"></param>
        /// <returns></returns>
        public static Search CreateArticleSearchFromDatabase(IDnaDataReaderCreator readerCreator, 
                                                            int siteId, 
                                                            string searchString, 
                                                            string searchType, 
                                                            bool showApproved, 
                                                            bool showNormal, 
                                                            bool showSubmitted, 
                                                            int startIndex, 
                                                            int itemsPerPage,
                                                            bool useFastSearch)
        {
            Search search = null;
            int count = 0;

            string query = GenerateANDSearchQuery(searchString);

            string searchSP = "searcharticlesadvanced";
            if (useFastSearch)
            {
                searchSP = "searcharticlesfast";
            }


            // fetch all the lovely intellectual property from the database
            using (IDnaDataReader reader = readerCreator.CreateDnaDataReader(searchSP))
            {
                if (useFastSearch)
                {
                    reader.AddParameter("condition", query);
                    //Cap max results to 200 and then allow skip and show within that range
                    //reader.AddParameter("top", startIndex + itemsPerPage);
                    reader.AddParameter("top", 200);
                }
                else
                {
                    reader.AddParameter("subjectcondition", query);
                    reader.AddParameter("bodycondition", query);
                }

                reader.AddParameter("shownormal", Convert.ToInt32(showNormal));
                reader.AddParameter("showsubmitted", Convert.ToInt32(showSubmitted));
                reader.AddParameter("showapproved", Convert.ToInt32(showApproved));

                reader.AddParameter("primarysite", siteId);
                //Cap max results to 200 and then allow skip and show within that range
                reader.AddParameter("maxresults", 200);

                reader.Execute();

                // Make sure we got something back
                if (!reader.HasRows || !reader.Read())
                {
                    throw ApiException.GetError(ErrorType.NoResults);
                }
                else
                {
                    if (startIndex > 0)
                    {
                        for (int i = 1; i < startIndex; i++)
                        {
                            reader.Read();
                        }
                    }

                    search = new Search();
                    search.SearchResults = new SearchResults();
                    search.SearchResults.SearchTerm = HtmlUtils.HtmlDecode(searchString);
                    search.SearchResults.Type = searchType;
                    search.SearchResults.StartIndex = startIndex;
                    search.SearchResults.ItemsPerPage = itemsPerPage;

                    search.SearchResults.ArticleResults = new List<ArticleResult>();
                    do
                    {                      
                        ArticleResult articleResult = new ArticleResult();

                        articleResult.EntryId = reader.GetInt32NullAsZero("entryid");
                        articleResult.Status = reader.GetInt32NullAsZero("status");
                        articleResult.Type = reader.GetInt32NullAsZero("type");

                        articleResult.Subject = reader.GetStringNullAsEmpty("subject");
                        articleResult.H2G2Id = reader.GetInt32NullAsZero("h2g2id");

                        DateTime dateCreated = DateTime.MinValue;
                        bool existsDateCreated = !reader.IsDBNull("datecreated");
                        if (existsDateCreated)
                        {
                            dateCreated = reader.GetDateTime("datecreated");
                        }
                        articleResult.DateCreated = new DateElement(dateCreated);

                        DateTime dateLastUpdated = DateTime.MinValue;
                        bool existsLastUpdated = !reader.IsDBNull("lastupdated");
                        if (existsLastUpdated)
                        {
                            dateLastUpdated = reader.GetDateTime("lastupdated");
                        }
                        articleResult.LastUpdated = new DateElement(dateLastUpdated);

                        int scorePercent = 0;
                        scorePercent = Convert.ToInt32(reader.GetDouble(reader.GetOrdinal("score")) * 100.0);
                        //scorePercent = reader.GetInt32NullAsZero("score");
                        articleResult.Score = scorePercent;

                        articleResult.SiteId = reader.GetInt32NullAsZero("siteid");
                        articleResult.PrimarySite = (siteId == reader.GetInt32NullAsZero("siteid") ? 1 : 0);

                        articleResult.ExtraInfo = new ExtraInfoCreator();

                        //Only add primary site articles returned for now
                        if (articleResult.PrimarySite == 1)
                        {
                            search.SearchResults.ArticleResults.Add(articleResult);
                            count++;
                        }

                    } while (reader.Read());
                }
                search.SearchResults.Count = count;
            }
            return search;
        }