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