private static void SearchPPT(string[] searchText, Document d)
 {
     try
     {
         string fileContent = String.Empty;
         FileInfo fi = new FileInfo(d.SaveLocation);
         if (fi.Exists)
         {
             int numberOfSlides = CountSlides(d.SaveLocation);
             string slideText;
             for (int i = 0; i < numberOfSlides; i++)
             {
                 fileContent += GetSlideIdAndText(d.SaveLocation, i);
             }
             SearchText(searchText, d, fileContent);
         }
     }
     catch (Exception exception)
     {
         if (exception.Message.Contains("File contains corrupted data"))
             d.FullText = "File Can't Be Scanned.  Corruption Issue.";
         else
             ErrorDatabaseManager.AddException(exception, exception.GetType());
     }
 }
        private static void SearchRTF(string[] searchText, Document d)
        {
            FileInfo fi = new FileInfo(d.SaveLocation);
            if (fi.Exists)
            {
                //Create the RichTextBox. (Requires a reference to System.Windows.Forms.)
                System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();

                // Get the contents of the RTF file. When the contents of the file are   
                // stored in the string (rtfText), the contents are encoded as UTF-16.  
                string rtfText = System.IO.File.ReadAllText(d.SaveLocation);

                // Use the RichTextBox to convert the RTF code to plain text.
                rtBox.Rtf = rtfText;
                string fileContent = rtBox.Text;

                SearchText(searchText, d, fileContent);
            }

        }
        private static void SearchPDF(string[] searchText, Document d)
        {
            try
            {
                string fileContent = String.Empty;
                FileInfo fi = new FileInfo(d.SaveLocation);
                if (fi.Exists)
                {
                    PdfReader reader = new PdfReader(d.SaveLocation);

                    for (int i = 1; i <= reader.NumberOfPages; i++)
                        fileContent += PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy());

                    SearchText(searchText, d, fileContent);
                }
            }
            catch (Exception exception)
            {
                if (exception.Message.Contains("Bad user password"))
                    d.FullText = "Password Protected";
                else
                    ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: d.SaveLocation);
            }
        }
 /// <summary>
 /// searches .xlsx files
 /// </summary>
 /// <param name="searchText"></param>
 /// <param name="d"></param>
 private static void SearchExcel(string[] searchText, Document d)
 {
     try
     {
         FileInfo fi = new FileInfo(d.SaveLocation);
         if (fi.Exists)
         {
             ExcelPackage workbook = new ExcelPackage(fi);
             string fileContent = String.Empty;
             try
             {
                 for (int i = 1; i < workbook.Workbook.Worksheets.Count; i++)
                 {
                     try
                     {
                         foreach (var cell in workbook.Workbook.Worksheets[i].Cells)
                         {
                             fileContent += cell.Value + " ";
                         }
                     }
                     catch (Exception exception)
                     {
                         ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: d.SaveLocation);
                     }
                 }
             }
             catch (Exception exception)
             {
                 ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: d.SaveLocation);
             }
             SearchText(searchText, d, fileContent);
         }
     }
     catch (Exception exception)
     {
         ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: d.SaveLocation);
     }
 }
        /// <summary>
        /// searches .xls files.
        /// </summary>
        /// <param name="searchText"></param>
        /// <param name="d"></param>
        private static void SearchExcelOld(string[] searchText, Document d)
        {
            try
            {

                ExcelLibrary.SpreadSheet.Workbook workbook = ExcelLibrary.SpreadSheet.Workbook.Load(d.SaveLocation);
                string fileContent = String.Empty;
                for (int i = 1; i < workbook.Worksheets.Count; i++)
                {
                    foreach (var cell in workbook.Worksheets[i].Cells)
                    {
                        fileContent += cell.Right.Value + " ";
                    }
                }
                SearchText(searchText, d, fileContent);

            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: d.SaveLocation);
            }
        }
 private static void SearchDocument(string[] searchText, Document d)
 {
     if (new FileInfo(d.SaveLocation).Exists)
     {
         using (StreamReader sr = new StreamReader(d.SaveLocation))
         {
             string fileContent = sr.ReadToEnd();
             SearchText(searchText, d, fileContent);
         }
     }
 }
 private static void SearchText(string[] searchText, Document d, string fileContent)
 {
     d.FullText = fileContent;
     //its been shown that Index Ordinal is faster than boyer-Moore.
     //because of the managed code.
     //http://www.blackbeltcoder.com/Articles/algorithms/fast-text-search-with-boyer-moore
     foreach (var t in searchText)
     {
         int pos = fileContent.IndexOf(t, 0, StringComparison.OrdinalIgnoreCase);
         while (pos != -1)
         {
             d.SearchMatches++;
             pos = fileContent.IndexOf(t, pos + t.Length, StringComparison.OrdinalIgnoreCase);
         }
     }
 }
        public static Document GetDocumentLocation(Guid documentId)
        {
            try
            {
                Document doc = new Document();
                var dc = new ManagementContext();
                var docs = dc.LeagueDocuments.Where(x => x.Document.DocumentId == documentId).FirstOrDefault();

                if (docs != null)
                {
                    return LeagueDocument.DisplayDocument(docs, true);
                }

            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="leagueId"></param>
        /// <param name="fileStream"></param>
        /// <param name="nameOfFile"></param>
        /// <returns>document id of the league</returns>
        public static Document UploadLeagueDocument(Guid leagueId, Stream fileStream, string nameOfFile, string folderName = "", long groupId = 0, long folderId = 0)
        {
            try
            {
                nameOfFile = RDN.Utilities.Strings.StringExt.ToFileNameFriendly(nameOfFile);

                ManagementContext dc = new ManagementContext();
                var league = dc.Leagues.Where(x => x.LeagueId == leagueId).FirstOrDefault();
                //time stamp for the save location
                DateTime timeOfSave = DateTime.UtcNow;
                DataModels.League.Documents.DocumentCategory folderDb = null;
                if (folderName != "")
                {
                    folderDb = dc.LeagueDocumentFolders.Where(x => x.League.LeagueId == leagueId && x.CategoryName == folderName && x.IsRemoved == false).FirstOrDefault();
                    if (folderDb == null)
                    {
                        folderDb = new DataModels.League.Documents.DocumentCategory();
                        folderDb.CategoryName = folderName;
                        folderDb.League = league;
                        dc.LeagueDocumentFolders.Add(folderDb);
                    }
                }
                FileInfo info = new FileInfo(nameOfFile);

                string saveLocation = @"C:\WebSiteDocuments\rdnation\" + timeOfSave.Year + @"\" + timeOfSave.Month + @"\" + timeOfSave.Day + @"\";

                if (!Directory.Exists(saveLocation))
                    Directory.CreateDirectory(saveLocation);

                DataModels.Document.Document doc = new DataModels.Document.Document();
                doc.DocumentSize = (int)fileStream.Length;

                doc.SaveLocation = "WebSiteDocuments";
                dc.Documents.Add(doc);
                int c = dc.SaveChanges();
                doc.SaveLocation = saveLocation + doc.DocumentId.ToString().Replace("-", "") + info.Extension;
                c = dc.SaveChanges();

                DataModels.League.Documents.LeagueDocument docL = new DataModels.League.Documents.LeagueDocument();
                docL.Document = doc;
                docL.League = league;
                if (folderName != "")
                    docL.Category = folderDb;
                docL.Name = info.Name;
                if (groupId > 0)
                    docL.Group = dc.LeagueGroups.Where(x => x.Id == groupId).FirstOrDefault();
                if (folderId > 0)
                    docL.Category = dc.LeagueDocumentFolders.Where(x => x.CategoryId == folderId).FirstOrDefault();
                dc.LeagueDocuments.Add(docL);
                c = dc.SaveChanges();

                using (var newfileStream = new FileStream(doc.SaveLocation, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    fileStream.CopyTo(newfileStream);
                }
                Document newDoc = new Document();
                newDoc.OwnerDocId = docL.DocumentId;
                newDoc.DocumentId = doc.DocumentId;
                newDoc.SaveLocation = doc.SaveLocation;
                return newDoc;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: nameOfFile);
            }
            return null;
        }
        public static bool UpdateFullTextForDocument(Guid documentId, string fullText)
        {
            try
            {
                if (!String.IsNullOrEmpty(fullText))
                {
                    Document doc = new Document();
                    var dc = new ManagementContext();
                    var docs = dc.Documents.Where(x => x.DocumentId == documentId).FirstOrDefault();

                    if (docs != null)
                    {
                        docs.FullText = fullText;
                        if (!String.IsNullOrEmpty(fullText))
                            docs.HasScannedText = true;
                        docs.SaveLocation = docs.SaveLocation;
                        return dc.SaveChanges() > 0;
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }
        public static bool MoveDocumentToGroup(Guid ownerId, long groupId, long docId)
        {
            try
            {
                Document doc = new Document();
                var dc = new ManagementContext();
                var docs = dc.LeagueDocuments.Where(x => x.League.LeagueId == ownerId && x.DocumentId == docId).FirstOrDefault();
                bool isMem = MemberCache.IsMemberApartOfLeague(RDN.Library.Classes.Account.User.GetMemberId(), docs.League.LeagueId);
                if (isMem)
                {
                    if (docs != null)
                    {
                        docs.Document = docs.Document;
                        docs.Group = dc.LeagueGroups.Where(x => x.Id == groupId).FirstOrDefault();
                        dc.Entry(docs).Reference(x => x.Category).CurrentValue = null;
                        int c = dc.SaveChanges();

                        return c > 0;
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }
        public static Document RenameDocument(Guid ownerId, long docId, string documentName)
        {
            try
            {
                Document doc = new Document();
                var dc = new ManagementContext();

                documentName = Path.GetFileNameWithoutExtension(documentName);
                var docs = dc.LeagueDocuments.Where(x => x.League.LeagueId == ownerId && x.DocumentId == docId).FirstOrDefault();
                bool isMem = MemberCache.IsMemberApartOfLeague(RDN.Library.Classes.Account.User.GetMemberId(), docs.League.LeagueId);
                if (isMem)
                {
                    if (docs != null)
                    {
                        docs.Document = docs.Document;
                        FileInfo f = new FileInfo(docs.Name);
                        docs.Name = documentName + f.Extension;
                        int c = dc.SaveChanges();
                        Document d = new Document();
                        d.DocumentId = docs.Document.DocumentId;
                        d.DocumentName = docs.Name;
                        return d;
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return null;
        }
        public static bool RestoreDocument(long docId)
        {
            try
            {
                Document doc = new Document();
                var dc = new ManagementContext();
                var docs = dc.LeagueDocuments.Where(x => x.DocumentId == docId).FirstOrDefault();
                docs.Document = docs.Document;
                docs.Document.FullText = "";
                docs.IsRemoved = false;
                int c = dc.SaveChanges();

                return c > 0;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }
        public static bool DeleteDocument(Guid ownerId, long docId)
        {
            try
            {
                Document doc = new Document();
                var dc = new ManagementContext();
                var docs = dc.LeagueDocuments.Where(x => x.League.LeagueId == ownerId && x.DocumentId == docId).FirstOrDefault();
                bool isMem = MemberCache.IsMemberApartOfLeague(RDN.Library.Classes.Account.User.GetMemberId(), docs.League.LeagueId);
                if (isMem)
                {
                    if (docs != null)
                    {
                        docs.Document = docs.Document;
                        docs.Document.FullText = "";
                        docs.IsRemoved = true;
                        int c = dc.SaveChanges();

                        return c > 0;
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }