Search in sources :

Example 1 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class DefaultDimensionService method getProgramDataElementDimensionItem.

/**
     * Returns a {@link ProgramDataElementDimensionItem}.
     *
     * @param idScheme      the identifier scheme.
     * @param programId     the program identifier.
     * @param dataElementId the data element identifier.
     */
private ProgramDataElementDimensionItem getProgramDataElementDimensionItem(IdScheme idScheme, String programId, String dataElementId) {
    Program program = idObjectManager.getObject(Program.class, idScheme, programId);
    DataElement dataElement = idObjectManager.getObject(DataElement.class, idScheme, dataElementId);
    if (program == null || dataElement == null) {
        return null;
    }
    return new ProgramDataElementDimensionItem(program, dataElement);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Program(org.hisp.dhis.program.Program) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem)

Example 2 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method savePatient.

@Override
public Patient savePatient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId, String programIdText) throws NotAllowedException {
    TrackedEntityInstance patientWeb = new TrackedEntityInstance();
    patientWeb.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
    Set<TrackedEntityAttribute> patientAttributeSet = new HashSet<>();
    Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
    Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
    if (attributesMobile != null) {
        for (org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : attributesMobile) {
            TrackedEntityAttribute patientAttribute = attributeService.getTrackedEntityAttributeByName(paAtt.getName());
            patientAttributeSet.add(patientAttribute);
            TrackedEntityAttributeValue patientAttributeValue = new TrackedEntityAttributeValue();
            patientAttributeValue.setEntityInstance(patientWeb);
            patientAttributeValue.setAttribute(patientAttribute);
            patientAttributeValue.setValue(paAtt.getValue());
            patientAttributeValues.add(patientAttributeValue);
        }
    }
    patientWeb.setTrackedEntity(trackedEntityService.getTrackedEntityByName(patient.getTrackedEntityName()));
    patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, null, null, patientAttributeValues);
    TrackedEntityInstance newTrackedEntityInstance = entityInstanceService.getTrackedEntityInstance(this.patientId);
    String errorMsg = null;
    try {
        for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance mobileProgramInstance : patient.getEnrollmentPrograms()) {
            Date incidentDate = DateUtils.getMediumDate(mobileProgramInstance.getDateOfIncident());
            enrollProgram(patientId + "-" + mobileProgramInstance.getProgramId(), mobileProgramInstance.getProgramStageInstances(), incidentDate);
        }
        Program program = programService.getProgram(Integer.parseInt(programIdText));
        String[] errorCode = entityInstanceService.validateTrackedEntityInstance(newTrackedEntityInstance, program).split("_");
        int code = Integer.parseInt(errorCode[0]);
        if (code >= 1) {
            entityInstanceService.deleteTrackedEntityInstance(newTrackedEntityInstance);
            if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
                errorMsg = "Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName();
            } else {
                errorMsg = "Validation error";
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (errorMsg != null) {
        throw new NotAllowedException(errorMsg);
    }
    return getPatientModel(newTrackedEntityInstance);
}
Also used : Program(org.hisp.dhis.program.Program) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) HashSet(java.util.HashSet)

Example 3 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method generateRepeatableEvent.

@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient generateRepeatableEvent(int orgUnitId, String eventInfo) throws NotAllowedException {
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
    String[] keys = eventInfo.split("_");
    ProgramStage programStage = programStageService.getProgramStage(Integer.parseInt(keys[4]));
    int mobileProgramStageId = Integer.parseInt(keys[3]);
    String nextDueDate = keys[2];
    Program program = programService.getProgram(Integer.parseInt(keys[1]));
    TrackedEntityInstance trackedEntityInstance = entityInstanceService.getTrackedEntityInstance(Integer.parseInt(keys[0]));
    ProgramInstance programInstance = null;
    ProgramStageInstance newProgramStageInstance = null;
    if (mobileProgramStageId != 0) {
        ProgramStageInstance oldProgramStageIntance = programStageInstanceService.getProgramStageInstance(mobileProgramStageId);
        programInstance = oldProgramStageIntance.getProgramInstance();
        newProgramStageInstance = new ProgramStageInstance(programInstance, oldProgramStageIntance.getProgramStage());
        newProgramStageInstance.setDueDate(DateUtils.getMediumDate(nextDueDate));
    } else {
        programInstance = programInstanceService.getProgramInstances(new ProgramInstanceQueryParams().setTrackedEntityInstance(trackedEntityInstance).setProgram(program).setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL)).iterator().next();
        newProgramStageInstance = new ProgramStageInstance();
        newProgramStageInstance.setProgramInstance(programInstance);
        newProgramStageInstance.setProgramStage(programStage);
        newProgramStageInstance.setDueDate(DateUtils.getMediumDate(nextDueDate));
        newProgramStageInstance.setExecutionDate(DateUtils.getMediumDate(nextDueDate));
    }
    newProgramStageInstance.setOrganisationUnit(orgUnit);
    programInstance.getProgramStageInstances().add(newProgramStageInstance);
    List<ProgramStageInstance> proStageInstanceList = new ArrayList<>(programInstance.getProgramStageInstances());
    Collections.sort(proStageInstanceList, new ProgramStageInstanceVisitDateComparator());
    programInstance.getProgramStageInstances().removeAll(proStageInstanceList);
    programInstance.getProgramStageInstances().addAll(proStageInstanceList);
    programStageInstanceService.addProgramStageInstance(newProgramStageInstance);
    programInstanceService.updateProgramInstance(programInstance);
    TrackedEntityInstance tei = entityInstanceService.getTrackedEntityInstance(programInstance.getEntityInstance().getId());
    org.hisp.dhis.api.mobile.model.LWUITmodel.Patient mobilePatient = getPatientModel(tei);
    return mobilePatient;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Patient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient) ArrayList(java.util.ArrayList) ProgramStageInstanceVisitDateComparator(org.hisp.dhis.program.comparator.ProgramStageInstanceVisitDateComparator) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams)

Example 4 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class NamebasedUtilsImpl method getProgramStage.

@Override
public ProgramStage getProgramStage(int programId, int programStageId) {
    Program program = programService.getProgram(programId);
    Collection<ProgramStage> stages = program.getProgramStages();
    for (ProgramStage programStage : stages) {
        if (programStage.getId() == programStageId) {
            return programStage;
        }
    }
    return null;
}
Also used : Program(org.hisp.dhis.program.Program) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 5 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class AbstractEventService method addEvent.

protected ImportSummary addEvent(Event event, User user, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    Program program = getProgram(importOptions.getIdSchemes().getProgramIdScheme(), event.getProgram());
    ProgramStage programStage = getProgramStage(importOptions.getIdSchemes().getProgramStageIdScheme(), event.getProgramStage());
    ProgramInstance programInstance;
    ProgramStageInstance programStageInstance = null;
    if (program == null) {
        return new ImportSummary(ImportStatus.ERROR, "Event.program does not point to a valid program: " + event.getProgram()).incrementIgnored();
    }
    if (programStage == null && program.isRegistration()) {
        return new ImportSummary(ImportStatus.ERROR, "Event.programStage does not point to a valid programStage, and program is multi stage: " + event.getProgramStage()).incrementIgnored();
    } else if (programStage == null) {
        programStage = program.getProgramStageByStage(1);
    }
    Assert.notNull(programStage, "Program stage cannot be null");
    if (!canAccess(program, user)) {
        return new ImportSummary(ImportStatus.ERROR, "Current user does not have permission to access this program").incrementIgnored();
    }
    if (program.isRegistration()) {
        if (event.getTrackedEntityInstance() == null) {
            return new ImportSummary(ImportStatus.ERROR, "No Event.trackedEntityInstance was provided for registration based program").incrementIgnored();
        }
        org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = entityInstanceService.getTrackedEntityInstance(event.getTrackedEntityInstance());
        if (entityInstance == null) {
            return new ImportSummary(ImportStatus.ERROR, "Event.trackedEntityInstance does not point to a valid tracked entity instance: " + event.getTrackedEntityInstance()).incrementIgnored();
        }
        List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(entityInstance, program, ProgramStatus.ACTIVE));
        if (programInstances.isEmpty()) {
            return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance: " + entityInstance.getUid() + " is not enrolled in program: " + program.getUid()).incrementIgnored();
        } else if (programInstances.size() > 1) {
            return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance: " + entityInstance.getUid() + " have multiple active enrollments in program: " + program.getUid()).incrementIgnored();
        }
        programInstance = programInstances.get(0);
        if (!programStage.getRepeatable()) {
            programStageInstance = programStageInstanceService.getProgramStageInstance(programInstance, programStage);
            if (programStageInstance != null && !programStageInstance.getUid().equals(event.getEvent())) {
                return new ImportSummary(ImportStatus.ERROR, "Program stage is not repeatable and an event already exists").incrementIgnored();
            }
        } else {
            if (event.getEvent() != null) {
                programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
                if (programStageInstance == null) {
                    if (!CodeGenerator.isValidUid(event.getEvent())) {
                        return new ImportSummary(ImportStatus.ERROR, "Event.event did not point to a valid event: " + event.getEvent()).incrementIgnored();
                    }
                }
            }
        }
    } else {
        List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(program, ProgramStatus.ACTIVE));
        if (programInstances.isEmpty()) {
            // Create PI if it doesn't exist (should only be one)
            ProgramInstance pi = new ProgramInstance();
            pi.setEnrollmentDate(new Date());
            pi.setIncidentDate(new Date());
            pi.setProgram(program);
            pi.setStatus(ProgramStatus.ACTIVE);
            programInstanceService.addProgramInstance(pi);
            programInstances.add(pi);
        } else if (programInstances.size() > 1) {
            return new ImportSummary(ImportStatus.ERROR, "Multiple active program instances exists for program: " + program.getUid()).incrementIgnored();
        }
        programInstance = programInstances.get(0);
        if (event.getEvent() != null) {
            programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
            if (programStageInstance == null) {
                if (importOptions.getIdSchemes().getProgramStageInstanceIdScheme().equals(IdScheme.UID) && !CodeGenerator.isValidUid(event.getEvent())) {
                    return new ImportSummary(ImportStatus.ERROR, "Event.event did not point to a valid event: " + event.getEvent()).incrementIgnored();
                }
            }
        }
    }
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
    program = programInstance.getProgram();
    if (programStageInstance != null) {
        programStage = programStageInstance.getProgramStage();
    }
    if (organisationUnit == null) {
        return new ImportSummary(ImportStatus.ERROR, "Event.orgUnit does not point to a valid organisation unit: " + event.getOrgUnit()).incrementIgnored();
    }
    if (!programInstance.getProgram().hasOrganisationUnit(organisationUnit)) {
        return new ImportSummary(ImportStatus.ERROR, "Program is not assigned to this organisation unit: " + event.getOrgUnit()).incrementIgnored();
    }
    validateExpiryDays(event, program, null);
    return saveEvent(program, programInstance, programStage, programStageInstance, organisationUnit, event, user, importOptions);
}
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) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ArrayList(java.util.ArrayList) Date(java.util.Date) ProgramStage(org.hisp.dhis.program.ProgramStage) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

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