private void BuildDefaultTrustLine(TrustComparisonViewModel comparisonList, List <ChartViewModel> benchmarkCharts, StringBuilder csv)
        {
            if (!string.IsNullOrEmpty(comparisonList.DefaultTrustMatNo) && comparisonList.Trusts.Any(s => s.MatNo == comparisonList.DefaultTrustMatNo))
            {
                var valuesLine = new StringBuilder();
                var data       = benchmarkCharts.First().BenchmarkData.Find(d => d.Urn == comparisonList.DefaultTrustMatNo);
                var term       = data.Term;

                valuesLine.Append($"Your trust,{data.Urn},{term},{data.PupilCount},{data.TeacherCount},");

                foreach (var chart in benchmarkCharts.Where(bc => bc.Downloadable))
                {
                    var amount = chart.BenchmarkData.Find(d => d.Urn == comparisonList.DefaultTrustMatNo).Amount;
                    valuesLine.Append(amount == null ? "N/A" : amount.ToString());
                    valuesLine.Append(",");
                }

                foreach (var col in benchmarkCharts.Where(bc => bc.TableColumns != null).SelectMany(bc => bc.TableColumns))
                {
                    var amount = col.BenchmarkData.Find(d => d.Urn == comparisonList.DefaultTrustMatNo).Amount;
                    valuesLine.Append(amount == null ? "N/A" : amount.ToString());
                    valuesLine.Append(",");
                }

                csv.AppendLine(valuesLine.ToString().TrimEnd(','));
            }
        }
        private void BuildOtherTrustsLines(TrustComparisonViewModel comparisonList, List <ChartViewModel> benchmarkCharts, StringBuilder csv)
        {
            var otherTrusts = comparisonList.Trusts.Where(b => b.MatNo != comparisonList.DefaultTrustMatNo);

            foreach (var trust in otherTrusts)
            {
                var valuesLine = new StringBuilder();
                var data       = benchmarkCharts.First().BenchmarkData.Find(d => d.Urn == trust.MatNo);
                var term       = benchmarkCharts.First().BenchmarkData.Find(d => d.Urn == trust.MatNo).Term;
                valuesLine.Append($"\"{trust.Name}\",{data.Urn},{term},{data.PupilCount},{data.TeacherCount},");

                foreach (var chart in benchmarkCharts.Where(bc => bc.Downloadable))
                {
                    var amount = chart.BenchmarkData.Find(d => d.Urn == trust.MatNo).Amount;
                    valuesLine.Append(amount == null ? "N/A" : amount.ToString());
                    valuesLine.Append(",");
                }

                foreach (var col in benchmarkCharts.Where(bc => bc.TableColumns != null).SelectMany(bc => bc.TableColumns))
                {
                    var amount = col.BenchmarkData.Find(d => d.Urn == trust.MatNo).Amount;
                    valuesLine.Append(amount == null ? "N/A" : amount.ToString());
                    valuesLine.Append(",");
                }

                csv.AppendLine(valuesLine.ToString().TrimEnd(','));
            }
        }
        protected TrustComparisonViewModel ExtractTrustComparisonListFromCookie()
        {
            TrustComparisonViewModel comparisonList = null;
            var cookie = Request.Cookies[CookieNames.COMPARISON_LIST_MAT];

            if (cookie != null)
            {
                comparisonList = JsonConvert.DeserializeObject <TrustComparisonViewModel>(cookie.Value);
            }
            return(comparisonList);
        }
        public string BuildCSVContentForTrusts(TrustComparisonViewModel comparisonList, List <ChartViewModel> benchmarkCharts)
        {
            var csv = new StringBuilder();

            BuildHeaderLine(benchmarkCharts, csv, EstablishmentType.MAT);

            BuildDefaultTrustLine(comparisonList, benchmarkCharts, csv);

            BuildOtherTrustsLines(comparisonList, benchmarkCharts, csv);

            return(csv.ToString());
        }
Exemplo n.º 5
0
        private TrustComparisonViewModel UpdateTrustCookie(string withAction, string matNo = null, string matName = null)
        {
            TrustComparisonViewModel vm = null;
            HttpCookie cookie           = Request.Cookies[CookieNames.COMPARISON_LIST_MAT];

            switch (withAction)
            {
            case "SetDefault":
                if (cookie == null)
                {
                    cookie = new HttpCookie(CookieNames.COMPARISON_LIST_MAT);
                    vm     = new TrustComparisonViewModel(matNo, matName)
                    {
                        Trusts = new List <TrustToCompareViewModel> {
                            new TrustToCompareViewModel(matNo, matName)
                        }
                    };
                }
                else
                {
                    vm = JsonConvert.DeserializeObject <TrustComparisonViewModel>(cookie.Value);
                    vm.DefaultTrustMatNo = matNo;
                    vm.DefaultTrustName  = matName;
                    if (vm.Trusts.All(s => s.MatNo != matNo))
                    {
                        vm.Trusts.Add(new TrustToCompareViewModel(matNo, matName));
                    }
                }
                break;

            case "Add":
                if (cookie == null)
                {
                    cookie = new HttpCookie(CookieNames.COMPARISON_LIST_MAT);
                    vm     = new TrustComparisonViewModel(matNo, matName)
                    {
                        Trusts = new List <TrustToCompareViewModel> {
                            new TrustToCompareViewModel(matNo, matName)
                        }
                    };
                }
                else
                {
                    vm = JsonConvert.DeserializeObject <TrustComparisonViewModel>(cookie.Value);
                    if (vm.DefaultTrustMatNo == matNo || vm.Trusts.Any(s => s.MatNo == matNo))
                    {
                        ViewBag.Error = ErrorMessages.DuplicateTrust;
                    }
                    else
                    {
                        vm.Trusts.Add(new TrustToCompareViewModel(matNo, matName));
                    }
                }
                break;

            case "Remove":
                vm = JsonConvert.DeserializeObject <TrustComparisonViewModel>(cookie.Value);
                vm.Trusts.Remove(new TrustToCompareViewModel(matNo));
                break;

            case "RemoveAll":
                vm = JsonConvert.DeserializeObject <TrustComparisonViewModel>(cookie.Value);
                vm.Trusts.Clear();
                break;
            }

            cookie.Value   = JsonConvert.SerializeObject(vm);
            cookie.Expires = DateTime.MaxValue;
            Response.Cookies.Add(cookie);

            return(vm);
        }