Search in sources :

Example 6 with TrackedEntityTypeAttribute

use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.

the class HibernateTrackedEntityAttributeStore method getTrackedEntityAttributesByTrackedEntityTypes.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Set<TrackedEntityAttribute> getTrackedEntityAttributesByTrackedEntityTypes() {
    Query query = sessionFactory.getCurrentSession().createQuery("select trackedEntityTypeAttributes from TrackedEntityType");
    Set<TrackedEntityTypeAttribute> trackedEntityTypeAttributes = new HashSet<>(query.list());
    return trackedEntityTypeAttributes.stream().map(TrackedEntityTypeAttribute::getTrackedEntityAttribute).collect(Collectors.toSet());
}
Also used : Query(org.hibernate.query.Query) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) HashSet(java.util.HashSet)

Example 7 with TrackedEntityTypeAttribute

use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.

the class TrackedEntityInstanceAttributesAggregateTest method populatePrerequisites.

private void populatePrerequisites(boolean removeOwnership) {
    doInTransaction(() -> {
        ProgramStage programStageA = createProgramStage(programB, true);
        ProgramStage programStageB = createProgramStage(programB, true);
        ProgramStage programStageA1 = createProgramStage(programA, true);
        ProgramStage programStageA2 = createProgramStage(programA, true);
        // Create 5 Tracked Entity Attributes (named A .. E)
        IntStream.range(A, F).mapToObj(i -> Character.toString((char) i)).forEach(c -> attributeService.addTrackedEntityAttribute(createTrackedEntityAttribute(c.charAt(0), ValueType.TEXT)));
        // Transform the Tracked Entity Attributes into a List of
        // TrackedEntityTypeAttribute
        List<TrackedEntityTypeAttribute> teatList = IntStream.range(A, C).mapToObj(i -> Character.toString((char) i)).map(s -> new TrackedEntityTypeAttribute(trackedEntityTypeA, attributeService.getTrackedEntityAttributeByName("Attribute" + s))).collect(Collectors.toList());
        // Assign 2 (A, B) TrackedEntityTypeAttribute to Tracked Entity Type
        // A
        trackedEntityTypeA.getTrackedEntityTypeAttributes().addAll(teatList);
        // Make TET public
        trackedEntityTypeA.setPublicAccess(AccessStringHelper.FULL);
        manager.update(trackedEntityTypeA);
        programB = createProgram('B', new HashSet<>(), organisationUnitA);
        programB.setProgramType(ProgramType.WITH_REGISTRATION);
        programB.setCategoryCombo(categoryComboA);
        programB.setAccessLevel(AccessLevel.PROTECTED);
        programB.setUid(CodeGenerator.generateUid());
        programB.setCode(RandomStringUtils.randomAlphanumeric(10));
        Set<UserAccess> programBUserAccess = new HashSet<>();
        programBUserAccess.add(new UserAccess(currentUserService.getCurrentUser(), AccessStringHelper.FULL));
        programB.setUserAccesses(programBUserAccess);
        programB.setProgramStages(Stream.of(programStageA, programStageB).collect(Collectors.toCollection(HashSet::new)));
        programService.addProgram(programB);
        programA = createProgram('A', new HashSet<>(), organisationUnitA);
        programA.setProgramType(ProgramType.WITH_REGISTRATION);
        programA.setCategoryCombo(categoryComboA);
        programA.setUid(CodeGenerator.generateUid());
        programA.setCode(RandomStringUtils.randomAlphanumeric(10));
        programA.setProgramStages(Stream.of(programStageA1, programStageA2).collect(Collectors.toCollection(HashSet::new)));
        programService.addProgram(programA);
        // Because access strings isnt getting persisted with programService
        // methods for some reason
        programB.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programB);
        programA.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programA);
        programStageA.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programStageA);
        programStageB.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programStageB);
        programStageA1.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programStageA1);
        programStageA2.setPublicAccess(AccessStringHelper.FULL);
        manager.update(programStageA2);
        // Assign ProgramTrackedEntityAttribute C to program A
        List<ProgramTrackedEntityAttribute> pteaListA = IntStream.range(C, D).mapToObj(i -> Character.toString((char) i)).map(s -> new ProgramTrackedEntityAttribute(programA, attributeService.getTrackedEntityAttributeByName("Attribute" + s))).collect(Collectors.toList());
        // Assign ProgramTrackedEntityAttribute D, E to program B
        List<ProgramTrackedEntityAttribute> pteaListB = IntStream.range(D, F).mapToObj(i -> Character.toString((char) i)).map(s -> new ProgramTrackedEntityAttribute(programB, attributeService.getTrackedEntityAttributeByName("Attribute" + s))).collect(Collectors.toList());
        programA.getProgramAttributes().addAll(pteaListA);
        programB.getProgramAttributes().addAll(pteaListB);
        manager.update(programA);
        manager.update(programB);
        // Create a TEI associated to program B
        final org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = persistTrackedEntityInstance(ImmutableMap.of("program", programB));
        ProgramInstance piB = new ProgramInstance(programB, trackedEntityInstance, organisationUnitA);
        piB.setEnrollmentDate(new Date());
        manager.save(piB);
        ProgramInstance piA = new ProgramInstance(programA, trackedEntityInstance, organisationUnitA);
        piA.setEnrollmentDate(new Date());
        manager.save(piA);
        if (removeOwnership) {
            trackerOwnershipManager.assignOwnership(trackedEntityInstance, programB, organisationUnitB, true, true);
            trackerOwnershipManager.assignOwnership(trackedEntityInstance, programA, organisationUnitA, true, true);
        } else {
            trackerOwnershipManager.assignOwnership(trackedEntityInstance, programB, organisationUnitA, true, true);
            trackerOwnershipManager.assignOwnership(trackedEntityInstance, programA, organisationUnitA, true, true);
        }
        // Assign Attribute A,B,E to Tracked Entity Instance
        attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(attributeService.getTrackedEntityAttributeByName("AttributeA"), trackedEntityInstance, "A"));
        attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(attributeService.getTrackedEntityAttributeByName("AttributeB"), trackedEntityInstance, "B"));
        attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(attributeService.getTrackedEntityAttributeByName("AttributeC"), trackedEntityInstance, "C"));
        attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(attributeService.getTrackedEntityAttributeByName("AttributeE"), trackedEntityInstance, "E"));
    });
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Date(java.util.Date) ValueType(org.hisp.dhis.common.ValueType) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Program(org.hisp.dhis.program.Program) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) HashSet(java.util.HashSet) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ImmutableMap(org.testcontainers.shaded.com.google.common.collect.ImmutableMap) Matchers.hasSize(org.hamcrest.Matchers.hasSize) User(org.hisp.dhis.user.User) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) AccessStringHelper(org.hisp.dhis.security.acl.AccessStringHelper) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Set(java.util.Set) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Collectors(java.util.stream.Collectors) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) ProgramStage(org.hisp.dhis.program.ProgramStage) Sets(com.google.common.collect.Sets) Test(org.junit.jupiter.api.Test) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) List(java.util.List) Stream(java.util.stream.Stream) UserAccess(org.hisp.dhis.user.UserAccess) ProgramType(org.hisp.dhis.program.ProgramType) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) Matchers.is(org.hamcrest.Matchers.is) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) CodeGenerator(org.hisp.dhis.common.CodeGenerator) TrackerTest(org.hisp.dhis.dxf2.TrackerTest) ProgramService(org.hisp.dhis.program.ProgramService) AccessLevel(org.hisp.dhis.common.AccessLevel) UserAccess(org.hisp.dhis.user.UserAccess) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Date(java.util.Date) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) ProgramStage(org.hisp.dhis.program.ProgramStage) HashSet(java.util.HashSet)

Example 8 with TrackedEntityTypeAttribute

use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.

the class TrackedEntityTypeObjectBundleHookTest method shouldReportNoErrorTeaExists.

@Test
void shouldReportNoErrorTeaExists() {
    when(preheat.get(any(), any())).thenReturn(new TrackedEntityAttribute());
    TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
    trackedEntityTypeAttribute.setTrackedEntityType(trackedEntityType);
    trackedEntityTypeAttribute.setTrackedEntityAttribute(new TrackedEntityAttribute());
    trackedEntityType.setTrackedEntityTypeAttributes(Arrays.asList(trackedEntityTypeAttribute, null));
    assertEquals(0, trackedEntityTypeObjectBundleHook.validate(trackedEntityType, bundle).size());
    verify(bundle, times(1)).getPreheat();
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) Test(org.junit.jupiter.api.Test)

Example 9 with TrackedEntityTypeAttribute

use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.

the class TrackedEntityTypeObjectBundleHookTest method shouldReportErrorTeaNotExists.

@Test
void shouldReportErrorTeaNotExists() {
    trackedEntityAttribute = new TrackedEntityAttribute();
    trackedEntityAttribute.setUid("teaUid");
    when(preheat.get(any(), any())).thenReturn(null);
    TrackedEntityTypeAttribute trackedEntityTypeAttribute = new TrackedEntityTypeAttribute();
    trackedEntityTypeAttribute.setTrackedEntityType(trackedEntityType);
    trackedEntityTypeAttribute.setTrackedEntityAttribute(new TrackedEntityAttribute());
    trackedEntityType.setTrackedEntityTypeAttributes(Collections.singletonList(trackedEntityTypeAttribute));
    assertEquals(1, trackedEntityTypeObjectBundleHook.validate(trackedEntityType, bundle).size());
    verify(bundle, times(1)).getPreheat();
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) Test(org.junit.jupiter.api.Test)

Example 10 with TrackedEntityTypeAttribute

use of org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute in project dhis2-core by dhis2.

the class TrackedEntityAttributeValidationHook method validateAttributes.

protected void validateAttributes(ValidationErrorReporter reporter, TrackedEntity trackedEntity, TrackedEntityInstance tei, OrganisationUnit orgUnit, TrackedEntityType trackedEntityType) {
    checkNotNull(trackedEntity, TrackerImporterAssertErrors.TRACKED_ENTITY_CANT_BE_NULL);
    checkNotNull(trackedEntityType, TrackerImporterAssertErrors.TRACKED_ENTITY_TYPE_CANT_BE_NULL);
    Map<String, TrackedEntityAttributeValue> valueMap = new HashMap<>();
    if (tei != null) {
        valueMap = tei.getTrackedEntityAttributeValues().stream().collect(Collectors.toMap(v -> v.getAttribute().getUid(), v -> v));
    }
    for (Attribute attribute : trackedEntity.getAttributes()) {
        TrackedEntityAttribute tea = reporter.getValidationContext().getTrackedEntityAttribute(attribute.getAttribute());
        if (tea == null) {
            reporter.addError(trackedEntity, E1006, attribute.getAttribute());
            continue;
        }
        if (attribute.getValue() == null) {
            Optional<TrackedEntityTypeAttribute> optionalTea = Optional.of(trackedEntityType).map(tet -> tet.getTrackedEntityTypeAttributes().stream()).flatMap(tetAtts -> tetAtts.filter(teaAtt -> teaAtt.getTrackedEntityAttribute().getUid().equals(attribute.getAttribute()) && teaAtt.isMandatory() != null && teaAtt.isMandatory()).findFirst());
            if (optionalTea.isPresent())
                reporter.addError(trackedEntity, E1076, TrackedEntityAttribute.class.getSimpleName(), attribute.getAttribute());
            continue;
        }
        validateAttributeValue(reporter, trackedEntity, tea, attribute.getValue());
        validateAttrValueType(reporter, trackedEntity, attribute, tea);
        validateOptionSet(reporter, trackedEntity, tea, attribute.getValue());
        validateAttributeUniqueness(reporter, trackedEntity, attribute.getValue(), tea, tei, orgUnit);
        validateFileNotAlreadyAssigned(reporter, trackedEntity, attribute, valueMap);
    }
}
Also used : Attribute(org.hisp.dhis.tracker.domain.Attribute) Constant(org.hisp.dhis.tracker.util.Constant) HashMap(java.util.HashMap) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ValidationErrorReporter(org.hisp.dhis.tracker.report.ValidationErrorReporter) E1084(org.hisp.dhis.tracker.report.TrackerErrorCode.E1084) E1085(org.hisp.dhis.tracker.report.TrackerErrorCode.E1085) TRACKED_ENTITY_ATTRIBUTE_VALUE_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.TRACKED_ENTITY_ATTRIBUTE_VALUE_CANT_BE_NULL) TrackerImportValidationContext(org.hisp.dhis.tracker.validation.TrackerImportValidationContext) Map(java.util.Map) E1090(org.hisp.dhis.tracker.report.TrackerErrorCode.E1090) ATTRIBUTE_CANT_BE_NULL(org.hisp.dhis.tracker.validation.hooks.TrackerImporterAssertErrors.ATTRIBUTE_CANT_BE_NULL) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) FileResource(org.hisp.dhis.fileresource.FileResource) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) E1077(org.hisp.dhis.tracker.report.TrackerErrorCode.E1077) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) E1076(org.hisp.dhis.tracker.report.TrackerErrorCode.E1076) Set(java.util.Set) E1112(org.hisp.dhis.tracker.report.TrackerErrorCode.E1112) TrackedAttributeValidationService(org.hisp.dhis.tracker.validation.service.attribute.TrackedAttributeValidationService) Collectors(java.util.stream.Collectors) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ValidationUtils.dataValueIsValid(org.hisp.dhis.system.util.ValidationUtils.dataValueIsValid) Component(org.springframework.stereotype.Component) E1009(org.hisp.dhis.tracker.report.TrackerErrorCode.E1009) E1006(org.hisp.dhis.tracker.report.TrackerErrorCode.E1006) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) HashMap(java.util.HashMap) Attribute(org.hisp.dhis.tracker.domain.Attribute) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityTypeAttribute(org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute)

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