use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.
the class HibernateTrackedEntityTypeAttributeStore method getAttributes.
@Override
public List<TrackedEntityAttribute> getAttributes(List<TrackedEntityType> trackedEntityTypes) {
CriteriaBuilder builder = getCriteriaBuilder();
CriteriaQuery<TrackedEntityAttribute> query = builder.createQuery(TrackedEntityAttribute.class);
Root<TrackedEntityTypeAttribute> root = query.from(TrackedEntityTypeAttribute.class);
query.select(root.get("trackedEntityAttribute"));
query.where(root.get("trackedEntityType").in(trackedEntityTypes));
query.distinct(true);
return getSession().createQuery(query).getResultList();
}
use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.
the class TrackedEntityInstanceServiceTest method setUpTest.
@Override
protected void setUpTest() throws Exception {
organisationUnitA = createOrganisationUnit('A');
organisationUnitB = createOrganisationUnit('B');
organisationUnitB.setParent(organisationUnitA);
uniqueIdAttribute = createTrackedEntityAttribute('A');
uniqueIdAttribute.setGenerated(true);
// uniqueIdAttribute.setPattern( "RANDOM(#####)" );
TextPattern textPattern = new TextPattern(Lists.newArrayList(new TextPatternSegment(TextPatternMethod.RANDOM, "RANDOM(#####)")));
textPattern.setOwnerObject(Objects.TRACKEDENTITYATTRIBUTE);
textPattern.setOwnerUid(uniqueIdAttribute.getUid());
uniqueIdAttribute.setTextPattern(textPattern);
trackedEntityAttributeService.addTrackedEntityAttribute(uniqueIdAttribute);
trackedEntityAttributeB = createTrackedEntityAttribute('B');
trackedEntityAttributeService.addTrackedEntityAttribute(trackedEntityAttributeB);
trackedEntityType = createTrackedEntityType('A');
TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
trackedEntityTypeAttribute.setTrackedEntityAttribute(uniqueIdAttribute);
trackedEntityTypeAttribute.setTrackedEntityType(trackedEntityType);
trackedEntityType.setTrackedEntityTypeAttributes(Lists.newArrayList(trackedEntityTypeAttribute));
trackedEntityTypeService.addTrackedEntityType(trackedEntityType);
maleA = createTrackedEntityInstance(organisationUnitA);
maleB = createTrackedEntityInstance(organisationUnitB);
femaleA = createTrackedEntityInstance(organisationUnitA);
femaleB = createTrackedEntityInstance(organisationUnitB);
dateConflictsMaleA = createTrackedEntityInstance(organisationUnitA);
TrackedEntityAttributeValue uniqueId = createTrackedEntityAttributeValue('A', maleA, uniqueIdAttribute);
uniqueId.setValue("12345");
maleA.setTrackedEntityType(trackedEntityType);
maleA.setTrackedEntityAttributeValues(Sets.newHashSet(uniqueId));
maleB.setTrackedEntityType(trackedEntityType);
femaleA.setTrackedEntityType(trackedEntityType);
femaleB.setTrackedEntityType(trackedEntityType);
dateConflictsMaleA.setTrackedEntityType(trackedEntityType);
programA = createProgram('A', new HashSet<>(), organisationUnitA);
programA.setProgramType(ProgramType.WITH_REGISTRATION);
programStageA1 = createProgramStage('1', programA);
programStageA2 = createProgramStage('2', programA);
programA.setProgramStages(Stream.of(programStageA1, programStageA2).collect(Collectors.toCollection(HashSet::new)));
manager.save(organisationUnitA);
manager.save(organisationUnitB);
manager.save(maleA);
manager.save(maleB);
manager.save(femaleA);
manager.save(femaleB);
manager.save(dateConflictsMaleA);
manager.save(programA);
manager.save(programStageA1);
manager.save(programStageA2);
teiMaleA = trackedEntityInstanceService.getTrackedEntityInstance(maleA);
teiMaleB = trackedEntityInstanceService.getTrackedEntityInstance(maleB);
teiFemaleA = trackedEntityInstanceService.getTrackedEntityInstance(femaleA);
trackedEntityAttributeValueService.addTrackedEntityAttributeValue(uniqueId);
programInstanceService.enrollTrackedEntityInstance(maleA, programA, null, null, organisationUnitA);
programInstanceService.enrollTrackedEntityInstance(femaleA, programA, DateTime.now().plusMonths(1).toDate(), null, organisationUnitA);
programInstanceService.enrollTrackedEntityInstance(dateConflictsMaleA, programA, DateTime.now().plusMonths(1).toDate(), DateTime.now().plusMonths(2).toDate(), organisationUnitA);
manager.flush();
}
use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.
the class TrackerSynchronizationTest method prepareDataForTest.
private void prepareDataForTest() {
TrackedEntityAttribute teaA = createTrackedEntityAttribute('a');
TrackedEntityAttribute teaB = createTrackedEntityAttribute('b');
teaB.setSkipSynchronization(true);
currentSession.save(teaA);
currentSession.save(teaB);
TrackedEntityType tet = createTrackedEntityType('a');
TrackedEntityTypeAttribute tetaA = new TrackedEntityTypeAttribute(tet, teaA, true, false);
TrackedEntityTypeAttribute tetaB = new TrackedEntityTypeAttribute(tet, teaB, true, false);
tet.getTrackedEntityTypeAttributes().add(tetaA);
tet.getTrackedEntityTypeAttributes().add(tetaB);
currentSession.save(tet);
OrganisationUnit ou = createOrganisationUnit('a');
TrackedEntityInstance tei = createTrackedEntityInstance('a', ou, teaA);
tei.setTrackedEntityType(tet);
TrackedEntityAttributeValue teavB = createTrackedEntityAttributeValue('b', tei, teaB);
tei.getTrackedEntityAttributeValues().add(teavB);
TrackedEntityAttributeValue teavA = createTrackedEntityAttributeValue('a', tei, teaA);
tei.getTrackedEntityAttributeValues().add(teavA);
currentSession.save(ou);
currentSession.save(tei);
currentSession.save(teavA);
currentSession.save(teavB);
}
use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.
the class TrackedEntityAttributeValidationHookTest method shouldFailValidationWhenValueIsNullAndAttributeIsMandatory.
@Test
void shouldFailValidationWhenValueIsNullAndAttributeIsMandatory() {
TrackedEntity trackedEntity = TrackedEntity.builder().attributes(Collections.singletonList(Attribute.builder().attribute("trackedEntityAttribute").build())).trackedEntityType("trackedEntityType").build();
TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
TrackedEntityAttribute trackedEntityAttribute = new TrackedEntityAttribute();
trackedEntityAttribute.setUid("trackedEntityAttribute");
trackedEntityAttribute.setValueType(ValueType.TEXT);
trackedEntityTypeAttribute.setTrackedEntityAttribute(trackedEntityAttribute);
trackedEntityTypeAttribute.setMandatory(true);
TrackedEntityType trackedEntityType = new TrackedEntityType();
trackedEntityType.setTrackedEntityTypeAttributes(Collections.singletonList(trackedEntityTypeAttribute));
when(validationContext.getTrackedEntityAttribute("trackedEntityAttribute")).thenReturn(trackedEntityAttribute);
when(validationContext.getTrackedEntityType(anyString())).thenReturn(trackedEntityType);
ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
trackedEntityAttributeValidationHook.validateTrackedEntity(reporter, trackedEntity);
assertTrue(reporter.hasErrors());
assertEquals(1, reporter.getReportList().size());
assertEquals(TrackerErrorCode.E1076, reporter.getReportList().get(0).getErrorCode());
}
use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.
the class TrackedEntityAttributeValidationHookTest method shouldFailMissingAttributeValue.
@Test
void shouldFailMissingAttributeValue() {
String tea = "tea";
String tet = "tet";
TrackedEntity trackedEntity = TrackedEntity.builder().attributes(Collections.singletonList(Attribute.builder().attribute(tea).build())).trackedEntityType(tet).build();
TrackedEntityType trackedEntityType = new TrackedEntityType();
TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
trackedEntityTypeAttribute.setMandatory(true);
TrackedEntityAttribute trackedEntityAttribute = new TrackedEntityAttribute();
trackedEntityAttribute.setUid(tea);
trackedEntityTypeAttribute.setTrackedEntityAttribute(trackedEntityAttribute);
trackedEntityType.setTrackedEntityTypeAttributes(Collections.singletonList(trackedEntityTypeAttribute));
when(validationContext.getTrackedEntityType(tet)).thenReturn(trackedEntityType);
when(validationContext.getTrackedEntityAttribute(tea)).thenReturn(trackedEntityAttribute);
ValidationErrorReporter reporter = new ValidationErrorReporter(validationContext);
trackedEntityAttributeValidationHook.validateTrackedEntity(reporter, trackedEntity);
assertTrue(reporter.hasErrors());
assertEquals(1, reporter.getReportList().size());
assertEquals(1, reporter.getReportList().stream().filter(e -> e.getErrorCode() == TrackerErrorCode.E1076).count());
}
Aggregations