Search in sources :

Example 26 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method findPatientInAdvanced.

/**
     * keyword is on format of
     * {attribute-id1}:{operator1}:{filter-value1};{attribute
     * -id2}:{operator2}:{filter-value2}
     */
@Override
public String findPatientInAdvanced(String keyword, int orgUnitId, int programId) throws NotAllowedException {
    TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams();
    List<TrackedEntityAttribute> displayAttributes = new ArrayList<>(attributeService.getTrackedEntityAttributesDisplayInList());
    for (TrackedEntityAttribute trackedEntityAttribute : displayAttributes) {
        QueryItem queryItem = new QueryItem(trackedEntityAttribute);
        param.addAttribute(queryItem);
    }
    if (programId != 0) {
        param.setProgram(programService.getProgram(programId));
    }
    if (orgUnitId != 0) {
        param.addOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
        param.setOrganisationUnitMode(OrganisationUnitSelectionMode.SELECTED);
    } else {
        param.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
    }
    String[] items = keyword.split(";");
    if (items == null) {
        items = new String[1];
        items[0] = keyword;
    }
    for (int i = 0; i < items.length; i++) {
        String[] split = keyword.split(":");
        if (split == null || (split.length != 3 && split.length != 2)) {
            throw NotAllowedException.INVALID_FILTER;
        }
        if (split.length == 2) {
            QueryOperator operator = QueryOperator.fromString(split[0]);
            param.setQuery(new QueryFilter(operator, split[1]));
        } else {
            TrackedEntityAttribute at = attributeService.getTrackedEntityAttributeByName(split[0]);
            QueryItem queryItem = new QueryItem(at, at.getLegendSets().get(0), at.getValueType(), at.getAggregationType(), at.getOptionSet());
            QueryOperator operator = QueryOperator.fromString(split[1]);
            queryItem.getFilters().add(new QueryFilter(operator, split[2]));
            param.getFilters().add(queryItem);
        }
    }
    Grid trackedEntityInstanceGrid = entityInstanceService.getTrackedEntityInstancesGrid(param);
    List<List<Object>> listOfTrackedEntityInstance = trackedEntityInstanceGrid.getRows();
    if (listOfTrackedEntityInstance.size() == 0) {
        throw NotAllowedException.NO_BENEFICIARY_FOUND;
    }
    /**
         * Grid columns: 0 = instance 1 = created 2 = lastupdated 3 = ou 4 = te
         * 5 onwards = attributes
         */
    int instanceIndex = 0;
    int teIndex = 4;
    List<Integer> attributesIndex = new ArrayList<>();
    List<GridHeader> headers = trackedEntityInstanceGrid.getHeaders();
    int index = 0;
    for (GridHeader header : headers) {
        if (header.getName().equals("instance")) {
            instanceIndex = index;
        } else if (header.getName().equals("te")) {
            teIndex = index;
        } else if (!header.getName().equals("created") && !header.getName().equals("lastupdated") && !header.getName().equals("ou")) {
            attributesIndex.add(new Integer(index));
        }
        index++;
    }
    String instanceInfo = "";
    String trackedEntityName = "";
    for (List<Object> row : listOfTrackedEntityInstance) {
        TrackedEntity te = trackedEntityService.getTrackedEntity((String) row.get(teIndex));
        if (!trackedEntityName.equals(te.getDisplayName())) {
            trackedEntityName = te.getDisplayName();
            instanceInfo += te.getDisplayName() + "$";
        }
        // NOTE: this line should be here but because the mobile client uses
        // the int TEI id, we will temprarily get the int id for now.
        // instanceInfo += (String) row.get( instanceIndex ) + "/";
        TrackedEntityInstance tei = entityInstanceService.getTrackedEntityInstance((String) row.get(instanceIndex));
        instanceInfo += tei.getId() + "/";
        // end of temproary fix
        String attText = "";
        for (Integer attIndex : attributesIndex) {
            if (row.get(attIndex.intValue()) != null) {
                attText += (String) row.get(attIndex.intValue()) + " ";
            }
        }
        instanceInfo += attText.trim() + "$";
    }
    return instanceInfo;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) GridHeader(org.hisp.dhis.common.GridHeader) QueryFilter(org.hisp.dhis.common.QueryFilter) List(java.util.List) PatientList(org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList) ArrayList(java.util.ArrayList) QueryOperator(org.hisp.dhis.common.QueryOperator) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)

Example 27 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method addRelationship.

@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient addRelationship(org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship enrollmentRelationship, int orgUnitId) throws NotAllowedException {
    TrackedEntityInstance patientB;
    if (enrollmentRelationship.getPersonBId() != 0) {
        patientB = entityInstanceService.getTrackedEntityInstance(enrollmentRelationship.getPersonBId());
    } else {
        String instanceInfo = findPatientInAdvanced(enrollmentRelationship.getPersonBName(), orgUnitId, 0);
        if (instanceInfo == null || instanceInfo.trim().length() == 0) {
            throw NotAllowedException.NO_BENEFICIARY_FOUND;
        } else {
            throw new NotAllowedException(instanceInfo);
        }
    }
    TrackedEntityInstance patientA = entityInstanceService.getTrackedEntityInstance(enrollmentRelationship.getPersonAId());
    RelationshipType relationshipType = relationshipTypeService.getRelationshipType(enrollmentRelationship.getId());
    Relationship relationship = new Relationship();
    relationship.setRelationshipType(relationshipType);
    if (enrollmentRelationship.getChosenRelationship().equals(relationshipType.getaIsToB())) {
        relationship.setEntityInstanceA(patientA);
        relationship.setEntityInstanceB(patientB);
    } else {
        relationship.setEntityInstanceA(patientB);
        relationship.setEntityInstanceB(patientA);
    }
    relationshipService.addRelationship(relationship);
    // return getPatientModel( orgUnitId, patientA );
    return getPatientModel(patientA);
}
Also used : NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) Relationship(org.hisp.dhis.relationship.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 28 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method findPatient.

@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient findPatient(String patientId) throws NotAllowedException {
    TrackedEntityInstance patient = entityInstanceService.getTrackedEntityInstance(patientId);
    // Temporary fix
    if (patient == null) {
        patient = entityInstanceService.getTrackedEntityInstance(Integer.parseInt(patientId));
    }
    org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patientMobile = getPatientModel(patient);
    return patientMobile;
}
Also used : Patient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 29 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method getActivity.

// -------------------------------------------------------------------------
// Supportive method
// -------------------------------------------------------------------------
private Activity getActivity(ProgramStageInstance instance, boolean late) {
    Activity activity = new Activity();
    TrackedEntityInstance patient = instance.getProgramInstance().getEntityInstance();
    activity.setBeneficiary(getBeneficiaryModel(patient));
    activity.setDueDate(instance.getDueDate());
    activity.setTask(getTask(instance));
    activity.setLate(late);
    activity.setExpireDate(DateUtils.getDateAfterAddition(instance.getDueDate(), 30));
    return activity;
}
Also used : Activity(org.hisp.dhis.api.mobile.model.Activity) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 30 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance 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)

Aggregations

TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)33 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)11 Date (java.util.Date)10 HashSet (java.util.HashSet)9 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)8 ArrayList (java.util.ArrayList)7 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)7 Program (org.hisp.dhis.program.Program)6 ProgramInstance (org.hisp.dhis.program.ProgramInstance)6 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)6 List (java.util.List)5 NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)5 PatientList (org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList)5 PatientAttribute (org.hisp.dhis.api.mobile.model.PatientAttribute)4 Grid (org.hisp.dhis.common.Grid)4 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)4 Activity (org.hisp.dhis.api.mobile.model.Activity)3 Patient (org.hisp.dhis.api.mobile.model.LWUITmodel.Patient)3 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)3 QueryItem (org.hisp.dhis.common.QueryItem)3