public ActionResult Result(LibrarySearchOption option)
 {
     option.PageSize = 50;
     ViewData["Keyword"] = option.Keyword;
     ViewData["Page"] = option.Page;
     return View();
 }
示例#2
0
 bool turnPage(LibrarySearchOption option, int targetPage, out string errMsg)
 {
     errMsg = null;
     if (option.PageCount == -1)
     {
         errMsg = "上次查询结果正在构建中,请发送.r重试";
         return(false);
     }
     else
     {
         if (targetPage < 1)
         {
             errMsg = "前面没有结果了";
             return(false);
         }
         else if (targetPage > option.PageCount)
         {
             errMsg = "后面没有结果了";
             return(false);
         }
         else
         {
             option.Page = targetPage;
             return(true);
         }
     }
 }
示例#3
0
        private string getSearchUrl(LibrarySearchOption option)
        {
            var encodedKeyword = HttpUtility.UrlEncode(option.Keyword, System.Text.Encoding.GetEncoding("GB2312"));
            var url            = String.Format("http://222.200.98.171:81/searchresult.aspx?dt=0&sf=M_PUB_YEAR&ob=DESC&sm=table&anywords={0}&dept={1}&cl={2}&page={3}&dp={4}",
                                               encodedKeyword, option.DeptPlace, option.Language, option.Page, option.PageSize);

            return(url);
        }
示例#4
0
 private void openNewSearch(string user, string msg, out LibrarySearchOption option)
 {
     option = new LibrarySearchOption
     {
         User    = user,
         Keyword = msg,
         Page    = 1
     };
     mCache[user] = option;
 }
示例#5
0
        public bool OnMessage(string user, string msg, out LibrarySearchOption option, out string errMsg)
        {
            errMsg = null;
            option = null;
            int  quickPage;
            bool isQuickPageMsg = isQuickPage(msg, out quickPage);

            mCache.TryGetValue(user, out option);
            if (option == null)
            {
                if (msg == ".r" || msg == ".n" || msg == ".p" || isQuickPageMsg)
                {
                    errMsg = "没有相应的记录,请重新发送关键字搜索";
                    return(false);
                }
                else
                {
                    openNewSearch(user, msg, out option);
                    return(true);
                }
            }
            else
            {
                int targePage = -1;
                if (msg == ".r")
                {
                    return(true);
                }
                else if (msg == ".n")
                {
                    targePage = option.Page + 1;
                }
                else if (msg == ".p")
                {
                    targePage = option.Page - 1;
                }
                else if (isQuickPageMsg)
                {
                    targePage = quickPage;
                }
                else
                {
                    openNewSearch(user, msg, out option);
                    return(true);
                }
                return(turnPage(option, targePage, out errMsg));
            }
        }
示例#6
0
 public bool OnMessage(string user, string msg, out LibrarySearchOption option, out string errMsg)
 {
     errMsg = null;
     option = null;
     int quickPage;
     bool isQuickPageMsg = isQuickPage(msg, out quickPage);
     mCache.TryGetValue(user, out option);
     if (option == null)
     {
         if (msg == ".r" || msg == ".n" || msg == ".p" || isQuickPageMsg)
         {
             errMsg = "没有相应的记录,请重新发送关键字搜索";
             return false;
         }
         else
         {
             openNewSearch(user, msg, out option);
             return true;
         }
     }
     else
     {
         int targePage = -1;
         if (msg == ".r")
         {
             return true;
         }
         else if (msg == ".n")
         {
             targePage = option.Page + 1;
         }
         else if (msg == ".p")
         {
             targePage = option.Page - 1;
         }
         else if (isQuickPageMsg)
         {
             targePage = quickPage;
         }
         else
         {
             openNewSearch(user, msg, out option);
             return true;
         }
         return turnPage(option, targePage, out errMsg);
     }
 }
示例#7
0
        public bool Search(LibrarySearchOption option, out LibrarySearchResult result)
        {
            /*
             #if DEBUG
             * result = getLocalResult();
             * result.ResultCount = expectedCount;
             * if (pageIndex < expectedCount / option.PageSize)
             * {
             *      pageIndex++;
             *      result.Books = result.Books.GetRange(0, option.PageSize);
             * }
             * else
             * {
             *      result.Books = result.Books.GetRange(0, expectedCount - pageIndex * option.PageSize);
             * }
             * return true;
             #endif
             */
            List <Book> books       = null;
            int         pageCount   = 0;
            int         resultCount = 0;
            var         url         = getSearchUrl(option);

            try
            {
                var    request = WebRequest.Create(url) as HttpWebRequest;
                Stream stream  = (request.GetResponse() as HttpWebResponse).GetResponseStream();
                Parse(stream, out books, out pageCount, out resultCount);
                result = new LibrarySearchResult
                {
                    Books       = books,
                    PageCount   = pageCount,
                    ResultCount = resultCount,
                    Error       = null
                };
                return(true);
            }
            catch (WebException e)
            {
                result = new LibrarySearchResult
                {
                    Error = e
                };
                return(false);
            }
        }
示例#8
0
 public string GetRspForSearch(HttpSessionStateBase session, HttpRequestBase request, LibrarySearchOption option)
 {
     WeixinResponse rsp = null;
     object error;
     var libStopwatch = Stopwatch.StartNew();
     var result = Library.GetInstance().SearchBooksFor(session, option, out error);
     if (error == null)
     {
         libStopwatch.Stop();
         rsp = LibrarySearchResponse.Create(request, result);
         ApplicationLogger.GetLogger().Info(String.Format("(" + session.SessionID  + ")"
             + "search library with " + option.Keyword + " consume " + libStopwatch.ElapsedMilliseconds));
     }
     else
     {
         rsp = new TextResponse(option.User, String.Format("查询出错: {0}", error));
     }
     return rsp.ToString();
 }
示例#9
0
        public LibrarySearchResultRecord SearchBooksFor(HttpSessionStateBase session, LibrarySearchOption option, out object error)
        {
            error = null;
            var user    = option.User;
            var keyword = option.Keyword;
            var page    = option.Page;
            LibrarySearchResultRecord cached;

            if ((cached = mResultCache.Try2Hit(keyword, page)) != null)
            {
                ApplicationLogger.GetLogger().Info("(" + session.SessionID + ")" + keyword + " " + page + " hited");
                option.PageCount = cached.PageCount;
                return(cached);
            }
            option = option == null ? DEFAULT : option;
            LibrarySearchResult result;

            if (Search(option, out result))
            {
                cached = new LibrarySearchResultRecord
                {
                    Keyword     = keyword,
                    User        = user,
                    Books       = result.Books,
                    PageCount   = result.PageCount,
                    CurrentPage = option.Page
                };
                //记录查询结果的总页数
                option.PageCount = result.PageCount;
                mResultCache.Push(cached);
                ApplicationLogger.GetLogger().Info("(" + session.SessionID + ")" +
                                                   "push cache: " + keyword + " " + page + " current cache count: " + mResultCache.Count);
                return(cached);
            }
            else
            {
                return(null);
            }
        }
示例#10
0
        public string GetRspForSearch(HttpSessionStateBase session, HttpRequestBase request, LibrarySearchOption option)
        {
            WeixinResponse rsp = null;
            object         error;
            var            libStopwatch = Stopwatch.StartNew();
            var            result       = Library.GetInstance().SearchBooksFor(session, option, out error);

            if (error == null)
            {
                libStopwatch.Stop();
                rsp = LibrarySearchResponse.Create(request, result);
                ApplicationLogger.GetLogger().Info(String.Format("(" + session.SessionID + ")"
                                                                 + "search library with " + option.Keyword + " consume " + libStopwatch.ElapsedMilliseconds));
            }
            else
            {
                rsp = new TextResponse(option.User, String.Format("查询出错: {0}", error));
            }
            return(rsp.ToString());
        }
示例#11
0
 private void openNewSearch(string user, string msg, out LibrarySearchOption option)
 {
     option = new LibrarySearchOption
     {
         User = user,
         Keyword = msg,
         Page = 1
     };
     mCache[user] = option;
 }
示例#12
0
 bool turnPage(LibrarySearchOption option, int targetPage, out string errMsg)
 {
     errMsg = null;
     if (option.PageCount == -1)
     {
         errMsg = "上次查询结果正在构建中,请发送.r重试";
         return false;
     }
     else
     {
         if (targetPage < 1)
         {
             errMsg = "前面没有结果了";
             return false;
         }
         else if (targetPage > option.PageCount)
         {
             errMsg = "后面没有结果了";
             return false;
         }
         else
         {
             option.Page = targetPage;
             return true;
         }
     }
 }
示例#13
0
 private string getSearchUrl(LibrarySearchOption option)
 {
     var encodedKeyword = HttpUtility.UrlEncode(option.Keyword, System.Text.Encoding.GetEncoding("GB2312"));
     var url = String.Format("http://222.200.98.171:81/searchresult.aspx?dt=0&sf=M_PUB_YEAR&ob=DESC&sm=table&anywords={0}&dept={1}&cl={2}&page={3}&dp={4}",
         encodedKeyword, option.DeptPlace, option.Language, option.Page, option.PageSize);
     return url;
 }
示例#14
0
 public LibrarySearchResultRecord SearchBooksFor(HttpSessionStateBase session, LibrarySearchOption option, out object error)
 {
     error = null;
     var user = option.User;
     var keyword = option.Keyword;
     var page = option.Page;
     LibrarySearchResultRecord cached;
     if ((cached = mResultCache.Try2Hit(keyword, page)) != null)
     {
         ApplicationLogger.GetLogger().Info("(" + session.SessionID + ")" + keyword + " " + page + " hited");
         option.PageCount = cached.PageCount;
         return cached;
     }
     option = option == null ? DEFAULT : option;
     LibrarySearchResult result;
     if (Search(option, out result))
     {
         cached = new LibrarySearchResultRecord
                     {
                         Keyword = keyword,
                         User = user,
                         Books = result.Books,
                         PageCount = result.PageCount,
                         CurrentPage = option.Page
                     };
         //记录查询结果的总页数
         option.PageCount = result.PageCount;
         mResultCache.Push(cached);
         ApplicationLogger.GetLogger().Info("(" + session.SessionID + ")" +
             "push cache: " + keyword + " " + page + " current cache count: " + mResultCache.Count);
         return cached;
     }
     else
     {
         return null;
     }
 }
示例#15
0
 public bool Search(LibrarySearchOption option, out LibrarySearchResult result)
 {
     /*
     #if DEBUG
     result = getLocalResult();
     result.ResultCount = expectedCount;
     if (pageIndex < expectedCount / option.PageSize)
     {
         pageIndex++;
         result.Books = result.Books.GetRange(0, option.PageSize);
     }
     else
     {
         result.Books = result.Books.GetRange(0, expectedCount - pageIndex * option.PageSize);
     }
     return true;
     #endif
     */
     List<Book> books = null;
     int pageCount = 0;
     int resultCount = 0;
     var url = getSearchUrl(option);
     try
     {
         var request = WebRequest.Create(url) as HttpWebRequest;
         Stream stream = (request.GetResponse() as HttpWebResponse).GetResponseStream();
         Parse(stream, out books, out pageCount, out resultCount);
         result = new LibrarySearchResult
         {
             Books = books,
             PageCount = pageCount,
             ResultCount = resultCount,
             Error = null
         };
         return true;
     }
     catch(WebException e)
     {
         result = new LibrarySearchResult
         {
             Error = e
         };
         return false;
     }
 }
示例#16
0
 public LibrarySearchResult SearchBooksFor(HttpSessionStateBase session, LibrarySearchOption option, out object error)
 {
     error = null;
     var user = option.User;
     var keyword = option.Keyword;
     var page = option.Page;
     LibrarySearchResult cached;
     if ((cached = mResultCache.Try2Hit(keyword, page)) != null)
     {
         ApplicationLogger.GetLogger().Info("(" + session.SessionID + ")" + keyword + " " + page + " hited");
         option.PageCount = cached.PageCount;
         return cached;
     }
     option = option == null ? DEFAULT : option;
     var url = getSearchUrl(keyword, option);
     try
     {
         var request = WebRequest.Create(url) as HttpWebRequest;
         Stream stream = (request.GetResponse() as HttpWebResponse).GetResponseStream();
         List<Book> books = null;
         int pageCount = 0;
         Parse(stream, out books, out pageCount);
         cached = new LibrarySearchResult
                     {
                         Keyword = keyword,
                         User = user,
                         Books = books,
                         MoreUrl = url,
                         PageCount = pageCount,
                         CurrentPage = option.Page
                     };
         //记录查询结果的总页数
         option.PageCount = pageCount;
         mResultCache.Push(cached);
         ApplicationLogger.GetLogger().Info("(" + session.SessionID + ")" +
             "push cache: " + keyword + " " + page + " current cache count: " + mResultCache.Count);
         return cached;
     }
     catch (WebException e)
     {
         error = e;
         return null;
     }
 }