Search in sources :

Example 76 with Program

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

Example 77 with Program

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()));
    }
}
Also used : Program(org.hisp.dhis.program.Program) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 78 with Program

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());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Program(org.hisp.dhis.program.Program) List(java.util.List) ProgramStage(org.hisp.dhis.program.ProgramStage) Test(org.junit.jupiter.api.Test)

Example 79 with Program

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));
    }
}
Also used : Authorities(org.hisp.dhis.security.Authorities) FIRST_PAGE(org.hisp.dhis.common.SlimPager.FIRST_PAGE) EventService(org.hisp.dhis.dxf2.events.event.EventService) Date(java.util.Date) Restrictions(org.hisp.dhis.query.Restrictions) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) NoteHelper(org.hisp.dhis.dxf2.events.NoteHelper) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) SetValuedMap(org.apache.commons.collections4.SetValuedMap) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) Transactional(javax.transaction.Transactional) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) QueryService(org.hisp.dhis.query.QueryService) Event(org.hisp.dhis.dxf2.events.event.Event) Objects(java.util.Objects) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) TEA_VALUE_MAX_LENGTH(org.hisp.dhis.trackedentity.TrackedEntityAttributeService.TEA_VALUE_MAX_LENGTH) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) TrackedEntityCommentService(org.hisp.dhis.trackedentitycomment.TrackedEntityCommentService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) Constants(org.hisp.dhis.dxf2.Constants) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) GeoUtils(org.hisp.dhis.system.util.GeoUtils) ObjectUtils.defaultIfNull(org.apache.commons.lang3.ObjectUtils.defaultIfNull) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Notifier(org.hisp.dhis.system.notification.Notifier) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) SlimPager(org.hisp.dhis.common.SlimPager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) User(org.hisp.dhis.user.User) IdentifiableObjectCallable(org.hisp.dhis.system.callable.IdentifiableObjectCallable) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) I18nManager(org.hisp.dhis.i18n.I18nManager) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) IdSchemes(org.hisp.dhis.common.IdSchemes) Pager(org.hisp.dhis.common.Pager) RelationshipService(org.hisp.dhis.dxf2.events.relationship.RelationshipService) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DEFAULT_PAGE_SIZE(org.hisp.dhis.common.Pager.DEFAULT_PAGE_SIZE) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Maps(com.google.common.collect.Maps) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) Note(org.hisp.dhis.dxf2.events.event.Note) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStatus(org.hisp.dhis.program.ProgramStatus) Collectors.toList(java.util.stream.Collectors.toList) CollectionUtils(org.hisp.dhis.commons.collection.CollectionUtils) FeatureType(org.hisp.dhis.organisationunit.FeatureType) NotificationLevel(org.hisp.dhis.system.notification.NotificationLevel) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ERROR(org.hisp.dhis.system.notification.NotificationLevel.ERROR) CodeGenerator(org.hisp.dhis.common.CodeGenerator) ProgramService(org.hisp.dhis.program.ProgramService) Collections(java.util.Collections) DateUtils(org.hisp.dhis.util.DateUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) Query(org.hisp.dhis.query.Query) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) HashSet(java.util.HashSet)

Example 80 with Program

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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) Event(org.hisp.dhis.dxf2.events.event.Event) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) Date(java.util.Date)

Aggregations

Program (org.hisp.dhis.program.Program)344 Test (org.junit.jupiter.api.Test)160 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)123 ProgramStage (org.hisp.dhis.program.ProgramStage)109 ProgramInstance (org.hisp.dhis.program.ProgramInstance)79 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)68 Date (java.util.Date)47 Collectors (java.util.stream.Collectors)44 User (org.hisp.dhis.user.User)44 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)42 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)42 BeforeEach (org.junit.jupiter.api.BeforeEach)42 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)41 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)41 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)40 DhisConvenienceTest.createProgram (org.hisp.dhis.DhisConvenienceTest.createProgram)39 CategoryCombo (org.hisp.dhis.category.CategoryCombo)39 DataElement (org.hisp.dhis.dataelement.DataElement)39 Event (org.hisp.dhis.tracker.domain.Event)38 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)37