Ejemplo n.º 1
0
        private void InsertData(IList<InterlockRiskDataAdapter> importData)
        {
            if (importData.Count == 0)
            {
                RaiseMessage(CommonUtils.MessageType.Warning, NoDataFoundForWorkSheetMessage());
                return;
            }

            for (int i = 0; i < importData.Count; i++)
            {
                InterlockRiskDataAdapter adapter = importData[i];

                //ControlSystem  - - must find existing
                ControlSystem matchingControl = (from x in Cee.ControlSystems where x.Name.Equals(adapter.ControlSystemName, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();

                if (matchingControl == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNameDoesNotExistInDbMessage(adapter.ControlSystemName, i + 1)));
                    continue;
                }

                //InterlockType - must find existing
                InterlockType matchingInterlockType = mExistInterlockTypes.FirstOrDefault(x => string.Compare(x.Name, adapter.InterlockTypeName, true, CultureInfo.CurrentCulture) == 0);
                if (matchingInterlockType == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNameDoesNotExistInDbMessage(adapter.InterlockTypeName, i + 1)));
                    continue;
                }

                //Interlock - must find existing
                Interlock matchingInterlock = (from x in Cee.Interlocks where x.ControlSystemId == matchingControl.Id && x.InterlockTypeId == matchingInterlockType.Id select x).FirstOrDefault();
                if (matchingInterlock == null)
                {
                    string message = string.Format("Unable to find Interlock for Control '{0}' and InterlockTypeCode '{1}'. Row {2}.", matchingControl.Name, matchingInterlockType.Name, i + 1);
                    RaiseMessage(CommonUtils.MessageType.Error, message);
                    continue;
                }

                //Category
                InterlockRiskCategory matchingRiskCategory = (from x in mExistingInterlockRiskCategorys where x.Code.Equals(adapter.RiskCategory, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
                if (matchingRiskCategory == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNameDoesNotExistInDbMessage(adapter.RiskCategory, i + 1)));
                    continue;
                }

                //Likelihood
                InterlockRiskLikelihood liklihoodMatch = (from x in Cee.InterlockRiskLikelihoods where x.Name.Equals(adapter.Likelihood, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
                if (liklihoodMatch == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNameDoesNotExistInDbMessage(adapter.Likelihood, i + 1)));
                    continue;
                }

                //Consequence
                InterlockRiskConsequence consequenceMatch = (from x in Cee.InterlockRiskConsequences where x.Name.Equals(adapter.Consequence, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
                if (consequenceMatch == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNameDoesNotExistInDbMessage(adapter.Consequence, i + 1)));
                    continue;
                }

                //RiskMatrix
                InterlockRiskMatrix riskMatrixkMatch = (from x in Cee.InterlockRiskMatrices.Include("InterlockRiskRating") where x.ConsequenceId == consequenceMatch.ConsequenceRatingId && x.LikelihoodId == liklihoodMatch.Id select x).FirstOrDefault();
                if (riskMatrixkMatch == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format("Could not find a Risk Matrix database record using consequenceId '{0}' and LikelihoodId '{1}'.", consequenceMatch.Id, liklihoodMatch.Id));
                    continue;
                }

                //Description
                if (adapter.Description.Trim().Length > 4000)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, BuildPropertyTooLong("Description", 4000, i));
                    {
                        continue;
                    }
                }

                var newRisk = new InterlockRisk
                {
                    InterlockId = matchingInterlock.Id,
                    CategoryId = matchingRiskCategory.Id,
                    RiskLikelihoodId = liklihoodMatch.Id,
                    ConsequenceId = consequenceMatch.Id,
                    RiskMatrixId = riskMatrixkMatch.Id,
                    Description = adapter.Description
                };

                Cee.InterlockRisks.Add(newRisk);
                mOutPutResults.Add(new InterlockRiskResult(matchingControl, matchingInterlockType, matchingRiskCategory, consequenceMatch, liklihoodMatch, riskMatrixkMatch));
            }

            if (mOutPutResults.Count == 0)
            {
                RaiseMessage(CommonUtils.MessageType.Warning, string.Format("No Interlock Risks were added from from worksheet {0}.", WorkSheetName));
            }
            else
            {
                //SAVE
                Cee.SaveChanges();

                RaiseMessage(CommonUtils.MessageType.Added, string.Format("Processed {0} Interlock Risks.", mOutPutResults.Count));

                foreach (InterlockRiskResult result in mOutPutResults)
                {
                    RaiseMessage(CommonUtils.MessageType.Added, result.ToString());
                }
            }
        }
        private void SaveInterlockRisks(Interlock interlock, CmsEntities cee)
        {
            //Delete all existing risks that belongs to the interlock
            cee.DeleteWhere<InterlockRisk>(cee, x => x.InterlockId == interlock.Id);

            cee.Configuration.AutoDetectChangesEnabled = false;
            foreach (var item in interlock.InterlockRisks)
            {
                var newInterlockRisk = new InterlockRisk();
                newInterlockRisk.InterlockId = item.InterlockId;
                newInterlockRisk.CategoryId = item.CategoryId;
                newInterlockRisk.RiskLikelihoodId = item.RiskLikelihoodId;
                newInterlockRisk.ConsequenceId = item.ConsequenceId;
                newInterlockRisk.RiskMatrixId = item.RiskMatrixId;
                newInterlockRisk.Description = item.Description;

                cee.InterlockRisks.Add(newInterlockRisk);
            }
            cee.Configuration.AutoDetectChangesEnabled = true;
        }