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