/// <summary> /// Save the user's data into the database using the Entity Framework /// </summary> /// <param name="wizard">The wizard that took the user's details</param> protected void PersistUserData(CreateUserWizard wizard) { TextBox FirstNameInput = wizard.CreateUserStep.ContentTemplateContainer.FindControl("FirstName") as TextBox; TextBox LastNameInput = wizard.CreateUserStep.ContentTemplateContainer.FindControl("LastName") as TextBox; using (DMSContext database = new DMSContext()) { // Check whether this user already exists first... // Create a new row if it doesn't exist or update the old one if it does. var existingUser = database.Users.SingleOrDefault( u => u.UserName == wizard.UserName); if (existingUser == null) { User user = new User(); user.UserName = wizard.UserName; user.Email = wizard.Email; user.FirstName = FirstNameInput.Text; user.LastName = LastNameInput.Text; database.Users.Add(user); database.SaveChanges(); } else { existingUser.UserName = wizard.UserName; existingUser.Email = wizard.Email; existingUser.FirstName = FirstNameInput.Text; existingUser.LastName = LastNameInput.Text; database.SaveChanges(); } } }
/// <summary> /// This returns a query that can be used to list documents by a given search term. /// </summary> /// <param name="database">The database context</param> /// <param name="userName">The current username</param> /// <param name="searchTerm">The search term</param> /// <returns>A queryable object containing the data</returns> public static IQueryable<Document> GetDocumentsBySearchTerm(DMSContext database, string userName, string searchTerm) { // TODO: I'd Prefer to use full text search here, if the database supports it! // Unfortunately SQL Azure and LocalDB both don't support this feature. // Instead, for now search based on document name and tags. searchTerm = searchTerm.ToUpperInvariant(); // Construct a query to find all documents that contain the term in the name. IQueryable<Document> documents = (from d in database.Documents.Include("Author") where (((d.Private == false) || (d.Private == true && d.Author.UserName == userName)) && (d.Name.ToUpper().Contains(searchTerm))) select d) .Union( // Construct a query to find all documents that contain the term in a tag. (from d in database.Documents.Include("Author") from l in database.DocumentTagLinks .Where(l => l.Document == d) .DefaultIfEmpty() from t in database.Tags .Where(t => t == l.Tag) .DefaultIfEmpty() where (((d.Private == false) || (d.Private == true && d.Author.UserName == userName)) && (t.TagName.ToUpper().Contains(searchTerm))) select d)); return documents; }
/// <summary> /// This returns a query that can be used to all documents in descending order. /// </summary> /// <param name="database">The database context</param> /// <param name="userName">The current username</param> /// <returns>A queryable object containing the data</returns> public static IQueryable<Document> GetDocumentsInDescendingDateOrder(DMSContext database, string userName) { return database.Documents .Include("Author") .OrderByDescending(d => d.CreatedDate) .Where(d => (d.Private == false) || (d.Private == true && d.Author.UserName == userName)); }
public List<TagResult> GetTags(string searchTerm, int limit) { List<TagResult> results = new List<TagResult>(); using (DMSContext database = new DMSContext()) { foreach (string foundTag in SearchTags.SearchForTags(database, searchTerm, limit)) { TagResult tagResult = new TagResult(); tagResult.Tag = foundTag; results.Add(tagResult); } } return results; }
/// <summary> /// This function is used to return a list of tags that start with a given string /// Used for autocomplete on the search box. /// </summary> /// <param name="database">The database context</param> /// <param name="searchTerm">The search term that we're using</param> /// <param name="limit">The maximum number of tags to return</param> /// <returns>A queryable object containing the tags</returns> public static List<string> SearchForTags(DMSContext database, string searchTerm, int limit) { List<string> results = new List<string>(); var tagList = (from t in database.Tags join l in database.DocumentTagLinks on t equals l.Tag where t.TagName.StartsWith(searchTerm) group l by t.TagName into g select new { Name = g.Key, Sum = g.Sum(l => l.Count), }).Take(limit); foreach (var foundTag in tagList) { results.Add(foundTag.Name); } return results; }
/// <summary> /// Scans a document and extracts its tags in a format suitable for saving to the database. /// </summary> /// <param name="database">The database context</param> /// <param name="fileName">The filename of the document</param> /// <param name="docType">The document type of the document</param> /// <param name="mimeType">The Mime Type of the document</param> /// <returns>A list of tags that were returned, ready to insert into the database.</returns> public static Dictionary<string, DocumentTagLink> ScanDocumentForTags(DMSContext database, string fileName, DocumentType docType, string mimeType) { Dictionary<string, DocumentTagLink> tagsList = null; // Allow scanning of documents for text for search purposes. switch (mimeType) { case "application/vnd.openxmlformats-officedocument.wordprocessingml.document": case "application/vnd.openxmlformats-officedocument.wordprocessingml.template": case "application/vnd.ms-word.document.macroEnabled.12": case "application/vnd.ms-word.template.macroEnabled.12": string plainText = ExtractWordDocumentText(fileName); tagsList = ConvertWordsIntoTags(database, plainText); break; default: tagsList = new Dictionary<string, DocumentTagLink>(); break; } return tagsList; }
/// <summary> /// Given a list of words, this will convert each one into the appropriate Tag /// </summary> /// <param name="database">The database context</param> /// <param name="plainText">The plain text content of the document</param> /// <returns>A list of tags that were returned, ready to insert into the database.</returns> private static Dictionary<string, DocumentTagLink> ConvertWordsIntoTags(DMSContext database, string plainText) { Dictionary<string, DocumentTagLink> tagsList = new Dictionary<string, DocumentTagLink>(); plainText = plainText.ToUpperInvariant(); List<String> words = new List<string>(plainText.Split(null as string[], StringSplitOptions.RemoveEmptyEntries)); CleanUpWords(words); // Loop over the list of words. // For each one, find a matching tag and create a new one if we couldn't. if (words.Count > 0) { foreach (string word in words) { DocumentTagLink documentTagLink = null; if (!tagsList.ContainsKey(word)) { // Link to an existing tag if it exists, or create a new one. Tag tag = database.Tags.SingleOrDefault( t => t.TagName.ToUpper() == word.ToUpper()); if (tag == null) { tag = new Tag(); tag.TagName = word; database.Tags.Add(tag); } documentTagLink = new DocumentTagLink(); documentTagLink.Tag = tag; tagsList.Add(word, documentTagLink); } else { documentTagLink = tagsList[word]; } // Increment the counter variable here. documentTagLink.Count++; } } return tagsList; }
/// <summary> /// This returns a query that can be used to list documents for the current user. /// </summary> /// <param name="database">The database context</param> /// <param name="userName">The current username</param> /// <returns>A queryable object containing the data</returns> public static IQueryable<Document> GetDocumentsForCurrentUser(DMSContext database, string userName) { return database.Documents .Include("Author") .Where(d => d.Author.UserName == userName); }