/*
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 Everest.Search;
using Everest.CmsServices.MvcHelper;
using Everest.CmsServices.Models;
using Everest.Library;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Search{
/// <summary>
///
/// </summary>
public class SearchService
{
CmsContext cmsContext;
public SearchService(CmsContext cmsContext)
{
this.cmsContext = cmsContext;
}
public SearchedContent[] Search(string key, int pageIndex, int pageSize, out int totalCount)
{
return Search(key, "", false, pageIndex, pageSize, out totalCount);
}
public SearchedContent[] Search(string key, string category, bool searchBaseApps, int pageIndex, int pageSize, out int totalCount)
{
if (key != null)
{
key = key.Trim();
}
List<SearchOption> searchOptions = new List<SearchOption>();
if (!StringExtensions.IsNullOrEmptyTrim(category))
{
searchOptions.Add(new SearchOption() { FieldName = "Categories", FieldValue = category, Occur = Occur.MUST });
}
string searchApp = cmsContext.Cms_Application.ApplicationName;
if (searchBaseApps)
{
searchApp = string.Join(" OR ", CachedData.GetBaseApplications(cmsContext.Cms_Application.ApplicationName).ToArray());
}
searchOptions.Add(new SearchOption() { FieldName = "Applications", FieldValue = searchApp, Occur = Occur.MUST });
IEnumerable<SearchSetting> searchSettings = CachedData.GetSearchSettings(cmsContext.Cms_Application.ApplicationName);
if (searchSettings.Count() == 0)
{
totalCount = 0;
return new SearchedContent[0];
}
string searchFolders = string.Join(" OR ", searchSettings.Select(s => s.OriginalFolderUUID.ToString()).ToArray());
searchOptions.Add(new SearchOption() { FieldName = "OriginalFolderUUID", FieldValue = searchFolders, Occur = Occur.MUST });
int start = (pageIndex - 1) * pageSize + 1;
LuceneSearcher searcher = new LuceneSearcher();
var items = searcher.Search<SearchedContent>(key, start, pageSize, out totalCount, searchOptions.ToArray());
SetUrl(items, searchSettings);
return items.ToArray();
}
private void SetUrl(IEnumerable<SearchedContent> items, IEnumerable<SearchSetting> searchSettings)
{
foreach (var item in items)
{
var searchSetting = searchSettings.Where(s => s.OriginalFolderUUID == item.OriginalFolderUUID).First();
if (searchSetting != null && !StringExtensions.IsNullOrEmptyTrim(searchSetting.Url))
{
var url = searchSetting.Url;
if (url.StartsWith("/"))
{
url = url.Substring(1);
}
item.Url = cmsContext.UrlHelper.GenerateContextPageUrl("", string.Format(url, item.ContentId, item.UserKey, item.ContentUUID, item.Application, item.Title));
}
}
}
}
}
|