/// <summary> /// Imports the user content from V2 /// </summary> /// <param name="userStoriesTable">User Story data</param> /// <param name="userContentTable">User Content data</param> /// <param name="delimiter">Delimiter between data</param> /// <param name="work">The DB access</param> private void ImportAchievementUserStuff( HttpPostedFileBase userStoriesTable, HttpPostedFileBase userContentTable, String delimiter, UnitOfWork work) { // Grab the data List<Dictionary<String, String>> storyData = GetDataFromFile(userStoriesTable, delimiter); if (storyData == null) { ModelState.AddModelError("", "Error with User Story table. Check Debug Output"); return; } List<Dictionary<String, String>> contentData = GetDataFromFile(userContentTable, delimiter); if (contentData == null) { ModelState.AddModelError("", "Error with User Content table. Check Debug Output"); return; } // Go through stories try { work.EntityContext.Configuration.AutoDetectChangesEnabled = false; foreach (Dictionary<String, String> row in storyData) { int oldID = int.Parse(row["userstoryID"]); achievement_user_story story = new achievement_user_story() { date_submitted = DateTime.Parse(row["date_submitted"]), image = row["uc_url"], text = row["uc_text"] }; ImportedEarnable impStory = new ImportedEarnable() { OldID = oldID, UniqueData = row["date_submitted"] }; work.EntityContext.achievement_user_story.Add(story); _userStoryMap.Add(impStory.OldID, impStory); } } finally { work.EntityContext.Configuration.AutoDetectChangesEnabled = true; } // Save, then get new ids work.SaveChanges(); foreach (ImportedEarnable impStory in _userStoryMap.Values) { DateTime submitTime = DateTime.Parse(impStory.UniqueData); impStory.NewID = (from s in work.EntityContext.achievement_user_story where s.date_submitted == submitTime select s.id).FirstOrDefault(); } // Go through content try { work.EntityContext.Configuration.AutoDetectChangesEnabled = false; foreach (Dictionary<String, String> row in contentData) { // Get the user ImportedUser approver = GetImportedUserByOldID(row["approverID"]); if (approver == null) continue; // Make the data entry int oldID = int.Parse(row["usercontentID"]); achievement_user_content content = new achievement_user_content() { submitted_date = DateTime.Parse(row["date_submitted"]), text = row["uc_text"], approved_by_id = approver.NewID, approved_date = DateTime.Parse(row["date_handled"]) }; // content type // 1 - image // 2 - url // 3 - text int contentType = int.Parse(row["typeID"]); switch (contentType) { case 1: // image content.content_type = (int)JPPConstants.UserSubmissionTypes.Image; content.image = row["uc_url"]; break; case 2: // url content.content_type = (int)JPPConstants.UserSubmissionTypes.URL; content.url = row["uc_url"]; break; case 3: // text content.content_type = (int)JPPConstants.UserSubmissionTypes.Text; break; } // Make the map item ImportedEarnable impContent = new ImportedEarnable() { OldID = oldID, UniqueData = row["date_submitted"] }; work.EntityContext.achievement_user_content.Add(content); _userContentMap.Add(impContent.OldID, impContent); } } finally { work.EntityContext.Configuration.AutoDetectChangesEnabled = true; } // Save, then get new ids work.SaveChanges(); foreach (ImportedEarnable impStory in _userContentMap.Values) { DateTime submitTime = DateTime.Parse(impStory.UniqueData); impStory.NewID = (from s in work.EntityContext.achievement_user_content where s.submitted_date == submitTime select s.id).FirstOrDefault(); } }
/// <summary> /// Assigns an achievement with user content associated with it. /// TODO: CHECK THE LOGIC TO MAKE SURE IT ALL WORKS THE WAY IT SHOULD /// </summary> private void AssignContentSubmissionAchievement(int approvedByID, achievement_user_content_pending pendingContent) { //Assign the achievement var test = AssignAchievement(pendingContent.submitted_by_id, pendingContent.achievement_id, approvedByID); //Get the newly assigned achievement achievement_instance newInstance = _dbContext.achievement_instance.SingleOrDefault(ai => ai.user_id == pendingContent.submitted_by_id && ai.achievement_id == pendingContent.achievement_id); //Create the user content to be added achievement_user_content newUserContent = new achievement_user_content() { approved_by_id = approvedByID, approved_date = DateTime.Now, content_type = pendingContent.content_type, image = pendingContent.content_type == (int)JPPConstants.UserSubmissionTypes.Image ? pendingContent.image : null, submitted_date = pendingContent.submitted_date, text = pendingContent.text, url = pendingContent.content_type == (int)JPPConstants.UserSubmissionTypes.URL ? pendingContent.url : null }; achievement_user_story newuserStory = new achievement_user_story() { image = pendingContent.content_type == (int)JPPConstants.UserSubmissionTypes.Image ? pendingContent.image : null, text = pendingContent.text, date_submitted = DateTime.Now }; //Add the new user content to the database _dbContext.achievement_user_content.Add(newUserContent); _dbContext.achievement_user_story.Add(newuserStory); //append the instance to point to the new user content newInstance.has_user_content = true; newInstance.user_content_id = newUserContent.id; newInstance.has_user_story = true; newInstance.user_story_id = newuserStory.id; //Remove the content from the pending list _dbContext.achievement_user_content_pending.Remove(pendingContent); //Save changes Save(); }