Search in sources :

Example 1 with ProgramInstance

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

the class ActivityReportingServiceImpl method saveProgramStage.

@Override
public String saveProgramStage(org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStage mobileProgramStage, int patientId, int orgUnitId) throws NotAllowedException {
    if (mobileProgramStage.isSingleEvent()) {
        TrackedEntityInstance patient = entityInstanceService.getTrackedEntityInstance(patientId);
        ProgramStageInstance prStageInstance = programStageInstanceService.getProgramStageInstance(mobileProgramStage.getId());
        ProgramStage programStage = programStageService.getProgramStage(prStageInstance.getProgramStage().getId());
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
        // ---------------------------------------------------------------------
        // Add a new program-instance
        //
        // ---------------------------------------------------------------------
        ProgramInstance programInstance = new ProgramInstance();
        programInstance.setEnrollmentDate(new Date());
        programInstance.setIncidentDate(new Date());
        programInstance.setProgram(programStage.getProgram());
        programInstance.setStatus(ProgramStatus.COMPLETED);
        programInstance.setEntityInstance(patient);
        programInstanceService.addProgramInstance(programInstance);
        // ---------------------------------------------------------------------
        // Add a new program-stage-instance
        //
        // ---------------------------------------------------------------------
        ProgramStageInstance programStageInstance = new ProgramStageInstance();
        programStageInstance.setProgramInstance(programInstance);
        programStageInstance.setProgramStage(programStage);
        programStageInstance.setDueDate(new Date());
        programStageInstance.setExecutionDate(new Date());
        programStageInstance.setOrganisationUnit(organisationUnit);
        programStageInstance.setStatus(EventStatus.COMPLETED);
        programStageInstanceService.addProgramStageInstance(programStageInstance);
        // ---------------------------------------------------------------------
        // Save value
        //
        // ---------------------------------------------------------------------
        List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement> dataElements = mobileProgramStage.getDataElements();
        for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement dataElement1 : dataElements) {
            DataElement dataElement = dataElementService.getDataElement(dataElement1.getId());
            String value = dataElement1.getValue();
            if (ValueType.DATE == dataElement.getValueType() && !value.trim().equals("")) {
                value = PeriodUtil.convertDateFormat(value);
            }
            TrackedEntityDataValue patientDataValue = new TrackedEntityDataValue();
            patientDataValue.setDataElement(dataElement);
            patientDataValue.setValue(value);
            patientDataValue.setProgramStageInstance(programStageInstance);
            patientDataValue.setLastUpdated(new Date());
            dataValueService.saveTrackedEntityDataValue(patientDataValue);
        }
        return SINGLE_EVENT_UPLOADED;
    } else {
        ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(mobileProgramStage.getId());
        List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement> dataElements = mobileProgramStage.getDataElements();
        try {
            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
            programStageInstance.setOrganisationUnit(organisationUnit);
        } catch (Exception e) {
            programStageInstance.setOrganisationUnit(null);
        }
        for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement dataElement1 : dataElements) {
            DataElement dataElement = dataElementService.getDataElement(dataElement1.getId());
            String value = dataElement1.getValue();
            if (value != null) {
                if (ValueType.DATE == dataElement.getValueType() && !value.trim().equals("")) {
                    value = PeriodUtil.convertDateFormat(value);
                }
                TrackedEntityDataValue previousPatientDataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
                if (previousPatientDataValue == null) {
                    TrackedEntityDataValue patientDataValue = new TrackedEntityDataValue(programStageInstance, dataElement, value);
                    dataValueService.saveTrackedEntityDataValue(patientDataValue);
                } else {
                    previousPatientDataValue.setValue(value);
                    previousPatientDataValue.setLastUpdated(new Date());
                    previousPatientDataValue.setProvidedElsewhere(false);
                    dataValueService.updateTrackedEntityDataValue(previousPatientDataValue);
                }
            }
        }
        if (DateUtils.getMediumDate(mobileProgramStage.getReportDate()) != null) {
            programStageInstance.setExecutionDate(DateUtils.getMediumDate(mobileProgramStage.getReportDate()));
        } else {
            programStageInstance.setExecutionDate(new Date());
        }
        if (programStageInstance.getProgramStage().getProgramStageDataElements().size() > dataElements.size()) {
            programStageInstanceService.updateProgramStageInstance(programStageInstance);
            return PROGRAM_STAGE_SECTION_UPLOADED;
        } else {
            if (mobileProgramStage.isCompleted()) {
                programStageInstance.setStatus(EventStatus.COMPLETED);
            }
            programStageInstanceService.updateProgramStageInstance(programStageInstance);
            // check if all belonged program stage are completed
            if (!mobileProgramStage.isRepeatable() && isAllProgramStageFinished(programStageInstance) == true) {
                ProgramInstance programInstance = programStageInstance.getProgramInstance();
                programInstance.setStatus(ProgramStatus.COMPLETED);
                programInstanceService.updateProgramInstance(programInstance);
            }
            if (mobileProgramStage.isRepeatable()) {
                Date nextDate = DateUtils.getDateAfterAddition(new Date(), mobileProgramStage.getStandardInterval());
                return PROGRAM_STAGE_UPLOADED + "$" + DateUtils.getMediumDateString(nextDate);
            } else {
                return PROGRAM_STAGE_UPLOADED;
            }
        }
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 2 with ProgramInstance

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

the class ActivityReportingServiceImpl method isAllProgramStageFinished.

private boolean isAllProgramStageFinished(ProgramStageInstance programStageInstance) {
    ProgramInstance programInstance = programStageInstance.getProgramInstance();
    Collection<ProgramStageInstance> programStageInstances = programInstance.getProgramStageInstances();
    if (programStageInstances != null) {
        if (programStageInstances.size() < programInstance.getProgram().getProgramStages().size()) {
            return false;
        }
        Iterator<ProgramStageInstance> iterator = programStageInstances.iterator();
        while (iterator.hasNext()) {
            ProgramStageInstance each = iterator.next();
            if (!each.isCompleted()) {
                return false;
            }
        }
    }
    return true;
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 3 with ProgramInstance

use of org.hisp.dhis.program.ProgramInstance 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 ProgramInstance

use of org.hisp.dhis.program.ProgramInstance 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)

Example 5 with ProgramInstance

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

the class AbstractEventService method updateTrackedEntityInstance.

private void updateTrackedEntityInstance(List<ProgramStageInstance> programStageInstances) {
    Set<ProgramInstance> programInstances = new HashSet<>();
    Set<TrackedEntityInstance> trackedEntityInstances = new HashSet<>();
    for (ProgramStageInstance programStageInstance : programStageInstances) {
        if (programStageInstance.getProgramInstance() != null) {
            programInstances.add(programStageInstance.getProgramInstance());
            if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
                trackedEntityInstances.add(programStageInstance.getProgramInstance().getEntityInstance());
            }
        }
    }
    programInstances.forEach(manager::update);
    trackedEntityInstances.forEach(manager::update);
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) HashSet(java.util.HashSet)

Aggregations

ProgramInstance (org.hisp.dhis.program.ProgramInstance)24 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)10 Date (java.util.Date)9 Program (org.hisp.dhis.program.Program)9 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 ProgramStage (org.hisp.dhis.program.ProgramStage)7 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)7 HashSet (java.util.HashSet)5 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)5 ArrayList (java.util.ArrayList)4 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)3 ProgramInstanceQueryParams (org.hisp.dhis.program.ProgramInstanceQueryParams)3 Patient (org.hisp.dhis.api.mobile.model.LWUITmodel.Patient)2 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)2 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)2 Relationship (org.hisp.dhis.relationship.Relationship)2 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)2 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)2 TrackedEntityDataValue (org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)2 Lists (com.google.common.collect.Lists)1