Search in sources :

Example 11 with TrackedEntityAttributeValue

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

the class TrackedEntityInstanceStoreTest method testQuery.

@Test
public void testQuery() {
    teiStore.save(teiA);
    teiStore.save(teiB);
    teiStore.save(teiC);
    teiStore.save(teiD);
    teiStore.save(teiE);
    teiStore.save(teiF);
    attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(atA, teiD, "Male"));
    attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(atA, teiE, "Male"));
    attributeValueService.addTrackedEntityAttributeValue(new TrackedEntityAttributeValue(atA, teiF, "Female"));
    programInstanceService.enrollTrackedEntityInstance(teiB, prA, new Date(), new Date(), ouB);
    programInstanceService.enrollTrackedEntityInstance(teiE, prA, new Date(), new Date(), ouB);
    // Get all
    TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
    List<TrackedEntityInstance> teis = teiStore.getTrackedEntityInstances(params);
    assertEquals(6, teis.size());
    // Filter by attribute
    params = new TrackedEntityInstanceQueryParams().addFilter(new QueryItem(atA, QueryOperator.EQ, "Male", ValueType.TEXT, AggregationType.NONE, null));
    teis = teiStore.getTrackedEntityInstances(params);
    assertEquals(2, teis.size());
    assertTrue(teis.contains(teiD));
    assertTrue(teis.contains(teiE));
    // Filter by attribute
    params = new TrackedEntityInstanceQueryParams().addFilter(new QueryItem(atA, QueryOperator.EQ, "Female", ValueType.TEXT, AggregationType.NONE, null));
    teis = teiStore.getTrackedEntityInstances(params);
    assertEquals(1, teis.size());
    assertTrue(teis.contains(teiF));
    // Filter by selected org units
    params = new TrackedEntityInstanceQueryParams().addOrganisationUnit(ouB).setOrganisationUnitMode(OrganisationUnitSelectionMode.SELECTED);
    teis = teiStore.getTrackedEntityInstances(params);
    assertEquals(2, teis.size());
    assertTrue(teis.contains(teiB));
    assertTrue(teis.contains(teiC));
    // Filter by descendants org units
    params = new TrackedEntityInstanceQueryParams().addOrganisationUnit(ouB).setOrganisationUnitMode(OrganisationUnitSelectionMode.DESCENDANTS);
    teis = teiStore.getTrackedEntityInstances(params);
    assertEquals(5, teis.size());
    assertTrue(teis.contains(teiB));
    assertTrue(teis.contains(teiC));
    assertTrue(teis.contains(teiD));
    assertTrue(teis.contains(teiE));
    assertTrue(teis.contains(teiF));
    // Filter by program enrollment
    params = new TrackedEntityInstanceQueryParams().setProgram(prA);
    teis = teiStore.getTrackedEntityInstances(params);
    assertEquals(2, teis.size());
    assertTrue(teis.contains(teiB));
    assertTrue(teis.contains(teiE));
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Date(java.util.Date) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 12 with TrackedEntityAttributeValue

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

the class ActivityReportingServiceImpl method updatePatient.

@Override
public Patient updatePatient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId, String programIdText) throws NotAllowedException {
    TrackedEntityInstance entityInstance = entityInstanceService.getTrackedEntityInstance(patient.getId());
    TrackedEntityInstance tempTEI = entityInstance;
    TrackedEntity trackedEntity = null;
    Program program = programService.getProgram(Integer.parseInt(programIdText));
    trackedEntity = program.getTrackedEntity();
    entityInstance.setTrackedEntity(trackedEntity);
    // get attributes to be saved/updated/deleted
    Collection<TrackedEntityAttribute> attributes = attributeService.getAllTrackedEntityAttributes();
    List<TrackedEntityAttributeValue> valuesForSave = new ArrayList<>();
    List<TrackedEntityAttributeValue> valuesForUpdate = new ArrayList<>();
    Collection<TrackedEntityAttributeValue> valuesForDelete = null;
    TrackedEntityAttributeValue attributeValue = null;
    Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
    if (attributes != null && attributes.size() > 0) {
        valuesForDelete = attValueService.getTrackedEntityAttributeValues(entityInstance);
        tempTEI.getTrackedEntityAttributeValues().clear();
        for (TrackedEntityAttribute attribute : attributes) {
            String value = getAttributeValue(attributesMobile, attribute.getName());
            if (value != null) {
                attributeValue = attValueService.getTrackedEntityAttributeValue(entityInstance, attribute);
                if (attributeValue == null) {
                    attributeValue = new TrackedEntityAttributeValue();
                    attributeValue.setEntityInstance(entityInstance);
                    attributeValue.setAttribute(attribute);
                    attributeValue.setValue(value.trim());
                    valuesForSave.add(attributeValue);
                } else {
                    attributeValue.setValue(value.trim());
                    valuesForUpdate.add(attributeValue);
                    valuesForDelete.remove(attributeValue);
                }
                tempTEI.getTrackedEntityAttributeValues().add(attributeValue);
            }
        }
    }
    // validate
    String[] errorCode = entityInstanceService.validateTrackedEntityInstance(tempTEI, program).split("_");
    int code = Integer.parseInt(errorCode[0]);
    if (code >= 1) {
        if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
            throw new NotAllowedException("Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName());
        } else {
            throw new NotAllowedException("Validation error");
        }
    }
    entityInstanceService.updateTrackedEntityInstance(entityInstance, null, null, valuesForSave, valuesForUpdate, valuesForDelete);
    enrollProgram(patient.getId() + "-" + programIdText, null, new Date());
    entityInstance = entityInstanceService.getTrackedEntityInstance(patient.getId());
    entityInstance.setTrackedEntity(trackedEntity);
    return getPatientModel(entityInstance);
}
Also used : Program(org.hisp.dhis.program.Program) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date)

Example 13 with TrackedEntityAttributeValue

use of org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue 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 14 with TrackedEntityAttributeValue

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

the class ActivityReportingServiceImpl method getBeneficiaryModel.

private Beneficiary getBeneficiaryModel(TrackedEntityInstance patient) {
    Beneficiary beneficiary = new Beneficiary();
    List<org.hisp.dhis.api.mobile.model.PatientAttribute> patientAtts = new ArrayList<>();
    beneficiary.setId(patient.getId());
    beneficiary.setName(patient.getName());
    // Set attribute which is used to group beneficiary on mobile (only if
    // there is attribute which is set to be group factor)
    org.hisp.dhis.api.mobile.model.PatientAttribute beneficiaryAttribute = null;
    if (groupByAttribute != null) {
        beneficiaryAttribute = new org.hisp.dhis.api.mobile.model.PatientAttribute();
        beneficiaryAttribute.setName(groupByAttribute.getName());
        TrackedEntityAttributeValue value = attValueService.getTrackedEntityAttributeValue(patient, groupByAttribute);
        beneficiaryAttribute.setValue(value == null ? "Unknown" : value.getValue());
        beneficiary.setGroupAttribute(beneficiaryAttribute);
    }
    beneficiary.setPatientAttValues(patientAtts);
    return beneficiary;
}
Also used : PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ArrayList(java.util.ArrayList) Beneficiary(org.hisp.dhis.api.mobile.model.Beneficiary)

Example 15 with TrackedEntityAttributeValue

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

the class TrackedEntityRegistrationSMSListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    String message = sms.getText();
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.TRACKED_ENTITY_REGISTRATION_PARSER);
    Map<String, String> parsedMessage = this.parse(message, smsCommand);
    Date date = SmsUtils.lookForDate(message);
    String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
    Collection<OrganisationUnit> orgUnits = SmsUtils.getOrganisationUnitsByPhoneNumber(senderPhoneNumber, userService.getUsersByPhoneNumber(senderPhoneNumber));
    if (orgUnits == null || orgUnits.size() == 0) {
        if (StringUtils.isEmpty(smsCommand.getNoUserMessage())) {
            throw new SMSParserException(SMSCommand.NO_USER_MESSAGE);
        } else {
            throw new SMSParserException(smsCommand.getNoUserMessage());
        }
    }
    OrganisationUnit orgUnit = SmsUtils.selectOrganisationUnit(orgUnits, parsedMessage, smsCommand);
    TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
    trackedEntityInstance.setOrganisationUnit(orgUnit);
    trackedEntityInstance.setTrackedEntity(trackedEntityService.getTrackedEntityByName("Person"));
    Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
    for (SMSCode code : smsCommand.getCodes()) {
        if (parsedMessage.containsKey(code.getCode().toUpperCase())) {
            TrackedEntityAttributeValue trackedEntityAttributeValue = this.createTrackedEntityAttributeValue(parsedMessage, code, smsCommand, trackedEntityInstance);
            patientAttributeValues.add(trackedEntityAttributeValue);
        }
    }
    int trackedEntityInstanceId = 0;
    if (patientAttributeValues.size() > 0) {
        trackedEntityInstanceId = trackedEntityInstanceService.createTrackedEntityInstance(trackedEntityInstance, null, null, patientAttributeValues);
    }
    programInstanceService.enrollTrackedEntityInstance(trackedEntityInstanceService.getTrackedEntityInstance(trackedEntityInstanceId), smsCommand.getProgram(), new Date(), date, orgUnit);
    smsSender.sendMessage(null, "Entity Registered Successfully ", senderPhoneNumber);
    sms.setStatus(SmsMessageStatus.PROCESSED);
    sms.setParsed(true);
    incomingSmsService.update(sms);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

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