Exemplo n.º 1
0
        private void btn_Save_DataBase_Click(object sender, EventArgs e)
        {
            //try
            {
                //
                //Remember that this button could be used to add new elements to the DB as it could be used to update them.
                //Put in mind to treat this issue.Abdelalim
                //

                //Check for emty fields.
                if (Check_Fields())
                {
                    if (this.procedure.steps.Count > 0)
                    {
                        //Set the procedure final values
                        this.procedure.setName(txtBx_Title.Text);
                        this.procedure.setDescription(txtBx_Description.Text);

                        //Set the failuretype final values
                        if (this.failureType == null)
                        {
                            this.failureType = new TypePanne();
                        }
                        this.failureType.setName(txtBx_FailureType.Text);
                        this.failureType.setDescription(FailureTypeDesc);

                        //Set the failure final values
                        if (this.Failure == null)
                        {
                            this.Failure = new Panne();
                        }
                        this.Failure.setName(txtBx_Failure.Text);
                        this.Failure.setDescription(FailureDesc);
                        this.failureType.addPanne(Failure);

                        //Add new proecedure.
                        if (this._Operation == Operation.AddNew)
                        {
                            //If no existing Failure type is chosen create new one.
                            if (this.failureType == null)
                            {
                                //Get the Id of the new failure type (type de panne).
                                this.failureType = new TypePanne();
                                this.failureType.setName(txtBx_FailureType.Text);
                                this.failureType.setDescription(FailureTypeDesc);
                            }
                            //If no existing Failure is chosen create new one.
                            if (this.Failure == null)
                            {
                                this.Failure = new Panne();
                                this.Failure.setName(txtBx_Failure.Text);
                                this.Failure.setDescription(FailureDesc);

                                //Add _Failure to the list of failures of the FailureType object.
                                this.failureType.addPanne(this.Failure);
                            }

                            //Addthe procedure to the Failure's list of procedures.
                            this.Failure.addProcedure(procedure);

                            //Save the failure type (le type de panne).
                            if (!failureType.Exists())//If a new type is created we save it to the data base.
                            {
                                failureType.Add();
                                Failure.Add();// Save the failure to the DB since it belongs to a newly created FailureType.
                            }
                            else//If the FailureType exists we check for the the failure(panne).
                            //Save the failure (la panne).
                            {
                                failureType.Update(failureType.getId());
                                if (!Failure.Exists())//If a new type is created we save it to the data base.
                                {
                                    Failure.Add();
                                }
                                else
                                {
                                    Failure.Update(Failure.getId());
                                }
                            }
                            //
                            //Saving the procedure.
                            //
                            procedure.AddToDB();
                            //MessageBox.Show("Proc.Steps.count: " + this.procedure.getEtapes().Count, "frm_AddProcedure.btn_Save");
                            //Save the steps.
                            foreach (Etape step in this.procedure.getEtapes())
                            {
                                //Associates each step with the current procedure.
                                //procedure.addEtape(step);
                                step.setprocedure(this.procedure);
                                //MessageBox.Show("Etape.Proc: " + step.getprocedure().getId(), "frm_AddProcedure.btn_Save");
                                //Save the step and its objects to the file etape.xml.
                                step.Add();
                            }
                        }
                        else//Update an existing procedure.
                        {
                            //Addthe procedure to the Failure's list of procedures.
                            this.Failure.addProcedure(procedure);
                            //MessageBox.Show("Falure.name: " + Failure.getId(), "frm_AddPrecedure.btn_Save");
                            //
                            //Saving the procedure.
                            //
                            procedure.Update(this.procedure.getId());

                            //Save the steps.
                            if (StepsRemoved)
                            {
                                //Remove all the steps of the current procedure from the data base.
                                //MessageBox.Show("Proc.steps.count: " + this.procedure.getEtapes().Count, "frm_AddProcedure.btn_Save");
                                Etape.RemoveByProcedure(this.procedure.getId());
                            }

                            //In case we allow the user to update the failure and its type from this GUI.
                            //Update the FailureType.
                            failureType.Update(failureType.getId());
                            //Update the failure (la panne).
                            Failure.Update(Failure.getId());

                            //MessageBox.Show("Steps.Count: "+this.procedure.getEtapes().Count, "frm_AddProcedure.btn_Save");
                            foreach (Etape step in this.procedure.steps)
                            {
                                //Associates each step with the current procedure.
                                step.setprocedure(this.procedure);
                                //If the current step exists in the DB means we're about to update it.
                                if (step.Exists())
                                {
                                    step.Update(step.getId());
                                }
                                else
                                {
                                    //Save the step and its objects to the file etape.xml.
                                    step.Add();
                                    //MessageBox.Show("Step "+step.getName()+" added","frm_AddProcedure.btn_Save");
                                }
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show(this, "La procedure doit avoir au minimum une étape.", "Message d'erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show(this, "Remplir les champs importants d'abord.", "Message d'erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                MessageBox.Show(this, "Opération terminée avec succès.", "Message d'information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Tag = this.procedure;
                this.Close();
            }
            //catch (Exception x) { MessageBox.Show(x.ToString()); }
        }