/// <summary>
        /// Edits the kpi.
        /// </summary>
        /// <param name="kpiUpdate">The kpi update.</param>
        /// <param name="pfNumber">The pf number.</param>
        /// <param name="cycle">The cycle.</param>
        /// <returns>Flag indicating whether the operation was successful.</returns>
        public bool EditKpi(EditKpi kpiUpdate, int pfNumber, int? cycle)
        {
            try
            {
                if (this.kpiRule.IsPfNumberValid(pfNumber))
                {
                    if (this.kpiRule.IsKpiEditPostValid(pfNumber, kpiUpdate, cycle))
                    {
                        Kpi kpiEdit = new Kpi
                        {
                            Id = kpiUpdate.KpiId,
                            EmployeeObjectiveId = kpiUpdate.EmployeeObjectiveId,
                            Title = kpiUpdate.KpiTitle,
                            Weight = kpiUpdate.Weight,
                            Measure = kpiUpdate.Measure,
                            EmployeeRating = kpiUpdate.EmployeeRating,
                            FinalRating = kpiUpdate.FinalRating
                        };
                        this.db.Entry(kpiEdit).State = System.Data.Entity.EntityState.Modified;
                        this.db.SaveChanges();

                        return true;
                    }
                    else
                    {
                        //
                        // Handle invalid edit post.
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (DataException)
            {
                //
                // Handle exception.
                return false;
            }
            catch (Exception)
            {
                //
                // Handle Exception.
                return false;
            }
        }
        /// <summary>
        /// Determines whether the KPI edit post is valid.
        /// </summary>
        /// <param name="PfNumber">The pf number of the user posting the KPI edit.</param>
        /// <param name="UpdateKpi">The update objective.</param>
        /// <param name="PerformanceCycleId">The performance cycle identifier.</param>
        /// <returns>Flag indicating whether the KPI edit post is valid.</returns>
        public bool IsKpiEditPostValid(int PfNumber, EditKpi UpdateKpi, int? PerformanceCycleId)
        {
            try
            {
                //
                // Get the total weight of the KPI objective.
                int? objectiveTotalWeight = this.db.EmployeeObjectives
                                                .Where(eo => eo.Id == UpdateKpi.EmployeeObjectiveId)
                                                .Select(eo => (int?)eo.Weight
                                                )
                                                .FirstOrDefault();

                //
                // Get the sum of all the KPI weight that belong to the objective
                int? kpiTotalWeight = (from k in this.db.Kpis
                                       join eo in this.db.EmployeeObjectives on k.EmployeeObjectiveId equals eo.Id
                                       join o in this.db.Objectives on eo.ObjectiveId equals o.Id
                                       join e in this.db.employees on eo.EmployeeId equals e.ID
                                       where e.Code == PfNumber.ToString() && o.CycleId == PerformanceCycleId && k.EmployeeObjectiveId == UpdateKpi.EmployeeObjectiveId
                                       select new
                                       {
                                           k.Weight
                                       }).Sum(x => x.Weight);

                //
                // Get the current weight of the KPI being updated.
                int? currentWeight = this.db.Kpis
                                            .Where(k => k.Id == UpdateKpi.KpiId)
                                            .Select(k => (int?)k.Weight)
                                            .FirstOrDefault();

                if (kpiTotalWeight != null && currentWeight != null && objectiveTotalWeight != null)
                {
                    if (((kpiTotalWeight - currentWeight) + UpdateKpi.Weight) <= objectiveTotalWeight)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (DataException)
            {
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }