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;
}
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;
}
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;
}
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);
}
}
}
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;
}
Aggregations