/*
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.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Everest.Search.Configuration;
using NBear.Mapping;
using Lucene.Net.Documents;
namespace Everest.Search.Convertor{
internal class ConfigurationConvertor : IDocumentConvertor
{
/// <summary>
///
/// </summary>
Fields config;
Type classType;
IDocumentConvertor convertor;
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationConvertor"/> class.
/// </summary>
/// <param name="config">The config.</param>
public ConfigurationConvertor(Fields fieldsConfig, Type classType)
{
this.config = fieldsConfig;
this.classType = classType;
if (fieldsConfig.ConvertorType != null)
{
convertor = Activator.CreateInstance(fieldsConfig.ConvertorType) as IDocumentConvertor;
}
}
#region IDocumentConvertor Members
/// <summary>
/// Toes the document.
/// </summary>
/// <param name="o">The o.</param>
/// <returns></returns>
public Lucene.Net.Documents.Document ToDocument(object o)
{
if (convertor != null)
{
return convertor.ToDocument(o);
}
Document document = new Document();
NameValueCollection properties = ObjectConvertor.ToObject<NameValueCollection>(o);
if (config.CompositionFields != null)
{
foreach (CompositionField field in config.CompositionFields)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (string name in field.Properties)
{
stringBuilder.AppendFormat("{0} ", properties[name]);
}
document.Add(new Field(field.Name, stringBuilder.ToString(), Field.Store.NO, field.Index));
}
}
if (config.PropertyFields != null)
{
foreach (PropertyField field in config.PropertyFields.Values)
{
string value = properties[field.Property];
if (string.IsNullOrEmpty(value))
{
value = "";
}
document.Add(new Field(field.Name, value, field.Store, field.Index));
}
}
return document;
}
/// <summary>
/// Toes the object.
/// </summary>
/// <param name="document">The document.</param>
/// <returns></returns>
public object ToObject(Lucene.Net.Documents.Document document)
{
if (convertor != null)
{
return convertor.ToObject(document);
}
NameValueCollection properties = new NameValueCollection();
foreach (Field field in document.GetFields())
{
if (field.IsStored())
{
properties.Add(config.PropertyFields[field.Name()].Property, document.Get(field.Name()));
}
}
object o = ObjectConvertor.ToObject(typeof(NameValueCollection), classType, classType, properties);
return o;
}
#endregion
#region IDocumentConvertor Members
public void SetAnalyzer(Lucene.Net.Analysis.Analyzer analyzer)
{
if (convertor != null)
{
convertor.SetAnalyzer(analyzer);
}
}
public void SetHighlighter(Lucene.Net.Highlight.Highlighter highlighter)
{
if (convertor != null)
{
convertor.SetHighlighter(highlighter);
}
}
#endregion
}
}
|