Search in sources :

Example 1 with TrackedEntityType

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

the class EnrollmentSMSListener method postProcess.

@Override
protected SmsResponse postProcess(IncomingSms sms, SmsSubmission submission) throws SMSProcessingException {
    EnrollmentSmsSubmission subm = (EnrollmentSmsSubmission) submission;
    Date enrollmentDate = subm.getEnrollmentDate();
    Date incidentDate = subm.getIncidentDate();
    Uid teiUid = subm.getTrackedEntityInstance();
    Uid progid = subm.getTrackerProgram();
    Uid tetid = subm.getTrackedEntityType();
    Uid ouid = subm.getOrgUnit();
    Uid enrollmentid = subm.getEnrollment();
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(ouid.getUid());
    Program program = programService.getProgram(progid.getUid());
    if (program == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_PROGRAM.set(progid));
    }
    TrackedEntityType entityType = trackedEntityTypeService.getTrackedEntityType(tetid.getUid());
    if (entityType == null) {
        throw new SMSProcessingException(SmsResponse.INVALID_TETYPE.set(tetid));
    }
    if (!programService.hasOrgUnit(program, orgUnit)) {
        throw new SMSProcessingException(SmsResponse.OU_NOTIN_PROGRAM.set(ouid, progid));
    }
    TrackedEntityInstance entityInstance;
    boolean teiExists = teiService.trackedEntityInstanceExists(teiUid.getUid());
    if (teiExists) {
        log.info(String.format("Given TEI [%s] exists. Updating...", teiUid));
        entityInstance = teiService.getTrackedEntityInstance(teiUid.getUid());
    } else {
        log.info(String.format("Given TEI [%s] does not exist. Creating...", teiUid));
        entityInstance = new TrackedEntityInstance();
        entityInstance.setUid(teiUid.getUid());
        entityInstance.setOrganisationUnit(orgUnit);
        entityInstance.setTrackedEntityType(entityType);
    }
    Set<TrackedEntityAttributeValue> attributeValues = getSMSAttributeValues(subm, entityInstance);
    if (teiExists) {
        updateAttributeValues(attributeValues, entityInstance.getTrackedEntityAttributeValues());
        entityInstance.setTrackedEntityAttributeValues(attributeValues);
        teiService.updateTrackedEntityInstance(entityInstance);
    } else {
        teiService.createTrackedEntityInstance(entityInstance, attributeValues);
    }
    TrackedEntityInstance tei = teiService.getTrackedEntityInstance(teiUid.getUid());
    // TODO: Unsure about this handling for enrollments, this needs to be
    // checked closely
    ProgramInstance enrollment;
    boolean enrollmentExists = programInstanceService.programInstanceExists(enrollmentid.getUid());
    if (enrollmentExists) {
        enrollment = programInstanceService.getProgramInstance(enrollmentid.getUid());
        // Update these dates in case they've changed
        enrollment.setEnrollmentDate(enrollmentDate);
        enrollment.setIncidentDate(incidentDate);
    } else {
        enrollment = programInstanceService.enrollTrackedEntityInstance(tei, program, enrollmentDate, incidentDate, orgUnit, enrollmentid.getUid());
    }
    if (enrollment == null) {
        throw new SMSProcessingException(SmsResponse.ENROLL_FAILED.set(teiUid, progid));
    }
    enrollment.setStatus(getCoreProgramStatus(subm.getEnrollmentStatus()));
    enrollment.setGeometry(convertGeoPointToGeometry(subm.getCoordinates()));
    programInstanceService.updateProgramInstance(enrollment);
    // We now check if the enrollment has events to process
    User user = userService.getUser(subm.getUserId().getUid());
    List<Object> errorUIDs = new ArrayList<>();
    if (subm.getEvents() != null) {
        for (SmsEvent event : subm.getEvents()) {
            errorUIDs.addAll(processEvent(event, user, enrollment, sms));
        }
    }
    enrollment.setStatus(getCoreProgramStatus(subm.getEnrollmentStatus()));
    enrollment.setGeometry(convertGeoPointToGeometry(subm.getCoordinates()));
    programInstanceService.updateProgramInstance(enrollment);
    if (!errorUIDs.isEmpty()) {
        return SmsResponse.WARN_DVERR.setList(errorUIDs);
    }
    if (attributeValues == null || attributeValues.isEmpty()) {
        // TODO: Is this correct handling?
        return SmsResponse.WARN_AVEMPTY;
    }
    return SmsResponse.SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) User(org.hisp.dhis.user.User) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ArrayList(java.util.ArrayList) SmsEvent(org.hisp.dhis.smscompression.models.SmsEvent) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) Uid(org.hisp.dhis.smscompression.models.Uid) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) EnrollmentSmsSubmission(org.hisp.dhis.smscompression.models.EnrollmentSmsSubmission)

Example 2 with TrackedEntityType

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

the class RelationshipTypeObjectBundleHook method handleRelationshipConstraintReferences.

/**
 * Handles the references for RelationshipConstraint, persisting any bject
 * that might end up in a transient state.
 */
private void handleRelationshipConstraintReferences(RelationshipConstraint relationshipConstraint) {
    TrackedEntityType trackedEntityType = relationshipConstraint.getTrackedEntityType();
    Program program = relationshipConstraint.getProgram();
    ProgramStage programStage = relationshipConstraint.getProgramStage();
    if (trackedEntityType != null) {
        trackedEntityType = trackedEntityTypeService.getTrackedEntityType(trackedEntityType.getUid());
        relationshipConstraint.setTrackedEntityType(trackedEntityType);
    }
    if (program != null) {
        program = programService.getProgram(program.getUid());
        relationshipConstraint.setProgram(program);
    }
    if (programStage != null) {
        programStage = programStageService.getProgramStage(programStage.getUid());
        relationshipConstraint.setProgramStage(programStage);
    }
    sessionFactory.getCurrentSession().save(relationshipConstraint);
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Program(org.hisp.dhis.program.Program) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 3 with TrackedEntityType

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

the class DeduplicationServiceMergeIntegrationTest method shouldManualMergeWithAuthorityAll.

@Test
void shouldManualMergeWithAuthorityAll() throws PotentialDuplicateConflictException, PotentialDuplicateForbiddenException {
    OrganisationUnit ou = createOrganisationUnit("OU_A");
    organisationUnitService.addOrganisationUnit(ou);
    creteUser(new HashSet<>(Collections.singletonList(ou)), UserAuthorityGroup.AUTHORITY_ALL);
    TrackedEntityType trackedEntityType = createTrackedEntityType('A');
    trackedEntityTypeService.addTrackedEntityType(trackedEntityType);
    TrackedEntityInstance original = createTrackedEntityInstance(ou);
    TrackedEntityInstance duplicate = createTrackedEntityInstance(ou);
    original.setTrackedEntityType(trackedEntityType);
    duplicate.setTrackedEntityType(trackedEntityType);
    trackedEntityInstanceService.addTrackedEntityInstance(original);
    trackedEntityInstanceService.addTrackedEntityInstance(duplicate);
    Program program = createProgram('A');
    Program program1 = createProgram('B');
    programService.addProgram(program);
    programService.addProgram(program1);
    ProgramInstance programInstance1 = createProgramInstance(program, original, ou);
    ProgramInstance programInstance2 = createProgramInstance(program1, duplicate, ou);
    programInstanceService.addProgramInstance(programInstance1);
    programInstanceService.addProgramInstance(programInstance2);
    original.getProgramInstances().add(programInstance1);
    duplicate.getProgramInstances().add(programInstance2);
    trackedEntityInstanceService.updateTrackedEntityInstance(original);
    trackedEntityInstanceService.updateTrackedEntityInstance(duplicate);
    PotentialDuplicate potentialDuplicate = new PotentialDuplicate(original.getUid(), duplicate.getUid());
    deduplicationService.addPotentialDuplicate(potentialDuplicate);
    DeduplicationMergeParams deduplicationMergeParams = DeduplicationMergeParams.builder().potentialDuplicate(potentialDuplicate).original(original).duplicate(duplicate).build();
    Date lastUpdatedOriginal = trackedEntityInstanceService.getTrackedEntityInstance(original.getUid()).getLastUpdated();
    deduplicationService.autoMerge(deduplicationMergeParams);
    assertEquals(deduplicationService.getPotentialDuplicateByUid(potentialDuplicate.getUid()).getStatus(), DeduplicationStatus.MERGED);
    assertTrue(trackedEntityInstanceService.getTrackedEntityInstance(original.getUid()).getLastUpdated().getTime() > lastUpdatedOriginal.getTime());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 4 with TrackedEntityType

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

the class TrackedEntityInstanceQueryTest method testValidateNoOrgUnitsModeAll.

@Test
void testValidateNoOrgUnitsModeAll() {
    TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
    TrackedEntityType trackedEntityTypeA = createTrackedEntityType('A');
    params.setTrackedEntityType(trackedEntityTypeA);
    params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
    instanceService.validate(params);
}
Also used : TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 5 with TrackedEntityType

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

the class EventAnalyticsServiceTest method stubAnalyticsEventsData.

private void stubAnalyticsEventsData() {
    Period peJan = createPeriod("2017-01");
    Period peFeb = createPeriod("2017-02");
    Period peMar = createPeriod("2017-03");
    Period peApril = createPeriod("2017-04");
    periodService.addPeriod(peJan);
    periodService.addPeriod(peFeb);
    periodService.addPeriod(peMar);
    periodService.addPeriod(peApril);
    DataElement deA = createDataElement('A');
    DataElement deB = createDataElement('B');
    DataElement deC = createDataElement('C');
    DataElement deD = createDataElement('D');
    dataElementService.addDataElement(deA);
    dataElementService.addDataElement(deB);
    dataElementService.addDataElement(deC);
    dataElementService.addDataElement(deD);
    programA = createProgram('A', null, null, Sets.newHashSet(ouA, ouB), null);
    programA.setUid("programA123");
    idObjectManager.save(programA);
    ProgramStage psA = createProgramStage('A', 0);
    psA.setUid("programStgA");
    psA.addDataElement(deA, 0);
    psA.addDataElement(deB, 1);
    idObjectManager.save(psA);
    ProgramStage psB = createProgramStage('B', 0);
    psB.setUid("programStgB");
    psB.addDataElement(deA, 0);
    psB.addDataElement(deB, 1);
    idObjectManager.save(psB);
    ProgramStage psC = createProgramStage('C', 0);
    psC.setUid("programStgC");
    psC.addDataElement(deA, 0);
    psC.addDataElement(deB, 1);
    idObjectManager.save(psC);
    programA.getProgramStages().add(psA);
    TrackedEntityType trackedEntityType = createTrackedEntityType('A');
    idObjectManager.save(trackedEntityType);
    org.hisp.dhis.trackedentity.TrackedEntityInstance maleA = createTrackedEntityInstance(ouA);
    maleA.setUid("person1234A");
    org.hisp.dhis.trackedentity.TrackedEntityInstance femaleB = createTrackedEntityInstance(ouB);
    femaleB.setUid("person1234B");
    maleA.setTrackedEntityType(trackedEntityType);
    femaleB.setTrackedEntityType(trackedEntityType);
    idObjectManager.save(maleA);
    idObjectManager.save(femaleB);
    programInstanceService.enrollTrackedEntityInstance(maleA, programA, null, null, ouA);
    programInstanceService.enrollTrackedEntityInstance(femaleB, programA, null, null, ouA);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Period(org.hisp.dhis.period.Period) ProgramStage(org.hisp.dhis.program.ProgramStage)

Aggregations

TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)57 Test (org.junit.jupiter.api.Test)27 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)22 Program (org.hisp.dhis.program.Program)19 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)16 User (org.hisp.dhis.user.User)16 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)15 TrackedEntity (org.hisp.dhis.tracker.domain.TrackedEntity)15 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)12 ProgramInstance (org.hisp.dhis.program.ProgramInstance)11 Date (java.util.Date)10 HashSet (java.util.HashSet)10 ProgramStage (org.hisp.dhis.program.ProgramStage)8 CategoryCombo (org.hisp.dhis.category.CategoryCombo)6 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)6 RelationshipType (org.hisp.dhis.relationship.RelationshipType)6 TrackedEntityTypeAttribute (org.hisp.dhis.trackedentity.TrackedEntityTypeAttribute)6 Collectors (java.util.stream.Collectors)5 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)5 CategoryOption (org.hisp.dhis.category.CategoryOption)5