Search in sources :

Example 61 with TrackedEntityInstance

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;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) Pager(org.hisp.dhis.common.Pager) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 62 with TrackedEntityInstance

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());
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 63 with TrackedEntityInstance

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;
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 64 with TrackedEntityInstance

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;
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) EventService(org.hisp.dhis.dxf2.events.event.EventService) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Coordinate(org.hisp.dhis.dxf2.events.event.Coordinate) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) UserService(org.hisp.dhis.user.UserService) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) Set(java.util.Set) DateUtils(org.hisp.dhis.system.util.DateUtils) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) Event(org.hisp.dhis.dxf2.events.event.Event) List(java.util.List) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) TrackedEntityCommentService(org.hisp.dhis.trackedentitycomment.TrackedEntityCommentService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) IdentifiableObjectCallable(org.hisp.dhis.system.callable.IdentifiableObjectCallable) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) I18nManager(org.hisp.dhis.i18n.I18nManager) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) IdSchemes(org.hisp.dhis.common.IdSchemes) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) IOException(java.io.IOException) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) Maps(com.google.common.collect.Maps) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Note(org.hisp.dhis.dxf2.events.event.Note) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStatus(org.hisp.dhis.program.ProgramStatus) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ProgramService(org.hisp.dhis.program.ProgramService) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 65 with TrackedEntityInstance

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;
}
Also used : TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) ArrayList(java.util.ArrayList) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Aggregations

TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)61 Test (org.junit.jupiter.api.Test)57 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)50 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)37 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)34 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)29 Date (java.util.Date)26 TrackerTest (org.hisp.dhis.dxf2.TrackerTest)26 ArrayList (java.util.ArrayList)25 List (java.util.List)25 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)25 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)24 Event (org.hisp.dhis.dxf2.events.event.Event)23 Program (org.hisp.dhis.program.Program)22 ProgramInstance (org.hisp.dhis.program.ProgramInstance)21 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)20 Collectors (java.util.stream.Collectors)19 User (org.hisp.dhis.user.User)17 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)16 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)16