Search in sources :

Example 1 with TrackedEntityTypeAttribute

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute)

Example 2 with TrackedEntityTypeAttribute

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();
}
Also used : TextPattern(org.hisp.dhis.textpattern.TextPattern) TextPatternSegment(org.hisp.dhis.textpattern.TextPatternSegment) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) HashSet(java.util.HashSet)

Example 3 with TrackedEntityTypeAttribute

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);
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 4 with TrackedEntityTypeAttribute

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());
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) Test(org.junit.jupiter.api.Test)

Example 5 with TrackedEntityTypeAttribute

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());
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Aggregations

TrackedEntityTypeAttribute (org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute)12 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)9 Test (org.junit.jupiter.api.Test)7 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)6 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)5 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)5 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)4 HashSet (java.util.HashSet)3 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Sets (com.google.common.collect.Sets)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1