public async Task <IActionResult> Datatool(string language, int index = 1, bool api = false) { var strata = await _context.Strata .Include(s => s.Measure) .ThenInclude(m => m.Indicator) .ThenInclude(i => i.LifeCourse) .ThenInclude(lc => lc.IndicatorGroup) .ThenInclude(ig => ig.Activity) .ThenInclude(a => a.DefaultIndicatorGroup.DefaultLifeCourse.DefaultIndicator.DefaultMeasure.DefaultStrata) .Include(s => s.Points) .Where(s => s.Index >= index && s.Measure.Included) .OrderBy(s => s.Index) .FirstOrDefaultAsync(); if (strata == null) { return(NotFound()); } ChartData chart = chart = new ChartData { XAxis = strata.StrataName(language), YAxis = strata.Measure.MeasureUnitLong(language), Unit = strata.Measure.MeasureUnitShort(language), Source = strata.StrataSource(language), Population = strata.Measure.MeasurePopulationGroup(language), Notes = strata.StrataNotes(language), Remarks = strata.Measure.MeasureAdditionalRemarks(language), Definition = strata.Measure.MeasureDefinition(language), Method = strata.Measure.MeasureMethod(language), DataAvailable = strata.Measure.MeasureDataAvailable(language), Points = strata.Points.OrderBy(p => p.Index).Select(p => new ChartData.Point { CVInterpretation = p.CVInterpretation, CVValue = p.CVValue, Value = p.ValueAverage, ValueUpper = p.ValueUpper, ValueLower = p.ValueLower, Label = p.PointLabel(language), Text = p.PointText(language), Type = p.Type }), WarningCV = strata.Measure.CVWarnAt, SuppressCV = strata.Measure.CVSuppressAt, MeasureName = strata.Measure.MeasureNameDataTool(language), Title = strata.Measure.MeasureNameIndex(language) + (strata.StrataPopulationTitleFragment(language) != null ? ", " + strata.StrataPopulationTitleFragment(language) : "") }; var cpm = new ChartPageModel(language, chart); // top level requires a new query var activities = _context.Activity .Where(ac => ac.DefaultIndicatorGroupId != null) .OrderBy(x => x.Index) .Select(ac => new DropdownItem { Value = ac.DefaultIndicatorGroup.DefaultLifeCourse.DefaultIndicator.DefaultMeasure.DefaultStrata.Index, Text = ac.ActivityName(language) }); cpm.filters.Add(new DropdownMenuModel(language == "fr-ca" ? "Activité" : "Activity", activities, strata.Index)); var indicatorGroups = _context.IndicatorGroup .Where(ig => ig.DefaultLifeCourseId != null && ig.ActivityId == strata.Measure.Indicator.LifeCourse.IndicatorGroup.ActivityId) .OrderBy(x => x.Index) .Select(ig => new DropdownItem { Value = ig.DefaultLifeCourse.DefaultIndicator.DefaultMeasure.DefaultStrata.Index, Text = ig.IndicatorGroupName(language) }); cpm.filters.Add(new DropdownMenuModel(language == "fr-ca" ? "Groupe d'indicateur" : "Indicator Group", indicatorGroups, strata.Index)); var lifeCourses = _context.LifeCourse .Where(lc => lc.DefaultIndicator != null && lc.IndicatorGroupId == strata.Measure.Indicator.LifeCourse.IndicatorGroupId) .OrderBy(x => x.Index) .Select(lc => new DropdownItem { Value = lc.DefaultIndicator.DefaultMeasure.DefaultStrata.Index, Text = lc.LifeCourseName(language) }); cpm.filters.Add(new DropdownMenuModel(language == "fr-ca" ? "Cours de la vie" : "Life Course", lifeCourses, strata.Index)); var indicators = _context.Indicator .Where(i => i.DefaultMeasureId != null && i.LifeCourseId == strata.Measure.Indicator.LifeCourseId) .OrderBy(x => x.Index) .Select(i => new DropdownItem { Value = i.DefaultMeasure.DefaultStrata.Index, Text = i.IndicatorName(language) }); cpm.filters.Add(new DropdownMenuModel(language == "fr-ca" ? "Indicateurs" : "Indicators", indicators, strata.Index)); var measures = _context.Measure .Where(i => i.DefaultStrataId != null && i.IndicatorId == strata.Measure.IndicatorId) .OrderBy(x => x.DefaultStrata.Index) .Select(m => new DropdownItem { Value = m.Index, Text = m.MeasureNameIndex(language) }); cpm.filters.Add(new DropdownMenuModel(language == "fr-ca" ? "Mesures" : "Measures", measures, strata.Index)); var stratas = _context.Strata .Where(i => i.MeasureId == strata.MeasureId) .OrderBy(x => x.Index) .Select(s => new DropdownItem { Value = s.Index, Text = s.StrataName(language) }); cpm.filters.Add(new DropdownMenuModel(language == "fr-ca" ? "Répartition des données" : "Data Breakdowns", stratas, strata.Index)); if (Request.Method == "GET" && !api) { return(View(cpm)); } else { return(Json(cpm)); } }
public IActionResult Datatool(string datatool, int index = 1, bool api = false) { var context = GetContext(datatool); var dataBreakdownLevelType = context.Keys.SkipLast(1).Last(); var disaggregatorLevelType = context.Keys.Last(); var selectedBreakdown = context[dataBreakdownLevelType] .Where(s => (int)s.Index >= index) .First(); var children = Enumerable.Cast <dynamic>((IEnumerable)Metadata .FindPropertyOnType <ChildrenAttribute>(dataBreakdownLevelType) .GetValue((object)selectedBreakdown)).ToList(); var measureDescription = Metadata .FindTextPropertiesOnTree((object)selectedBreakdown, Language, TextAppearance.MeasureDescription) .Where(mp => !string.IsNullOrEmpty(mp.Name) && !string.IsNullOrEmpty((string)mp.Value)) .ToList(); var notes = Metadata .FindTextPropertiesOnTree((object)selectedBreakdown, Language, TextAppearance.Notes) .Where(mp => !string.IsNullOrEmpty(mp.Name) && !string.IsNullOrEmpty((string)mp.Value)) .ToList(); PropertyInfo GetProperty <T>() where T : Attribute { return(Metadata.FindPropertyOnType <T>(disaggregatorLevelType)); } var averageValueProperty = GetProperty <PointAverageAttribute>(); var lowerValueProperty = GetProperty <PointLowerAttribute>(); var upperValueProperty = GetProperty <PointUpperAttribute>(); var cVValueProperty = GetProperty <CVValueAttribute>(); var cVInterpretationProperty = GetProperty <CVInterpretationAttribute>(); var typeProperty = GetProperty <TypeAttribute>(); var xAxis = (string)Metadata.FindTextPropertiesOnNode <ShowOnAttribute>((object)selectedBreakdown, Language, TextAppearance.Filter).FirstOrDefault()?.Value; var yAxis = (string)Metadata.FindTextPropertiesOnTree <UnitLongAttribute>((object)selectedBreakdown, Language).FirstOrDefault()?.Value; var unit = (string)Metadata.FindTextPropertiesOnTree <UnitShortAttribute>((object)selectedBreakdown, Language).FirstOrDefault()?.Value; var title = (string)Metadata.FindTextPropertiesOnTree <TitleAttribute>((object)selectedBreakdown, Language).FirstOrDefault()?.Value; var chart = new ChartData { XAxis = xAxis, YAxis = yAxis, Unit = unit, Title = title, Points = children.Select((child) => new Point { Label = (string)Metadata.FindTextPropertiesOnTree <DataLabelChartAttribute>((object)child, Language).FirstOrDefault()?.Value, Text = (string)Metadata.FindTextPropertiesOnTree <DataLabelTableAttribute>((object)child, Language).FirstOrDefault()?.Value, CVInterpretation = (int)cVInterpretationProperty.GetValue(child), CVValue = (double?)cVValueProperty.GetValue(child), Value = (double?)averageValueProperty.GetValue(child), ValueLower = (double?)lowerValueProperty.GetValue(child), ValueUpper = (double?)upperValueProperty.GetValue(child), Type = typeProperty?.GetValue(child) as int? ?? 0, AggregatorLabel = (string)Metadata.FindTextPropertiesOnTree <AggregatorLabelAttribute>((object)child, Language).FirstOrDefault()?.Value, AggregatorReference = (string)GetProperty <AggregatorReferenceAttribute>()?.GetValue((object)child) }).ToList(), WarningCV = null, SuppressCV = null, DescriptionTable = measureDescription.Select(mp => new MeasureAttribute { Name = mp.Name, Body = (string)mp.Value }).ToList(), Notes = notes.Select(mp => new MeasureAttribute { Name = mp.Name, Body = (string)mp.Value }).ToList(), ChartType = Metadata.FindPropertyOnType <ChartTypeAttribute>(dataBreakdownLevelType).GetValue(selectedBreakdown) as ChartType? ?? ChartType.Bar }; var cpm = new ChartPageModel(datatool, Language, chart); var dropdowns = context.SkipLast(1).Select(pair => { Type type = pair.Key; ICollection <dynamic> entities = pair.Value; var parentOfCurrentType = Metadata.GetAllParentNodes((object)selectedBreakdown).First(p => p.GetType() == type); var dropdownItems = entities .Where(entity => Metadata.FindPropertyOnType <DefaultChildAttribute>(type).GetValue(entity) != null) .Where(entity => { var parentOfEntity = Metadata.GetParentOf(entity); // Top-level selectors should always be displayed if (parentOfEntity == null) { return(true); } // Check for common ancestor return(Metadata.GetParentOf(parentOfCurrentType) == parentOfEntity); }) .Select(entity => { var currentLevel = entity; var entityText = (string)Metadata.FindTextPropertiesOnNode <ShowOnAttribute>((object)entity, Language, TextAppearance.Filter).First().Value; while (currentLevel.GetType() != dataBreakdownLevelType) { currentLevel = Metadata.FindPropertyOnType <DefaultChildAttribute>(((object)currentLevel).GetType()).GetValue(currentLevel); if (currentLevel == null) { throw new Exception("Default tree structure is broken"); } } var entityIndex = currentLevel.Index; return(new { Text = entityText, Value = entityIndex, Entity = entity }); }).ToList(); var filterName = Metadata.FindTextPropertiesOnNode <ShowOnAttribute>((object)dropdownItems.First().Entity, Language, TextAppearance.Filter).First().Name; return(new DropdownMenuModel( filterName, dropdownItems.Select(di => new DropdownItem { Text = di.Text, Value = di.Value }), dropdownItems.First(di => di.Entity == parentOfCurrentType).Value )); }); foreach (var dropdown in dropdowns) { cpm.Filters.Add(dropdown); } ; if (Request.Method == "GET" && !api) { return(View("data-tool", cpm)); } else { return(Json(cpm)); } }
// GET: Strata/Details/ public async Task <IActionResult> Datatool(string language, int?measureId, int?indicatorId, int?lifeCourseId, int?indicatorGroupId, int?activityId, int strataId = -1, bool api = false) { /* Figure out a strataId to use. Not terribly efficient. A better solution is needed. */ if (activityId != null) { indicatorGroupId = _context.Activity.FirstOrDefault(m => m.ActivityId == activityId && m.DefaultIndicatorGroupId != null).DefaultIndicatorGroupId; } if (indicatorGroupId != null) { lifeCourseId = _context.IndicatorGroup.FirstOrDefault(m => m.IndicatorGroupId == indicatorGroupId && m.DefaultLifeCourseId != null).DefaultLifeCourseId; } if (lifeCourseId != null) { indicatorId = _context.LifeCourse.FirstOrDefault(m => m.LifeCourseId == lifeCourseId && m.DefaultIndicatorId != null).DefaultIndicatorId; } if (indicatorId != null) { measureId = _context.Indicator.FirstOrDefault(m => m.IndicatorId == indicatorId && m.DefaultMeasureId != null).DefaultMeasureId; } if (measureId != null) { strataId = _context.Measure.FirstOrDefault(m => m.MeasureId == measureId && m.DefaultStrataId != null).DefaultStrataId ?? -1; } var strata = await _context.Strata // Translations // Strata .Include(s => s.StrataNameTranslations) .ThenInclude(t => t.Translation) .Include(s => s.StrataNotesTranslations) .ThenInclude(t => t.Translation) .Include(s => s.StrataSourceTranslations) .ThenInclude(t => t.Translation) .Include(s => s.StrataPopulationTranslations) .ThenInclude(t => t.Translation) // Measure .Include(s => s.Measure.MeasureUnitTranslations) .ThenInclude(t => t.Translation) .Include(s => s.Measure.MeasureNameTranslations) .ThenInclude(t => t.Translation) .Include(s => s.Measure.MeasureDefinitionTranslations) .ThenInclude(t => t.Translation) .Include(s => s.Measure.MeasurePopulationTranslations) .ThenInclude(t => t.Translation) .Include(s => s.Measure.MeasureSourceTranslations) .ThenInclude(t => t.Translation) .Include(s => s.Measure.MeasureAdditionalRemarksTranslations) .ThenInclude(t => t.Translation) .Include(s => s.Measure.MeasureMethodTranslations) .ThenInclude(t => t.Translation) .Include(s => s.Measure.MeasureDataAvailableTranslations) .ThenInclude(t => t.Translation) // Points .Include(s => s.Points) .ThenInclude(p => p.PointLabelTranslations) .ThenInclude(t => t.Translation) // Indicator .Include(s => s.Measure.Indicator) .ThenInclude(p => p.IndicatorNameTranslations) .ThenInclude(t => t.Translation) // Life Course .Include(s => s.Measure.Indicator.LifeCourse) .ThenInclude(p => p.LifeCourseNameTranslations) .ThenInclude(t => t.Translation) // Indicator Group .Include(s => s.Measure.Indicator.LifeCourse.IndicatorGroup) .ThenInclude(p => p.IndicatorGroupNameTranslations) .ThenInclude(t => t.Translation) // Activity .Include(s => s.Measure.Indicator.LifeCourse.IndicatorGroup.Activity) .ThenInclude(p => p.ActivityNameTranslations) .ThenInclude(t => t.Translation) // Include associated stratas .Include(s => s.Measure) .ThenInclude(p => p.Stratas) .ThenInclude(p => p.StrataNameTranslations) .ThenInclude(t => t.Translation) // Include associated measures .Include(s => s.Measure.Indicator) .ThenInclude(p => p.Measures) .ThenInclude(p => p.MeasureNameTranslations) .ThenInclude(t => t.Translation) // Include associated indicators .Include(s => s.Measure.Indicator.LifeCourse) .ThenInclude(p => p.Indicators) .ThenInclude(p => p.IndicatorNameTranslations) .ThenInclude(t => t.Translation) // Include associated life courses .Include(s => s.Measure.Indicator.LifeCourse.IndicatorGroup) .ThenInclude(p => p.LifeCourses) .ThenInclude(p => p.LifeCourseNameTranslations) .ThenInclude(t => t.Translation) // Include associated indicator groups .Include(s => s.Measure.Indicator.LifeCourse.IndicatorGroup.Activity) .ThenInclude(p => p.IndicatorGroups) .ThenInclude(p => p.IndicatorGroupNameTranslations) .ThenInclude(t => t.Translation) .OrderBy(m => m.StrataId == strataId ? 0 : 1) .ThenBy(m => m.Index) .FirstOrDefaultAsync(); if (strata == null) { return(NotFound()); } var chart = new ChartData { XAxis = strata.StrataName, YAxis = strata.Measure.MeasureUnit, Source = new Translatable(strata.Measure.MeasureSource.Union(strata.StrataSource).ToDictionary(p => p.Key, p => p.Value)), // Both stratas AND measure contain populations. They must be merged. Population = new Translatable(strata.Measure.MeasurePopulation.Union(strata.StrataPopulation).ToDictionary(p => p.Key, p => p.Value)), Notes = strata.StrataNotes, Remarks = strata.Measure.MeasureAdditionalRemarks, Definition = strata.Measure.MeasureDefinition, Method = strata.Measure.MeasureMethod, DataAvailable = strata.Measure.MeasureDataAvailable, Points = strata.Points.OrderBy(p => p.Index).Select(p => new ChartData.Point { CVInterpretation = p.CVInterpretation, CVValue = p.CVValue, Value = p.ValueAverage, ValueUpper = p.ValueUpper, ValueLower = p.ValueLower, Label = p.PointLabel, Type = p.Type }), WarningCV = strata.Measure.CVWarnAt, SuppressCV = strata.Measure.CVSuppressAt, MeasureName = strata.Measure.MeasureName }; var cpm = new ChartPageModel(language, chart); // top level requires a new query var activities = _context.Activity .Where(ac => ac.DefaultIndicatorGroupId != null) .Include(ac => ac.ActivityNameTranslations) .ThenInclude(at => at.Translation) .AsEnumerable() .OrderBy(x => x.Index) .Select(ac => new DropdownItem { Value = ac.ActivityId, Text = ac.ActivityName.Get((language, null)) });