use of de.symeda.sormas.api.sample.PathogenTestResultType in project SORMAS-Project by hzi-braunschweig.
the class TestEntityCreator method createSampleTest.
public static PathogenTest createSampleTest(Sample sample) {
PathogenTestType pathogenTestType = PathogenTestType.RAPID_TEST;
PathogenTestResultType pathogenTestResultType = PathogenTestResultType.NEGATIVE;
Date sampleTestDateTime = new Date();
PathogenTest pathogenTest = DatabaseHelper.getSampleTestDao().build(sample);
pathogenTest.setTestType(pathogenTestType);
pathogenTest.setTestResult(pathogenTestResultType);
pathogenTest.setTestDateTime(sampleTestDateTime);
try {
DatabaseHelper.getSampleTestDao().saveAndSnapshot(pathogenTest);
DatabaseHelper.getSampleTestDao().accept(pathogenTest);
} catch (DaoException e) {
throw new RuntimeException(e);
}
return DatabaseHelper.getSampleTestDao().queryForIdWithEmbedded(pathogenTest.getId());
}
use of de.symeda.sormas.api.sample.PathogenTestResultType in project SORMAS-Project by hzi-braunschweig.
the class SampleFacadeEjbTest method testGetNewTestResultCountByResultType.
@Test
public void testGetNewTestResultCountByResultType() {
RDCFEntities rdcf = creator.createRDCFEntities();
UserReferenceDto user = creator.createUser(rdcf).toReference();
PersonReferenceDto person1 = creator.createPerson("Heinz", "First").toReference();
PersonReferenceDto person2 = creator.createPerson("Heinz", "Second").toReference();
CaseDataDto case1 = creator.createCase(user, person1, rdcf);
CaseDataDto case2 = creator.createCase(user, person2, rdcf);
List<Long> caseIds = getCaseService().getAllIds(null);
// no existing samples
SampleFacade sampleFacade = getSampleFacade();
Map<PathogenTestResultType, Long> resultMap = sampleFacade.getNewTestResultCountByResultType(caseIds);
assertEquals(new Long(0), resultMap.values().stream().collect(Collectors.summingLong(Long::longValue)));
assertNull(resultMap.getOrDefault(PathogenTestResultType.INDETERMINATE, null));
assertNull(resultMap.getOrDefault(PathogenTestResultType.NEGATIVE, null));
assertNull(resultMap.getOrDefault(PathogenTestResultType.PENDING, null));
assertNull(resultMap.getOrDefault(PathogenTestResultType.POSITIVE, null));
// one pending sample with in one case
Facility lab = creator.createFacility("facility", rdcf.region, rdcf.district, rdcf.community);
creator.createSample(case1.toReference(), user, lab);
resultMap = sampleFacade.getNewTestResultCountByResultType(caseIds);
assertEquals(new Long(1), resultMap.values().stream().collect(Collectors.summingLong(Long::longValue)));
assertNull(resultMap.getOrDefault(PathogenTestResultType.INDETERMINATE, null));
assertNull(resultMap.getOrDefault(PathogenTestResultType.NEGATIVE, null));
assertEquals(new Long(1), resultMap.getOrDefault(PathogenTestResultType.PENDING, null));
assertNull(resultMap.getOrDefault(PathogenTestResultType.POSITIVE, null));
// one pending sample in each of two cases
creator.createSample(case2.toReference(), user, lab);
resultMap = sampleFacade.getNewTestResultCountByResultType(caseIds);
assertEquals(new Long(2), resultMap.values().stream().collect(Collectors.summingLong(Long::longValue)));
assertNull(resultMap.getOrDefault(PathogenTestResultType.INDETERMINATE, null));
assertNull(resultMap.getOrDefault(PathogenTestResultType.NEGATIVE, null));
assertEquals(new Long(2), resultMap.getOrDefault(PathogenTestResultType.PENDING, null));
assertNull(resultMap.getOrDefault(PathogenTestResultType.POSITIVE, null));
// one pending sample in each of two cases
// and one positive sample in one of the two cases
SampleDto sample = creator.createSample(case1.toReference(), user, lab);
sample.setPathogenTestResult(PathogenTestResultType.POSITIVE);
sampleFacade.saveSample(sample);
resultMap = sampleFacade.getNewTestResultCountByResultType(caseIds);
assertEquals(new Long(2), resultMap.values().stream().collect(Collectors.summingLong(Long::longValue)));
assertNull(resultMap.getOrDefault(PathogenTestResultType.INDETERMINATE, null));
assertNull(resultMap.getOrDefault(PathogenTestResultType.NEGATIVE, null));
assertEquals(new Long(1), resultMap.getOrDefault(PathogenTestResultType.PENDING, null));
assertEquals(new Long(1), resultMap.getOrDefault(PathogenTestResultType.POSITIVE, null));
}
use of de.symeda.sormas.api.sample.PathogenTestResultType in project SORMAS-Project by hzi-braunschweig.
the class PathogenTestEditFragment method onAfterLayoutBinding.
@Override
public void onAfterLayoutBinding(FragmentPathogenTestEditLayoutBinding contentBinding) {
setFieldVisibilitiesAndAccesses(PathogenTestDto.class, contentBinding.mainContent);
// Initialize ControlSpinnerFields
contentBinding.pathogenTestTestType.initializeSpinner(testTypeList, new ValueChangeListener() {
@Override
public void onChange(ControlPropertyField field) {
PathogenTestType currentTestType = (PathogenTestType) field.getValue();
if ((PathogenTestType.PCR_RT_PCR == currentTestType && PathogenTestResultType.POSITIVE == contentBinding.pathogenTestTestResult.getValue()) || PathogenTestType.CQ_VALUE_DETECTION == currentTestType) {
contentBinding.pathogenTestCqValue.setVisibility(View.VISIBLE);
} else {
contentBinding.pathogenTestCqValue.hideField(true);
}
if (PathogenTestType.PCR_RT_PCR == currentTestType && Disease.CORONAVIRUS == record.getTestedDisease()) {
getContentBinding().pathogenTestPcrTestSpecification.setVisibility(View.VISIBLE);
} else {
getContentBinding().pathogenTestPcrTestSpecification.hideField(false);
}
}
});
contentBinding.pathogenTestPcrTestSpecification.initializeSpinner(pcrTestSpecificationList);
contentBinding.pathogenTestTestedDiseaseVariant.initializeSpinner(diseaseVariantList);
contentBinding.pathogenTestTestedDisease.initializeSpinner(diseaseList, new ValueChangeListener() {
final Disease currentDisease = record.getTestedDisease();
@Override
public void onChange(ControlPropertyField field) {
if (PathogenTestType.PCR_RT_PCR == record.getTestType() && Disease.CORONAVIRUS == field.getValue()) {
getContentBinding().pathogenTestPcrTestSpecification.setVisibility(View.VISIBLE);
} else {
getContentBinding().pathogenTestPcrTestSpecification.hideField(false);
}
if (this.currentDisease == null || contentBinding.pathogenTestTestedDisease.getValue() != currentDisease) {
updateDiseaseVariantsField(contentBinding);
}
testTypeList = DataUtils.toItems(Arrays.asList(PathogenTestType.values()), true, FieldVisibilityCheckers.withDisease((Disease) field.getValue()), PathogenTestType.class);
contentBinding.pathogenTestTestType.setSpinnerData(testTypeList);
}
});
contentBinding.pathogenTestTestResult.initializeSpinner(testResultList, new ValueChangeListener() {
@Override
public void onChange(ControlPropertyField field) {
PathogenTestResultType currentPathogenTestResult = (PathogenTestResultType) field.getValue();
if ((PathogenTestType.PCR_RT_PCR == contentBinding.pathogenTestTestType.getValue() && PathogenTestResultType.POSITIVE == currentPathogenTestResult) || PathogenTestType.CQ_VALUE_DETECTION == contentBinding.pathogenTestTestType.getValue()) {
contentBinding.pathogenTestCqValue.setVisibility(View.VISIBLE);
} else {
contentBinding.pathogenTestCqValue.hideField(true);
}
}
});
contentBinding.pathogenTestLab.initializeSpinner(DataUtils.toItems(labList), new ValueChangeListener() {
@Override
public void onChange(ControlPropertyField field) {
Facility laboratory = (Facility) field.getValue();
if (laboratory != null && laboratory.getUuid().equals(FacilityDto.OTHER_FACILITY_UUID)) {
contentBinding.pathogenTestLabDetails.setVisibility(View.VISIBLE);
} else {
contentBinding.pathogenTestLabDetails.hideField(true);
}
}
});
// // Initialize ControlDateFields
contentBinding.pathogenTestReportDate.initializeDateField(getFragmentManager());
contentBinding.pathogenTestTestDateTime.initializeDateTimeField(getFragmentManager());
if (sample.getSamplePurpose() == SamplePurpose.INTERNAL) {
contentBinding.pathogenTestLab.setRequired(false);
}
}
use of de.symeda.sormas.api.sample.PathogenTestResultType in project SORMAS-Project by hzi-braunschweig.
the class SampleService method getNewTestResultCountByResultType.
public Map<PathogenTestResultType, Long> getNewTestResultCountByResultType(List<Long> caseIds) {
if (CollectionUtils.isEmpty(caseIds)) {
// Avoid empty IN clause
return new HashMap<>();
}
// Avoid parameter limit by joining caseIds to a String instead of n parameters
StringBuilder queryBuilder = new StringBuilder();
// @formatter:off
queryBuilder.append("WITH sortedsamples AS (SELECT DISTINCT ON (").append(Sample.ASSOCIATED_CASE).append("_id) ").append(Sample.ASSOCIATED_CASE).append("_id, ").append(Sample.PATHOGEN_TEST_RESULT).append(", ").append(Sample.SAMPLE_DATE_TIME).append(" FROM ").append(Sample.TABLE_NAME).append(" WHERE (").append(Sample.SPECIMEN_CONDITION).append(" IS NULL OR ").append(Sample.SPECIMEN_CONDITION).append(" = '").append(SpecimenCondition.ADEQUATE.name()).append("') AND ").append(Sample.TABLE_NAME).append(".").append(Sample.DELETED).append(" = false ORDER BY ").append(Sample.ASSOCIATED_CASE).append("_id, ").append(Sample.SAMPLE_DATE_TIME).append(" desc) SELECT sortedsamples.").append(Sample.PATHOGEN_TEST_RESULT).append(", COUNT(").append(Sample.ASSOCIATED_CASE).append("_id) FROM sortedsamples JOIN ").append(Case.TABLE_NAME).append(" ON sortedsamples.").append(Sample.ASSOCIATED_CASE).append("_id = ").append(Case.TABLE_NAME).append(".id ").append(" WHERE sortedsamples.").append(Sample.ASSOCIATED_CASE).append("_id IN (:caseIds)").append(" GROUP BY sortedsamples." + Sample.PATHOGEN_TEST_RESULT);
if (caseIds.size() < ModelConstants.PARAMETER_LIMIT) {
List<Object[]> results;
Query query = em.createNativeQuery(queryBuilder.toString());
query.setParameter("caseIds", caseIds);
results = query.getResultList();
return results.stream().filter(e -> e[0] != null).collect(Collectors.toMap(e -> PathogenTestResultType.valueOf((String) e[0]), e -> ((BigInteger) e[1]).longValue()));
} else {
List<Object[]> results = new LinkedList<>();
IterableHelper.executeBatched(caseIds, ModelConstants.PARAMETER_LIMIT, batchedCaseIds -> {
Query query = em.createNativeQuery(queryBuilder.toString());
query.setParameter("caseIds", batchedCaseIds);
results.addAll(query.getResultList());
});
return results.stream().filter(e -> e[0] != null).collect(Collectors.toMap(e -> PathogenTestResultType.valueOf((String) e[0]), e -> ((BigInteger) e[1]).longValue(), Long::sum));
}
}
use of de.symeda.sormas.api.sample.PathogenTestResultType in project SORMAS-Project by hzi-braunschweig.
the class PathogenTestService method getPathogenTestResultsForCase.
public List<PathogenTestResultType> getPathogenTestResultsForCase(long caseId) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<PathogenTestResultType> cq = cb.createQuery(PathogenTestResultType.class);
Root<PathogenTest> root = cq.from(getElementClass());
cq.where(cb.and(createDefaultFilter(cb, root), cb.equal(root.get(PathogenTest.SAMPLE).get(Sample.ASSOCIATED_CASE).get(Case.ID), caseId)));
cq.select(root.get(PathogenTest.TEST_RESULT));
List<PathogenTestResultType> result = em.createQuery(cq).getResultList();
return result;
}
Aggregations