public GovernorsGridViewModel(GovernorsDetailsDto dto, bool editMode, int?groupUId, int?establishmentUrn, NomenclatureService nomenclatureService, IEnumerable <LookupDto> nationalities, IEnumerable <LookupDto> appointingBodies) { _nomenclatureService = nomenclatureService; DomainModel = dto; EditMode = editMode; GroupUId = groupUId; Nationalities = nationalities; AppointingBodies = appointingBodies; EstablishmentUrn = establishmentUrn; CreateGrids(dto, dto.CurrentGovernors, false, groupUId, establishmentUrn); CreateGrids(dto, dto.HistoricalGovernors, true, groupUId, establishmentUrn); AlignGrids(); }
private void CreateGrids(GovernorsDetailsDto dto, IEnumerable <GovernorModel> governors, bool isHistoric, int?groupUid, int?establishmentUrn) { var roles = dto.ApplicableRoles.Where(role => !EnumSets.eSharedGovernorRoles.Contains(role) || (RoleEquivalence.GetLocalEquivalentToSharedRole(role) != null && !dto.ApplicableRoles.Contains(RoleEquivalence.GetLocalEquivalentToSharedRole(role).Value))); foreach (var role in roles) { var equivalantRoles = RoleEquivalence.GetEquivalentToLocalRole(role).Cast <int>().ToList(); var pluralise = !EnumSets.eSingularGovernorRoles.Contains(role); var grid = new GovernorGridViewModel($"{_nomenclatureService.GetGovernorRoleName(role, eTextCase.SentenceCase, pluralise)}{(isHistoric ? " (in past 12 months)" : string.Empty)}") { Tag = isHistoric ? "historic" : "current", Role = role, IsSharedRole = EnumSets.eSharedGovernorRoles.Contains(role), GroupUid = groupUid, EstablishmentUrn = establishmentUrn, IsHistoricRole = isHistoric, RoleName = _nomenclatureService.GetGovernorRoleName(role) }; var displayPolicy = dto.RoleDisplayPolicies.Get(role); Guard.IsNotNull(displayPolicy, () => new Exception($"The display policy should not be null for the role '{role}'")); bool includeEndDate = ((isHistoric && role == GR.Member || role != GR.Member) && displayPolicy.AppointmentEndDate) || (role.OneOfThese(GR.ChiefFinancialOfficer, GR.AccountingOfficer) && isHistoric); SetupHeader(role, grid, displayPolicy, includeEndDate); var list = governors.Where(x => x.RoleId.HasValue && equivalantRoles.Contains(x.RoleId.Value)); foreach (var governor in list) { var isShared = governor.RoleId.HasValue && EnumSets.SharedGovernorRoles.Contains(governor.RoleId.Value); var establishments = string.Join(", ", governor.Appointments?.Select(a => $"{a.EstablishmentName}, URN: {a.EstablishmentUrn}") ?? new string[] { }); var appointment = governor.Appointments?.SingleOrDefault(a => a.EstablishmentUrn == EstablishmentUrn); var startDate = (isShared && appointment != null) ? appointment.AppointmentStartDate : governor.AppointmentStartDate; var endDate = (isShared && appointment != null) ? appointment.AppointmentEndDate : governor.AppointmentEndDate; var row = grid.AddRow(governor, endDate) .AddCell(governor.GetFullName(), displayPolicy.FullName) .AddCell(string.IsNullOrWhiteSpace(establishments) ? null : establishments, role.OneOfThese(GR.LocalGovernor, GR.ChairOfLocalGoverningBody)) .AddCell(governor.Id, displayPolicy.Id) .AddCell(AppointingBodies.FirstOrDefault(x => x.Id == governor.AppointingBodyId)?.Name, displayPolicy.AppointingBodyId) .AddCell(startDate?.ToString("dd/MM/yyyy"), displayPolicy.AppointmentStartDate) .AddCell(endDate?.ToString("dd/MM/yyyy"), includeEndDate) .AddCell(governor.PostCode, displayPolicy.PostCode) .AddCell(governor.DOB?.ToString("dd/MM/yyyy"), displayPolicy.DOB) .AddCell(governor.GetPreviousFullName(), displayPolicy.PreviousFullName) .AddCell(governor.EmailAddress, displayPolicy.EmailAddress) .AddCell(governor.TelephoneNumber, displayPolicy.TelephoneNumber); if (isHistoric) { var gov = new HistoricGovernorViewModel { AppointingBodyId = governor.AppointingBodyId, AppointingBody = AppointingBodies.FirstOrDefault(x => x.Id == governor.AppointingBodyId)?.Name, AppointmentEndDate = new DateTimeViewModel(governor.AppointmentEndDate), AppointmentStartDate = new DateTimeViewModel(governor.AppointmentStartDate), FullName = governor.GetFullName(), RoleName = _nomenclatureService.GetGovernorRoleName(role) }; HistoricGovernors.Add(gov); } } grid.Rows = grid.Rows.OrderByDescending(x => x.SortValue).ThenBy(x => x.Model.GetFullName()).ToList(); HistoricGovernors = HistoricGovernors.OrderByDescending(x => x.AppointmentEndDate.ToDateTime()).ThenBy(x => x.FullName).ToList(); if (isHistoric) { HistoricGrids.Add(grid); } else { Grids.Add(grid); } } }