示例#1
0
        public ActionResult SubmitRisk(RCSARiskInputModel rCSARiskInputModel, HttpPostedFileBase imageFile)
        {
            //var formData = Request.Form;
            HttpPostedFile files = System.Web.HttpContext.Current.Request.Files["files"];

            byte[]         fileData;
            RiskAttachment attachmentFile = new RiskAttachment();

            if (files != null)
            {
                using (var binaryReader = new BinaryReader(files.InputStream))
                {
                    fileData = binaryReader.ReadBytes(files.ContentLength);
                }
                attachmentFile = new RiskAttachment()
                {
                    Data          = fileData,
                    Filename      = files.FileName,
                    ContentLength = files.ContentLength,
                    ContentType   = files.ContentType
                };
                attachmentFile.AttachName = rCSARiskInputModel.attachName;
                attachmentFile.Notes      = rCSARiskInputModel.notes;
            }
            var risk_event_id   = rCSARiskInputModel.risk_event;          //int.Parse(Request.Form["risk_event"]);
            var risk_event_note = rCSARiskInputModel.risk_event_note;     //Request.Form["risk_event_note"];

            var risk_date = rCSARiskInputModel.risk_date;                 //Request.Form["risk_date"];

            var risk_causes      = rCSARiskInputModel.risk_causes;        //Request.Form.GetValues("risk_causes").Select(int.Parse).ToArray();
            var risk_cause_notes = rCSARiskInputModel.risk_cause_notes;   //Request.Form.GetValues("risk_cause_notes");

            var risk_effects      = rCSARiskInputModel.risk_effects;      //Request.Form.GetValues("risk_effects").Select(int.Parse).ToArray();
            var risk_effect_notes = rCSARiskInputModel.risk_effect_notes; //Request.Form.GetValues("risk_effect_notes");

            var riskEvent = db.RiskEvents.First(d => d.RiskEventID == risk_event_id);

            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    var riskData = new Risk();
                    var data     = Utils.LoadUserDataFromSession();

                    Risk risk = new Risk();
                    var  re   = db.RiskEvents.Where(m => m.RiskEventID == risk_event_id).FirstOrDefault();
                    risk.UserId      = data.UserId;
                    risk.JobTitle    = data.JobTitle;
                    risk.RiskCode    = Utils.GetFormattedSerialNumber(data);
                    risk.RiskName    = re.RiskEvent1;
                    risk.RiskDate    = DateTime.Now;
                    risk.OrgPos      = data.OrgPos;
                    risk.DeptId      = data.DeptId;
                    risk.SubDeptId   = data.SubDeptId;
                    risk.DivisionId  = data.DivisionId;
                    risk.SubDivId    = data.SubDivId;
                    risk.BranchId    = data.BranchId;
                    risk.SubBranchId = data.SubBranchId;
                    risk.BizUnitId   = data.BizUnitId;

                    risk.RiskCatId   = riskEvent.RiskType.RiskGroup.RiskCat.RiskCatId;
                    risk.RiskGroupId = riskEvent.RiskType.RiskGroup.RiskGroupId;
                    risk.RiskTypeId  = riskEvent.RiskType.RiskTypeId;

                    risk.ProbLevelId   = Utils.PROBLEVEL1;
                    risk.ImpactLevelId = rCSARiskInputModel.impactLevelId;
                    risk.RiskLevel     = risk.ProbLevelId * risk.ImpactLevelId;
                    risk.IsReadOnly    = false;
                    risk.RiskEventId   = risk_event_id;
                    risk.FRiskDate     = DateTime.ParseExact(risk_date, "dddd, dd MMMM yyyy", null);

                    risk.IsMultiCE = true;

                    db.Risks.Add(risk);
                    db.SaveChanges();
                    if (attachmentFile.Data != null)
                    {
                        attachmentFile.RiskId = risk.RiskId;
                        db.RiskAttachments.Add(attachmentFile);
                        db.SaveChanges();
                    }
                    RiskProb prob = new RiskProb();
                    prob.RiskId      = risk.RiskId;
                    prob.ProbOption  = rCSARiskInputModel.prob_level;
                    prob.FreqId      = rCSARiskInputModel.sb_freq;
                    prob.ProbLevelId = rCSARiskInputModel.sb_freq;
                    db.RiskProbs.Add(prob);
                    db.SaveChanges();

                    RiskImpact impact = new RiskImpact();
                    impact.RiskId        = risk.RiskId;
                    impact.IsMoneyImpact = rCSARiskInputModel.impact_type == "money";
                    impact.ImpactLevelId = rCSARiskInputModel.impactLevelId;
                    db.RiskImpacts.Add(impact);
                    db.SaveChanges();

                    Utils.CreateFirstApprovalSchedule(risk);
                    Utils.IncrementSerialNumber(db);

                    var riskCausesData  = db.Causes.Where(d => risk_causes.Contains(d.CauseId)).ToList();
                    var riskEffectsData = db.Effects.Where(d => risk_effects.Contains(d.EffectId)).ToList();

                    for (var i = 0; i < risk_causes.Length; i++)
                    {
                        var current_value = riskCausesData.Find(d => d.CauseId == risk_causes[i]);
                        db.RiskCauseLines.Add(new RiskCauseLine
                        {
                            CauseGroupCauseGroupId = current_value.CauseType.CauseGroup.CauseGroupId,
                            CauseTypeCauseTypeId   = current_value.CauseType.CauseTypeId,
                            CauseCauseId           = current_value.CauseId,
                            Note        = "",
                            RiskRiskId  = risk.RiskId,
                            CustomCause = ""
                        });
                    }

                    for (var i = 0; i < risk_effects.Length; i++)
                    {
                        var current_value = riskEffectsData.Find(d => d.EffectId == risk_effects[i]);
                        db.RiskEffectLines.Add(new RiskEffectLine
                        {
                            EffectGroupEffectGroupId = current_value.EffectType.EffectGroup.EffectGroupId,
                            EffectTypeEffectTypeId   = current_value.EffectType.EffectTypeId,
                            EffectEffectId           = current_value.EffectId,
                            Note         = "",
                            RiskRiskId   = risk.RiskId,
                            CustomEffect = ""
                        });
                    }


                    //alvintan
                    var probType = rCSARiskInputModel.prob_type; ///formData["prob-type"];
                    var riskId   = risk.RiskId;                  //int.Parse(formData["riskId"]);

                    double?value = null;
                    int    probLevelId;

                    switch (probType)
                    {
                    case "avail":
                        var tbAvail = rCSARiskInputModel.tb_avail;

                        prob.ProbValue  = (decimal)tbAvail;
                        prob.ProbOption = 1;

                        value = tbAvail;
                        break;

                    case "appr":
                        var tbAp1 = rCSARiskInputModel.tb_ap1;
                        var tbAp2 = rCSARiskInputModel.tb_ap2;
                        var tbAp3 = rCSARiskInputModel.tb_ap3;

                        prob.Approx1 = (decimal)tbAp1;
                        prob.Approx2 = (decimal)tbAp2;
                        prob.Approx3 = (decimal)tbAp3;

                        value         = ((double)prob.Approx1 + 4 * (double)prob.Approx2 + (double)prob.Approx3) / 6;
                        prob.Poisson1 = null;
                        prob.Poisson2 = null;
                        prob.Binom1   = null;
                        prob.Binom2   = null;
                        prob.Compare  = null;
                        prob.FreqId   = null;

                        prob.ProbOption = 3;
                        break;

                    case "diff":
                        var tbDiff = rCSARiskInputModel.tb_diff;

                        prob.Compare = (decimal)tbDiff;

                        value         = tbDiff;
                        prob.Poisson1 = null;
                        prob.Poisson2 = null;
                        prob.Binom1   = null;
                        prob.Binom2   = null;
                        prob.Approx1  = null;
                        prob.Approx2  = null;
                        prob.Approx3  = null;
                        prob.FreqId   = null;

                        prob.ProbOption = 4;
                        break;

                    case "freq":
                        var sbFreq = rCSARiskInputModel.sb_freq;
                        prob.FreqId = sbFreq;

                        prob.ProbOption = 5;

                        break;

                    default:
                        break;
                    }


                    decimal?probValue = null;
                    if (value != null)
                    {
                        probValue   = Convert.ToDecimal(value);
                        probLevelId = Utils.GetProbLevelFromValue((decimal)probValue);
                    }
                    else
                    {
                        probLevelId = (int)prob.FreqId;
                    }

                    prob.ProbValue   = probValue;
                    prob.ProbLevelId = probLevelId;

                    risk.ProbValue   = prob.ProbValue;
                    risk.ProbLevelId = prob.ProbLevelId;
                    Utils.CalcRiskLevel(risk);

                    risk.IsProbSet = true;
                    //
                    //alvintan2
                    if (rCSARiskInputModel.impact_type == "money")
                    {
                        impact.MoneyDirect   = (decimal)rCSARiskInputModel.moneyDirect;
                        impact.MoneyIndirect = (decimal)rCSARiskInputModel.moneyIndirect;
                        decimal moneyValue = 0M;
                        if (impact.MoneyDirect != null)
                        {
                            moneyValue += (decimal)impact.MoneyDirect;
                        }
                        if (impact.MoneyIndirect != null)
                        {
                            moneyValue += (decimal)impact.MoneyIndirect;
                        }
                        var impactPos = Utils.GetImpactPos(data);
                        var level     = Utils.GetImpactLevelFromMoney(moneyValue, impactPos);
                        risk.ImpactMoney   = moneyValue;
                        risk.ImpactLevelId = level;
                        Utils.CalcRiskLevel(risk);
                        SetImpactFlag(risk);
                    }
                    else
                    {
                        var riskNM = new RiskNonMoneyImpact()
                        {
                            RiskId        = risk.RiskId,
                            ImpactTypeId  = rCSARiskInputModel.impactTypeId,
                            ImpactLevelId = rCSARiskInputModel.impactLevelId
                        };
                        riskNM.ImpactDetailId = db.ImpactDetails.Where(d => d.ImpactTypeId == riskNM.ImpactTypeId && d.ImpactLevelId == riskNM.ImpactLevelId).First().ImpactDetailId;
                        db.RiskNonMoneyImpacts.Add(riskNM);
                        SetImpactFlag(risk);
                    }
                    //
                    db.SaveChanges();
                    trans.Complete();
                    return(Json(risk.RiskId));
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                              ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return(Json(e.ToString()));
                }
                catch (Exception e)
                {
                    Console.WriteLine("- Source: \"{0}\", Error: \"{1}\"", e.Source, e.Message);
                    return(Json(e.ToString()));
                }
            }
        }
        // Handle hashes for everything but the managers / service owners isCertified and the
        // risk rating. The isCertified stuff gets reset everytime a new cycle happens and
        // risk rating is calculated from threat and likelihood ratings so we can ignore those
        public override int GetHashCode()
        {
            // Role stuff
            int hashRolePrivId = RolePrivId == null ? 0 : RolePrivId.GetHashCode();

            int hashRoleId           = RoleId == null ? 0 : RoleId.GetHashCode();
            int hashRoleName         = RoleName == null ? 0 : RoleName.GetHashCode();
            int hashRoleDescription  = RoleDescription == null ? 0 : RoleDescription.GetHashCode();
            int hashRoleOwner_RoleId = RoleOwner_RoleId == null ? 0 : RoleOwner_RoleId.GetHashCode();

            // Manager stuff
            int hashRoleOwner_PrivId                  = RoleOwner_PrivId == null ? 0 : RoleOwner_PrivId.GetHashCode();
            int hashRoleOwner_PermissionGroup         = RoleOwner_PermissionGroup == null ? 0 : RoleOwner_PermissionGroup.GetHashCode();
            int hashRoleOwner_ServicePrivSummary      = RoleOwner_ServicePrivSummary == null ? 0 : RoleOwner_ServicePrivSummary.GetHashCode();
            int hashRoleOwner_CredentialStorageMethod = RoleOwner_CredentialStorageMethod == null ? 0 : RoleOwner_CredentialStorageMethod.GetHashCode();
            int hashRoleOwner_ServiceId               = RoleOwner_ServiceId == null ? 0 : RoleOwner_ServiceId.GetHashCode();
            int hashRoleOwner_ServiceName             = RoleOwner_ServiceName == null ? 0 : RoleOwner_ServiceName.GetHashCode();
            int hashRoleOwner_ServiceDescription      = RoleOwner_ServiceDescription == null ? 0 : RoleOwner_ServiceDescription.GetHashCode();

            int hashRoleOwner_RoleAccessJustification = RoleOwner_RoleAccessJustification == null ? 0 : RoleOwner_RoleAccessJustification.GetHashCode();
            int hashRoleOwner_RemovalImpact           = RoleOwner_RemovalImpact == null ? 0 : RoleOwner_RemovalImpact.GetHashCode();
            int hashRoleOwner_IsRevoked     = RoleOwner_IsRevoked == null ? 0 : RoleOwner_IsRevoked.GetHashCode();
            int hashRoleOwner_DateCertified = RoleOwner_DateCertified == null ? 0 : RoleOwner_DateCertified.GetHashCode();

            // Service Owner Stuff
            int hashServiceOwner_PrivId                  = ServiceOwner_PrivId == null ? 0 : ServiceOwner_PrivId.GetHashCode();
            int hashServiceOwner_PermissionGroup         = ServiceOwner_PermissionGroup == null ? 0 : ServiceOwner_PermissionGroup.GetHashCode();
            int hashServiceOwner_ServicePrivSummary      = ServiceOwner_ServicePrivSummary == null ? 0 : ServiceOwner_ServicePrivSummary.GetHashCode();
            int hashServiceOwner_CredentialStorageMethod = ServiceOwner_CredentialStorageMethod == null ? 0 : ServiceOwner_CredentialStorageMethod.GetHashCode();
            int hashServiceOwner_ServiceId               = ServiceOwner_ServiceId == null ? 0 : ServiceOwner_ServiceId.GetHashCode();
            int hashServiceOwner_ServiceName             = ServiceOwner_ServiceName == null ? 0 : ServiceOwner_ServiceName.GetHashCode();
            int hashServiceOwner_ServiceDescription      = ServiceOwner_ServiceDescription == null ? 0 : ServiceOwner_ServiceDescription.GetHashCode();

            int hashServiceOwner_RoleAccessJustification = ServiceOwner_RoleAccessJustification == null ? 0 : ServiceOwner_RoleAccessJustification.GetHashCode();
            int hashServiceOwner_RemovalImpact           = ServiceOwner_RemovalImpact == null ? 0 : ServiceOwner_RemovalImpact.GetHashCode();
            int hashServiceOwner_IsRevoked     = ServiceOwner_IsRevoked == null ? 0 : ServiceOwner_IsRevoked.GetHashCode();
            int hashServiceOwner_DateCertified = ServiceOwner_DateCertified == null ? 0 : ServiceOwner_DateCertified.GetHashCode();


            // Risk Stuff
            int hashRiskImpact         = RiskImpact == null ? 0 : RiskImpact.GetHashCode();
            int hashRiskLikelihood     = RiskLikelihood == null ? 0 : RiskLikelihood.GetHashCode();
            int hashRiskNotes          = RiskNotes == null ? 0 : RiskNotes.GetHashCode();
            int hashRiskAssessmentDate = RiskAssessmentDate == null ? 0 : ServiceOwner_DateCertified.GetHashCode();


            // Calculate the hash code for the object.
            return // Role stuff
                   (hashRolePrivId ^
                    hashRoleId ^
                    hashRoleName ^
                    hashRoleDescription ^
                    hashRoleOwner_RoleId ^

                    // Manager stuff
                    hashRoleOwner_PrivId ^
                    hashRoleOwner_PermissionGroup ^
                    hashRoleOwner_ServicePrivSummary ^
                    hashRoleOwner_CredentialStorageMethod ^
                    hashRoleOwner_ServiceId ^
                    hashRoleOwner_ServiceName ^
                    hashRoleOwner_ServiceDescription ^

                    hashRoleOwner_RoleAccessJustification ^
                    hashRoleOwner_RemovalImpact ^
                    hashRoleOwner_IsRevoked ^
                    hashRoleOwner_DateCertified ^

                    // Service Owner Stuff
                    hashServiceOwner_PrivId ^
                    hashServiceOwner_PermissionGroup ^
                    hashServiceOwner_ServicePrivSummary ^
                    hashServiceOwner_CredentialStorageMethod ^
                    hashServiceOwner_ServiceId ^
                    hashServiceOwner_ServiceName ^
                    hashServiceOwner_ServiceDescription ^

                    hashServiceOwner_RoleAccessJustification ^
                    hashServiceOwner_RemovalImpact ^
                    hashServiceOwner_IsRevoked ^
                    hashServiceOwner_DateCertified ^

                    // Risk Stuff
                    hashRiskImpact ^
                    hashRiskLikelihood ^
                    hashRiskNotes ^
                    hashRiskAssessmentDate);
        }