Search in sources :

Example 1 with Relationship

use of org.hisp.dhis.relationship.Relationship in project dhis2-core by dhis2.

the class DefaultTrackedEntityInstanceService method updateTrackedEntityInstance.

@Override
public void updateTrackedEntityInstance(TrackedEntityInstance instance, String representativeId, Integer relationshipTypeId, List<TrackedEntityAttributeValue> valuesForSave, List<TrackedEntityAttributeValue> valuesForUpdate, Collection<TrackedEntityAttributeValue> valuesForDelete) {
    trackedEntityInstanceStore.update(instance);
    valuesForSave.forEach(attributeValueService::addTrackedEntityAttributeValue);
    valuesForUpdate.forEach(attributeValueService::updateTrackedEntityAttributeValue);
    valuesForDelete.forEach(attributeValueService::deleteTrackedEntityAttributeValue);
    if (shouldSaveRepresentativeInformation(instance, representativeId)) {
        TrackedEntityInstance representative = trackedEntityInstanceStore.getByUid(representativeId);
        if (representative != null) {
            instance.setRepresentative(representative);
            Relationship rel = new Relationship();
            rel.setEntityInstanceA(representative);
            rel.setEntityInstanceB(instance);
            if (relationshipTypeId != null) {
                RelationshipType relType = relationshipTypeService.getRelationshipType(relationshipTypeId);
                if (relType != null) {
                    rel.setRelationshipType(relType);
                    relationshipService.addRelationship(rel);
                }
            }
        }
    }
}
Also used : Relationship(org.hisp.dhis.relationship.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType)

Example 2 with Relationship

use of org.hisp.dhis.relationship.Relationship in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method getTrackedEntityInstance.

@Override
public TrackedEntityInstance getTrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance, TrackedEntityInstanceParams params) {
    if (entityInstance == null) {
        return null;
    }
    TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
    trackedEntityInstance.setTrackedEntityInstance(entityInstance.getUid());
    trackedEntityInstance.setOrgUnit(entityInstance.getOrganisationUnit().getUid());
    trackedEntityInstance.setTrackedEntity(entityInstance.getTrackedEntity().getUid());
    trackedEntityInstance.setCreated(DateUtils.getIso8601NoTz(entityInstance.getCreated()));
    trackedEntityInstance.setCreatedAtClient(DateUtils.getIso8601NoTz(entityInstance.getLastUpdatedAtClient()));
    trackedEntityInstance.setLastUpdated(DateUtils.getIso8601NoTz(entityInstance.getLastUpdated()));
    trackedEntityInstance.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(entityInstance.getLastUpdatedAtClient()));
    trackedEntityInstance.setInactive(entityInstance.isInactive());
    if (params.isIncludeRelationships()) {
        //TODO include relationships in data model and void transactional query in for-loop
        Collection<Relationship> relationships = relationshipService.getRelationshipsForTrackedEntityInstance(entityInstance);
        for (Relationship entityRelationship : relationships) {
            org.hisp.dhis.dxf2.events.trackedentity.Relationship relationship = new org.hisp.dhis.dxf2.events.trackedentity.Relationship();
            relationship.setDisplayName(entityRelationship.getRelationshipType().getDisplayName());
            relationship.setTrackedEntityInstanceA(entityRelationship.getEntityInstanceA().getUid());
            relationship.setTrackedEntityInstanceB(entityRelationship.getEntityInstanceB().getUid());
            relationship.setRelationship(entityRelationship.getRelationshipType().getUid());
            // we might have cases where A <=> A, so we only include the relative if the UIDs do not match
            if (!entityRelationship.getEntityInstanceA().getUid().equals(entityInstance.getUid())) {
                relationship.setRelative(getTrackedEntityInstance(entityRelationship.getEntityInstanceA(), TrackedEntityInstanceParams.FALSE));
            } else if (!entityRelationship.getEntityInstanceB().getUid().equals(entityInstance.getUid())) {
                relationship.setRelative(getTrackedEntityInstance(entityRelationship.getEntityInstanceB(), TrackedEntityInstanceParams.FALSE));
            }
            trackedEntityInstance.getRelationships().add(relationship);
        }
    }
    if (params.isIncludeEnrollments()) {
        for (ProgramInstance programInstance : entityInstance.getProgramInstances()) {
            trackedEntityInstance.getEnrollments().add(enrollmentService.getEnrollment(programInstance, params));
        }
    }
    for (TrackedEntityAttributeValue attributeValue : entityInstance.getTrackedEntityAttributeValues()) {
        Attribute attribute = new Attribute();
        attribute.setCreated(DateUtils.getIso8601NoTz(attributeValue.getCreated()));
        attribute.setLastUpdated(DateUtils.getIso8601NoTz(attributeValue.getLastUpdated()));
        attribute.setDisplayName(attributeValue.getAttribute().getDisplayName());
        attribute.setAttribute(attributeValue.getAttribute().getUid());
        attribute.setValueType(attributeValue.getAttribute().getValueType());
        attribute.setCode(attributeValue.getAttribute().getCode());
        attribute.setValue(attributeValue.getValue());
        trackedEntityInstance.getAttributes().add(attribute);
    }
    return trackedEntityInstance;
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Relationship(org.hisp.dhis.relationship.Relationship)

Example 3 with Relationship

use of org.hisp.dhis.relationship.Relationship in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method updateRelationships.

private void updateRelationships(TrackedEntityInstance trackedEntityInstance, org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance) {
    for (org.hisp.dhis.dxf2.events.trackedentity.Relationship relationship : trackedEntityInstance.getRelationships()) {
        org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstanceA = manager.get(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, relationship.getTrackedEntityInstanceA());
        org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstanceB = manager.get(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, relationship.getTrackedEntityInstanceB());
        RelationshipType relationshipType = manager.get(RelationshipType.class, relationship.getRelationship());
        Relationship entityRelationship = new Relationship();
        entityRelationship.setEntityInstanceA(entityInstanceA);
        entityRelationship.setEntityInstanceB(entityInstanceB);
        entityRelationship.setRelationshipType(relationshipType);
        relationshipService.addRelationship(entityRelationship);
    }
}
Also used : Relationship(org.hisp.dhis.relationship.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType)

Example 4 with Relationship

use of org.hisp.dhis.relationship.Relationship 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 5 with Relationship

use of org.hisp.dhis.relationship.Relationship 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

Relationship (org.hisp.dhis.relationship.Relationship)6 RelationshipType (org.hisp.dhis.relationship.RelationshipType)4 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)3 ProgramInstance (org.hisp.dhis.program.ProgramInstance)2 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)2 ArrayList (java.util.ArrayList)1 NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)1 Patient (org.hisp.dhis.api.mobile.model.LWUITmodel.Patient)1 OptionSet (org.hisp.dhis.api.mobile.model.OptionSet)1 PatientAttribute (org.hisp.dhis.api.mobile.model.PatientAttribute)1 TrackedEntityAttributeValueSortOrderComparator (org.hisp.dhis.api.mobile.model.comparator.TrackedEntityAttributeValueSortOrderComparator)1 ProgramInstanceQueryParams (org.hisp.dhis.program.ProgramInstanceQueryParams)1 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)1