in clns-acuity-vahub/vahub-model/src/main/java/com/acuity/visualisations/rawdatamodel/dataproviders/PopulationDatasetsDataProvider.java [122:240]
public Collection<Subject> getData(Dataset dataset) {
return dataProvider.getData(Subject.class, dataset, ds -> {
Collection<Subject> rawData = populationRepository.getRawData(ds.getId());
List<DrugDoseRaw> drugDoseRawData = drugDoseRepository.getRawData(dataset.getId());
List<DeathRaw> deathRawData = deathRepository.getRawData(dataset.getId());
List<DoseDiscRaw> doseDiscRawData = doseDiscRepository.getRawData(dataset.getId());
Map<String, SubjectMapFieldsHolder> subjectWithDrugInfoMap = new HashMap<>();
Map<String, List<DrugDoseRaw>> drugDosesBySubject = drugDoseRawData.stream().collect(Collectors.groupingBy(DrugDoseRaw::getSubjectId));
Set<String> studyDrugs = drugDoseRawData.stream().map(DrugDoseRaw::getDrug).collect(Collectors.toSet());
List<DrugDosed> drugsDosed = rawData.stream().map(e -> buildDrugDosed(e, drugDosesBySubject.get(e.getSubjectId()), studyDrugs))
.flatMap(List::stream).collect(Collectors.toList());
Map<String, List<DoseDiscRaw>> drugDiscBySubject = doseDiscRawData.stream().collect(Collectors.groupingBy(DoseDiscRaw::getSubjectId));
List<DrugDiscontinued> drugsDisc = rawData.stream().map(e -> buildDrugDiscontinued(e, drugDiscBySubject.get(e.getSubjectId()), studyDrugs))
.flatMap(List::stream).collect(Collectors.toList());
drugsDosed.forEach(drugDosed -> populateSubjectDrugsDosed(subjectWithDrugInfoMap, drugDosed));
drugsDisc.forEach(drugDisc -> populateSubjectDrugsDisc(subjectWithDrugInfoMap, drugDisc));
List<SubjectVisit> subjVisits = populationRepository.getAttendedVisits(ds.getId());
Map<String, List<String>> attendedVisits = subjVisits
.stream()
.collect(Collectors
.groupingBy(SubjectVisit::getSubjectId, Collectors.mapping(SubjectVisit::getVisit, Collectors
.toList())));
Map<String, SubjectVisit> lastVisitBySubject = subjVisits
.stream()
.filter(subjectVisit -> subjectVisit.getVisit() != null && subjectVisit.getDate() != null)
.collect(Collectors.toMap(e -> e.getSubjectId(), e -> e, (sv1, sv2) ->
sv1.getDate().compareTo(sv2.getDate()) > 0 ? sv1 : sv2
));
Map<String, Set<String>> medicalHistory = populationRepository.getMedicalHistories(ds.getId())
.stream()
.collect(Collectors
.groupingBy(SubjectMedicalHistories::getSubjectId,
Collectors.mapping(SubjectMedicalHistories::getReportedTerm, Collectors.toSet())));
Map<String, List<String>> studySpecificFilters = populationRepository.getSubjectStudySpecificFilters(ds.getId())
.stream().collect(Collectors.groupingBy(SubjectStudySpecificFilters::getSubjectId,
Collectors.mapping(SubjectStudySpecificFilters::getStudySpecificFilter, Collectors.toList())));
Map<String, Date> deathDateBySubject = deathRawData.stream().filter(e -> Objects.nonNull(e.getDateOfDeath()))
.collect(Collectors.groupingBy(DeathRaw::getSubjectId, Collectors.mapping(DeathRaw::getDateOfDeath,
Collectors.minBy(Date::compareTo))))
.entrySet()
.stream()
.filter(e -> e.getValue().isPresent())
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get()));
Map<String, Subject.SubjectEthnicGroup> ethnicGroupBySubject = populationRepository.getSubjectEthnicGroup(dataset
.getId()).stream()
.collect(Collectors.toMap(Subject.SubjectEthnicGroup::getSubjectId, Function.identity()));
Map<String, List<Subject.SubjectVitalsInfo>> vitalsInfoBySubject = populationRepository.getSubjectVitalsInfo(dataset
.getId()).stream()
.collect(Collectors.groupingBy(Subject.SubjectVitalsInfo::getSubjectId));
Map<String, List<Subject.SubjectGroup>> groupBySubject = populationRepository.getSubjectGroup(dataset.getId())
.stream()
.collect(Collectors.groupingBy(Subject.SubjectGroup::getSubjectId, Collectors.toList()));
StudyInfo studyInfo = studyInfoRepository.getRawData(dataset.getId()).stream().findAny().orElse(StudyInfo.EMPTY);
rawData = rawData.stream().map(subject -> {
String subjectId = subject.getSubjectId();
Subject.SubjectBuilder builder = subject.toBuilder();
SubjectMapFieldsHolder subjectWithDrugInfo = subjectWithDrugInfoMap.get(subjectId);
builder = setDrugInfo(builder, subjectWithDrugInfo);
List<String> subjectVisits = attendedVisits.get(subjectId);
if (subjectVisits != null) {
subjectVisits = subjectVisits.stream()
.map(VisitNumber::normalizeVisitNumberString)
.collect(Collectors.toList());
} else {
subjectVisits = new ArrayList<>();
}
Set<String> subjectMedicalHistory = medicalHistory.get(subjectId);
List<String> subjectStudySpecificFilters = studySpecificFilters.get(subjectId);
String visitNumber = lastVisitBySubject.get(subjectId) != null
? lastVisitBySubject.get(subjectId).getVisit()
: null;
visitNumber = VisitNumber.normalizeVisitNumberString(visitNumber);
builder.attendedVisitNumbers(subjectVisits)
.lastVisitNumber(visitNumber)
.medicalHistories(subjectMedicalHistory == null ? new HashSet<>() : subjectMedicalHistory)
.studySpecificFilters(subjectStudySpecificFilters == null ? new ArrayList<>() : subjectStudySpecificFilters)
.randomised(subject.getDateOfRandomisation() == null ? NO : YES)
.withdrawal(subject.getDateOfWithdrawal() == null ? NO : YES)
.dateOfDeath(deathDateBySubject.get(subjectId))
.deathFlag(deathDateBySubject.get(subjectId) == null ? NO : YES)
.durationOnStudy(getDurationOnStudy(subject.getDateOfWithdrawal(), deathDateBySubject.get(subjectId),
subject.getLastEtlDate(), subject.getFirstTreatmentDate()))
.lastTreatmentDate(getLastTreatmentDate(drugDosesBySubject.get(subjectId), subject.getLastEtlDate()))
.ethnicGroup(getEthnicGroup(ethnicGroupBySubject.get(subjectId)))
.specifiedEthnicGroup(getSpecifiedEthnicGroup(ethnicGroupBySubject.get(subjectId)))
.age(getAge(subject.getFirstTreatmentDate(), subject.getDateOfBirth()))
.weight(getValueOfLastTest(vitalsInfoBySubject.get(subjectId), WEIGHT_TEST))
.height(getValueOfLastTest(vitalsInfoBySubject.get(subjectId), HEIGHT_TEST))
.studyInfo(studyInfo);
setGroupingsData(builder, groupBySubject.get(subjectId));
return builder.build();
}).collect(Collectors.toList());
return rawData;
}
);
}