use of org.hisp.dhis.trackedentity.TrackedEntityAttribute 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.trackedentity.TrackedEntityAttribute in project dhis2-core by dhis2.
the class HibernateTrackedEntityInstanceStore method validate.
@Override
public String validate(TrackedEntityInstance instance, TrackedEntityAttributeValue attributeValue, Program program) {
TrackedEntityAttribute attribute = attributeValue.getAttribute();
try {
if (attribute.isUnique()) {
Criteria criteria = getCriteria();
criteria.add(Restrictions.ne("id", instance.getId()));
criteria.createAlias("trackedEntityAttributeValues", "attributeValue");
criteria.createAlias("attributeValue.attribute", "attribute");
criteria.add(Restrictions.eq("attributeValue.value", attributeValue.getValue()));
criteria.add(Restrictions.eq("attributeValue.attribute", attribute));
if (attribute.getId() != 0) {
criteria.add(Restrictions.ne("id", attribute.getId()));
}
if (attribute.getOrgunitScope()) {
criteria.add(Restrictions.eq("organisationUnit", instance.getOrganisationUnit()));
}
if (program != null && attribute.getProgramScope()) {
criteria.createAlias("programInstances", "programInstance");
criteria.add(Restrictions.eq("programInstance.program", program));
}
Number rs = (Number) criteria.setProjection(Projections.projectionList().add(Projections.property("attribute.id"))).uniqueResult();
if (rs != null && rs.intValue() > 0) {
return ERROR_DUPLICATE_IDENTIFIER + SEPARATOR + rs.intValue();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of org.hisp.dhis.trackedentity.TrackedEntityAttribute in project dhis2-core by dhis2.
the class DhisConvenienceTest method createProgram.
public static Program createProgram(char uniqueCharacter, Set<ProgramStage> programStages, Set<TrackedEntityAttribute> attributes, Set<OrganisationUnit> organisationUnits, DataElementCategoryCombo categoryCombo) {
Program program = new Program();
program.setAutoFields();
program.setName("Program" + uniqueCharacter);
program.setCode("ProgramCode" + uniqueCharacter);
program.setShortName("ProgramShort" + uniqueCharacter);
program.setDescription("Description" + uniqueCharacter);
program.setEnrollmentDateLabel("DateOfEnrollmentDescription");
program.setIncidentDateLabel("DateOfIncidentDescription");
program.setProgramType(ProgramType.WITH_REGISTRATION);
if (programStages != null) {
for (ProgramStage programStage : programStages) {
programStage.setProgram(program);
program.getProgramStages().add(programStage);
}
}
if (attributes != null) {
for (TrackedEntityAttribute attribute : attributes) {
ProgramTrackedEntityAttribute ptea = new ProgramTrackedEntityAttribute(program, attribute, false, false);
ptea.setAutoFields();
program.getProgramAttributes().add(ptea);
}
}
if (organisationUnits != null) {
program.getOrganisationUnits().addAll(organisationUnits);
}
if (categoryCombo != null) {
program.setCategoryCombo(categoryCombo);
} else if (categoryService != null) {
program.setCategoryCombo(categoryService.getDefaultDataElementCategoryCombo());
}
return program;
}
use of org.hisp.dhis.trackedentity.TrackedEntityAttribute in project dhis2-core by dhis2.
the class AbstractEnrollmentService method checkAttributes.
private List<ImportConflict> checkAttributes(Enrollment enrollment, ImportOptions importOptions) {
List<ImportConflict> importConflicts = new ArrayList<>();
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = teiService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
Map<TrackedEntityAttribute, Boolean> mandatoryMap = Maps.newHashMap();
Map<String, String> attributeValueMap = Maps.newHashMap();
for (ProgramTrackedEntityAttribute programTrackedEntityAttribute : program.getProgramAttributes()) {
mandatoryMap.put(programTrackedEntityAttribute.getAttribute(), programTrackedEntityAttribute.isMandatory());
}
// ignore attributes which do not belong to this program
trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> mandatoryMap.containsKey(value.getAttribute())).forEach(value -> attributeValueMap.put(value.getAttribute().getUid(), value.getValue()));
for (Attribute attribute : enrollment.getAttributes()) {
attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
importConflicts.addAll(validateAttributeType(attribute, importOptions));
}
TrackedEntityInstance instance = trackedEntityInstanceService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
for (TrackedEntityAttribute trackedEntityAttribute : mandatoryMap.keySet()) {
Boolean mandatory = mandatoryMap.get(trackedEntityAttribute);
if (mandatory && !attributeValueMap.containsKey(trackedEntityAttribute.getUid())) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Missing mandatory attribute " + trackedEntityAttribute.getUid()));
continue;
}
if (trackedEntityAttribute.isUnique()) {
OrganisationUnit organisationUnit = manager.get(OrganisationUnit.class, instance.getOrgUnit());
importConflicts.addAll(checkScope(trackedEntityInstance, trackedEntityAttribute, attributeValueMap.get(trackedEntityAttribute.getUid()), organisationUnit, program));
}
attributeValueMap.remove(trackedEntityAttribute.getUid());
}
if (!attributeValueMap.isEmpty()) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Only program attributes is allowed for enrollment " + attributeValueMap));
}
return importConflicts;
}
use of org.hisp.dhis.trackedentity.TrackedEntityAttribute 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);
}
}
}
Aggregations