use of de.symeda.sormas.api.campaign.diagram.CampaignDiagramDataDto in project SORMAS-Project by hzi-braunschweig.
the class CampaignDashboardDiagramComponent method totalsNeedClampTo100.
private boolean totalsNeedClampTo100() {
if (!showPercentages || totalValuesMap == null) {
return false;
}
boolean result = false;
for (CampaignDiagramSeries series : diagramDefinition.getCampaignDiagramSeries()) {
String seriesKey = series.getFormId() + series.getFieldId();
if (!diagramDataBySeriesAndXAxis.containsKey(seriesKey))
continue;
Map<Object, CampaignDiagramDataDto> seriesData = diagramDataBySeriesAndXAxis.get(seriesKey);
for (Object axisKey : xAxisInfo.keySet()) {
if (seriesData.containsKey(axisKey)) {
Double totalValue = totalValuesMap.get(new CampaignDashboardTotalsReference(seriesData.get(axisKey).getGroupingKey(), totalValuesWithoutStacks ? null : series.getStack()));
if (totalValue != null && totalValue > 0) {
final double originalValue = seriesData.get(axisKey).getValueSum().doubleValue() / totalValue * 100;
final double scaledValue = BigDecimal.valueOf(originalValue).setScale(originalValue < 2 ? 1 : 0, RoundingMode.HALF_UP).doubleValue();
if (scaledValue > MAX_YAXIS_VALUE_DYNAMIC_CHART_HEIGHT_UPPER_BOUND) {
return false;
}
result |= scaledValue > MAX_YAXIS_VALUE_DYNAMIC_CHART_HEIGHT_LOWER_BOUND;
}
}
}
}
return result;
}
use of de.symeda.sormas.api.campaign.diagram.CampaignDiagramDataDto in project SORMAS-Project by hzi-braunschweig.
the class CampaignDashboardDataProvider method createDiagramsData.
protected void createDiagramsData(String tabId, String subTabId) {
getCampaignDashboardDiagrams().forEach(campaignDashboardDiagramDto -> {
final CampaignDashboardElement campaignDashboardElement = campaignDashboardDiagramDto.getCampaignDashboardElement();
if (campaignDashboardElement.getTabId().equals(tabId) && (subTabId == null || campaignDashboardElement.getSubTabId().equals(subTabId))) {
List<CampaignDiagramDataDto> diagramData = FacadeProvider.getCampaignFormDataFacade().getDiagramData(campaignDashboardDiagramDto.getCampaignDiagramDefinitionDto().getCampaignDiagramSeries(), new CampaignDiagramCriteria(campaign, area, region, district, campaignJurisdictionLevelGroupBy));
campaignFormDataMap.put(campaignDashboardDiagramDto, diagramData);
List<CampaignDiagramSeries> campaignSeriesTotal = campaignDashboardDiagramDto.getCampaignDiagramDefinitionDto().getCampaignSeriesTotal();
List<CampaignDiagramDataDto> percentageDiagramData = null;
if (campaignSeriesTotal != null) {
Optional populationGroup = campaignSeriesTotal.stream().filter(e -> Objects.nonNull(e.getPopulationGroup())).findFirst();
Optional formIdOptional = campaignSeriesTotal.stream().filter(e -> Objects.nonNull(e.getFormId())).findFirst();
{
if (populationGroup.isPresent()) {
percentageDiagramData = FacadeProvider.getCampaignFormDataFacade().getDiagramDataByAgeGroup((CampaignDiagramSeries) populationGroup.get(), campaignDashboardDiagramDto.getCampaignDiagramDefinitionDto().getCampaignDiagramSeries().get(0), new CampaignDiagramCriteria(campaign, area, region, district, campaignJurisdictionLevelGroupBy));
if (formIdOptional.isPresent()) {
logger.warn(String.format(I18nProperties.getString(Strings.errorFormIdPopulationAgeGroup)));
}
} else {
percentageDiagramData = FacadeProvider.getCampaignFormDataFacade().getDiagramData(campaignDashboardDiagramDto.getCampaignDiagramDefinitionDto().getCampaignSeriesTotal(), new CampaignDiagramCriteria(campaign, area, region, district, campaignJurisdictionLevelGroupBy));
}
Map<CampaignDashboardTotalsReference, Double> percentageMap = new HashMap<>();
for (CampaignDiagramDataDto data : percentageDiagramData) {
CampaignDashboardTotalsReference totals = new CampaignDashboardTotalsReference(data.getGroupingKey(), data.getStack());
Double value = percentageMap.getOrDefault(totals, 0D);
value += data.getValueSum().doubleValue();
percentageMap.put(totals, value);
}
campaignFormTotalsMap.put(campaignDashboardDiagramDto, percentageMap);
}
}
}
});
}
use of de.symeda.sormas.api.campaign.diagram.CampaignDiagramDataDto in project SORMAS-Project by hzi-braunschweig.
the class CampaignFormDataFacadeEjb method getDiagramData.
@Override
public List<CampaignDiagramDataDto> getDiagramData(List<CampaignDiagramSeries> diagramSeries, CampaignDiagramCriteria campaignDiagramCriteria) {
List<CampaignDiagramDataDto> resultData = new ArrayList<>();
final AreaReferenceDto area = campaignDiagramCriteria.getArea();
final RegionReferenceDto region = campaignDiagramCriteria.getRegion();
final DistrictReferenceDto district = campaignDiagramCriteria.getDistrict();
final CampaignReferenceDto campaign = campaignDiagramCriteria.getCampaign();
for (CampaignDiagramSeries series : diagramSeries) {
// @formatter:off
final String areaFilter = area != null ? " AND " + Area.TABLE_NAME + "." + Area.UUID + " = :areaUuid" : "";
final String regionFilter = region != null ? " AND " + CampaignFormData.REGION + "." + Region.UUID + " = :regionUuid" : "";
final String districtFilter = district != null ? " AND " + CampaignFormData.DISTRICT + "." + District.UUID + " = :districtUuid" : "";
final String campaignFilter = campaign != null ? " AND " + Campaign.TABLE_NAME + "." + Campaign.UUID + " = :campaignUuid" : "";
// @formatter:on
// SELECT
StringBuilder selectBuilder = new StringBuilder("SELECT ").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.UUID).append(" as formUuid,").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.FORM_ID).append(" as formId");
if (series.getFieldId() != null) {
selectBuilder.append(", jsonData->>'").append(CampaignFormDataEntry.ID).append("' as fieldId, jsonMeta->>'").append(CampaignFormElement.CAPTION).append("' as fieldCaption,").append("CASE WHEN (jsonMeta ->> '").append(CampaignFormElement.TYPE).append("') = '").append(CampaignFormElementType.NUMBER.toString()).append("' THEN sum(cast_to_int(jsonData->>'").append(CampaignFormDataEntry.VALUE).append("', 0)) ELSE sum(CASE WHEN(jsonData->>'").append(CampaignFormDataEntry.VALUE).append("') = '").append(series.getReferenceValue()).append("' THEN 1 ELSE 0 END) END as sumValue,");
} else {
selectBuilder.append(", null as fieldId, null as fieldCaption, count(formId) as sumValue,");
}
final String jurisdictionGrouping;
switch(campaignDiagramCriteria.getCampaignJurisdictionLevelGroupBy()) {
case REGION:
appendInfrastructureSelection(selectBuilder, Region.TABLE_NAME, Region.NAME);
break;
case DISTRICT:
appendInfrastructureSelection(selectBuilder, District.TABLE_NAME, District.NAME);
break;
case COMMUNITY:
appendInfrastructureSelection(selectBuilder, Community.TABLE_NAME, Community.NAME);
break;
case AREA:
default:
appendInfrastructureSelection(selectBuilder, Area.TABLE_NAME, Area.NAME);
}
// JOINS
StringBuilder joinBuilder = new StringBuilder(" LEFT JOIN ").append(CampaignFormMeta.TABLE_NAME).append(" ON ").append(CampaignFormData.CAMPAIGN_FORM_META).append("_id = ").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.ID).append(" LEFT JOIN ").append(Region.TABLE_NAME).append(" ON ").append(CampaignFormData.REGION).append("_id =").append(Region.TABLE_NAME).append(".").append(Region.ID).append(" LEFT JOIN ").append(Area.TABLE_NAME).append(" ON ").append(Region.AREA).append("_id = ").append(Area.TABLE_NAME).append(".").append(Area.ID).append(" LEFT JOIN ").append(District.TABLE_NAME).append(" ON ").append(CampaignFormData.DISTRICT).append("_id = ").append(District.TABLE_NAME).append(".").append(District.ID).append(" LEFT JOIN ").append(Community.TABLE_NAME).append(" ON ").append(CampaignFormData.COMMUNITY).append("_id = ").append(Community.TABLE_NAME).append(".").append(Community.ID).append(" LEFT JOIN ").append(Campaign.TABLE_NAME).append(" ON ").append(CampaignFormData.CAMPAIGN).append("_id = ").append(Campaign.TABLE_NAME).append(".").append(Campaign.ID);
if (series.getFieldId() != null) {
joinBuilder.append(", json_array_elements(").append(CampaignFormData.FORM_VALUES).append(") as jsonData, json_array_elements(").append(CampaignFormMeta.CAMPAIGN_FORM_ELEMENTS).append(") as jsonMeta");
}
// WHERE
StringBuilder whereBuilder = new StringBuilder(" WHERE ").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.FORM_ID).append(" = :campaignFormMetaId");
if (series.getFieldId() != null) {
whereBuilder.append(" AND jsonData->>'").append(CampaignFormDataEntry.ID).append("' = :campaignFormDataId").append(" AND jsonData->>'").append(CampaignFormDataEntry.VALUE).append("' IS NOT NULL AND jsonData->>'").append(CampaignFormDataEntry.ID).append("' = jsonMeta->>'").append(CampaignFormElement.ID).append("'");
}
whereBuilder.append(areaFilter).append(regionFilter).append(districtFilter).append(campaignFilter);
// GROUP BY
StringBuilder groupByBuilder = new StringBuilder(" GROUP BY ").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.UUID).append(",").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.FORM_ID);
if (series.getFieldId() != null) {
groupByBuilder.append(", jsonData->>'").append(CampaignFormDataEntry.ID).append("', jsonMeta->>'").append(CampaignFormElement.CAPTION).append("', jsonMeta->>'").append(CampaignFormElement.TYPE).append("'");
}
switch(campaignDiagramCriteria.getCampaignJurisdictionLevelGroupBy()) {
case REGION:
jurisdictionGrouping = ", " + Region.TABLE_NAME + "." + Region.UUID + ", " + Region.TABLE_NAME + "." + Region.NAME;
break;
case DISTRICT:
jurisdictionGrouping = ", " + District.TABLE_NAME + "." + District.UUID + ", " + District.TABLE_NAME + "." + District.NAME;
break;
case COMMUNITY:
jurisdictionGrouping = ", " + Community.TABLE_NAME + "." + Community.UUID + ", " + Community.TABLE_NAME + "." + Community.NAME;
break;
case AREA:
default:
jurisdictionGrouping = ", " + Area.TABLE_NAME + "." + Area.UUID + ", " + Area.TABLE_NAME + "." + Area.NAME;
}
groupByBuilder.append(jurisdictionGrouping);
// @formatter:off
Query seriesDataQuery = em.createNativeQuery(selectBuilder.toString() + " FROM " + CampaignFormData.TABLE_NAME + joinBuilder + whereBuilder + groupByBuilder);
// @formatter:on
seriesDataQuery.setParameter("campaignFormMetaId", series.getFormId());
if (area != null) {
seriesDataQuery.setParameter("areaUuid", area.getUuid());
}
if (region != null) {
seriesDataQuery.setParameter("regionUuid", region.getUuid());
}
if (district != null) {
seriesDataQuery.setParameter("districtUuid", district.getUuid());
}
if (campaign != null) {
seriesDataQuery.setParameter("campaignUuid", campaign.getUuid());
}
if (series.getFieldId() != null) {
seriesDataQuery.setParameter("campaignFormDataId", series.getFieldId());
}
@SuppressWarnings("unchecked") List<Object[]> resultList = seriesDataQuery.getResultList();
resultData.addAll(resultList.stream().map((result) -> new CampaignDiagramDataDto((String) result[0], (String) result[1], (String) result[2], (String) result[3], (Number) result[4], (String) result[5], (String) result[6], series.getStack())).collect(Collectors.toList()));
}
return resultData;
}
use of de.symeda.sormas.api.campaign.diagram.CampaignDiagramDataDto in project SORMAS-Project by hzi-braunschweig.
the class CampaignFormDataFacadeEjb method getDiagramDataByAgeGroup.
public List<CampaignDiagramDataDto> getDiagramDataByAgeGroup(CampaignDiagramSeries diagramSeriesTotal, CampaignDiagramSeries diagramSeries, CampaignDiagramCriteria campaignDiagramCriteria) {
List<CampaignDiagramDataDto> resultData = new ArrayList<>();
final AreaReferenceDto area = campaignDiagramCriteria.getArea();
final RegionReferenceDto region = campaignDiagramCriteria.getRegion();
final DistrictReferenceDto district = campaignDiagramCriteria.getDistrict();
final CampaignJurisdictionLevel grouping = campaignDiagramCriteria.getCampaignJurisdictionLevelGroupBy();
if (grouping == CampaignJurisdictionLevel.AREA) {
List<Area> areas = areaService.getAll();
areas.forEach(areaItem -> {
Integer population = populationDataFacadeEjb.getAreaPopulation(areaItem.getUuid(), diagramSeriesTotal.getPopulationGroup());
if (population == 0) {
resultData.add(new CampaignDiagramDataDto(areaItem.getName(), 0, areaItem.getUuid(), areaItem.getName(), diagramSeries.getFieldId(), diagramSeries.getFormId(), false));
} else {
resultData.add(new CampaignDiagramDataDto(areaItem.getName(), population, areaItem.getUuid(), areaItem.getName(), diagramSeries.getFieldId(), diagramSeries.getFormId(), true));
}
});
} else if (grouping == CampaignJurisdictionLevel.REGION) {
List<RegionReferenceDto> regions;
if (area != null)
regions = regionFacadeEjb.getAllActiveByArea(area.getUuid());
else
regions = regionFacadeEjb.getAllActiveAsReference();
// this should not be needed
// if (regions.isEmpty()) {
// resultData.add(
// new CampaignDiagramDataDto(
// area.getCaption(),
// 0,
// area.getUuid(),
// area.getCaption(),
// diagramSeries.getFieldId(),
// diagramSeries.getFormId(),
// false));
// } else {
regions.stream().forEach(regionReferenceDto -> {
PopulationDataCriteria criteria = new PopulationDataCriteria();
criteria.sexIsNull(true);
criteria.region(regionReferenceDto);
criteria.ageGroup(diagramSeriesTotal.getPopulationGroup());
List<PopulationDataDto> populationDataDto = populationDataFacadeEjb.getPopulationData(criteria);
Integer populationSum = 0;
if (!populationDataDto.isEmpty()) {
populationSum = populationDataDto.stream().mapToInt(e -> e.getPopulation()).sum();
resultData.add(new CampaignDiagramDataDto(regionReferenceDto.getCaption(), populationSum, regionReferenceDto.getUuid(), regionReferenceDto.getCaption(), diagramSeries.getFieldId(), diagramSeries.getFormId(), true));
} else {
resultData.add(new CampaignDiagramDataDto(regionReferenceDto.getCaption(), 0, regionReferenceDto.getUuid(), regionReferenceDto.getCaption(), diagramSeries.getFieldId(), diagramSeries.getFormId(), false));
}
});
// }
} else if (grouping == CampaignJurisdictionLevel.DISTRICT || Objects.isNull(district)) {
List<DistrictReferenceDto> districts;
if (region != null) {
districts = districtFacadeEjb.getAllActiveByRegion(region.getUuid());
} else if (area != null) {
districts = districtFacadeEjb.getAllActiveByArea(area.getUuid());
} else {
districts = districtFacadeEjb.getAllActiveAsReference();
}
// this should not be needed
// if (districts.isEmpty()) {
// resultData.add(
// new CampaignDiagramDataDto(
// region.getCaption(),
// 0,
// region.getUuid(),
// region.getCaption(),
// diagramSeries.getFieldId(),
// diagramSeries.getFormId(),
// false));
// } else {
districts.stream().forEach(districtReferenceDto -> {
PopulationDataCriteria criteria = new PopulationDataCriteria();
criteria.sexIsNull(true);
criteria.district(districtReferenceDto);
criteria.region(region);
criteria.ageGroup(diagramSeriesTotal.getPopulationGroup());
List<PopulationDataDto> populationDataDtoList = populationDataFacadeEjb.getPopulationData(criteria);
Integer populationSum = 0;
if (!populationDataDtoList.isEmpty()) {
populationSum = populationDataDtoList.stream().mapToInt(e -> e.getPopulation()).sum();
resultData.add(new CampaignDiagramDataDto(districtReferenceDto.getCaption(), populationSum, districtReferenceDto.getUuid(), districtReferenceDto.getCaption(), diagramSeries.getFieldId(), diagramSeries.getFormId(), true));
} else {
resultData.add(new CampaignDiagramDataDto(districtReferenceDto.getCaption(), populationSum, districtReferenceDto.getUuid(), districtReferenceDto.getCaption(), diagramSeries.getFieldId(), diagramSeries.getFormId(), false));
}
});
// }
} else if (district != null) {
resultData.add(new CampaignDiagramDataDto(district.getCaption(), 0, district.getUuid(), district.getCaption(), diagramSeries.getFieldId(), diagramSeries.getFormId(), true));
}
return resultData;
}
use of de.symeda.sormas.api.campaign.diagram.CampaignDiagramDataDto in project SORMAS-Project by hzi-braunschweig.
the class CampaignDashboardDiagramComponent method assembleFieldname.
private String assembleFieldname(final Collection<CampaignDiagramDataDto> values, final CampaignDiagramSeries series, final String defaultValue) {
CampaignDiagramTranslations translations = getCampaignDiagramTranslations();
if (translations != null && translations.getSeriesNames() != null) {
TranslationElement seriesName = translations.getSeriesNames().stream().filter(s -> s.getElementId().equalsIgnoreCase(defaultValue)).findFirst().orElse(null);
if (seriesName != null) {
return seriesName.getCaption();
}
}
if (series.getCaption() != null && !series.getCaption().isEmpty()) {
return series.getCaption();
}
Iterator<CampaignDiagramDataDto> iterator = values.iterator();
return iterator.hasNext() ? iterator.next().getFieldCaption() : defaultValue;
}
Aggregations