Search in sources :

Example 21 with ProgramInstance

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

the class ActivityReportingServiceImpl method getPatientModel.

// get patient model for LWUIT
private org.hisp.dhis.api.mobile.model.LWUITmodel.Patient getPatientModel(TrackedEntityInstance patient) {
    org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patientModel = new org.hisp.dhis.api.mobile.model.LWUITmodel.Patient();
    List<org.hisp.dhis.api.mobile.model.PatientAttribute> patientAtts = new ArrayList<>();
    List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance> mobileProgramInstanceList = new ArrayList<>();
    List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance> mobileCompletedProgramInstanceList = new ArrayList<>();
    patientModel.setId(patient.getId());
    if (patient.getOrganisationUnit() != null) {
        patientModel.setOrganisationUnitName(patient.getOrganisationUnit().getName());
    }
    if (patient.getTrackedEntity() != null) {
        patientModel.setTrackedEntityName(patient.getTrackedEntity().getName());
    } else {
        patientModel.setTrackedEntityName("");
    }
    List<TrackedEntityAttributeValue> atts = new ArrayList<>(patient.getTrackedEntityAttributeValues());
    for (TrackedEntityAttributeValue value : atts) {
        if (value != null) {
            org.hisp.dhis.api.mobile.model.PatientAttribute patientAttribute = new org.hisp.dhis.api.mobile.model.PatientAttribute(value.getAttribute().getName(), value.getValue(), null, false, value.getAttribute().getDisplayInListNoProgram(), new OptionSet());
            patientAttribute.setType(value.getAttribute().getValueType());
            patientAtts.add(patientAttribute);
        }
    }
    patientModel.setAttributes(patientAtts);
    List<ProgramInstance> listOfProgramInstance = new ArrayList<>(programInstanceService.getProgramInstances(new ProgramInstanceQueryParams().setTrackedEntityInstance(patient).setProgramStatus(ProgramStatus.ACTIVE).setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL)));
    if (listOfProgramInstance.size() > 0) {
        for (ProgramInstance each : listOfProgramInstance) {
            mobileProgramInstanceList.add(getMobileProgramInstance(each));
        }
    }
    patientModel.setEnrollmentPrograms(mobileProgramInstanceList);
    List<ProgramInstance> listOfCompletedProgramInstance = new ArrayList<>(programInstanceService.getProgramInstances(new ProgramInstanceQueryParams().setTrackedEntityInstance(patient).setProgramStatus(ProgramStatus.COMPLETED).setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL)));
    if (listOfCompletedProgramInstance.size() > 0) {
        for (ProgramInstance each : listOfCompletedProgramInstance) {
            mobileCompletedProgramInstanceList.add(getMobileProgramInstance(each));
        }
    }
    patientModel.setCompletedPrograms(mobileCompletedProgramInstanceList);
    // Set Relationship
    Collection<Relationship> relationships = relationshipService.getRelationshipsForTrackedEntityInstance(patient);
    List<org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship> relationshipList = new ArrayList<>();
    for (Relationship eachRelationship : relationships) {
        org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship relationshipMobile = new org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship();
        relationshipMobile.setId(eachRelationship.getId());
        if (eachRelationship.getEntityInstanceA().getId() == patient.getId()) {
            relationshipMobile.setName(eachRelationship.getRelationshipType().getaIsToB());
            relationshipMobile.setaIsToB(eachRelationship.getRelationshipType().getaIsToB());
            relationshipMobile.setbIsToA(eachRelationship.getRelationshipType().getbIsToA());
            relationshipMobile.setPersonBId(eachRelationship.getEntityInstanceB().getId());
        } else {
            relationshipMobile.setName(eachRelationship.getRelationshipType().getbIsToA());
            relationshipMobile.setaIsToB(eachRelationship.getRelationshipType().getbIsToA());
            relationshipMobile.setbIsToA(eachRelationship.getRelationshipType().getaIsToB());
            relationshipMobile.setPersonBId(eachRelationship.getEntityInstanceA().getId());
        }
        // get relative's name
        TrackedEntityInstance relative = entityInstanceService.getTrackedEntityInstance(relationshipMobile.getPersonBId());
        List<TrackedEntityAttributeValue> attributes = new ArrayList<>(relative.getTrackedEntityAttributeValues());
        List<TrackedEntityAttributeValue> attributesInList = new ArrayList<>();
        for (TrackedEntityAttributeValue value : attributes) {
            if (value != null && value.getAttribute().getDisplayInListNoProgram()) {
                attributesInList.add(value);
            }
        }
        Collections.sort(attributesInList, new TrackedEntityAttributeValueSortOrderComparator());
        String relativeName = "";
        for (TrackedEntityAttributeValue value : attributesInList) {
            if (value != null && value.getAttribute().getDisplayInListNoProgram()) {
                relativeName += value.getValue() + " ";
            }
        }
        relationshipMobile.setPersonBName(relativeName);
        relationshipList.add(relationshipMobile);
    }
    patientModel.setRelationships(relationshipList);
    return patientModel;
}
Also used : Patient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityAttributeValueSortOrderComparator(org.hisp.dhis.api.mobile.model.comparator.TrackedEntityAttributeValueSortOrderComparator) ArrayList(java.util.ArrayList) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Patient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Relationship(org.hisp.dhis.relationship.Relationship) OptionSet(org.hisp.dhis.api.mobile.model.OptionSet) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams)

Example 22 with ProgramInstance

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

the class ActivityReportingServiceImpl method completeProgramInstance.

@Override
public String completeProgramInstance(int programId) throws NotAllowedException {
    ProgramInstance programInstance = programInstanceService.getProgramInstance(programId);
    programInstance.setStatus(ProgramStatus.COMPLETED);
    programInstanceService.updateProgramInstance(programInstance);
    return PROGRAM_COMPLETED;
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance)

Example 23 with ProgramInstance

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

the class ActivityReportingServiceImpl method enrollProgram.

@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient enrollProgram(String enrollInfo, List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStage> mobileProgramStageList, Date incidentDate) throws NotAllowedException {
    String[] enrollProgramInfo = enrollInfo.split("-");
    int patientId = Integer.parseInt(enrollProgramInfo[0]);
    int programId = Integer.parseInt(enrollProgramInfo[1]);
    TrackedEntityInstance patient = entityInstanceService.getTrackedEntityInstance(patientId);
    Program program = programService.getProgram(programId);
    ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(patient, program, new Date(), incidentDate, patient.getOrganisationUnit());
    Iterator<ProgramStage> programStagesIterator = program.getProgramStages().iterator();
    for (int i = 0; i < program.getProgramStages().size(); i++) {
        ProgramStage programStage = programStagesIterator.next();
        if (programStage.getAutoGenerateEvent()) {
            ProgramStageInstance programStageInstance = programStageInstanceService.createProgramStageInstance(programInstance, programStage, new Date(), incidentDate, patient.getOrganisationUnit());
            int programStageInstanceId = programStageInstance.getId();
            // Inject Datavalue avaiable on-the-fly
            if (mobileProgramStageList != null && mobileProgramStageList.size() > 0) {
                org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStage mobileProgramStage = mobileProgramStageList.get(i);
                if (mobileProgramStage != null && mobileProgramStage.getDataElements().size() > 0) {
                    mobileProgramStage.setId(programStageInstanceId);
                    if (mobileProgramStage.isSingleEvent()) {
                        this.saveProgramStage(mobileProgramStage, patientId, patient.getOrganisationUnit().getId());
                    } else {
                        this.saveProgramStage(mobileProgramStage, patientId, 0);
                    }
                }
            }
            programInstance.getProgramStageInstances().add(programStageInstance);
        }
    }
    programInstanceService.updateProgramInstance(programInstance);
    patient.getProgramInstances().add(programInstance);
    entityInstanceService.updateTrackedEntityInstance(patient);
    patient = entityInstanceService.getTrackedEntityInstance(patientId);
    return getPatientModel(patient);
}
Also used : Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 24 with ProgramInstance

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

the class SingleEventListener method registerEvent.

// -------------------------------------------------------------------------
// Supportive Methods
// -------------------------------------------------------------------------
private void registerEvent(Map<String, String> commandValuePairs, SMSCommand smsCommand, IncomingSms sms) {
    OrganisationUnit orgUnit = getOrganisationUnits(sms).iterator().next();
    List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(smsCommand.getProgram(), ProgramStatus.ACTIVE));
    if (programInstances.isEmpty()) {
        ProgramInstance pi = new ProgramInstance();
        pi.setEnrollmentDate(new Date());
        pi.setIncidentDate(new Date());
        pi.setProgram(smsCommand.getProgram());
        pi.setStatus(ProgramStatus.ACTIVE);
        programInstanceService.addProgramInstance(pi);
        programInstances.add(pi);
    } else if (programInstances.size() > 1) {
        update(sms, SmsMessageStatus.FAILED, false);
        sendFeedback("Multiple active program instances exists for program: " + smsCommand.getProgram().getUid(), sms.getOriginator(), ERROR);
        return;
    }
    ProgramInstance programInstance = null;
    programInstance = programInstances.get(0);
    ProgramStageInstance programStageInstance = new ProgramStageInstance();
    programStageInstance.setOrganisationUnit(orgUnit);
    programStageInstance.setProgramStage(smsCommand.getProgramStage());
    programStageInstance.setProgramInstance(programInstance);
    programStageInstance.setExecutionDate(sms.getSentDate());
    programStageInstance.setDueDate(sms.getSentDate());
    programStageInstance.setAttributeOptionCombo(dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
    programStageInstance.setCompletedBy("DHIS 2");
    programStageInstanceService.addProgramStageInstance(programStageInstance);
    for (SMSCode smsCode : smsCommand.getCodes()) {
        TrackedEntityDataValue dataValue = new TrackedEntityDataValue();
        dataValue.setAutoFields();
        dataValue.setDataElement(smsCode.getDataElement());
        dataValue.setProgramStageInstance(programStageInstance);
        dataValue.setValue(commandValuePairs.get(smsCode.getCode()));
        trackedEntityDataValueService.saveTrackedEntityDataValue(dataValue);
    }
    update(sms, SmsMessageStatus.PROCESSED, true);
    sendFeedback(StringUtils.defaultIfEmpty(smsCommand.getSuccessMessage(), EVENT_REGISTERED), sms.getOriginator(), INFO);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ArrayList(java.util.ArrayList) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

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