CourseDataManager.cs :  » Network-Clients » iReaper » IReaper » CourseData » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » iReaper 
iReaper » IReaper » CourseData » CourseDataManager.cs
//
//,,,,,,
//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; }
        }


    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.