public static void SendMailsInTheQueue()
 {
     int i = 0;
     using (var db = new MatchingDB())
     {
         foreach (var m in GetMessages(db))
         {
             EmailService emailService = new EmailService(m.To, m.Subject, m.Body);
             String status = emailService.SendMessage();
             LogMessage(db, m, status);
             if (status == EmailStatus.Success.ToString())
                 db.EmailQueueMessages.Remove(m);
             i++;
         }
         try
         {
             db.SaveChanges();
         }
         catch (Exception ex)
         {
             log.Info("An error has happened while trying to persist the log info after e-mail has been sent and emaillog has been generated but before queue message has been deleted. Exception is:",ex);
         }
     }
     if(i>0)
         log.Info("The number of emails successfully sent: "+i.ToString()+" at "+DateTime.Now.ToString());
 }
 /// <summary>
 /// Drops the message in the email queueing table for Quartz job to pick it up. This is the main method to send templated -emails to project contacts and students.
 /// </summary>
 /// <param name="to">Recipient</param>
 /// <param name="subject">Subject of the email</param>
 /// <param name="body">Body of the email</param>
 public static void QueueMessage(EmailQueueMessage message)
 {
     using (var db = new MatchingDB())
     {
         db.EmailQueueMessages.Add(message);
         db.SaveChanges();
     }
 }
Пример #3
0
 public static void DeleteProjectRejectsReferencingStudent(int studentId)
 {
     using(MatchingDB db = new MatchingDB())
     {
         var projectRejectsToDelete = db.ProjectRejects.Where(pr => pr.Student.Id == studentId).ToList();
         foreach (ProjectReject pr in projectRejectsToDelete)
             db.ProjectRejects.Remove(pr);
         db.SaveChanges();
     }
 }
Пример #4
0
 public static void DeleteStudentFeedbacksReferencingProject(int projectId)
 {
     using (MatchingDB db = new MatchingDB())
     {
         var studentFeedbacksToDelete = db.StudentFeedbacks.Where(sf => sf.Project.Id == projectId).ToList();
         foreach (StudentFeedback sf in studentFeedbacksToDelete)
             db.StudentFeedbacks.Remove(sf);
         db.SaveChanges();
     }
 }
Пример #5
0
        public static Student DeleteStudent(int studentId)
        {
            Student s = null;
            DeleteMatchingsForStudent(studentId);
            using (MatchingDB db = new MatchingDB())
            {
                s = db.Students.Include("EmailLogs").SingleOrDefault(st => st.Id == studentId);

                #region Remove EmailLogs for the project
                IList<EmailLog> emailLogsToBeDeleted = s.EmailLogs.ToList();
                foreach (EmailLog log in emailLogsToBeDeleted)
                    db.EmailLogs.Remove(log);
                db.SaveChanges();
                #endregion

                db.Students.Remove(s);
                db.SaveChanges();
            }
            return s;
        }
Пример #6
0
        public static Project DeleteProject(int projectId)
        {
            Project p = null;
            DeleteMatchingsForProject(projectId);
            StudentService.DeleteStudentFeedbacksReferencingProject(projectId);
            using (MatchingDB db = new MatchingDB())
            {
                p = db.Projects.Include("EmailLogs").SingleOrDefault(pr => pr.Id == projectId);

                #region Remove EmailLogs for the project
                IList<EmailLog> emailLogsToBeDeleted = p.EmailLogs.ToList();
                foreach (EmailLog log in emailLogsToBeDeleted)
                    db.EmailLogs.Remove(log);
                db.SaveChanges();
                #endregion

                db.Projects.Remove(p);
                db.SaveChanges();
            }
            return p;
        }
Пример #7
0
 public static void DeleteStudentFeedbacksReferencingProjectForStudents(int projectId, params int[] studentsRemovedFromProject)
 {
     using (MatchingDB db = new MatchingDB())
     {
        var studentFeedbacksToDelete = db.StudentFeedbacks.Where(sf => sf.Project.Id == projectId && studentsRemovedFromProject.Contains(sf.Student.Id)).ToList();
        foreach (StudentFeedback sf in studentFeedbacksToDelete)
        {
            if (sf != null)// The projection above would insert null into projectRejectsToDelete list for the studentId when the project has not rejected that student.
                db.StudentFeedbacks.Remove(sf);
        }
        db.SaveChanges();
     }
 }
Пример #8
0
        public static void DeleteProjectRejectsReferencingStudentForProjects(int studentId, params int[] projectIds)
        {
            using (MatchingDB db = new MatchingDB())
            {
                var projectRejectsToDelete = db.Projects.Where(p => projectIds.Contains(p.Id)).Select(p => p.ProjectRejects.Where(pr=>pr.Student.Id==studentId).FirstOrDefault());

                foreach (ProjectReject pr in projectRejectsToDelete)
                {
                    if (pr != null) // The projection above would insert null into projectRejectsToDelete list for the studentId when the project has not rejected that student.
                        db.ProjectRejects.Remove(pr);
                }
                db.SaveChanges();
            }
        }
 public ActionResult Create(Student st)
 {
     if (!ValidateStudent(st))
         this.ModelState.AddModelError("UniqueName", uniqueNameErrorMsg);
     if (ModelState.IsValid)
     {
         MatchingDB db = new MatchingDB();
         st.Guid = Guid.NewGuid();
         db.Students.Add(st);
         db.SaveChanges();
         TempData["message"] = "Student \"" + st.FirstName+" "+st.LastName + "\" is added!";
         db.Dispose();
         return RedirectToAction("Index");
     }
     setViewDataForListOfDegrees();
     return View(st);
 }
        public JsonResult SubmitPreferences(ProjectPreferencesDto preferencesDto)
        {
            MatchingDB db = new MatchingDB();
            Project project = db.Projects.Include("Matchings.Student").Include("ProjectRejects.Student").Where(p => p.Id == preferencesDto.ProjectId && p.Guid == new Guid(preferencesDto.ProjectGuid)).FirstOrDefault();

            string message = project == null ? "Authentication failure: Project can not be identified." : "Success";
            var jsonResult = new JsonResult();
            jsonResult.Data = message;

            if (project == null)
                return jsonResult;

            // Update the project scores with the scores coming from UI
            foreach (ProjectScoreDto pSDto in preferencesDto.ProjectPreferences)
            {
                project.Matchings.Where(m => m.Student.Id == pSDto.StudentId).FirstOrDefault().ProjectScore = pSDto.Score;
            }
            // Remove all rejects
            ICollection<ProjectReject> projectRejects = project.ProjectRejects.ToList();
            project.ProjectRejects.Clear();
            foreach (ProjectReject reject in projectRejects)
            {
                db.ProjectRejects.Remove(reject);
            }

            // Add rejects that came from UI.
            IList<ProjectReject> userRejects = new List<ProjectReject>();
            if (preferencesDto.ProjectRejects != null)
            {
                foreach (ProjectRejectDto pRDto in preferencesDto.ProjectRejects)
                {
                    ProjectReject pr = new ProjectReject();
                    pr.Student = db.Students.Where(s => s.Id == pRDto.StudentId).FirstOrDefault();
                    pr.Reason = pRDto.Reason;
                    userRejects.Add(pr);
                }
            }
            project.ProjectRejects = userRejects;
            project.Feedback = preferencesDto.Feedback;
            project.ScoreDate = DateTime.Now;
            db.SaveChanges();
            db.Dispose();
            SendConfirmationMessage(project);
            return jsonResult;
        }
Пример #11
0
        /// <summary>
        /// Deletes all matching objects for a given student from the database.
        /// </summary>
        /// <param name="studentId">Student identifier</param>
        public static void DeleteMatchingsForStudent(int studentId)
        {
            using (MatchingDB db = new MatchingDB())
            {
                Student student = db.Students.Include("Matchings.Project").Include("StudentFeedbacks").Where(s => s.Id == studentId).FirstOrDefault();
                var existingMatchings = student.Matchings.ToList();
                var studentFeedbacksToBeDeleted = student.StudentFeedbacks.ToList();

                student.Matchings.Clear();
                foreach (Matching m in existingMatchings)
                    db.Matchings.Remove(m);

                #region Delete the student feedbacks of the student for the projects that appeared in the matchings deleted.
                foreach (StudentFeedback sf in studentFeedbacksToBeDeleted)
                    db.StudentFeedbacks.Remove(sf);
                #endregion
                db.SaveChanges();
                ProjectService.DeleteProjectRejectsReferencingStudent(studentId);
            }
        }
Пример #12
0
        /// <summary>
        /// Deletes all matching objects for a given project from the database.
        /// </summary>
        /// <param name="projectId">Project identifier</param>
        public static void DeleteMatchingsForProject(int projectId)
        {
            using (MatchingDB db = new MatchingDB())
            {
                Project project = db.Projects.Include("Matchings.Student").Include("ProjectRejects").Where(p => p.Id == projectId).FirstOrDefault();
                var existingMatchings = project.Matchings.ToList();
                // Find the children collection of students that used be matching the project but will not be matching following the deletion.
                var studentsRemovedFromProject = existingMatchings.Select(m => m.Student.Id).ToArray();
                var projectRejectsToRemove =project.ProjectRejects.Where(pr=>studentsRemovedFromProject.Contains(pr.Student.Id)).ToList();

                project.Matchings.Clear();
                foreach (Matching m in existingMatchings)
                    db.Matchings.Remove(m);

                #region Clear the collection for the students deleted off the db.
                foreach (ProjectReject pr in projectRejectsToRemove)
                    db.ProjectRejects.Remove(pr);
                #endregion
                db.SaveChanges();
             // Not needed because only non-matching students should be providing Positive|Constructive Feedback StudentService.DeleteStudentFeedbacksReferencingProjectForStudents(projectId, studentsRemovedFromProject);
            }
        }
Пример #13
0
        /// <summary>
        /// Updates the matchings for a project whenever user updates the details of a project
        /// </summary>
        /// <param name="studentIdsToAdd">List of student identifiers that will constitute the new matching list of the given project</param>
        public static void ReplaceMatchingsForProjectWith(int projectId, int[] studentIdsToAdd)
        {
            using (MatchingDB db = new MatchingDB())
            {
                Project project = db.Projects.Include("Matchings.Student").Include("ProjectRejects").Where(p => p.Id == projectId).FirstOrDefault();
                if (project.Matchings == null)
                    project.Matchings = new List<Matching>();
                IList<Matching> existingProjectMatchings = project.Matchings.ToList();
                IList<Matching> matchingsToRemove = existingProjectMatchings.Where(m => !studentIdsToAdd.Contains(m.Student.Id)).ToList();
                int[] studentIdsToRemove = matchingsToRemove.Select(m => m.Student.Id).ToArray();
                int[] newStudentIds = studentIdsToAdd.Where(sId => !existingProjectMatchings.Select(m => m.Student.Id).Contains(sId)).ToArray();
                var projectRejectsToRemove = project.ProjectRejects.Where(pr => studentIdsToRemove.Contains(pr.Student.Id)).ToList();
                #region Remove the students that is not on the UI but within the existing matchings of the projects and then add the students that did not appear within the list of existing matchings of the project but appeared on the UI
                //Remove students that do not appear in the list of students to add
                foreach (var m in matchingsToRemove)
                {
                    db.Matchings.Remove(m);
                }
                // Add students that did not exist before
                foreach (var sId in newStudentIds)
                {
                    Student st = db.Students.Include("StudentFeedbacks.Project").Where(s => s.Id == sId).FirstOrDefault();
                    Matching m = new Matching() { Project = project, Student = st, ProjectScore = ProjectScore.NoScore.ToString(), StudentScore = StudentScore.NoScore.ToString() };
                    db.Matchings.Add(m);
                    StudentFeedback studentFeedbackToRemove = st.StudentFeedbacks.Where(sf => sf.Project.Id == project.Id).FirstOrDefault();
                    if(studentFeedbackToRemove!=null)
                        db.StudentFeedbacks.Remove(studentFeedbackToRemove);
                }
                #endregion
                #region Clear the collection for the students replaced off the db.
                foreach (ProjectReject pr in projectRejectsToRemove)
                    db.ProjectRejects.Remove(pr);
                #endregion

                db.SaveChanges();
                // Not needed to delete constructive and positive feedback because only non-matching students should be providing Positive|Constructive Feedback StudentService.DeleteStudentFeedbacksReferencingProjectForStudents(projectId, studentsRemovedFromProject);
            }
        }
        public ActionResult Edit(Student st)
        {
            if (!ModelState.IsValid)
                return View(st);
            MatchingDB db = new MatchingDB();
            Student student = db.Students.FirstOrDefault(s => s.Id == st.Id);
            if (student.UniqueName.ToLower() != st.UniqueName.ToLower() && !ValidateStudent(st))
            {
                setViewDataForListOfDegrees();
                this.ModelState.AddModelError("UniqueName", uniqueNameErrorMsg);
                return View(st);
            }

            student.UniqueName = st.UniqueName;
            student.LastName = st.LastName;
            student.FirstName = st.FirstName;
            student.Email = st.Email;
            student.Degree = st.Degree;
            student.Comments = st.Comments;
            db.SaveChanges();
            TempData["message"] = "Student \"" + student.FirstName+" "+student.LastName + "\" is updated.";
            db.Dispose();
            return RedirectToAction("Index");
        }
Пример #15
0
        /// <summary>
        /// Updates matchings for a student with the user selections on Student Details screen
        /// </summary>
        /// <param name="projectIdsToAdd">List of project identifiers that will constitute the new matching list of the given student</param>
        public static void ReplaceMatchingsForStudentWith(int studentId, int[] projectIdsToAdd)
        {
            using (MatchingDB db = new MatchingDB())
            {
                Student student = db.Students.Include("Matchings.Project").Include("StudentFeedbacks.Project").Where(s => s.Id == studentId).FirstOrDefault();
                if (student.Matchings == null)
                    student.Matchings = new List<Matching>();
                IList<Matching> existingStudentMatchings = student.Matchings.ToList();
                IList<Matching> matchingsToRemove = existingStudentMatchings.Where(m => !projectIdsToAdd.Contains(m.Project.Id)).ToList();
                int[] projectIdsToRemove = matchingsToRemove.Select(m => m.Project.Id).ToArray();
                int[] newProjectIds = projectIdsToAdd.Where(pId => !existingStudentMatchings.Select(m => m.Project.Id).Contains(pId)).ToArray();
                int[] projectsRemovedFromStudent = matchingsToRemove.Where(m => m.Student.Id == studentId && !projectIdsToAdd.Contains(m.Project.Id)).Select(m => m.Project.Id).ToArray();
                IList<StudentFeedback> studentFeedbacksToBeDeleted = student.StudentFeedbacks.Where(sf => newProjectIds.Contains(sf.Project.Id)).ToList();

                //Remove projects that do not appear in the list of projects to add
                foreach (var m in matchingsToRemove)
                    db.Matchings.Remove(m);
                // Add projects that did not exist before
                foreach (var pId in newProjectIds)
                {
                    Project project = db.Projects.Where(p => p.Id == pId).FirstOrDefault();
                    Matching m = new Matching() { Project = project, Student = student, ProjectScore = ProjectScore.NoScore.ToString(), StudentScore = StudentScore.NoScore.ToString() };
                    db.Matchings.Add(m);
                }
                foreach (var sf in studentFeedbacksToBeDeleted)
                    db.StudentFeedbacks.Remove(sf);

                db.SaveChanges();
                ProjectService.DeleteProjectRejectsReferencingStudentForProjects(studentId, projectsRemovedFromStudent);
            }
        }
 private static void LogMessage(MatchingDB db, EmailQueueMessage qMessage,string status)
 {
     EmailLog eLog = new EmailLog(qMessage,status);
     Project pr; Student st;
     try
     {
         switch (qMessage.ContactType)
         {
             case "Project":
                 pr = db.Projects.Where(p => p.Id == qMessage.ContactId).First();
                 pr.Emailed = (status == EmailStatus.Success.ToString());
                 if (pr.EmailLogs == null)
                     pr.EmailLogs = new List<EmailLog>();
                 pr.EmailLogs.Add(eLog);
                 break;
             case "Student":
                 st = db.Students.Where(s => s.Id == qMessage.ContactId).First();
                 st.Emailed = (status == EmailStatus.Success.ToString());
                 if (st.EmailLogs == null)
                     st.EmailLogs = new List<EmailLog>();
                 st.EmailLogs.Add(eLog);
                 break;
             default:
                 break;
         }
         db.SaveChanges();
     }
     catch (Exception ex)
     {
         log.Error("An unexpected exception occured while creating a log record for the notification e-mail sent from the queue. The id for the " + qMessage.ContactType + " is: " + qMessage.ContactId + ". You can disregard this message if the " + qMessage.ContactType + " is deleted.", ex.InnerException ?? ex);
     }
 }
 /// <summary>
 /// Updates the config parameters in the database matching the properties of the method argument
 /// </summary>
 /// <param name="config">AppConfiguration object encapsulating application configuration parameters to be set in the db.</param>
 public static void UpdateConfigParameters(AppConfiguration config)
 {
     using (MatchingDB db = new MatchingDB())
     {
         IEnumerable<ConfigParameter> parameters = config.GetConfigParameters();
         foreach (var param in parameters)
         {
             var pm = db.ConfigParameters.FirstOrDefault(p => p.Id == param.Id);
             if (pm == null)
                 db.ConfigParameters.Add(param);
             else
                 pm.Value = param.Value;
         }
         db.SaveChanges();
     }
 }