use of org.obiba.mica.dataset.domain.DatasetCategory in project mica2 by obiba.
the class DatasetDtos method asContingencyDto.
public Mica.DatasetVariableContingencyDto.Builder asContingencyDto(@NotNull OpalTable opalTable, DatasetVariable variable, DatasetVariable crossVariable, @Nullable Search.QueryResultDto results) {
Mica.DatasetVariableContingencyDto.Builder crossDto = Mica.DatasetVariableContingencyDto.newBuilder();
if (opalTable instanceof StudyTable)
crossDto.setStudyTable(asDto((StudyTable) opalTable, true));
else if (opalTable instanceof HarmonizationStudyTable)
crossDto.setHarmonizationStudyTable(asDto((HarmonizationStudyTable) opalTable));
Mica.DatasetVariableAggregationDto.Builder allAggBuilder = Mica.DatasetVariableAggregationDto.newBuilder();
if (results == null) {
allAggBuilder.setN(0);
allAggBuilder.setTotal(0);
crossDto.setAll(allAggBuilder);
return crossDto;
}
allAggBuilder.setTotal(results.getTotalHits());
MicaConfig micaConfig = micaConfigService.getConfig();
int privacyThreshold = micaConfig.getPrivacyThreshold();
crossDto.setPrivacyThreshold(privacyThreshold);
boolean privacyChecks = !crossVariable.hasCategories() || validatePrivacyThreshold(results, privacyThreshold);
boolean totalPrivacyChecks = validateTotalPrivacyThreshold(results, privacyThreshold);
// add facet results in the same order as the variable categories
List<String> catNames = variable.getValueType().equals(BooleanType.get().getName()) ? Lists.newArrayList("true", "false") : variable.getCategories().stream().map(DatasetCategory::getName).collect(Collectors.toList());
catNames.forEach(catName -> results.getFacetsList().stream().filter(facet -> facet.hasFacet() && catName.equals(facet.getFacet())).forEach(facet -> {
boolean privacyCheck = privacyChecks && checkPrivacyThreshold(facet.getFilters(0).getCount(), privacyThreshold);
Mica.DatasetVariableAggregationDto.Builder aggBuilder = Mica.DatasetVariableAggregationDto.newBuilder();
aggBuilder.setTotal(totalPrivacyChecks ? results.getTotalHits() : 0);
aggBuilder.setTerm(facet.getFacet());
DatasetCategory category = variable.getCategory(facet.getFacet());
aggBuilder.setMissing(category != null && category.isMissing());
addSummaryStatistics(crossVariable, aggBuilder, facet, privacyCheck, totalPrivacyChecks);
crossDto.addAggregations(aggBuilder);
}));
// add total facet for all variable categories
results.getFacetsList().stream().filter(facet -> facet.hasFacet() && "_total".equals(facet.getFacet())).forEach(facet -> {
boolean privacyCheck = privacyChecks && facet.getFilters(0).getCount() >= micaConfig.getPrivacyThreshold();
addSummaryStatistics(crossVariable, allAggBuilder, facet, privacyCheck, totalPrivacyChecks);
});
crossDto.setAll(allAggBuilder);
return crossDto;
}
Aggregations