use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class TrackedEntityInstanceController method getTrackedEntityInstances.
// -------------------------------------------------------------------------
// READ
// -------------------------------------------------------------------------
@GetMapping(produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_XML, ContextUtils.CONTENT_TYPE_CSV })
@ResponseBody
public RootNode getTrackedEntityInstances(TrackedEntityInstanceCriteria criteria, HttpServletResponse response) {
List<String> fields = contextService.getFieldsFromRequestOrAll();
TrackedEntityInstanceQueryParams queryParams = criteriaMapper.map(criteria);
List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, getTrackedEntityInstanceParams(fields), false, false);
RootNode rootNode = NodeUtils.createMetadata();
if (queryParams.isPaging() && queryParams.isTotalPages()) {
int count = trackedEntityInstanceService.getTrackedEntityInstanceCount(queryParams, true, true);
Pager pager = new Pager(queryParams.getPageWithDefault(), count, queryParams.getPageSizeWithDefault());
rootNode.addChild(NodeUtils.createPager(pager));
}
if (!StringUtils.isEmpty(criteria.getAttachment())) {
response.addHeader(ContextUtils.HEADER_CONTENT_DISPOSITION, "attachment; filename=" + criteria.getAttachment());
response.addHeader(ContextUtils.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
}
rootNode.addChild(fieldFilterService.toCollectionNode(TrackedEntityInstance.class, new FieldFilterParams(trackedEntityInstances, fields)));
return rootNode;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class HandleRelationshipsTrackedEntityInstanceServiceTest method testUpdateTeiWithUniDirectionalRelationshipTeiToTei.
@Test
void testUpdateTeiWithUniDirectionalRelationshipTeiToTei() {
TrackedEntityInstance trackedEntityInstanceFrom = trackedEntityInstanceService.getTrackedEntityInstance(this.trackedEntityInstanceA.getUid());
TrackedEntityInstance trackedEntityInstanceTo = trackedEntityInstanceService.getTrackedEntityInstance(this.trackedEntityInstanceB.getUid());
RelationshipType personToPersonRelationshipType = createPersonToPersonRelationshipType('A', programA, trackedEntityType, false);
relationshipTypeService.addRelationshipType(personToPersonRelationshipType);
Relationship relationship = createTeiToTeiRelationship('A', personToPersonRelationshipType, trackedEntityInstanceFrom, trackedEntityInstanceTo);
trackedEntityInstanceFrom.setRelationships(Lists.newArrayList(relationship));
ImportSummary importSummaryFrom = trackedEntityInstanceService.updateTrackedEntityInstance(trackedEntityInstanceFrom, null, null, true);
assertEquals(ImportStatus.SUCCESS, importSummaryFrom.getStatus());
assertEquals(ImportStatus.SUCCESS, importSummaryFrom.getRelationships().getStatus());
trackedEntityInstanceTo.setRelationships(Lists.newArrayList(relationship));
ImportSummary importSummaryTo = trackedEntityInstanceService.updateTrackedEntityInstance(trackedEntityInstanceTo, null, null, true);
assertEquals(ImportStatus.SUCCESS, importSummaryTo.getStatus());
assertEquals(ImportStatus.ERROR, importSummaryTo.getRelationships().getStatus());
assertEquals("Can't update relationship '" + relationship.getRelationship() + "': TrackedEntityInstance '" + trackedEntityInstanceTo.getTrackedEntityInstance() + "' is not the owner of the relationship", importSummaryTo.getRelationships().getImportSummaries().get(0).getDescription());
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class TrackerTest method _persistTrackedEntityInstanceWithEnrollmentAndEvents.
private TrackedEntityInstance _persistTrackedEntityInstanceWithEnrollmentAndEvents(int eventSize, Map<String, Object> enrollmentValues) {
TrackedEntityInstance entityInstance = persistTrackedEntityInstance();
final ImportSummary importSummary = enrollmentService.addEnrollment(createEnrollmentWithEvents(this.programA, entityInstance, eventSize, enrollmentValues), ImportOptions.getDefaultImportOptions());
assertEquals(0, importSummary.getConflictCount());
assertThat(importSummary.getEvents().getImported(), is(eventSize));
return entityInstance;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractEnrollmentService method checkAttributes.
private List<ImportConflict> checkAttributes(Enrollment enrollment, ImportOptions importOptions) {
List<ImportConflict> importConflicts = new ArrayList<>();
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = teiService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
Map<TrackedEntityAttribute, Boolean> mandatoryMap = Maps.newHashMap();
Map<String, String> attributeValueMap = Maps.newHashMap();
for (ProgramTrackedEntityAttribute programTrackedEntityAttribute : program.getProgramAttributes()) {
mandatoryMap.put(programTrackedEntityAttribute.getAttribute(), programTrackedEntityAttribute.isMandatory());
}
// ignore attributes which do not belong to this program
trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> mandatoryMap.containsKey(value.getAttribute())).forEach(value -> attributeValueMap.put(value.getAttribute().getUid(), value.getValue()));
for (Attribute attribute : enrollment.getAttributes()) {
attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
importConflicts.addAll(validateAttributeType(attribute, importOptions));
}
TrackedEntityInstance instance = trackedEntityInstanceService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
for (TrackedEntityAttribute trackedEntityAttribute : mandatoryMap.keySet()) {
Boolean mandatory = mandatoryMap.get(trackedEntityAttribute);
if (mandatory && !attributeValueMap.containsKey(trackedEntityAttribute.getUid())) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Missing mandatory attribute " + trackedEntityAttribute.getUid()));
continue;
}
if (trackedEntityAttribute.isUnique()) {
OrganisationUnit organisationUnit = manager.get(OrganisationUnit.class, instance.getOrgUnit());
importConflicts.addAll(checkScope(trackedEntityInstance, trackedEntityAttribute, attributeValueMap.get(trackedEntityAttribute.getUid()), organisationUnit, program));
}
attributeValueMap.remove(trackedEntityAttribute.getUid());
}
if (!attributeValueMap.isEmpty()) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Only program attributes is allowed for enrollment " + attributeValueMap));
}
return importConflicts;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method checkTrackedEntity.
private List<ImportConflict> checkTrackedEntity(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
List<ImportConflict> importConflicts = new ArrayList<>();
if (trackedEntityInstance.getTrackedEntity() == null) {
importConflicts.add(new ImportConflict("TrackedEntityInstance.trackedEntity", "Missing required property trackedEntity"));
return importConflicts;
}
TrackedEntity trackedEntity = getTrackedEntity(importOptions.getIdSchemes(), trackedEntityInstance.getTrackedEntity());
if (trackedEntity == null) {
importConflicts.add(new ImportConflict("TrackedEntityInstance.trackedEntity", "Invalid trackedEntity" + trackedEntityInstance.getTrackedEntity()));
}
return importConflicts;
}
Aggregations