/// <summary> /// Method to add a Model Security to the DB /// </summary> /// <param name="modSec">ModSec object to add</param> internal void addModelSecurity(ModelSecurity modSec) { string selectString = "INSERT into ModelSecurity(ModelID, SecurityID, Percentage)" + "VALUES (" + modSec.modelID + ", '" + modSec.securityID + "', '" + modSec.percentage + "')"; UpdateDataSource(new SqlCommand(selectString, cnMain)); }
public ModelSecurity(ModelSecurity other) { this.modelID = other.modelID; this.securityID = other.securityID; this.securityName = other.securityName; this.percentage = other.percentage; this.modelName = other.modelName; }
/// <summary> /// Method to add a new security to a model /// </summary> /// <param name="modSec">model security object</param> internal void addSecToModel(ModelSecurity modSec) { try { // first check if the percentage being added does not exceed 100% for that model if (percIsValid(modSec)) { database.addModelSecurity(modSec); updateCollections(); } else throw new DatabaseException("The percentages for this model exceed 100%, please check the values again."); } catch (DatabaseException err) { throw new DatabaseException(err.error); } }
/// <summary> /// Listener to add a security to a form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { ModelSecurity modSec = new ModelSecurity((int)modID.Value, (int)secID.Value, (int)perc.Value); try { controller.addSecToModel(modSec); MessageBox.Show("Security successfully added to the model.", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (DatabaseException err) // catch any databse erros { MessageBox.Show(err.error, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private void button1_Click(object sender, EventArgs e) { try { ModelSecurity modSec = new ModelSecurity((int)modID.Value, (int)secID.Value, (int)perc.Value); controller.updateSecInModel(modSec); MessageBox.Show("Update successful.", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } catch (DatabaseException err) // catch any databse erros { MessageBox.Show(err.error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
/// <summary> /// Method to check if a percentage is valid /// Also checks if the security does not already exist in the model /// </summary> /// <param name="otherModSec"></param> /// <returns></returns> private bool percIsValid(ModelSecurity otherModSec) { if (otherModSec.percentage > 100) return false; int totalPerc = 0; foreach (ModelSecurity modsecDB in modSecs) { if (modsecDB.modelID == otherModSec.modelID) { if (modsecDB.securityID == otherModSec.securityID) return false; /// security already exists totalPerc += modsecDB.percentage; } } if (totalPerc < 100) return true; else return false; }
/// <summary> /// Methdo to update a model security in the DB /// </summary> /// <param name="modSec"></param> internal void updateSecInModel(ModelSecurity modSec) { try { database.updateModelSecurity(modSec); updateCollections(); } catch (DatabaseException err) { throw new DatabaseException(err.error); } }
/// <summary> /// Method to update an existing model security /// </summary> /// <param name="modSec"></param> internal void updateModelSecurity(ModelSecurity modSec) { string selectString = "UPDATE ModelSecurity SET " + "Percentage = " + modSec.percentage + " WHERE ModelID = " + modSec.modelID + " AND SecurityID = " + modSec.securityID; UpdateDataSource(new SqlCommand(selectString, cnMain)); }