Esempio n. 1
0
 /// <summary>
 /// get root node ,getfirstpage by add namespace LexisNexis.Red.Common.HelpClass
 /// </summary>
 /// <param name="bookId"></param>
 /// <returns></returns>
 public Task <TOCNode> GetTOCByBookId(int bookId)
 {
     return(Task.Run(async() =>
     {
         await RefreshCurrentPublication(bookId);
         var tocNodes = packageAccess.GetTOC(GlobalAccess.Instance.CurrentPublication.DecryptedDbFullName);
         TOCNode root = new TOCNode();
         root.Role = Constants.ANCESTOR;
         root.Title = "Table of Content";
         root.ChildNodes = tocNodes.FindAll(o => o.ParentId == 0);
         tocNodes.RemoveAll(o => o.ParentId == 0);
         if (root.ChildNodes != null)
         {
             foreach (TOCNode node in root.ChildNodes)
             {
                 publicationContentService.BuildTocNode(node, root, tocNodes);
             }
         }
         return root;
     }));
 }
Esempio n. 2
0
        /// <summary>
        /// Search
        /// </summary>
        /// <param name="bookID"></param>
        /// <param name="TocID"></param>
        /// <param name="keywords"></param>
        /// <param name="contentTypeList"></param>
        /// <returns></returns>
        public static SearchResult Search(int bookID, int TocID, string keywords, List <ContentCategory> contentTypeList = null)
        {
            if (contentTypeList == null)
            {
                contentTypeList = new List <ContentCategory>();
                contentTypeList.Add(ContentCategory.All);
            }

            SearchResult sr = new SearchResult();

            sr.SearchDisplayResultList = new List <SearchDisplayResult>();
            sr.FoundWordList           = new List <string>();
            sr.KeyWordList             = new List <string>();
            List <SearchDisplayResult> beforeRemoveDuplicateResult = new List <SearchDisplayResult>();
            List <SearchDisplayResult> publicationTempResult       = new List <SearchDisplayResult>();
            List <SearchDisplayResult> documentTempResult          = new List <SearchDisplayResult>();
            List <IndexData>           indexDataPubList            = new List <IndexData>();
            List <IndexData>           indexDataDocList            = new List <IndexData>();
            List <String>            keyWordList            = new List <string>();
            List <String>            foundWordList          = new List <string>();
            Dictionary <String, int> ExistedDocIdDictionary = new Dictionary <String, int>();
            String indexPath = publicationAccess.GetDlBookByBookId(bookID, GlobalAccess.Instance.UserCredential).GetIndexDbFullName();

            tocDetailList = packageAccess.GetAllTOCNodeDetails(GlobalAccess.Instance.CurrentPublication.DecryptedDbFullName);
            tocList       = packageAccess.GetTOC(GlobalAccess.Instance.CurrentPublication.DecryptedDbFullName);

            try
            {
                List <string> critieriaStringList = SegmentUtil.Instance.PhraseSegment(keywords);

                if (critieriaStringList.Count > 0)
                {
                    int SearchRound = 0;
                    foreach (String critieriaString in critieriaStringList)
                    {
                        SearchRound++;
                        keyWordList.AddRange(critieriaString.Split(new String[] { Constants.NEAR_SEPERATOR }, StringSplitOptions.RemoveEmptyEntries).ToList());
                        using (var db = new SQLiteConnection(indexPath))
                        {
                            //For Publication query
                            string queryStringPub = String.Empty;

                            //For Document query
                            string queryStringDoc = String.Empty;

                            string DocId = String.Empty;
                            DocId = (from tocD in tocDetailList where tocD.ID == TocID select tocD.DocID).ToList().FirstOrDefault();

                            int snippetNumber = (critieriaStringList.Count) * Constants.MAX_WORD_NUMBER;
                            BuildQueryString(contentTypeList, critieriaString, ref queryStringPub, ref queryStringDoc, DocId, snippetNumber);

                            // Get publication Index Data
                            var tempPublicationDataList = db.Query <IndexData>(queryStringPub);

                            foreach (IndexData element in tempPublicationDataList.ToList <IndexData>())
                            {
                                // get found word llist
                                string foundWordText = element.SnippetContent;
                                foundWordText = foundWordText.Replace("{|", "TargetSeperator|||").Replace("|}", "|||TargetSeperator");

                                var tempfoundWordList = foundWordText.Split(new String[] { "TargetSeperator" }, StringSplitOptions.None).ToList();

                                foreach (string foundWord in tempfoundWordList)
                                {
                                    if (foundWord.Contains("|||") && !foundWordList.Contains(foundWord.Replace("|||", "")))
                                    {
                                        foundWordList.Add(foundWord.Replace("|||", "").Trim());
                                    }
                                }

                                if (ExistedDocIdDictionary.ContainsKey(element.DocId))
                                {
                                    if (ExistedDocIdDictionary[element.DocId] < SearchRound)
                                    {
                                        ExistedDocIdDictionary[element.DocId]++;
                                    }
                                }
                                else
                                {
                                    ExistedDocIdDictionary.Add(element.DocId, 1);
                                }
                            }
                            indexDataPubList = tempPublicationDataList.ToList <IndexData>();
                            // Get document Index Data
                            indexDataDocList = db.Query <IndexData>(queryStringDoc);
                        }

                        if (indexDataPubList.Count > 0)
                        {
                            var result = from r in indexDataPubList
                                         join tocD in tocDetailList
                                         on r.DocId equals tocD.DocID
                                         join toc in tocList
                                         on tocD.ID equals toc.ID
                                         orderby toc.ID
                                         select new SearchDisplayResult
                            {
                                TocId          = tocD.ID,
                                TocTitle       = toc.Title,
                                GuideCardTitle = toc.GuideCardTitle,
                                SnippetContent = r.SnippetContent.Replace("{|", "").Replace("|}", "").Replace("HEADEND", ""),
                                ContentType    = (ContentCategory)Enum.Parse(typeof(ContentCategory), r.ContentType),
                                isDocument     = false,
                                DocId          = r.DocId
                            };
                            publicationTempResult = result.ToList();
                        }

                        if (indexDataDocList.Count > 0 && documentTempResult.Count == 0)
                        {
                            var result = from r in indexDataDocList
                                         select new SearchDisplayResult
                            {
                                Head           = r.Head,
                                SnippetContent = r.SnippetContent.Replace("{|", "").Replace("|}", "").Replace("HEADEND", ""),
                                ContentType    = (ContentCategory)Enum.Parse(typeof(ContentCategory), r.ContentType),
                                TocId          = TocID,
                                isDocument     = true
                            };
                            documentTempResult = result.ToList();
                            List <SearchDisplayResult> beforeGetHeadNoResult = GetHeadSequence(documentTempResult);

                            documentTempResult.Clear();
                            documentTempResult = beforeGetHeadNoResult;
                        }
                        beforeRemoveDuplicateResult.AddRange(publicationTempResult);
                    }
                }

                foreach (string keyword in keyWordList)
                {
                    if (!sr.KeyWordList.Contains(keyword))
                    {
                        sr.KeyWordList.Add(keyword);
                    }

                    if (!foundWordList.Contains(keyword))
                    {
                        foundWordList.Add(keyword);
                    }
                }

                foreach (string foundWord in foundWordList)
                {
                    if (!sr.FoundWordList.Contains(foundWord))
                    {
                        sr.FoundWordList.Add(foundWord);
                    }
                }

                HashSet <string> duplicateDocIdList = new HashSet <string>();
                HashSet <int>    duplicateTocIdList = new HashSet <int>();
                bool             inCurrentDocument  = false;
                foreach (SearchDisplayResult e in beforeRemoveDuplicateResult.OrderBy(b => b.TocId))
                {
                    if (e.TocId == TocID)
                    {
                        inCurrentDocument = true;
                    }

                    if (ExistedDocIdDictionary[e.DocId] == critieriaStringList.Count &&
                        !duplicateDocIdList.Contains(e.DocId) && !duplicateTocIdList.Contains(e.TocId))
                    {
                        sr.SearchDisplayResultList.Add(e);
                    }

                    duplicateTocIdList.Add(e.TocId);
                    duplicateDocIdList.Add(e.DocId);
                }

                if (inCurrentDocument)
                {
                    sr.SearchDisplayResultList.AddRange(documentTempResult);
                }

                sr.SearchDisplayResultList = sr.SearchDisplayResultList.OrderByDescending(e => e.isDocument).ThenBy(e => e.TocId).ToList();

                if (sr.SearchDisplayResultList.Count == 0)
                {
                    sr.FoundWordList.Clear();
                }

                Regex replaceSpanId = new Regex("SPANMARK([0-9]+)");
                Regex replaceSpace  = new Regex(@"\s{2,}", RegexOptions.IgnoreCase);
                foreach (SearchDisplayResult e in sr.SearchDisplayResultList)
                {
                    var match = replaceSpanId.Matches(e.SnippetContent);
                    if (match.Count > 0)
                    {
                        string highlightStartSpanId = match[0].Groups[1].Value;
                        string highlightEndSpanId   = match[match.Count - 1].Groups[1].Value;
                        int    spanId = -1;
                        if (Int32.TryParse(highlightStartSpanId, out spanId))
                        {
                            e.HighlightStartSpanId = spanId - 1;
                        }
                        if (Int32.TryParse(highlightEndSpanId, out spanId))
                        {
                            e.HighlightEndSpanId = spanId;
                        }
                    }

                    if (e.HighlightStartSpanId == e.HighlightEndSpanId && e.HighlightEndSpanId == 0)
                    {
                        e.HighlightStartSpanId = -1;
                        e.HighlightEndSpanId   = -1;
                    }

                    if (!String.IsNullOrEmpty(e.Head))
                    {
                        e.Head = replaceSpace.Replace(replaceSpanId.Replace(e.Head, ""), " ").Trim();
                    }
                    if (!String.IsNullOrEmpty(e.SnippetContent))
                    {
                        e.SnippetContent = replaceSpace.Replace(replaceSpanId.Replace(e.SnippetContent, ""), " ").Trim();
                    }
                }

                return(sr);
            }
            catch (Exception ex)
            {
                Logger.Log("Search Failed : " + ex.Message);
                return(null);
            }
        }