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());
}
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"));
});
}
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();
}
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();
}
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);
}
}
Aggregations