/*
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.Linq;
using System.Text;
using System.ComponentModel;
using Everest.Library;
using Everest.Search.Convertor;
using Everest.Search.Configuration;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Index;
namespace Everest.Search{
/// <summary>
///
/// </summary>
internal class ConfigurationSerivce
{
/// <summary>
/// Gets or sets the directory.
/// </summary>
/// <value>The directory.</value>
internal string Directory
{
get;
private set;
}
/// <summary>
/// config analyzer in the configuration.
/// </summary>
/// <value>The analyzer.</value>
internal Analyzer Analyzer
{
get;
private set;
}
internal string PrimaryKey
{
get;
private set;
}
/// <summary>
///
/// </summary>
DocumentTypeConfiguration documentTypeConfig;
Type type;
IDocumentConvertor defaultConvertor;
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationSerivce"/> class.
/// </summary>
/// <param name="type">The type.</param>
internal ConfigurationSerivce(Type type, string directory, IDocumentConvertor defaultConvertor)
{
Directory = directory;
this.type = type;
this.defaultConvertor = defaultConvertor;
Analyzer = new StandardAnalyzer();
SearchConfiguration configuration = SearchConfiguration.Open();
if (configuration != null)
{
if (string.IsNullOrEmpty(Directory))
{
Directory = configuration.Directory;
}
if (configuration.Types.ContainsKey(type))
{
documentTypeConfig = configuration.Types[type];
if (!string.IsNullOrEmpty(documentTypeConfig.Directory))
{
Directory = documentTypeConfig.Directory;
}
PrimaryKey = documentTypeConfig.PrimaryField;
if (documentTypeConfig.Analyzer != null)
{
this.Analyzer = documentTypeConfig.Analyzer;
}
}
}
Directory = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Directory);
}
/// <summary>
/// Gets the convertor.
/// </summary>
/// <returns></returns>
internal IDocumentConvertor GetConvertor()
{
if (documentTypeConfig != null && documentTypeConfig.Fields != null)
{
return new ConfigurationConvertor(documentTypeConfig.Fields, documentTypeConfig.ClassType);
}
else
{
if (defaultConvertor != null)
{
return defaultConvertor;
}
return new StandardConvertor(type);
}
}
/// <summary>
/// Parses the query.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="searchOptions">The search options.</param>
/// <returns></returns>
internal Query ParseQuery(string key, params SearchOption[] searchOptions)
{
BooleanQuery booleanQuery = new BooleanQuery();
if (!string.IsNullOrEmpty(key) && documentTypeConfig != null && documentTypeConfig.QueryFields != null)
{
BooleanQuery keyQuery = new BooleanQuery();
foreach (QueryField field in documentTypeConfig.QueryFields)
{
QueryParser parser = new QueryParser(field.Name, Analyzer);
keyQuery.Add(parser.Parse(key), field.Occur);
}
booleanQuery.Add(keyQuery, BooleanClause.Occur.MUST);
}
if (searchOptions != null && searchOptions.Length != 0)
{
foreach (var option in searchOptions)
{
QueryParser parser = new QueryParser(option.FieldName, Analyzer);
booleanQuery.Add(parser.Parse(option.FieldValue), GetOccur(option.Occur));
}
return booleanQuery;
}
//PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(type);
//foreach (PropertyDescriptor p in properties)
//{
// QueryParser parser = new QueryParser(p.Name, Analyzer);
// booleanQuery.Add(parser.Parse(key), BooleanClause.Occur.SHOULD);
//}
return booleanQuery;
}
/// <summary>
/// Gets the occur.
/// </summary>
/// <param name="occur">The occur.</param>
/// <returns></returns>
private BooleanClause.Occur GetOccur(Occur occur)
{
switch (occur)
{
case Occur.MUST:
return BooleanClause.Occur.MUST;
case Occur.MUST_NOT:
return BooleanClause.Occur.MUST_NOT;
case Occur.SHOULD:
return BooleanClause.Occur.SHOULD;
default:
return BooleanClause.Occur.SHOULD;
}
}
}
}
|