use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class ProgramInstanceRepeatableStageCheck method check.
@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
IdScheme scheme = ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme();
ProgramStage programStage = ctx.getProgramStage(scheme, event.getProgramStage());
ProgramInstance programInstance = ctx.getProgramInstanceMap().get(event.getUid());
Program program = ctx.getProgramsMap().get(event.getProgram());
TrackedEntityInstance tei = null;
if (program.isRegistration()) {
tei = ctx.getTrackedEntityInstanceMap().get(event.getUid()).getLeft();
}
// @formatter:off
if (programInstance != null && tei != null && program.isRegistration() && !programStage.getRepeatable() && hasProgramStageInstance(ctx.getServiceDelegator().getJdbcTemplate(), programInstance.getId(), programStage.getId(), tei.getId())) {
return new ImportSummary(ImportStatus.ERROR, "Program stage is not repeatable and an event already exists").setReference(event.getEvent()).incrementIgnored();
}
return success();
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class ProgramStagePreProcessor method process.
@Override
public void process(Event event, WorkContext ctx) {
Program program = ctx.getProgramsMap().get(event.getProgram());
if (program == null) {
// Program is a mandatory value, it will be caught by the
return;
// validation
}
ProgramStage programStage = ctx.getProgramStage(ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme(), event.getProgramStage());
if (programStage == null && program.isWithoutRegistration()) {
programStage = program.getProgramStageByStage(1);
}
if (programStage != null) {
event.setProgramStage(IdentifiableObjectUtils.getIdentifierBasedOnIdScheme(programStage, ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme()));
}
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class AuditIntegrationTest method testSaveProgram.
@Test
@SuppressWarnings("unchecked")
void testSaveProgram() throws IOException {
Program program = createProgram('A');
manager.save(program);
DataElement dataElement = createDataElement('A');
manager.save(dataElement);
ProgramStage programStage = createProgramStage('A', program);
programStage.addDataElement(dataElement, 0);
manager.save(programStage);
AuditQuery query = AuditQuery.builder().uid(Sets.newHashSet(programStage.getUid())).build();
await().atMost(TIMEOUT, TimeUnit.SECONDS).until(() -> auditService.countAudits(query) >= 0);
List<Audit> audits = auditService.getAudits(query);
assertEquals(1, audits.size());
Audit audit = audits.get(0);
assertEquals(ProgramStage.class.getName(), audit.getKlass());
assertEquals(programStage.getUid(), audit.getUid());
Map<String, Object> deserializeProgramStage = objectMapper.readValue(audit.getData(), Map.class);
assertNotNull(deserializeProgramStage.get("programStageDataElements"));
List<String> uids = (List<String>) deserializeProgramStage.get("programStageDataElements");
assertEquals(1, uids.size());
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class AbstractEnrollmentService method prepareCaches.
private void prepareCaches(List<Enrollment> enrollments, User user) {
Collection<String> orgUnits = enrollments.stream().map(Enrollment::getOrgUnit).collect(Collectors.toSet());
if (!orgUnits.isEmpty()) {
Query query = Query.from(schemaService.getDynamicSchema(OrganisationUnit.class));
query.setUser(user);
query.add(Restrictions.in("id", orgUnits));
queryService.query(query).forEach(ou -> organisationUnitCache.put(ou.getUid(), (OrganisationUnit) ou));
}
Collection<String> programs = enrollments.stream().map(Enrollment::getProgram).collect(Collectors.toSet());
if (!programs.isEmpty()) {
Query query = Query.from(schemaService.getDynamicSchema(Program.class));
query.setUser(user);
query.add(Restrictions.in("id", programs));
queryService.query(query).forEach(pr -> programCache.put(pr.getUid(), (Program) pr));
}
Collection<String> trackedEntityAttributes = new HashSet<>();
enrollments.forEach(e -> e.getAttributes().forEach(at -> trackedEntityAttributes.add(at.getAttribute())));
if (!trackedEntityAttributes.isEmpty()) {
Query query = Query.from(schemaService.getDynamicSchema(TrackedEntityAttribute.class));
query.setUser(user);
query.add(Restrictions.in("id", trackedEntityAttributes));
queryService.query(query).forEach(tea -> trackedEntityAttributeCache.put(tea.getUid(), (TrackedEntityAttribute) tea));
}
Collection<String> trackedEntityInstances = enrollments.stream().map(Enrollment::getTrackedEntityInstance).collect(toList());
if (!trackedEntityInstances.isEmpty()) {
Query query = Query.from(schemaService.getDynamicSchema(org.hisp.dhis.trackedentity.TrackedEntityInstance.class));
query.setUser(user);
query.add(Restrictions.in("id", trackedEntityInstances));
queryService.query(query).forEach(te -> trackedEntityInstanceCache.put(te.getUid(), (org.hisp.dhis.trackedentity.TrackedEntityInstance) te));
}
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class AbstractEnrollmentService method updateEnrollment.
private ImportSummary updateEnrollment(Enrollment enrollment, ImportOptions importOptions, boolean handleEvents) {
importOptions = updateImportOptions(importOptions);
if (enrollment == null || StringUtils.isEmpty(enrollment.getEnrollment())) {
return new ImportSummary(ImportStatus.ERROR, "No enrollment or enrollment ID was supplied").incrementIgnored();
}
ProgramInstance programInstance = programInstanceService.getProgramInstance(enrollment.getEnrollment());
List<String> errors = trackerAccessManager.canUpdate(importOptions.getUser(), programInstance, false);
if (programInstance == null) {
return new ImportSummary(ImportStatus.ERROR, "ID " + enrollment.getEnrollment() + " doesn't point to a valid enrollment.").incrementIgnored();
}
if (!errors.isEmpty()) {
return new ImportSummary(ImportStatus.ERROR, errors.toString()).incrementIgnored();
}
ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
checkAttributes(programInstance.getEntityInstance(), enrollment, importOptions, importSummary);
if (importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR).incrementIgnored();
return importSummary;
}
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
if (!program.isRegistration()) {
String descMsg = "Provided program " + program.getUid() + " is a program without registration. An enrollment cannot be created into program without registration.";
return new ImportSummary(ImportStatus.ERROR, descMsg).incrementIgnored();
}
programInstance.setProgram(program);
if (enrollment.getIncidentDate() != null) {
programInstance.setIncidentDate(enrollment.getIncidentDate());
}
if (enrollment.getEnrollmentDate() != null) {
programInstance.setEnrollmentDate(enrollment.getEnrollmentDate());
}
if (enrollment.getOrgUnit() != null) {
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
programInstance.setOrganisationUnit(organisationUnit);
}
programInstance.setFollowup(enrollment.getFollowup());
if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
return new ImportSummary(ImportStatus.ERROR, "DisplayIncidentDate is true but IncidentDate is null").incrementIgnored();
}
updateFeatureType(program, enrollment, programInstance);
if (EnrollmentStatus.fromProgramStatus(programInstance.getStatus()) != enrollment.getStatus()) {
Date endDate = enrollment.getCompletedDate();
String user = enrollment.getCompletedBy();
if (enrollment.getCompletedDate() == null) {
endDate = new Date();
}
if (user == null) {
user = importOptions.getUser().getUsername();
}
if (EnrollmentStatus.CANCELLED == enrollment.getStatus()) {
programInstance.setEndDate(endDate);
programInstanceService.cancelProgramInstanceStatus(programInstance);
} else if (EnrollmentStatus.COMPLETED == enrollment.getStatus()) {
programInstance.setEndDate(endDate);
programInstance.setCompletedBy(user);
programInstanceService.completeProgramInstanceStatus(programInstance);
} else if (EnrollmentStatus.ACTIVE == enrollment.getStatus()) {
programInstanceService.incompleteProgramInstanceStatus(programInstance);
}
}
validateProgramInstance(program, programInstance, enrollment, importSummary);
if (importSummary.getStatus() != ImportStatus.SUCCESS) {
return importSummary;
}
updateAttributeValues(enrollment, importOptions);
updateDateFields(enrollment, programInstance);
programInstanceService.updateProgramInstance(programInstance, importOptions.getUser());
teiService.updateTrackedEntityInstance(programInstance.getEntityInstance(), importOptions.getUser());
saveTrackedEntityComment(programInstance, enrollment, importOptions.getUser());
importSummary = new ImportSummary(enrollment.getEnrollment()).incrementUpdated();
importSummary.setReference(enrollment.getEnrollment());
if (handleEvents) {
importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
} else {
for (Event event : enrollment.getEvents()) {
event.setEnrollment(enrollment.getEnrollment());
event.setProgram(programInstance.getProgram().getUid());
event.setTrackedEntityInstance(enrollment.getTrackedEntityInstance());
}
}
return importSummary;
}
Aggregations