Search in sources :

Example 6 with ProgramStageInstance

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

the class ActivityReportingServiceImpl method saveActivityReport.

// -------------------------------------------------------------------------
// DataValueService
// -------------------------------------------------------------------------
@Override
public void saveActivityReport(OrganisationUnit unit, ActivityValue activityValue, Integer programStageSectionId) throws NotAllowedException {
    ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(activityValue.getProgramInstanceId());
    if (programStageInstance == null) {
        throw NotAllowedException.INVALID_PROGRAM_STAGE;
    }
    programStageInstance.getProgramStage();
    List<org.hisp.dhis.dataelement.DataElement> dataElements = new ArrayList<>();
    ProgramStageSection programStageSection = programStageSectionService.getProgramStageSection(programStageSectionId);
    if (programStageSectionId != 0) {
        dataElements.addAll(programStageSection.getDataElements());
    } else {
        for (ProgramStageDataElement de : programStageInstance.getProgramStage().getProgramStageDataElements()) {
            dataElements.add(de.getDataElement());
        }
    }
    programStageInstance.getProgramStage().getProgramStageDataElements();
    Collection<Integer> dataElementIds = new ArrayList<>(activityValue.getDataValues().size());
    for (DataValue dv : activityValue.getDataValues()) {
        dataElementIds.add(dv.getId());
    }
    if (dataElements.size() != dataElementIds.size()) {
        throw NotAllowedException.INVALID_PROGRAM_STAGE;
    }
    Map<Integer, org.hisp.dhis.dataelement.DataElement> dataElementMap = new HashMap<>();
    for (org.hisp.dhis.dataelement.DataElement dataElement : dataElements) {
        if (!dataElementIds.contains(dataElement.getId())) {
            throw NotAllowedException.INVALID_PROGRAM_STAGE;
        }
        dataElementMap.put(dataElement.getId(), dataElement);
    }
    // Set ProgramStageInstance to completed
    if (programStageSectionId == 0) {
        programStageInstance.setStatus(EventStatus.COMPLETED);
        programStageInstanceService.updateProgramStageInstance(programStageInstance);
    }
    // Everything is fine, hence save
    saveDataValues(activityValue, programStageInstance, dataElementMap);
}
Also used : ProgramStageSection(org.hisp.dhis.program.ProgramStageSection) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) DataValue(org.hisp.dhis.api.mobile.model.DataValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 7 with ProgramStageInstance

use of org.hisp.dhis.program.ProgramStageInstance 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 8 with ProgramStageInstance

use of org.hisp.dhis.program.ProgramStageInstance 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 9 with ProgramStageInstance

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

the class PdfDataEntryFormImportUtil method importProgramStage.

// -------------------------------------------------------------------------
// METHODS
// -------------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void importProgramStage(InputStream in, I18nFormat format) throws Exception {
    int programStageInstanceId;
    ProgramStageInstance programStageInstance;
    PdfReader reader = new PdfReader(in, null);
    AcroFields form = reader.getAcroFields();
    String strOrgID = form.getField(PdfDataEntryFormUtil.LABELCODE_ORGID);
    int organisationUnitId = Integer.parseInt(strOrgID);
    String strPeriodID = form.getField(PdfDataEntryFormUtil.LABELCODE_PERIODID);
    Date executionDateInput = format.parseDate(strPeriodID);
    Calendar executeDateCal = Calendar.getInstance();
    executeDateCal.setTime(executionDateInput);
    int programStageId = Integer.valueOf(form.getField(PdfDataEntryFormUtil.LABELCODE_PROGRAMSTAGEIDTEXTBOX));
    ProgramStageInstanceDataManager programStageInstanceDataManager = new ProgramStageInstanceDataManager();
    // Loop Through the Fields and get data.
    Set<String> fldNames = form.getFields().keySet();
    // Create Organized data
    for (String fldName : fldNames) {
        // If the value in the text field is not empty, proceed to add it.
        if (!form.getField(fldName).trim().isEmpty()) {
            if (fldName.startsWith(PdfDataEntryFormUtil.LABELCODE_DATADATETEXTFIELD)) {
                String[] strArrFldName = fldName.split("_");
                int rowNumber = Integer.parseInt(strArrFldName[1]);
                programStageInstanceDataManager.addDateData(Integer.parseInt(form.getField(fldName)), rowNumber);
            } else if (fldName.startsWith(PdfDataEntryFormUtil.LABELCODE_DATAENTRYTEXTFIELD)) {
                String[] strArrFldName = fldName.split("_");
                int dataElementId = Integer.parseInt(strArrFldName[1]);
                int rowNumber = Integer.parseInt(strArrFldName[2]);
                programStageInstanceDataManager.addDataElementData(dataElementId, form.getField(fldName), rowNumber);
            }
        }
    }
    // For each row, add new programStageInstance and add data elements to it.
    for (Map.Entry<Integer, ProgramStageInstanceStorage> entry : programStageInstanceDataManager.getProgramStageInstanceData().entrySet()) {
        ProgramStageInstanceStorage programStageInstanceStorage = entry.getValue();
        int date = programStageInstanceStorage.getDate();
        executeDateCal.set(Calendar.DATE, date);
        Date executionDate = executeDateCal.getTime();
        // Step 2. Create Program Stage Instance - If does not exists
        // already.
        programStageInstanceId = addNewProgramStageInstance(programStageId, organisationUnitId, executionDate);
        programStageInstance = programStageInstanceService.getProgramStageInstance(programStageInstanceId);
        for (Map.Entry<Integer, String> dataElementsEntry : programStageInstanceStorage.getDataElementsValue().entrySet()) {
            Integer dataElementId = dataElementsEntry.getKey();
            String value = dataElementsEntry.getValue();
            // Step 3. Insert Data
            insertValueProgramStageDataElement(programStageInstance, dataElementId, value);
        }
    }
    reader.close();
}
Also used : Calendar(java.util.Calendar) PdfReader(com.lowagie.text.pdf.PdfReader) Date(java.util.Date) AcroFields(com.lowagie.text.pdf.AcroFields) HashMap(java.util.HashMap) Map(java.util.Map) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 10 with ProgramStageInstance

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

ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)23 ArrayList (java.util.ArrayList)10 ProgramInstance (org.hisp.dhis.program.ProgramInstance)9 Date (java.util.Date)8 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 ProgramStage (org.hisp.dhis.program.ProgramStage)7 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)7 Program (org.hisp.dhis.program.Program)6 DataElement (org.hisp.dhis.dataelement.DataElement)5 TrackedEntityDataValue (org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)5 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)4 HashSet (java.util.HashSet)3 List (java.util.List)3 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)3 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)3 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)2 Activity (org.hisp.dhis.api.mobile.model.Activity)2 ActivityPlan (org.hisp.dhis.api.mobile.model.ActivityPlan)2