// // Swap(): // // If possible, function swaps the courses for two student. // So, if student s1 was enrolled in course c1, and student s2 // was enrolled in course c2, after succesful execution // student s1 is enrolled in s2, and student s2 is enrolled in s1. // // Return values: // 0 - failure // 1 - success // private int Swap(int s1, int s2, int c1, int c2) { int retries = 0; using (var db = new CoursemoDataContext()) { while (retries < 3) { try { var txOptions = new TransactionOptions(); txOptions.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (var transaction = new TransactionScope(TransactionScopeOption.Required, txOptions)) { // both students must be enrolled if (StudentEnrolled(s1, c1) && StudentEnrolled(s2, c2)) { db.UnregisterStudent(s1, c1); db.RegisterStudent(s2, c1); System.Threading.Thread.Sleep(_delay); db.UnregisterStudent(s2, c2); db.RegisterStudent(s1, c2); db.SubmitChanges(); transaction.Complete(); return(1); } else { return(0); } } } catch (SqlException exc) { // deadlock if (exc.Number == 1205) { retries++; } } catch (Exception e) { MessageBox.Show("Swap(): " + e.Message); return(0); } } // while } return(0); } // Swap()
// // ResetDatabase(): // // Revert database to its inintial state. // Deletes data from Registration and Waitlist tables. // private void ResetDatabase() { using (var db = new CoursemoDataContext()) { try { var query1 = from r in db.Registrations select r; db.Registrations.DeleteAllOnSubmit(query1); var query2 = from w in db.Waitlists select w; db.Waitlists.DeleteAllOnSubmit(query2); db.SubmitChanges(); UpdateUi(); } catch (Exception exc) { MessageBox.Show("ResetDatabase(): " + exc.Message); } } }
//Delete all rows in Registrations, Transactions, and Waitlist tables private void resetButton_Click(object sender, EventArgs e) { using (var db = new CoursemoDataContext()) { var TxOptions = new System.Transactions.TransactionOptions(); TxOptions.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (var Tx = new System.Transactions.TransactionScope( System.Transactions.TransactionScopeOption.Required, TxOptions)) { try { if (delay_flag) { Delay(); } db.Registrations.DeleteAllOnSubmit(from r in db.Registrations select r); if (delay_flag) { Delay(); } db.Waitlists.DeleteAllOnSubmit(from w in db.Waitlists select w); if (delay_flag) { Delay(); } db.Transactions.DeleteAllOnSubmit(from t in db.Transactions select t); db.SubmitChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Tx.Complete(); } } MessageBox.Show("Database reset."); } }
// // Drop(): // // If possible, function drops the student from the course. // // Return values: // -1 - error // 0 - dropped, no waitlist // 1 - dropped, enrolled student from waitlist // 2 - not enrolled, dropped from waitlist // 3 - student was not either enrolled or waitlisted // private int Drop(int sid, int cid) { int retries = 0; // make sure parameters are valid if (sid < 0 || cid < 0) { return(-1); } using (var db = new CoursemoDataContext()) { while (retries < 3) { try { var txOptions = new TransactionOptions(); txOptions.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (var transaction = new TransactionScope(TransactionScopeOption.Required, txOptions)) { // student enrolled, drop course and enroll first student on waitlist if (StudentEnrolled(sid, cid)) { db.UnregisterStudent(sid, cid); // candidate on waitlist exists int sidFromWaitList = GetFirstStudentFromWaitlist(cid); System.Threading.Thread.Sleep(_delay); if (sidFromWaitList != 0) { db.RegisterStudent(sidFromWaitList, cid); db.UnwaitlistStudent(sidFromWaitList, cid); db.SubmitChanges(); transaction.Complete(); return(1); } db.SubmitChanges(); transaction.Complete(); return(0); } // student not enrolled, but waitlisted, remove from waitlist else if (StudentWaitlisted(sid, cid)) { db.UnwaitlistStudent(sid, cid); db.SubmitChanges(); transaction.Complete(); return(2); } else { return(3); } } } catch (SqlException exc) { // deadlock if (exc.Number == 1205) { retries++; } } catch (Exception e) { MessageBox.Show("Drop(): " + e.Message); return(-1); } } // while } return(-1); } // Drop()
// // Enroll(): // // If possible, function enrolls the student in the course. // // Return values: // -1 - error // 0 - Student already enrolled // 1 - Student enrolled // 2 - Student already waitlisted // 3 - Student waitlisted // private int Enroll(int sid, int cid) { int retries = 0; // make sure parameters are valid if (sid < 0 || cid < 0) { return(-1); } using (var db = new CoursemoDataContext()) { while (retries < 3) { try { var txOptions = new TransactionOptions(); txOptions.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (var transaction = new TransactionScope(TransactionScopeOption.Required, txOptions)) { // check if student is already enrolled if (StudentEnrolled(sid, cid)) { return(0); } // class is full, add to waitlist if (GetCourseCapacity(cid) - GetCurrentEnrollment(cid) < 1) { // check if student is not on waitlist alread if (StudentWaitlisted(sid, cid)) { return(2); } db.WaitlistStudent(sid, cid); db.SubmitChanges(); transaction.Complete(); return(3); } // enroll else { System.Threading.Thread.Sleep(_delay); db.RegisterStudent(sid, cid); db.SubmitChanges(); transaction.Complete(); return(1); } } } catch (SqlException exc) { // deadlock if (exc.Number == 1205) { retries++; } } catch (Exception e) { MessageBox.Show("Enroll(): " + e.Message); return(-1); } } // while } return(-1); } // Enroll()
private void swapButton_Click(object sender, EventArgs e) { string netidA = this.netidATextBox.Text; if (netidA == null) { MessageBox.Show("Enter student A's netid."); return; } int crnA = Convert.ToInt32(this.crnATextBox.Text); if (this.crnATextBox.Text == null) { MessageBox.Show("Enter student A's CRN."); return; } string netidB = this.netidBTextBox.Text; if (netidB == null) { MessageBox.Show("Enter student B's netid."); return; } int crnB = Convert.ToInt32(this.crnBTextBox.Text); if (this.crnBTextBox.Text == null) { MessageBox.Show("Enter student B's CRN."); return; } string msg = null; using (var db = new CoursemoDataContext()) { var TxOptions = new System.Transactions.TransactionOptions(); TxOptions.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (var Tx = new System.Transactions.TransactionScope( System.Transactions.TransactionScopeOption.Required, TxOptions)) { try { //Check if student A exists if (delay_flag) { Delay(); } var aExists = from s in db.Students where s.Netid == netidA select s.Netid; if (!aExists.Any()) { msg = netidA + " does not exist."; throw new System.InvalidOperationException(msg); } //Check if student A is enrolled in crnA var aEnrolled = from r in db.Registrations where r.Netid == netidA where r.CRN == crnA select r; if (!aEnrolled.Any()) { msg = netidA + " is not enrolled in " + crnA.ToString() + "."; throw new System.InvalidOperationException(msg); } if (delay_flag) { Delay(); } //Check if student B exists var bExists = from s in db.Students where s.Netid == netidB select s.Netid; if (!bExists.Any()) { msg = netidB + " does not exist."; throw new System.InvalidOperationException(msg); } if (delay_flag) { Delay(); } //Check if student B is enrolled in crnB var bEnrolled = from r in db.Registrations where r.Netid == netidB where r.CRN == crnB select r; if (!bEnrolled.Any()) { msg = netidB + " is not enrolled in " + crnB.ToString() + "."; throw new System.InvalidOperationException(msg); } if (delay_flag) { Delay(); } //Clear out student a's waitlist entry for b's class var aWaiting = from w in db.Waitlists where w.Netid == netidA where w.CRN == crnB select w; db.Waitlists.DeleteAllOnSubmit(aWaiting); if (delay_flag) { Delay(); } //Clear out student b's waitlist entry for a's class var bWaiting = from w in db.Waitlists where w.Netid == netidB where w.CRN == crnA select w; db.Waitlists.DeleteAllOnSubmit(bWaiting); //Swap registrations if (delay_flag) { Delay(); } aEnrolled.First().CRN = crnB; if (delay_flag) { Delay(); } bEnrolled.First().CRN = crnA; //MessageBox.Show(netidA + " swapped course " + crnA.ToString() + " for " + crnB.ToString() + " with " + netidB); msg = netidA + " swapped course " + crnA.ToString() + " for " + crnB.ToString() + " with " + netidB; db.SubmitChanges(); Tx.Complete(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } MessageBox.Show(msg); }
//Get selected student and enrolled course //Delete entry in registrations table //Check waitlist for dropped course, enroll first in line private void dropCourseButton_Click(object sender, EventArgs e) { Student student = getSelectedStudent(); if (student == null) { MessageBox.Show("Select a student."); return; } if (this.registrationLB.SelectedItem == null) { MessageBox.Show("Select a registered course."); return; } int selectedCRN = Convert.ToInt32(this.registrationLB.SelectedItem.ToString().Split(' ')[0]); //MessageBox.Show(selectedCRN.ToString()); string msg = null; using (var db = new CoursemoDataContext()) { var TxOptions = new System.Transactions.TransactionOptions(); TxOptions.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (var Tx = new System.Transactions.TransactionScope( System.Transactions.TransactionScopeOption.Required, TxOptions)) { try { if (delay_flag) { Delay(); } db.Registrations.DeleteAllOnSubmit(from r in db.Registrations where r.CRN == selectedCRN where r.Netid == student.netid select r); //MessageBox.Show(student.netid + " dropped " + selectedCRN); msg = student.netid + " dropped " + selectedCRN; if (delay_flag) { Delay(); } var waiting = (from w in db.Waitlists where w.CRN == selectedCRN orderby w.RegTime select w).ToList(); //Is there a waitlist for this class? if (waiting.Any()) { if (delay_flag) { Delay(); } var front = students.Find( delegate(Student s) { return(s.netid == waiting[0].Netid); } ); //Enroll if (delay_flag) { Delay(); } Registration r = new Registration { Netid = front.netid, CRN = selectedCRN }; db.Registrations.InsertOnSubmit(r); //MessageBox.Show(front.netid + " enrolled in " + selectedCRN); //msg = front.netid + " enrolled in " + selectedCRN; //Remove enrolled student from waitlist if (delay_flag) { Delay(); } db.Waitlists.DeleteAllOnSubmit(from w in db.Waitlists where w.Netid == front.netid where w.CRN == selectedCRN select w); } db.SubmitChanges(); Tx.Complete(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } MessageBox.Show(msg); }
//Get the selected student and course //Try to enroll the student in the course //If the course is full, put students on waitlist private void enrollButton_Click(object sender, EventArgs e) { if (this.studentsLB.SelectedItem == null) { MessageBox.Show("Select a student to enroll."); return; } Student student = getSelectedStudent(); //MessageBox.Show(student.ToString()); if (this.coursesLB.SelectedItem == null) { MessageBox.Show("Select a course to enroll in."); return; } Course course = getSelectedCourse(); //MessageBox.Show(course.ToString()); string msg = null; using (var db = new CoursemoDataContext()) { var TxOptions = new System.Transactions.TransactionOptions(); TxOptions.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (var Tx = new System.Transactions.TransactionScope( System.Transactions.TransactionScopeOption.Required, TxOptions)) { try { if (delay_flag) { Delay(); } var enrolled = from r in db.Registrations where r.Netid == student.netid where r.CRN == course.crn select r; if (delay_flag) { Delay(); } var waiting = from w in db.Waitlists where w.Netid == student.netid where w.CRN == course.crn select w; //Is the student already enrolled in this class? if (enrolled.Any()) { //MessageBox.Show(student.netid + " is already enrolled in course " + course.crn); msg = student.netid + " is already enrolled in course " + course.crn; } //Is the student on the waitlist for this class? else if (waiting.Any()) { //MessageBox.Show(student.netid + " is already on the waitlist for course " + course.crn); msg = student.netid + " is already on the waitlist for course " + course.crn; } //Student not enrolled or waiting for this course else { //Get number of registrations for this course if (delay_flag) { Delay(); } var query = (from r in db.Registrations where r.CRN == course.crn select r.RID).ToList(); //Is this class full? if (query.Count == course.capacity) { //MessageBox.Show(course.crn + " is full, putting " + student.netid + " on the waitlist for this course."); //Put on waitlist if (delay_flag) { Delay(); } Waitlist wait = new Waitlist { Netid = student.netid, CRN = course.crn, RegTime = DateTime.Now }; db.Waitlists.InsertOnSubmit(wait); if (delay_flag) { Delay(); } var placeInLine = from w in db.Waitlists where w.CRN == course.crn orderby w.RegTime select w.WID; //MessageBox.Show(student.netid + " put on waitlist for " + course.crn + ". Place in line: " + (placeInLine.Count() + 1).ToString()); msg = student.netid + " put on waitlist for " + course.crn + ". Place in line: " + (placeInLine.Count() + 1).ToString(); } else { //Enroll if (delay_flag) { Delay(); } Registration r = new Registration { Netid = student.netid, CRN = course.crn }; db.Registrations.InsertOnSubmit(r); //MessageBox.Show(student.netid + " enrolled in " + course.crn); msg = student.netid + " enrolled in " + course.crn; } } db.SubmitChanges(); Tx.Complete(); } catch (Exception ex) { Console.WriteLine(ex.Message); //msg = "Enrollment failed. Try again."; //Tx.Dispose(); } } MessageBox.Show(msg); } }