private void SaveIssueTracking(Issue issue, Issue originalIssue, CmsEntities cee)
        {
            log.Verbose("SaveIssueTracking()");

            IssueTracking originalIssueTracking = (from x in cee.IssueTrackings where x.Id == issue.IssueTrackingId select x).FirstOrDefault();

            if (originalIssueTracking == null)
            {
                if (issue.IssueTracking != null)
                {
                    var newIssueTracking = new IssueTracking
                    {
                        Budgeted = issue.IssueTracking.Budgeted,
                        FundingType = issue.IssueTracking.FundingType,
                        EstimatedCost = issue.IssueTracking.EstimatedCost,
                        Effort = issue.IssueTracking.Effort,
                        Return = issue.IssueTracking.Return,
                        Scoped = issue.IssueTracking.Scoped,
                        ScopedUserId = issue.IssueTracking.ScopedUserId,
                        ScopedDate = issue.IssueTracking.ScopedDate,
                        PeerReviewed = issue.IssueTracking.PeerReviewed,
                        PeerReviewedUserId = issue.IssueTracking.PeerReviewedUserId,
                        PeerReviewedDate = issue.IssueTracking.PeerReviewedDate,
                        ScopedEstimate = issue.IssueTracking.ScopedEstimate,
                        ScopedActual = issue.IssueTracking.ScopedActual,
                        PeerReviewEstimate = issue.IssueTracking.PeerReviewEstimate,
                        PeerReviewActual = issue.IssueTracking.PeerReviewActual,
                        ImplementedEstimate = issue.IssueTracking.ImplementedEstimate,
                        ImplementedActual = issue.IssueTracking.ImplementedActual,
                        TestedEstimate = issue.IssueTracking.TestedEstimate,
                        TestedActual = issue.IssueTracking.TestedActual,
                        DocumentedEstimate = issue.IssueTracking.DocumentedEstimate,
                        DocumentedActual = issue.IssueTracking.DocumentedActual
                    };

                    cee.IssueTrackings.Add(newIssueTracking);
                    cee.SaveChanges();

                    issue.IssueTrackingId = newIssueTracking.Id;
                }
            }
            else
            {
                cee.Entry(originalIssueTracking).CurrentValues.SetValues(issue.IssueTracking);
                cee.SaveChanges();
            }
        }
        public List<IssueTracking> GetIssueRankings(List<int> issueIds)
        {
            //The IssueTracking class has been extended with a 'Ranking' property
            //and an 'IssueId' property..

            var result = new List<IssueTracking>(issueIds.Count);

            using (var cee = new CmsEntities())
            {
                List<IssueReward> rewards = cee.IssueRewards.ToList();

                int[] intIdArray = issueIds.ToArray();

                List<Issue> issues = (from x in cee.Issues.Include("IssueRisks")
                                      where intIdArray.Contains(x.Id)
                                      select x).ToList();

                double costCoefficient = (from x in cee.CalculatedRatingCoefficients where x.Code.Equals("Cost", StringComparison.CurrentCultureIgnoreCase) select x.Value).FirstOrDefault();
                double effortCoefficient = (from x in cee.CalculatedRatingCoefficients where x.Code.Equals("Effort", StringComparison.CurrentCultureIgnoreCase) select x.Value).FirstOrDefault();
                double returnCoefficient = (from x in cee.CalculatedRatingCoefficients where x.Code.Equals("Return", StringComparison.CurrentCultureIgnoreCase) select x.Value).FirstOrDefault();
                double existingRiskCoefficient = (from x in cee.CalculatedRatingCoefficients where x.Code.Equals("ExistingRisk", StringComparison.CurrentCultureIgnoreCase) select x.Value).FirstOrDefault();

                foreach (Issue issue in issues)
                {
                    var trackingResult = new IssueTracking { IssueId = issue.Id }; //new one up to keep the return very light/fast.

                    if (!issue.IssueTrackingId.HasValue)
                    {
                        trackingResult.Ranking = 0;
                        result.Add(trackingResult);
                        continue;
                    }

                    IssueTracking issueTracking = (from x in cee.IssueTrackings where x.Id == issue.IssueTrackingId.Value select x).FirstOrDefault();
                    if (issueTracking == null)
                    {
                        trackingResult.Ranking = 0;
                        result.Add(trackingResult);
                        continue;
                    }

                    if (costCoefficient.Equals(0))
                    {
                        trackingResult.Ranking = 0;
                        result.Add(trackingResult);
                        continue;
                    }

                    double costValue = issueTracking.EstimatedCost.HasValue ? issueTracking.EstimatedCost.Value : 0;
                    if (costValue.Equals(0))
                    {
                        trackingResult.Ranking = 0;
                    }

                    decimal returnDollar = (from x in rewards where x.Reward == issueTracking.Return select x.ReturnDollar).FirstOrDefault();

                    int effortValue = issueTracking.Effort.HasValue ? issueTracking.Effort.Value : 0;

                    int highestRiskRatingNumber = GetHighestRiskRatingNumber(issue, cee);

                    //updated as per https://jira.issgroup.com.au/browse/BODCMS-1289
                    double ranking = (highestRiskRatingNumber * highestRiskRatingNumber * existingRiskCoefficient) + ((13 - effortValue) * effortCoefficient) *
                          (((double)returnDollar * returnCoefficient) / (costValue * costCoefficient));

                    if (ranking.ToString().Equals("NaN", StringComparison.CurrentCultureIgnoreCase) || ranking.ToString().Equals("Infinity", StringComparison.CurrentCultureIgnoreCase))
                    {
                        ranking = 0;
                    }

                    trackingResult.Ranking = Math.Round(ranking, 0);

                    result.Add(trackingResult);
                }
            }

            return result;
        }