/*
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.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using Microsoft.Data.Extensions;
using Everest.Library;
using Everest.Library.Data;
using Everest.Library.Data.Entity;
using Everest.Library.HealthMonitor;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
using Everest.Search;
namespace Everest.CmsServices.Search{
public class IndexService
{
//const string SelectDeleted_SQL = "SELECT distinct ContentUUID FROM Cms_IndexTrigger WHERE [Action]=2";
//const string SelectChanged_SQL = "SELECT TOP 1000 ContentUUID FROM Cms_IndexTrigger WHERE [Action]<>2 ORDER BY ChangedDate DESC";
const string DeleteChanged_SQL = "DELETE FROM Cms_IndexTrigger WHERE {0}";
//const string SelectAllCount = "SELECT COUNT(*) FROM Cms_IndexTrigger";
/// <summary>
/// build index.
/// </summary>
public virtual void Indexing()
{
//could not use IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
IEverestCmsDataContext dataContext = EverestCmsEntities.CreateDataContext(false);
try
{
var indexed = new List<Guid>();
try
{
using (IIndexer index = new LuceneIndexer())
{
index.Open(typeof(SearchedContent), CmsGlobal.IndexDirectory, "ContentUUID", new ContentConvertor(), false);
//delete
var deleteIndex = dataContext.Cms_IndexTrigger.Where(it => it.Action == 2).Select(it => it.ContentUUID).Distinct();
foreach (var item in deleteIndex)
{
try
{
DeleteDocument(dataContext, index, item);
indexed.Add(item);
}
catch (Exception e)
{
HealthMonitoringLogging.LogError(e);
}
}
//update
var textContentService = UnityManager.Resolve<TextContentService>();
var updateIndex = dataContext.Cms_IndexTrigger.Where(it => it.Action != 2).Select(it => it.ContentUUID).Distinct();
foreach (var item in updateIndex)
{
try
{
SearchedContent searchedContent = textContentService.GetContentForIndex(item);
if (searchedContent == null)
{
DeleteDocument(dataContext, index, item);
}
else
{
index.UpdateDocument(searchedContent);
}
indexed.Add(item);
}
catch (Exception e)
{
HealthMonitoringLogging.LogError(e);
}
}
index.Close();
}
}
finally
{
DeleteChanged(dataContext, indexed);
}
}
catch (Exception e)
{
HealthMonitoringLogging.LogError(e);
}
}
private void DeleteDocument(IEverestCmsDataContext dataContext, IIndexer index, Guid contentUUID)
{
index.DeleteDocument(contentUUID.ToString());
}
private void DeleteChanged(IEverestCmsDataContext dataContext, IEnumerable<Guid> contentUUID)
{
StringBuilder stringBuilder = new StringBuilder("1 <> 1");
foreach (var guid in contentUUID)
{
stringBuilder.AppendFormat(" OR ContentUUID = '{0}'", guid);
}
DbCommand dbCommand = dataContext.ObjectContext.CreateStoreCommand(string.Format(DeleteChanged_SQL, stringBuilder.ToString()));
using (dbCommand.Connection.CreateConnectionScope())
{
dbCommand.ExecuteNonQuery();
}
}
}
}
|