private void mongoDBToolStripMenuItem_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; Form dataSources = new MongoDBForm(); dataSources.StartPosition = FormStartPosition.CenterParent; dataSources.ShowDialog(); Cursor.Current = Cursors.Default; }
private void gradingDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (gradingDataGrid.CurrentCell != null) { if (gradingDataGrid.Columns[e.ColumnIndex].HeaderText.Contains("URL") && gradingDataGrid.CurrentCell.EditedFormattedValue.ToString().Contains("https")) { var url = gradingDataGrid.CurrentCell.EditedFormattedValue.ToString(); Process.Start(url); } //Reserved Column gets clicked else if (gradingDataGrid.Columns[e.ColumnIndex].HeaderText.Contains("Reserved")) { //If there is no database connected let the user know. if (Properties.Settings.Default.MongoDBGradingCollection == "" && mongoWarningshown == false) { mongoWarningshown = true; var result = MessageBox.Show("To Reserve assignment you must add a MongoDB Connection.\nWould you like to connect to a database?", "Create Database", MessageBoxButtons.YesNo); switch (result) { case DialogResult.Yes: Cursor.Current = Cursors.WaitCursor; Form dataSources = new MongoDBForm(); dataSources.StartPosition = FormStartPosition.CenterParent; dataSources.ShowDialog(); Cursor.Current = Cursors.Default; ConnectToMongoDB(); break; case DialogResult.No: break; } } else //database { //Reserved is checked if (Convert.ToBoolean(gradingDataGrid.CurrentCell.Value) == true) { DialogResult dialogResult = MessageBox.Show("This assignment is currently reserved. Do you want to unassign it?", "Unreserve?", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { gradingDataGrid.CurrentCell.Value = false; //Remove the data from the database if (connectedToMongoDB == true) { var mongoCollection = mongoDatabase.GetCollection <BsonDocument>(Properties.Settings.Default.MongoDBGradingCollection); string url = gradingDataGrid.Rows[e.RowIndex].Cells[6].EditedFormattedValue.ToString(); //Make call for URL var filter = Builders <BsonDocument> .Filter.Eq("_id", url); mongoCollection.DeleteOne(filter); } } } else //Reserved is not checked { string url = gradingDataGrid.Rows[e.RowIndex].Cells[6].EditedFormattedValue.ToString(); //Add the data to the database if (connectedToMongoDB == true) { var mongoCollection = mongoDatabase.GetCollection <BsonDocument>(Properties.Settings.Default.MongoDBGradingCollection); BsonDocument documentToWrite = new BsonDocument { { "_id", url }, { "grader", Properties.Settings.Default.AppUserName }, { "reserved_at", DateTime.Now.ToString() } }; try { Process browserTab = Process.Start(url); //grading url in new tab mongoCollection.InsertOne(documentToWrite); //try to write to database //continue if successful: gradingDataGrid.CurrentCell.Value = true; } catch (MongoDB.Driver.MongoWriteException writeException) { //Make call for URL if (writeException.WriteError.Category == ServerErrorCategory.DuplicateKey) // if this entry has already been made (assignment already reserved) { var filter = Builders <BsonDocument> .Filter.Eq("_id", url); var conflictDocument = mongoCollection.Find(filter).FirstOrDefault(); var grader = conflictDocument.GetElement("grader"); gradingDataGrid.CurrentCell.Value = true; this.Activate(); //pulls the form into focus to display message MessageBox.Show($"This assignment was reserved by {grader.Value}"); RefreshQueue(); //refresh queue to update reserved checkbox } else { MessageBox.Show($"There was a MongoDB write error {writeException.Message}"); } } }//end if(connectedToMongo) } } } //Course column gets clicked else if (gradingDataGrid.Columns[e.ColumnIndex].HeaderText.Contains("Course")) { var url = gradingDataGrid.Rows[e.RowIndex].Cells[7].EditedFormattedValue.ToString(); Process.Start(url); } else if (gradingDataGrid.Columns[e.ColumnIndex].HeaderText.Contains("Assignment")) { var url = gradingDataGrid.Rows[e.RowIndex].Cells[6].EditedFormattedValue.ToString(); Process.Start(url); } } }