Source

main-vinayFork / Source / Excel / Excel.AddIn / Excel.AddIn / Extensions / ErrorPaneViewModelExtension.cs

Full commit
using System.Collections.Generic;
using System.Linq;
using DataUp.Excel.BizLogic;

namespace DataUp.Excel.Addin
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class ErrorPaneViewModelExtension
    {
        public static void Update(this ErrorPaneViewModel errorPaneViewModel, IEnumerable<ErrorDetail> errors, IEnumerable<string> sheets)
        {
            if (errorPaneViewModel != null && errors != null)
            {
                List<ErrorDetailViewModel> errorDetails = new List<ErrorDetailViewModel>();
                errorPaneViewModel.DisableRemoveAll = errors.Count() > 0;

                var distinctType = errors.Select(err => new { err.ControlName, err.SheetName }).Distinct();

                foreach (var item in distinctType)
                {
                    var typeErrors = errors.Where(err => err.ControlName == item.ControlName && string.Compare(err.SheetName, item.SheetName, true) == 0);

                    var errorLocations = string.Join(",", typeErrors.Select(err => err.ErrorAddress).ToList());

                    var firstError = typeErrors.FirstOrDefault();
                    errorDetails.Add(new ErrorDetailViewModel()
                    {
                        Error = firstError.Error,
                        Category = firstError.Category,
                        Title = firstError.ControlName.Replace("$", ""),
                        Description = firstError.Description,
                        Recommendation = firstError.Recommendation,
                        ErrorAddress = errorLocations,
                        DisplayErrorAddress = errorLocations.Replace("$", ""),
                        SheetName = firstError.SheetName,
                        IsRemovable = firstError.IsRemovable
                    });
                }

                List<SheetViewModel> sheetDetails = new List<SheetViewModel>();
                foreach (var item in sheets)
                {
                    var sheetErrors = from error in errorDetails
                                      where string.Compare(error.SheetName, item, true) == 0
                                      orderby error.IsRemovable descending
                                      select error;

                    var sheetDetail = new SheetViewModel()
                    {
                        SheetName = item,
                        TotalErrorCount = sheetErrors.Count()
                    };

                    foreach (var sheetError in sheetErrors.ToList())
                    {
                        sheetDetail.Errors.Add(sheetError);
                    }

                    sheetDetails.Add(sheetDetail);
                }

                errorPaneViewModel.SheetDetails.Clear();
                foreach (var item in sheetDetails)
                {
                    errorPaneViewModel.SheetDetails.Add(item);
                }
            }
        }
    }
}