//
//,,,,,,
//Dictionary<string,CourseCollection>
using System;
using System.Collections.Generic;
using System.IO;
using IReaper.FileData;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Lucene.Net.Store;
namespace IReaper.CourseData{
public class CourseDataManager
{
#region
private static CourseCollection courses;
private static CourseCollection completedcourses;
private static CourseCollection newcourses;
private static CourseCollection latestCourse;
private static IndexSearcher searcher = null;
private static QueryParser parser = null;
private static readonly TimeSpan OneMonthTimeSpan = TimeSpan.FromDays(30);
static CourseDataManager()
{
InitGlobalData();
CourseXmlParser.OnCourseAdded += new CourseProcessEventHandler(OnCourseAdded);
CourseXmlParser.OnCourseAddedFinished += new EventHandler(OnCourseAddedFinished);
CourseXmlParser.OnBeforeCourseAdded += new EventHandler(BeforeCourseAdded);
}
/// <summary>
///
/// </summary>
public static void InitGlobalData()
{
courses = new CourseCollection(CourseCollectionType.Common);
completedcourses = new CourseCollection(CourseCollectionType.Completed);
newcourses = new CourseCollection(CourseCollectionType.Common);
latestCourse = new CourseCollection(CourseCollectionType.Common);
}
private static void BeforeCourseAdded(object sender, EventArgs e)
{
latestCourse.Clear();
newcourses.Clear();
}
private static void OnCourseAddedFinished(object sender, EventArgs e)
{
courses.SortByTime();
//100
for (int i = 0; i < courses.Count && latestCourse.Count < 100; i++)
{
Course c = courses[i];
if (Course.IsNewCourse(c))
{
continue;
}
latestCourse.Add(c);
}
Core.CoreData[CoreDataType.HasCourse] = true;
// Load Index
SimpleFSDirectory dir = new SimpleFSDirectory(new DirectoryInfo(IReaper.Initializer.AbstractInitTask.AppDataFolder));
searcher = new IndexSearcher(dir, true);
// create parser
parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, "", new Lucene.Net.Analysis.CJK.CJKAnalyzer());
parser.SetDefaultOperator(QueryParser.Operator.AND);
}
public static CourseCollection Search(string query, CourseCollection baseCourses)
{
if (baseCourses == null)
{
return null;
}
// Create buffer
int[] IDs = new int[baseCourses.Count];
int index = 0;
foreach (Course c in baseCourses)
{
IDs[index] = int.Parse(c.Id);
index++;
}
Array.Sort<int>(IDs);
// Do the search
var q = parser.Parse(query);
TopScoreDocCollector collector = TopScoreDocCollector.create(CourseDataManager.AllCourses.Count,false);
searcher.Search(q, collector);
// Use a combined sort to filter result
var docs = collector.TopDocs().scoreDocs;
Array.Sort<ScoreDoc>(docs, new Comparison<ScoreDoc>(delegate(ScoreDoc left, ScoreDoc right)
{
if (left == null && right == null)
{
return 0;
}
if (left == null && right != null)
{
return -1;
}
if (left != null && right == null)
{
return 1;
}
return left.doc - right.doc;
}));
List<int> buffer = new List<int>();
for (int i = 0; i < docs.Length; i++)
{
string strid = searcher.Doc(docs[i].doc).GetField("id").StringValue();
if (string.IsNullOrEmpty(strid))
{
continue;
}
int id = int.Parse(strid);
buffer.Add(id);
}
buffer.Sort();
int iSearch = 0;
int iPos = 0;
List<int> buffer2 = new List<int>();
for(int i = 0;i < buffer.Count;i++)
{
int id = buffer[i];
iSearch = iPos;
bool done = false;
while (!done && iSearch < IDs.Length)
{
if (IDs[iSearch] > id)
{
done = true;
}
else if (IDs[iSearch] == id)
{
buffer2.Add(id);
iPos = iSearch;
done = true;
}
iSearch++;
}
}
var courseDict = CourseDataManager.AllCourses.KeydList;
CourseCollection result = new CourseCollection(baseCourses.Type);
foreach (int id in buffer2)
{
// Get this course
result.Add(courseDict[id.ToString()]);
}
return result;
}
private static void OnCourseAdded(Course course)
{
if (course == null)
{ return; }
//add to all
courses.Add(course);
//add to new
if (course.Time > DateTime.Now && Course.IsNewCourse(course))
{
newcourses.Add(course);
}
//add to latest
}
public static void CheckAndRemoveCourseRoot(Course course)
{
bool ppt = course.PPT == null || course.PPT.LifetimeStatue == LifetimePosition.TaskCreated;
bool code = course.Code == null || course.Code.LifetimeStatue == LifetimePosition.TaskCreated;
bool video = course.Video == null || course.Video.LifetimeStatue == LifetimePosition.TaskCreated;
bool wmv = course.WMV == null || course.WMV.LifetimeStatue == LifetimePosition.TaskCreated;
bool mp3 = course.MP3 == null || course.MP3.LifetimeStatue == LifetimePosition.TaskCreated;
bool mp4 = course.MP4 == null || course.MP4.LifetimeStatue == LifetimePosition.TaskCreated;
if (ppt & code & video & mp3 & mp4 & wmv)
{
string path = Path.Combine(IReaper.Properties.Settings.Default.RootPath, course.Headline);
try
{
System.IO.Directory.Delete(path);
}
catch (IOException)
{ }
}
}
#endregion
/// <summary>
///
/// </summary>
/// <returns></returns>
public static CourseCollection AllCourses
{
get { return courses; }
set { courses = value; }
}
/// <summary>
///
/// </summary>
public static CourseCollection CompletedCourses
{
get { return completedcourses; }
set { completedcourses = value; }
}
/// <summary>
///
/// </summary>
public static CourseCollection NewCourses
{
get { return newcourses; }
set { newcourses = value; }
}
/// <summary>
///
/// </summary>
public static CourseCollection LatestCourses
{
get { return CourseDataManager.latestCourse; }
set { CourseDataManager.latestCourse = value; }
}
}
}
|