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