/// <summary>
        /// Grid Element Exporting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radGridSnapshot_ElementExporting(object sender, GridViewElementExportingEventArgs e)
        {
            RadGridView_ElementExport.ElementExporting(e, isGroupFootersVisible: false, cellValueConverter: () =>
            {
                object result = e.Value;
                if (e.Value == null && e.Element == ExportElement.Cell)
                {
                    GridViewDataColumn column = (e.Context as GridViewDataColumn);
                    if (column != null)
                    {
                        if (column.DisplayIndex == 0)
                        {
                            MarketSnapshotPerformanceData data = column.DataControl.Items[++itemIndex] as MarketSnapshotPerformanceData;
                            if (data != null)
                            {
                                result = string.Format("{0}{1}{2}", data.MarketSnapshotPreferenceInfo.EntityNodeValueName
                                                       , String.IsNullOrEmpty(data.MarketSnapshotPreferenceInfo.EntityNodeValueName) ? "" : " "
                                                       , data.MarketSnapshotPreferenceInfo.EntityName);
                            }
                        }
                    }
                }

                return(result);
            });
        }
        private object radGridSnapshot_PdfElementExporting(int rowIndex, int columnIndex, object columnCollection, object itemCollection)
        {
            IList <GridViewBoundColumnBase> columns = columnCollection as IList <GridViewBoundColumnBase>;
            IList items = itemCollection as IList;

            if (columns == null || items == null)
            {
                return(null);
            }
            object result = columns[columnIndex].GetValueForItem(items[rowIndex]);

            if (columns[columnIndex].DataMemberBinding.Path.Path == "Description")
            {
                MarketSnapshotPerformanceData data = items[rowIndex] as MarketSnapshotPerformanceData;
                if (data != null)
                {
                    result = string.Format("{0}{1}{2}", data.MarketSnapshotPreferenceInfo.EntityNodeValueName
                                           , String.IsNullOrEmpty(data.MarketSnapshotPreferenceInfo.EntityNodeValueName) ? "" : " "
                                           , data.MarketSnapshotPreferenceInfo.EntityName);
                }
            }
            return(result);
        }
        /// <summary>
        /// Gets performance data for a specific snapshot preference where entity type is benchmark
        /// </summary>
        /// <param name="entity">Dimension service entity instance</param>
        /// <param name="preference">MarketSnapshotPreference object</param>
        /// <returns>MarketSnapshotPerformanceData</returns>
        public static MarketSnapshotPerformanceData GetBenchmarkPerformanceData(DimensionEntities entity, MarketSnapshotPreference preference)
        {
            MarketSnapshotPerformanceData result = new MarketSnapshotPerformanceData();

            try
            {
                List <GreenField.DAL.GF_PERF_DAILY_ATTRIBUTION> benchmarkRecords = entity.GF_PERF_DAILY_ATTRIBUTION
                                                                                   .Where(record => record.NODE_NAME == (preference.EntityNodeType == "Country" ? "Country" : "GICS Level 1") &&
                                                                                          record.AGG_LVL_1 == (preference.EntityNodeType == null ? "Undefined" : preference.EntityNodeValueCode) &&
                                                                                          record.AGG_LVL_1_LONG_NAME == (preference.EntityNodeType == null ? "-" : preference.EntityNodeValueName) &&
                                                                                          record.BM == preference.EntityId.ToUpper() &&
                                                                                          record.BMNAME == preference.EntityName &&
                                                                                          record.TO_DATE != null &&
                                                                                          record.POR_INCEPTION_DATE != null)
                                                                                   .OrderByDescending(record => record.TO_DATE).ToList();

                GreenField.DAL.GF_PERF_DAILY_ATTRIBUTION lastRecord = benchmarkRecords.FirstOrDefault();

                GreenField.DAL.GF_PERF_DAILY_ATTRIBUTION benchmarkRecord = lastRecord != null?GetMinInceptionDateRecord <GreenField.DAL.GF_PERF_DAILY_ATTRIBUTION>(benchmarkRecords
                                                                                                                                                                   .Where(record => record.TO_DATE == lastRecord.TO_DATE).ToList()) : null;

                result.DateToDateReturn = benchmarkRecord != null ? (preference.EntityNodeType == null
                    ? benchmarkRecord.BM1_TOP_RC_TWR_1D * Convert.ToDecimal(100) : benchmarkRecord.BM1_RC_TWR_1D * Convert.ToDecimal(100)) : null;
                result.WeekToDateReturn = benchmarkRecord != null ? (preference.EntityNodeType == null
                    ? benchmarkRecord.BM1_TOP_RC_TWR_1W * Convert.ToDecimal(100) : benchmarkRecord.BM1_RC_TWR_1W * Convert.ToDecimal(100)) : null;
                result.MonthToDateReturn = benchmarkRecord != null ? (preference.EntityNodeType == null
                    ? benchmarkRecord.BM1_TOP_RC_TWR_MTD * Convert.ToDecimal(100) : benchmarkRecord.BM1_RC_TWR_MTD * Convert.ToDecimal(100)) : null;
                result.QuarterToDateReturn = benchmarkRecord != null ? (preference.EntityNodeType == null
                    ? benchmarkRecord.BM1_TOP_RC_TWR_QTD * Convert.ToDecimal(100) : benchmarkRecord.BM1_RC_TWR_QTD * Convert.ToDecimal(100)) : null;
                result.YearToDateReturn = benchmarkRecord != null ? (preference.EntityNodeType == null
                    ? benchmarkRecord.BM1_TOP_RC_TWR_YTD * Convert.ToDecimal(100) : benchmarkRecord.BM1_RC_TWR_YTD * Convert.ToDecimal(100)) : null;

                if (preference.EntityNodeType == null)
                {
                    GreenField.DAL.GF_PERF_TOPLEVELYEAR benchmarkLastYearRecord = GetMinInceptionDateRecord <GreenField.DAL.GF_PERF_TOPLEVELYEAR>(entity.GF_PERF_TOPLEVELYEAR
                                                                                                                                                  .Where(g => g.CURRENCY.ToUpper() == "USD" &&
                                                                                                                                                         g.RETURN_TYPE.ToUpper() == "NET" &&
                                                                                                                                                         g.TO_DATE == "31/12/" + (DateTime.Today.Year - 1).ToString() &&
                                                                                                                                                         g.BM1ID.ToUpper() == preference.EntityId.ToUpper() &&
                                                                                                                                                         g.BM1NAME.ToUpper() == preference.EntityName.ToUpper() &&
                                                                                                                                                         g.POR_INCEPTION_DATE != null)
                                                                                                                                                  .ToList());

                    result.LastYearReturn = preference.EntityNodeType == null ? (benchmarkLastYearRecord != null
                        ? benchmarkLastYearRecord.BM1_RC_TWR_YTD * Convert.ToDecimal(100) : null) : null;

                    GreenField.DAL.GF_PERF_TOPLEVELYEAR benchmarkSecondLastYearRecord = GetMinInceptionDateRecord <GreenField.DAL.GF_PERF_TOPLEVELYEAR>(entity.GF_PERF_TOPLEVELYEAR
                                                                                                                                                        .Where(g => g.CURRENCY.ToUpper() == "USD" &&
                                                                                                                                                               g.RETURN_TYPE.ToUpper() == "NET" &&
                                                                                                                                                               g.TO_DATE == "31/12/" + (DateTime.Today.Year - 2).ToString() &&
                                                                                                                                                               g.BM1ID.ToUpper() == preference.EntityId.ToUpper() &&
                                                                                                                                                               g.BM1NAME.ToUpper() == preference.EntityName.ToUpper() &&
                                                                                                                                                               g.POR_INCEPTION_DATE != null)
                                                                                                                                                        .ToList());

                    result.SecondLastYearReturn = preference.EntityNodeType == null ? (benchmarkSecondLastYearRecord != null
                        ? benchmarkSecondLastYearRecord.BM1_RC_TWR_YTD * Convert.ToDecimal(100) : null) : null;

                    GreenField.DAL.GF_PERF_TOPLEVELYEAR benchmarkThirdLastYearRecord = GetMinInceptionDateRecord <GreenField.DAL.GF_PERF_TOPLEVELYEAR>(entity.GF_PERF_TOPLEVELYEAR
                                                                                                                                                       .Where(g => g.CURRENCY.ToUpper() == "USD" &&
                                                                                                                                                              g.RETURN_TYPE.ToUpper() == "NET" &&
                                                                                                                                                              g.TO_DATE == "31/12/" + (DateTime.Today.Year - 3).ToString() &&
                                                                                                                                                              g.BM1ID.ToUpper() == preference.EntityId.ToUpper() &&
                                                                                                                                                              g.BM1NAME.ToUpper() == preference.EntityName.ToUpper() &&
                                                                                                                                                              g.POR_INCEPTION_DATE != null)
                                                                                                                                                       .ToList());

                    result.ThirdLastYearReturn = preference.EntityNodeType == null ? (benchmarkThirdLastYearRecord != null
                        ? benchmarkThirdLastYearRecord.BM1_RC_TWR_YTD * Convert.ToDecimal(100) : null) : null;
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(result);
        }
        /// <summary>
        /// ReorderBehavior Reordering Event Handler - Adjusts Entity Order and GroupPreferenceId of entities after reordering
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">ReorderingEventArgs</param>
        private void ReorderBehavior_Reordering(object sender, ReorderingEventArgs e)
        {
            //if there are no dragged Items reordering is redundant
            if (e.DraggedItems.Count().Equals(0))
            {
                return;
            }

            #region Get Dragged Element
            //dragged Element
            MarketSnapshotPerformanceData draggedElement = e.DraggedItems.FirstOrDefault() as MarketSnapshotPerformanceData;

            //null Exception Handling
            if (draggedElement == null)
            {
                return;
            }
            #endregion

            #region Get Data Context
            //collection of MorningSnapshotData binded to the grid
            List <MarketSnapshotPerformanceData> dataContext = (e.SourceGrid.ItemsSource as ObservableCollection <MarketSnapshotPerformanceData>).ToList();

            //null Exception Handling
            if (dataContext == null)
            {
                return;
            }
            #endregion

            #region Get Drag Drop Details
            //drag drop Indexes
            int dragIndex = dataContext.IndexOf(draggedElement);
            int dropIndex = e.DropIndex;
            //true if insertion is done after an element and False if it's done  before the element
            bool isDropPositionAfter = DragDropPosition.Value == DropPosition.After;
            //check if the drop Index exceed the Count of List - item dropped after the last element
            bool isDropIndexExceedingCount = dropIndex >= dataContext.Count;
            #endregion

            #region Drag Location Parameters
            MarketSnapshotPreference dragBenchmarkDetails = new MarketSnapshotPreference()
            {
                EntityName        = dataContext.ElementAt(dragIndex).MarketSnapshotPreferenceInfo.EntityName,
                EntityReturnType  = dataContext.ElementAt(dragIndex).MarketSnapshotPreferenceInfo.EntityReturnType,
                EntityOrder       = dataContext.ElementAt(dragIndex).MarketSnapshotPreferenceInfo.EntityOrder,
                GroupName         = dataContext.ElementAt(dragIndex).MarketSnapshotPreferenceInfo.GroupName,
                GroupPreferenceID = dataContext.ElementAt(dragIndex).MarketSnapshotPreferenceInfo.GroupPreferenceID
            };
            #endregion

            #region Drop Location Parameters
            MarketSnapshotPreference dropBenchmarkDetails = new MarketSnapshotPreference()
            {
                EntityName       = isDropIndexExceedingCount ? null : dataContext.ElementAt(dropIndex).MarketSnapshotPreferenceInfo.EntityName,
                EntityReturnType = isDropIndexExceedingCount ? null : dataContext.ElementAt(e.DropIndex).MarketSnapshotPreferenceInfo.EntityReturnType,
                EntityOrder      = isDropIndexExceedingCount
                    ? dataContext.ElementAt(dropIndex - 1).MarketSnapshotPreferenceInfo.EntityOrder + 1
                    : dataContext.ElementAt(dropIndex).MarketSnapshotPreferenceInfo.EntityOrder,
                GroupName = isDropIndexExceedingCount
                    ? dataContext.ElementAt(dropIndex - 1).MarketSnapshotPreferenceInfo.GroupName
                    : dataContext.ElementAt(dropIndex).MarketSnapshotPreferenceInfo.GroupName,
                GroupPreferenceID = isDropIndexExceedingCount
                    ? dataContext.ElementAt(dropIndex - 1).MarketSnapshotPreferenceInfo.GroupPreferenceID
                    : dataContext.ElementAt(dropIndex).MarketSnapshotPreferenceInfo.GroupPreferenceID
            };
            #endregion

            #region Managing discrepancies in drop location
            //if drop location is after the last element of same or another group the behavior picks the next element nevertheless
            bool isDragdropGroupSame = dragBenchmarkDetails.GroupPreferenceID == dropBenchmarkDetails.GroupPreferenceID;
            bool isDropEntityMisread = dropBenchmarkDetails.EntityOrder == 1 && isDropPositionAfter;

            bool isDropLastOfSameGroup = GetLastGroupPreferenceId(dataContext, dropBenchmarkDetails.GroupPreferenceID)
                                         == dragBenchmarkDetails.GroupPreferenceID && isDropEntityMisread;
            bool isDropLastOfOtherGroup = (!isDropLastOfSameGroup) && isDropEntityMisread;
            #endregion

            bool isDragWithinGroup = isDragdropGroupSame ? (!isDropLastOfOtherGroup) : isDropLastOfSameGroup;

            if (isDragWithinGroup)
            {
                UpdateParametersForSameGroupReordering(dataContext, dragBenchmarkDetails, dropBenchmarkDetails, isDropLastOfSameGroup);
            }
            else
            {
                UpdateParametersForDiffGroupReordering(dataContext, dragBenchmarkDetails, dropBenchmarkDetails, isDropLastOfOtherGroup);
            }
        }
        /// <summary>
        /// Gets performance data for a specific snapshot preference where entity type is security, index or commodity
        /// </summary>
        /// <param name="entity">Dimension service entity instance</param>
        /// <param name="preference">MarketSnapshotPreference object</param>
        /// <returns>MarketSnapshotPerformanceData</returns>
        public static MarketSnapshotPerformanceData GetSecurityCommodityIndexPerformanceData(DimensionEntities entity, MarketSnapshotPreference preference)
        {
            MarketSnapshotPerformanceData result = new MarketSnapshotPerformanceData();

            try
            {
                DateTime TrackDate = DateTime.Today;

                GreenField.DAL.GF_PRICING_BASEVIEW lastDateToDateRecord = entity.GF_PRICING_BASEVIEW
                                                                          .Where(record => record.INSTRUMENT_ID == preference.EntityId &&
                                                                                 record.TYPE == preference.EntityType &&
                                                                                 record.ISSUE_NAME == preference.EntityName &&
                                                                                 record.FROMDATE != null)
                                                                          .OrderByDescending(record => record.FROMDATE)
                                                                          .Take(1).FirstOrDefault();

                if (lastDateToDateRecord == null)
                {
                    return(result);
                }

                GreenField.DAL.GF_PRICING_BASEVIEW firstRecord = entity.GF_PRICING_BASEVIEW
                                                                 .Where(record => record.INSTRUMENT_ID == preference.EntityId &&
                                                                        record.TYPE == preference.EntityType &&
                                                                        record.ISSUE_NAME == preference.EntityName &&
                                                                        record.FROMDATE != null)
                                                                 .OrderBy(record => record.FROMDATE)
                                                                 .Take(1).FirstOrDefault();

                DateTime firstRecordDateTime = DateTime.Today.AddYears(-5);
                if (firstRecord != null)
                {
                    firstRecordDateTime = Convert.ToDateTime(firstRecord.FROMDATE);
                }

                DateTime lastBusinessDateTime = TrackDate = Convert.ToDateTime(lastDateToDateRecord.FROMDATE);
                result.DateToDateReturn = lastDateToDateRecord != null ? (preference.EntityReturnType == "Price"
                    ? lastDateToDateRecord.DAILY_PRICE_RETURN != null ? lastDateToDateRecord.DAILY_PRICE_RETURN : 0
                    : lastDateToDateRecord.DAILY_GROSS_RETURN != null ? lastDateToDateRecord.DAILY_GROSS_RETURN : 0) : null;

                result.WeekToDateReturn = GetReturn(entity, preference, lastBusinessDateTime.AddDays(-6) > firstRecordDateTime
                    ? lastBusinessDateTime.AddDays(-6) : firstRecordDateTime, lastBusinessDateTime);
                result.MonthToDateReturn = GetReturn(entity, preference, new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1) > firstRecordDateTime
                    ? new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1) : firstRecordDateTime, lastBusinessDateTime);
                result.QuarterToDateReturn = GetReturn(entity, preference
                                                       , new DateTime(DateTime.Today.Year, (DateTime.Today.Month - ((DateTime.Today.Month - 1) % 3)), 1) > firstRecordDateTime
                    ? new DateTime(DateTime.Today.Year, (DateTime.Today.Month - ((DateTime.Today.Month - 1) % 3)), 1) : firstRecordDateTime, lastBusinessDateTime);
                result.YearToDateReturn = GetReturn(entity, preference, new DateTime(DateTime.Today.Year, 1, 1) > firstRecordDateTime
                    ? new DateTime(DateTime.Today.Year, 1, 1) : firstRecordDateTime, lastBusinessDateTime);
                result.LastYearReturn = GetReturn(entity, preference, new DateTime(DateTime.Today.Year - 1, 1, 1) > firstRecordDateTime
                    ? new DateTime(DateTime.Today.Year - 1, 1, 1) : firstRecordDateTime, new DateTime(DateTime.Today.Year - 1, 12, 31));
                result.SecondLastYearReturn = GetReturn(entity, preference, new DateTime(DateTime.Today.Year - 2, 1, 1) > firstRecordDateTime
                    ? new DateTime(DateTime.Today.Year - 2, 1, 1) : firstRecordDateTime, new DateTime(DateTime.Today.Year - 2, 12, 31));
                result.ThirdLastYearReturn = GetReturn(entity, preference, new DateTime(DateTime.Today.Year - 3, 1, 1) > firstRecordDateTime
                    ? new DateTime(DateTime.Today.Year - 3, 1, 1) : firstRecordDateTime, new DateTime(DateTime.Today.Year - 3, 12, 31));
            }
            catch (Exception)
            {
                throw;
            }
            return(result);
        }