private void button3_Click(object sender, EventArgs e) { //users wants to definitively save the changes disactivateControl();//disable all the content during the process StudentExitRegime regime; StudentExitRegimeAuthorization authorization; bool newItem; string msg_a = ""; string msg_r = ""; string msg = ""; //---------------- update authorizations into the database ---------------- List <StudentExitRegimeAuthorization> temp_list_authorization = new List <StudentExitRegimeAuthorization>(); for (int row = 0; row < Authorization_dataGridView.RowCount; row++)//loop through authorizations of the datagrid { //check if the authorization is into the database object tag = Authorization_dataGridView.Rows[row].Tag; if (tag == null || (int)tag == -1) { newItem = true;//not previously into the database } else//exists into the database { newItem = false; } authorization = new StudentExitRegimeAuthorization(); authorization.toDefault(); //get the updated or newauthorization var res = authorizationChecker(row, false, true, ref authorization); if (!(bool)res.Item1) { //the parameters of the authorization are not correct if (row != Authorization_dataGridView.RowCount - 1) //datagrid automatically add a blank row to allow user to add new rows { msg_a += res.Item2 + "\n"; } continue; } if (!newItem)//authorization already exists in the list { authorization.Id = (int)tag; } temp_list_authorization.Add(authorization); } //---------------- update exit regime into the database ---------------- List <StudentExitRegime> temp_list_regime = new List <StudentExitRegime>(); int id; for (int row = 0; row < Exit_regime_dataGridView.RowCount; row++)//loop through exit regime of the datagrid { //check if the authorization is into the database object tag = Exit_regime_dataGridView.Rows[row].Tag; if (tag == null || (int)tag == -1) { id = -1;//not previously into the database } else { id = (int)tag;//exists into the database } //check if label of the current regime is correct int column_index_label = Exit_regime_dataGridView.Columns["label_regime"].Index; var tmp = Exit_regime_dataGridView.Rows[row].Cells[column_index_label].Value; if (tmp == null) //parameters are not correct { if (row != Exit_regime_dataGridView.RowCount - 1) //datagrid automatically add a blank row to allow user to add new rows { msg_r += "La cellule de la ligne " + (row + 1) + " colonne " + (column_index_label + 1) + " dans la table des régimes de sortie est vide\n"; } continue; } string label_regime = tmp.ToString(); //check if it already exists into the database if (!dict_relation_RA.ContainsKey(label_regime)) { dict_relation_RA.Add(label_regime, new List <string>());//new regime } //update or create new regime regime = new StudentExitRegime(); regime.toDefault(); regime.Id = id; regime.name = label_regime; regime.authorizations = dict_relation_RA[label_regime]; int index = Exit_regime_dataGridView.Columns["exitEndOfDay_regime"].Index; DataGridViewCheckBoxCell cell_checkBox = (DataGridViewCheckBoxCell)Exit_regime_dataGridView.Rows[row].Cells[index]; if (cell_checkBox.Value == cell_checkBox.TrueValue) { regime.exitEndOfDay = true; } else { regime.exitEndOfDay = false; } temp_list_regime.Add(regime); } //---------------- solving incorrect format problems ---------------- if (msg_a.Length > 0) { msg += "--------------- PERMISSIONS ---------------" + "\n\n" + msg_a + "\n"; } if (msg_r.Length > 0) { msg += "--------------- REGIMES DE SORTIE ---------------" + "\n\n" + msg_r; } if (msg.Length > 0)//error { //ask user if he wants to ignore problemes if (MessageBox.Show("Certaines modifications ne seront pas prises en compte pour les raisons suivantes :\n" + msg + "\n\nVoulez vous continuer quand même ?", "Valider les changements", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) { activateControl();//active the previoulsy disabled content return; } } //---------------- update database ---------------- DataBase database = new DataBase(); int errCode; //remove deleted authorization (that previously were into the database) foreach (int table_id in list_deleted_authorization_id) { errCode = database.removeAuthorizationExitRegime(table_id); if (errCode != Definition.NO_ERROR_INT_VALUE) { MessageBox.Show("Une erreur est survenue : impossible de mettre à jour la base de données"); activateControl();//active the previoulsy disabled content this.Close(); } } //update (or add) the others errCode = database.updateAuthorizationExitRegime(temp_list_authorization); if (errCode != Definition.NO_ERROR_INT_VALUE) { MessageBox.Show("Une erreur est survenue : impossible de mettre à jour la base de données"); activateControl();//active the previoulsy disabled content this.Close(); } //remove deleted regime (that previously were into the database) foreach (int table_id in list_deleted_regime_id) { errCode = database.removeExitRegime(table_id); if (errCode != Definition.NO_ERROR_INT_VALUE) { MessageBox.Show("Une erreur est survenue : impossible de mettre à jour la base de données"); activateControl();//active the previoulsy disabled content this.Close(); } } //update (or add) the others errCode = database.updateExitRegime(temp_list_regime); if (errCode != Definition.NO_ERROR_INT_VALUE) { MessageBox.Show("Une erreur est survenue : impossible de mettre à jour la base de données"); activateControl();//active the previoulsy disabled content this.Close(); } //---------------- update default regime id ---------------- string default_regime_name = getNameFromCell(default_regime_cell); if (default_regime_name != null) { int val = database.getExitRegimeId(default_regime_name); Settings.DefaultExitRegimeDatabaseTableId = val; } else { Settings.DefaultExitRegimeDatabaseTableId = -1; } activateControl(); //active the previoulsy disabled content this.Close(); //close windows }
private Tuple <bool, string> authorizationChecker(int row_index, bool show, bool extract_authoriation, ref StudentExitRegimeAuthorization authorization) { if (row_index < 0) { return(Tuple.Create(false, "Cellules non prise en compte")); } //check if label of the current authorization is not null int column_index_label = Authorization_dataGridView.Columns["label_authorization"].Index; var tmp = Authorization_dataGridView.Rows[row_index].Cells[column_index_label].Value; if (tmp == null)//label is null { return(Tuple.Create(false, "Cellule ligne " + (row_index + 1) + " et colonne " + (column_index_label + 1) + " de la table des permissions vide")); } string label_regime = tmp.ToString(); //check if start>end and period < end-start after user leave the current row TimeSpan[] list_timespan = new TimeSpan[3]; string[] list_col_name = new string[] { "period_authorization", "start_authorization", "end_authorization" }; int column_index; string column_name; DataGridViewColumn column; //get all the timespan for (int i = 0; i < list_col_name.Length; i++) { string col_name = list_col_name[i]; column = Authorization_dataGridView.Columns[col_name]; column_index = column.Index; column_name = column.HeaderText; var temp = Authorization_dataGridView.Rows[row_index].Cells[column_index].Value; if (temp == null) { string msg = "La cellule de la ligne " + (row_index + 1) + " et de la colonne <<" + column_name + ">> de la table des permissions est vide"; if (show) { MessageBox.Show(msg); } return(Tuple.Create(false, msg)); } string value = temp.ToString(); list_timespan[i] = Tools.stringToTimeSpan(value); } TimeSpan period = list_timespan[0]; TimeSpan start = list_timespan[1]; TimeSpan end = list_timespan[2]; //check the timespan value if (end <= start) { string msg = "La fin de la plage horraire ne peut pas être supérieur ou égale au début de la plage horraire (ligne " + (row_index + 1) + " dans la table des permissions)"; if (show) { MessageBox.Show(msg); } return(Tuple.Create(false, msg)); } else if ((end - start) <= period) { string msg = "La période fournie ne peut pas être plus grande que la plage horraire (ligne " + (row_index + 1) + " dans la table des permissions)"; if (show) { MessageBox.Show(msg); } return(Tuple.Create(false, msg)); } if (extract_authoriation)//return an authorization struct with the collected data about the current authorization { authorization.toDefault(); authorization.name = label_regime; authorization.period = period; authorization.start = start; authorization.end = end; } return(Tuple.Create(true, "")); }