/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Everest.Search.Convertor;
using Lucene.Net.Documents;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.Models;
using Lucene.Net.Highlight;
using Lucene.Net.Analysis;
using System.IO;
namespace Everest.CmsServices.Search{
public class ContentConvertor : IDocumentConvertor
{
private Analyzer analyzer;
private Highlighter highlighter;
#region IDocumentConvertor Members
public Document ToDocument(object o)
{
SearchedContent searchedContent = (SearchedContent)o;
Document document = new Document();
document.Add(new Field("Title", searchedContent.Title, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES));
document.Add(new Field("ContentId", searchedContent.ContentId.ToString(), Field.Store.YES, Field.Index.NO));
document.Add(new Field("ContentUUID", searchedContent.ContentUUID.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
document.Add(new Field("UserKey", searchedContent.UserKey, Field.Store.YES, Field.Index.NO));
document.Add(new Field("Application", searchedContent.Application, Field.Store.YES, Field.Index.UN_TOKENIZED));
document.Add(new Field("SchemaUUID", searchedContent.SchemaUUID.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
document.Add(new Field("FolderUUID", searchedContent.FolderUUID.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
document.Add(new Field("OriginalFolderUUID", searchedContent.OriginalFolderUUID.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
document.Add(new Field("UserName", searchedContent.UserName, Field.Store.YES, Field.Index.NO));
document.Add(new Field("BaseUUID", (searchedContent.BaseUUID.HasValue ? searchedContent.BaseUUID : Guid.Empty).ToString(), Field.Store.YES, Field.Index.NO));
document.Add(new Field("OriginalUUID", searchedContent.OriginalUUID.ToString(), Field.Store.YES, Field.Index.NO));
document.Add(new Field("PostDate", searchedContent.PostDate.ToString(), Field.Store.YES, Field.Index.NO));
document.Add(new Field("ModifiedDate", searchedContent.ModifiedDate == null ? "" : searchedContent.ModifiedDate.ToString(), Field.Store.YES, Field.Index.NO));
document.Add(new Field("Categories", searchedContent.Categories, Field.Store.NO, Field.Index.TOKENIZED));
document.Add(new Field("Applications", searchedContent.Applications, Field.Store.NO, Field.Index.TOKENIZED));
document.Add(new Field("IndexedBody", searchedContent.IndexedBody, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES));
//foreach (var item in searchedContent.ExternalFields)
//{
// document.Add(new Field(string.Format("E_{0}", item.Key), item.Value == DBNull.Value ? "" : item.Value.ToString(), Field.Store.YES, Field.Index.NO));
//}
return document;
}
public object ToObject(Document document)
{
SearchedContent searchedContent = new SearchedContent();
searchedContent.Title = document.Get("Title");
searchedContent.ContentId = int.Parse(document.Get("ContentId"));
searchedContent.ContentUUID = new Guid(document.Get("ContentUUID"));
searchedContent.UserKey = document.Get("UserKey");
searchedContent.Application = document.Get("Application");
searchedContent.SchemaUUID = new Guid(document.Get("SchemaUUID"));
searchedContent.FolderUUID = new Guid(document.Get("FolderUUID"));
searchedContent.OriginalFolderUUID = new Guid(document.Get("OriginalFolderUUID"));
searchedContent.UserName = document.Get("UserName");
searchedContent.BaseUUID = new Guid(document.Get("BaseUUID"));
searchedContent.OriginalUUID = new Guid(document.Get("OriginalUUID"));
searchedContent.PostDate = DateTime.Parse(document.Get("PostDate"));
if (!StringExtensions.IsNullOrEmptyTrim(document.Get("ModifiedDate")))
searchedContent.ModifiedDate = DateTime.Parse(document.Get("ModifiedDate"));
searchedContent.IndexedBody = document.Get("IndexedBody");
searchedContent.HighlightedTitle = highlighter.GetBestFragments(analyzer.TokenStream("Title"
, new StringReader(searchedContent.Title)), searchedContent.Title, 5, "...");
if (StringExtensions.IsNullOrEmptyTrim(searchedContent.HighlightedTitle))
{
searchedContent.HighlightedTitle = searchedContent.Title;
}
searchedContent.HighlightedBody = highlighter.GetBestFragments(analyzer.TokenStream("IndexedBody"
, new StringReader(searchedContent.IndexedBody)), searchedContent.IndexedBody, 5, "...");
if (StringExtensions.IsNullOrEmptyTrim(searchedContent.HighlightedBody))
{
searchedContent.HighlightedBody = searchedContent.IndexedBody.Truncate(100);
if (searchedContent.IndexedBody.Length >100)
{
searchedContent.HighlightedBody += "...";
}
}
//IEnumerator fieldEnumerator = document.Fields();
//while (fieldEnumerator.MoveNext())
//{
// var field = (Field)fieldEnumerator.Current;
// if (field.Name().StartsWith("E_"))
// {
// searchedContent.ExternalFields.Add(field.Name().Substring(2), field.StringValue());
// }
//}
return searchedContent;
}
#endregion
#region IDocumentConvertor Members
public void SetAnalyzer(Analyzer analyzer)
{
this.analyzer = analyzer;
}
public void SetHighlighter(Highlighter highlighter)
{
this.highlighter = highlighter;
}
#endregion
}
}
|