Search in sources :

Example 16 with TrackedEntityAttributeValue

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

the class TrackedEntityRegistrationSMSListener method createTrackedEntityAttributeValue.

private TrackedEntityAttributeValue createTrackedEntityAttributeValue(Map<String, String> parsedMessage, SMSCode code, SMSCommand smsCommand, TrackedEntityInstance trackedEntityInstance) {
    String value = parsedMessage.get(code.getCode().toUpperCase());
    TrackedEntityAttribute trackedEntityAttribute = code.getTrackedEntityAttribute();
    TrackedEntityAttributeValue trackedEntityAttributeValue = new TrackedEntityAttributeValue();
    trackedEntityAttributeValue.setAttribute(trackedEntityAttribute);
    trackedEntityAttributeValue.setEntityInstance(trackedEntityInstance);
    trackedEntityAttributeValue.setValue(value);
    return trackedEntityAttributeValue;
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)

Example 17 with TrackedEntityAttributeValue

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

the class DefaultTrackedEntityInstanceService method validateEnrollment.

@Override
public ValidationCriteria validateEnrollment(TrackedEntityInstance instance, Program program) {
    for (ValidationCriteria criteria : program.getValidationCriteria()) {
        for (TrackedEntityAttributeValue attributeValue : instance.getTrackedEntityAttributeValues()) {
            if (attributeValue.getAttribute().getUid().equals(criteria.getProperty())) {
                String value = attributeValue.getValue();
                ValueType valueType = attributeValue.getAttribute().getValueType();
                if (valueType.isNumeric()) {
                    int value1 = Integer.parseInt(value);
                    int value2 = Integer.parseInt(criteria.getValue());
                    if ((criteria.getOperator() == ValidationCriteria.OPERATOR_LESS_THAN && value1 >= value2) || (criteria.getOperator() == ValidationCriteria.OPERATOR_EQUAL_TO && value1 != value2) || (criteria.getOperator() == ValidationCriteria.OPERATOR_GREATER_THAN && value1 <= value2)) {
                        return criteria;
                    }
                } else if (valueType.isDate()) {
                    Date value1 = DateUtils.parseDate(value);
                    Date value2 = DateUtils.parseDate(criteria.getValue());
                    int i = value1.compareTo(value2);
                    if (i != criteria.getOperator()) {
                        return criteria;
                    }
                } else {
                    if (criteria.getOperator() == ValidationCriteria.OPERATOR_EQUAL_TO && !value.equals(criteria.getValue())) {
                        return criteria;
                    }
                }
            }
        }
    }
    return null;
}
Also used : ValueType(org.hisp.dhis.common.ValueType) ValidationCriteria(org.hisp.dhis.validation.ValidationCriteria) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Date(java.util.Date)

Example 18 with TrackedEntityAttributeValue

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

the class DefaultTrackedEntityInstanceService method createTrackedEntityInstance.

@Override
public int createTrackedEntityInstance(TrackedEntityInstance instance, String representativeId, Integer relationshipTypeId, Set<TrackedEntityAttributeValue> attributeValues) {
    int id = addTrackedEntityInstance(instance);
    for (TrackedEntityAttributeValue pav : attributeValues) {
        attributeValueService.addTrackedEntityAttributeValue(pav);
        instance.getTrackedEntityAttributeValues().add(pav);
    }
    if (representativeId != null) {
        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);
                }
            }
        }
    }
    // Update associations
    updateTrackedEntityInstance(instance);
    return id;
}
Also used : TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Relationship(org.hisp.dhis.relationship.Relationship) RelationshipType(org.hisp.dhis.relationship.RelationshipType)

Example 19 with TrackedEntityAttributeValue

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

the class AbstractEnrollmentService method updateAttributeValues.

private void updateAttributeValues(Enrollment enrollment, ImportOptions importOptions) {
    org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = teiService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
    Map<String, String> attributeValueMap = Maps.newHashMap();
    for (Attribute attribute : enrollment.getAttributes()) {
        attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
    }
    trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> attributeValueMap.containsKey(value.getAttribute().getUid())).forEach(value -> {
        String newValue = attributeValueMap.get(value.getAttribute().getUid());
        value.setValue(newValue);
        trackedEntityAttributeValueService.updateTrackedEntityAttributeValue(value);
        attributeValueMap.remove(value.getAttribute().getUid());
    });
    for (String key : attributeValueMap.keySet()) {
        TrackedEntityAttribute attribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), key);
        if (attribute != null) {
            TrackedEntityAttributeValue value = new TrackedEntityAttributeValue();
            value.setValue(attributeValueMap.get(key));
            value.setAttribute(attribute);
            trackedEntityAttributeValueService.addTrackedEntityAttributeValue(value);
            trackedEntityInstance.addAttributeValue(value);
        }
    }
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) EventService(org.hisp.dhis.dxf2.events.event.EventService) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Coordinate(org.hisp.dhis.dxf2.events.event.Coordinate) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) UserService(org.hisp.dhis.user.UserService) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) Set(java.util.Set) DateUtils(org.hisp.dhis.system.util.DateUtils) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) Event(org.hisp.dhis.dxf2.events.event.Event) List(java.util.List) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) TrackedEntityCommentService(org.hisp.dhis.trackedentitycomment.TrackedEntityCommentService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) IdentifiableObjectCallable(org.hisp.dhis.system.callable.IdentifiableObjectCallable) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) I18nManager(org.hisp.dhis.i18n.I18nManager) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) IdSchemes(org.hisp.dhis.common.IdSchemes) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) IOException(java.io.IOException) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) Maps(com.google.common.collect.Maps) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Note(org.hisp.dhis.dxf2.events.event.Note) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStatus(org.hisp.dhis.program.ProgramStatus) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ProgramService(org.hisp.dhis.program.ProgramService) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)

Example 20 with TrackedEntityAttributeValue

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

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