Search in sources :

Example 6 with TrackedEntityAttributeValue

use of org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method savePatient.

@Override
public Patient savePatient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId, String programIdText) throws NotAllowedException {
    TrackedEntityInstance patientWeb = new TrackedEntityInstance();
    patientWeb.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
    Set<TrackedEntityAttribute> patientAttributeSet = new HashSet<>();
    Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
    Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
    if (attributesMobile != null) {
        for (org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : attributesMobile) {
            TrackedEntityAttribute patientAttribute = attributeService.getTrackedEntityAttributeByName(paAtt.getName());
            patientAttributeSet.add(patientAttribute);
            TrackedEntityAttributeValue patientAttributeValue = new TrackedEntityAttributeValue();
            patientAttributeValue.setEntityInstance(patientWeb);
            patientAttributeValue.setAttribute(patientAttribute);
            patientAttributeValue.setValue(paAtt.getValue());
            patientAttributeValues.add(patientAttributeValue);
        }
    }
    patientWeb.setTrackedEntity(trackedEntityService.getTrackedEntityByName(patient.getTrackedEntityName()));
    patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, null, null, patientAttributeValues);
    TrackedEntityInstance newTrackedEntityInstance = entityInstanceService.getTrackedEntityInstance(this.patientId);
    String errorMsg = null;
    try {
        for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance mobileProgramInstance : patient.getEnrollmentPrograms()) {
            Date incidentDate = DateUtils.getMediumDate(mobileProgramInstance.getDateOfIncident());
            enrollProgram(patientId + "-" + mobileProgramInstance.getProgramId(), mobileProgramInstance.getProgramStageInstances(), incidentDate);
        }
        Program program = programService.getProgram(Integer.parseInt(programIdText));
        String[] errorCode = entityInstanceService.validateTrackedEntityInstance(newTrackedEntityInstance, program).split("_");
        int code = Integer.parseInt(errorCode[0]);
        if (code >= 1) {
            entityInstanceService.deleteTrackedEntityInstance(newTrackedEntityInstance);
            if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
                errorMsg = "Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName();
            } else {
                errorMsg = "Validation error";
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (errorMsg != null) {
        throw new NotAllowedException(errorMsg);
    }
    return getPatientModel(newTrackedEntityInstance);
}
Also used : Program(org.hisp.dhis.program.Program) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) HashSet(java.util.HashSet)

Example 7 with TrackedEntityAttributeValue

use of org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method updateAttributeValues.

private void updateAttributeValues(TrackedEntityInstance trackedEntityInstance, org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance) {
    for (Attribute attribute : trackedEntityInstance.getAttributes()) {
        TrackedEntityAttribute entityAttribute = manager.get(TrackedEntityAttribute.class, attribute.getAttribute());
        if (entityAttribute != null) {
            TrackedEntityAttributeValue attributeValue = new TrackedEntityAttributeValue();
            attributeValue.setEntityInstance(entityInstance);
            attributeValue.setValue(attribute.getValue());
            attributeValue.setAttribute(entityAttribute);
            trackedEntityAttributeValueService.addTrackedEntityAttributeValue(attributeValue);
        }
    }
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)

Example 8 with TrackedEntityAttributeValue

use of org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue in project dhis2-core by dhis2.

the class TrackedEntityInstanceServiceTest method testCreateTrackedEntityInstanceAndRelative.

@Test
public void testCreateTrackedEntityInstanceAndRelative() {
    entityInstanceService.addTrackedEntityInstance(entityInstanceB1);
    RelationshipType relationshipType = createRelationshipType('A');
    int relationshipTypeId = relationshipTypeService.addRelationshipType(relationshipType);
    TrackedEntityAttributeValue attributeValue = createTrackedEntityAttributeValue('A', entityInstanceA1, entityInstanceAttribute);
    Set<TrackedEntityAttributeValue> entityInstanceAttributeValues = new HashSet<>();
    entityInstanceAttributeValues.add(attributeValue);
    int idA = entityInstanceService.createTrackedEntityInstance(entityInstanceA1, entityInstanceB1.getUid(), relationshipTypeId, entityInstanceAttributeValues);
    assertNotNull(entityInstanceService.getTrackedEntityInstance(idA));
}
Also used : TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) RelationshipType(org.hisp.dhis.relationship.RelationshipType) HashSet(java.util.HashSet) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 9 with TrackedEntityAttributeValue

use of org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method registerRelative.

@Override
public Patient registerRelative(Patient patient, int orgUnitId, String programId) throws NotAllowedException {
    TrackedEntityInstance patientWeb = new TrackedEntityInstance();
    patientWeb.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
    Set<TrackedEntityAttribute> patientAttributeSet = new HashSet<>();
    Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
    Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
    if (attributesMobile != null) {
        for (org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : attributesMobile) {
            TrackedEntityAttribute patientAttribute = attributeService.getTrackedEntityAttributeByName(paAtt.getName());
            patientAttributeSet.add(patientAttribute);
            TrackedEntityAttributeValue patientAttributeValue = new TrackedEntityAttributeValue();
            patientAttributeValue.setEntityInstance(patientWeb);
            patientAttributeValue.setAttribute(patientAttribute);
            patientAttributeValue.setValue(paAtt.getValue());
            patientAttributeValues.add(patientAttributeValue);
        }
    }
    patientWeb.setTrackedEntity(trackedEntityService.getTrackedEntityByName(patient.getTrackedEntityName()));
    if (patient.getIdToAddRelative() != 0) {
        TrackedEntityInstance relative = entityInstanceService.getTrackedEntityInstance(patient.getIdToAddRelative());
        if (relative == null) {
            throw new NotAllowedException("relative does not exist");
        }
        patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, relative.getUid(), patient.getRelTypeIdToAdd(), patientAttributeValues);
    } else {
        patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, null, null, patientAttributeValues);
    }
    TrackedEntityInstance newTrackedEntityInstance = entityInstanceService.getTrackedEntityInstance(this.patientId);
    String errorMsg = null;
    try {
        for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance mobileProgramInstance : patient.getEnrollmentPrograms()) {
            Date incidentDate = DateUtils.getMediumDate(mobileProgramInstance.getDateOfIncident());
            enrollProgram(patientId + "-" + mobileProgramInstance.getProgramId(), mobileProgramInstance.getProgramStageInstances(), incidentDate);
        }
        Program program = programService.getProgram(Integer.parseInt(programId));
        String[] errorCode = entityInstanceService.validateTrackedEntityInstance(newTrackedEntityInstance, program).split("_");
        int code = Integer.parseInt(errorCode[0]);
        if (code >= 1) {
            entityInstanceService.deleteTrackedEntityInstance(newTrackedEntityInstance);
            if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
                errorMsg = "Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName();
            } else {
                errorMsg = "Validation error";
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (errorMsg != null) {
        throw new NotAllowedException(errorMsg);
    }
    if (patient.getEnrollmentRelationship() != null) {
        org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship enrollmentRelationship = patient.getEnrollmentRelationship();
        enrollmentRelationship.setPersonBId(newTrackedEntityInstance.getId());
        addRelationship(enrollmentRelationship, orgUnitId);
    }
    return getPatientModel(newTrackedEntityInstance);
}
Also used : NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) HashSet(java.util.HashSet) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException)

Example 10 with TrackedEntityAttributeValue

use of org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue in project dhis2-core by dhis2.

the class DhisConvenienceTest method createTrackedEntityAttributeValue.

public static TrackedEntityAttributeValue createTrackedEntityAttributeValue(char uniqueChar, TrackedEntityInstance entityInstance, TrackedEntityAttribute attribute) {
    TrackedEntityAttributeValue attributeValue = new TrackedEntityAttributeValue();
    attributeValue.setEntityInstance(entityInstance);
    attributeValue.setAttribute(attribute);
    attributeValue.setValue("Attribute" + uniqueChar);
    return attributeValue;
}
Also used : TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)

Aggregations

TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)21 HashSet (java.util.HashSet)9 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)9 Date (java.util.Date)8 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)8 ArrayList (java.util.ArrayList)7 PatientAttribute (org.hisp.dhis.api.mobile.model.PatientAttribute)5 NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)4 Program (org.hisp.dhis.program.Program)4 DhisSpringTest (org.hisp.dhis.DhisSpringTest)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 ProgramInstance (org.hisp.dhis.program.ProgramInstance)3 Relationship (org.hisp.dhis.relationship.Relationship)3 RelationshipType (org.hisp.dhis.relationship.RelationshipType)3 Test (org.junit.Test)3 List (java.util.List)2 QueryItem (org.hisp.dhis.common.QueryItem)2 ProgramInstanceQueryParams (org.hisp.dhis.program.ProgramInstanceQueryParams)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1