private void EditRiskAndAction_Load(object sender, EventArgs e) { //TREEVIEW Model model = ERMTSession.Instance.CurrentModel; if (model != null) { tvRegions.Nodes.Clear(); Region current = RegionHelper.Get(model.IDRegion); List <Region> regions = new List <Region>(); regions.Add(RegionHelper.Get(1)); if (current.IDParent.HasValue && current.IDParent != 1) { regions.Add(RegionHelper.Get(current.IDParent.Value)); } regions.Add(current); regions.AddRange(RegionHelper.GetAllChilds(current.IDRegion)); TreeNode node = new TreeNode() { Text = current.RegionName, Name = current.IDRegion.ToString(), Tag = current }; int mainRegionLevel = RegionHelper.GetLevel(current.IDRegion); tvRegions.Nodes.Add(node); AddChildRegions(tvRegions.Nodes[0], regions, mainRegionLevel); tvRegions.SelectedNode = tvRegions.Nodes[0]; tvRegions.ExpandAll(); } //Electoral Phase cbElectoralPhases.Items.Add(new ComboBoxItemFace(null)); cbElectoralPhases.Items.AddRange(PhaseHelper.GetAll().Select(p => new ComboBoxItemFace(p)).ToArray()); cbElectoralPhases.SelectedIndex = 0; // Add data from alert: if (_alert.IDModel != 0) { tbCode.Text = _alert.Code; tbTitle.Text = _alert.Title; dtpDateFrom.Value = _alert.DateFrom; dtpDateTo.Value = _alert.DateTo; tbDescription.Text = _alert.RiskDescription; tbAction.Text = _alert.Action; tbResult.Text = _alert.Result; rbStatusActive.Checked = _alert.Active; rbStatusInactive.Checked = !_alert.Active; // Phases: List <ModelRiskAlertPhase> modelRiskAlertPhaseList = ModelRiskAlertHelper.GetPhases(_alert); foreach (ModelRiskAlertPhase mrap in modelRiskAlertPhaseList) { foreach (var cboPhase in cbElectoralPhases.Items) { if (((ComboBoxItemFace)cboPhase).IFase != null) { if (((ComboBoxItemFace)cboPhase).IFase.IDPhase == mrap.IDPhase) { lbElectoralPhases.Items.Add(cboPhase); } } } } // Regions: List <ModelRiskAlertRegion> modelRiskAlertRegionList = ModelRiskAlertHelper.GetModelRiskAlertRegions(_alert); foreach (ModelRiskAlertRegion region in modelRiskAlertRegionList) { foreach (TreeNode node in tvRegions.Nodes) { CheckRegion(node, region); } } // Add analysis: List <ModelRiskAlertAttachment> modelRiskAlertAttachment = ModelRiskAlertHelper.GetModelRiskAlertAttachments(_alert); foreach (ModelRiskAlertAttachment att in modelRiskAlertAttachment) { addUpdater(att); } // And a last button to add more: addUpdater(); } else { Label newLabel = new Label { Text = ResourceHelper.GetResourceText("RiskAndActionSaveWarning"), ForeColor = Color.Red, Width = 500 }; analysisPanel.Controls.Add(newLabel); } }
private void btnSaveRiskAndAction_Click(object sender, EventArgs e) { if (isValid()) { // Save data: Model model = ERMTSession.Instance.CurrentModel; _alert.IDModel = model.IDModel; _alert.Code = tbCode.Text; _alert.Title = tbTitle.Text; _alert.DateFrom = Convert.ToDateTime(dtpDateFrom.Value); _alert.DateTo = Convert.ToDateTime(dtpDateTo.Value); _alert.RiskDescription = tbDescription.Text; _alert.Action = tbAction.Text; _alert.Result = tbResult.Text; _alert.Active = rbStatusActive.Checked; List <ModelRiskAlertPhase> modelRiskAlertPhaseListToSave = new List <ModelRiskAlertPhase>(); ModelRiskAlertPhase modelRiskAlertPhase = null; List <ModelRiskAlertPhase> modelRiskAlertPhaseList = ModelRiskAlertHelper.GetPhases(_alert); foreach (var lbPhase in lbElectoralPhases.Items) { bool found = false; foreach (ModelRiskAlertPhase oPhase in modelRiskAlertPhaseList) { if (((ComboBoxItemFace)lbPhase).IFase.IDPhase == oPhase.IDPhase) { found = true; break; } } if (!found) { modelRiskAlertPhase = new ModelRiskAlertPhase(); modelRiskAlertPhase.IDPhase = ((ComboBoxItemFace)lbPhase).IFase.IDPhase; modelRiskAlertPhaseListToSave.Add(modelRiskAlertPhase); } } List <ModelRiskAlertRegion> modelRiskAlertRegionList = ModelRiskAlertHelper.GetModelRiskAlertRegions(_alert); List <ModelRiskAlertRegion> modelRiskAlertRegionsToDelete = GetModelRiskAlertRegionsToDelete(modelRiskAlertRegionList); List <int> regionIDsToAdd = GetRegionIDsToAdd(modelRiskAlertRegionList); // Add new attachments: foreach (Control control in analysisPanel.Controls) { if (control is Updater) { if (((Updater)control).HasFile && ((Updater)control).Id == 0) { ModelRiskAlertAttachment att = new ModelRiskAlertAttachment(); att.IDModelRiskAlert = _alert.IDModelRiskAlert; att.AttachmentFile = ((Updater)control).FileName; // Nombre del archivo, sin el ID adelante. att.Content = Convert.ToBase64String(((Updater)control).Content); ModelRiskAlertHelper.SaveAttachment(att); } } } // Save the Alert: _alert = ModelRiskAlertHelper.Save(_alert); if (modelRiskAlertPhaseListToSave.Count > 0) { foreach (ModelRiskAlertPhase mrap in modelRiskAlertPhaseListToSave) { mrap.IDModelRiskAlert = _alert.IDModelRiskAlert; ModelRiskAlertHelper.SavePhase(mrap); } } if (regionIDsToAdd.Count > 0) { foreach (int regionID in regionIDsToAdd) { ModelRiskAlertRegion mrar = new ModelRiskAlertRegion(); mrar.IDModelRiskAlert = _alert.IDModelRiskAlert; mrar.IDRegion = regionID; ModelRiskAlertHelper.SaveRegion(mrar); } } if (modelRiskAlertRegionsToDelete.Count > 0) { foreach (ModelRiskAlertRegion mrar in modelRiskAlertRegionsToDelete) { ModelRiskAlertHelper.DeleteRegion(mrar); } } this.Close(); CustomMessageBox.ShowMessage(ResourceHelper.GetResourceText("RiskAlertSaved")); } }