Skip to content

Commit

Permalink
BAH-439 | Person attributes with coded concept answers displa… (#30)
Browse files Browse the repository at this point in the history
* Bindu | BAH-439 | Person attributes with coded concept answers display concept IDs on registration search

* Bindu | BAH-439 | Changed concept shortName with FSN
  • Loading branch information
binduak authored and angshu committed May 30, 2018
1 parent 4b82757 commit cc9074e
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
import org.openmrs.PersonAttribute;
import org.openmrs.Visit;
import org.openmrs.VisitAttribute;
import org.openmrs.Concept;
import org.openmrs.ConceptName;
import org.openmrs.api.APIException;
import org.openmrs.api.VisitService;
import org.openmrs.api.context.Context;
import org.openmrs.module.bahmniemrapi.encountertransaction.command.impl.BahmniVisitAttributeService;
import org.openmrs.module.bahmniemrapi.visitlocation.BahmniVisitLocationServiceImpl;
import org.openmrs.util.LocaleUtility;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
Expand All @@ -39,7 +43,7 @@ public PatientResponse map(Patient patient, String loginLocationUuid, String[] s

Integer visitLocationId = bahmniVisitLocationService.getVisitLocation(loginLocationUuid).getLocationId();
List<Visit> activeVisitsByPatient = visitService.getActiveVisitsByPatient(patient);

patientResponse = new PatientResponse();
patientResponse.setUuid(patient.getUuid());
patientResponse.setPersonId(patient.getPatientId());
Expand Down Expand Up @@ -80,7 +84,18 @@ private void mapPersonAttributes(Patient patient, List<String> patientSearchResu
String queriedPersonAttributes = patientSearchResultFields.stream()
.map(attributeName -> {
PersonAttribute attribute = patient.getAttribute(attributeName);
return attribute == null ? null : formKeyPair(attributeName, attribute.getValue());
if(attribute != null) {
if("org.openmrs.Concept".equals(attribute.getAttributeType().getFormat())) {
Concept concept = Context.getConceptService().getConcept(attribute.getValue());
ConceptName fullySpecifiedName = concept.getFullySpecifiedName(Context.getLocale());
ConceptName conceptFullySpecifiedName = (fullySpecifiedName == null) ? concept.getFullySpecifiedName(LocaleUtility.getDefaultLocale()) : fullySpecifiedName;
return formKeyPair(attributeName, conceptFullySpecifiedName != null ? conceptFullySpecifiedName.getName() : null);
}
else {
return formKeyPair(attributeName, attribute.getValue());
}
}
return null;
}).filter(Objects::nonNull)
.collect(Collectors.joining(","));
patientResponse.setCustomAttribute(formJsonString(queriedPersonAttributes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.commons.lang3.StringUtils;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import org.openmrs.api.context.Context;

import java.util.HashMap;
import java.util.List;
Expand All @@ -25,7 +26,7 @@ public String selectClause(String select){

if(personAttributeResultsIds.size() > 0)
selectClause =
"concat('{',group_concat(DISTINCT (coalesce(concat('\"',attrt_results.name,'\":\"', REPLACE(REPLACE(pattr_results.value,'\\\\','\\\\\\\\'),'\"','\\\\\"'),'\"'))) SEPARATOR ','),'}')";
"concat('{',group_concat(DISTINCT (coalesce(concat('\"',attrt_results.name,'\":\"', REPLACE(REPLACE(coalesce(cn.name, def_loc_cn.name, pattr_results.value),'\\\\','\\\\\\\\'),'\"','\\\\\"'),'\"'))) SEPARATOR ','),'}')";

return String.format("%s,%s as customAttribute", select, selectClause);
}
Expand All @@ -36,7 +37,10 @@ public String appendToJoinClause(String join){
if(personAttributeResultsIds.size() > 0)
join +=
" LEFT OUTER JOIN person_attribute pattr_results on pattr_results.person_id = p.person_id and pattr_results.person_attribute_type_id in ("+ StringUtils.join(personAttributeResultsIds, ',')+") " +
" LEFT OUTER JOIN person_attribute_type attrt_results on attrt_results.person_attribute_type_id = pattr_results.person_attribute_type_id";
" LEFT OUTER JOIN person_attribute_type attrt_results on attrt_results.person_attribute_type_id = pattr_results.person_attribute_type_id and pattr_results.voided = 0 " +
" LEFT OUTER JOIN concept_name cn on cn.concept_id = pattr_results.value and cn.concept_name_type = 'FULLY_SPECIFIED' and attrt_results.format = 'org.openmrs.Concept' and cn.locale = '"+ Context.getLocale() + "'" +
" LEFT OUTER JOIN concept_name def_loc_cn on def_loc_cn.concept_id = pattr_results.value and def_loc_cn.concept_name_type = 'FULLY_SPECIFIED' and attrt_results.format = 'org.openmrs.Concept' and def_loc_cn.locale = 'en' ";

return join;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.mockito.Mock;
import org.mockito.internal.util.collections.Sets;
import org.openmrs.*;
import org.openmrs.api.ConceptNameType;
import org.openmrs.api.ConceptService;
import org.openmrs.api.VisitService;
import org.openmrs.api.context.Context;
import org.openmrs.module.bahmniemrapi.visitlocation.BahmniVisitLocationServiceImpl;
Expand All @@ -18,8 +20,10 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Context.class)
Expand All @@ -33,6 +37,9 @@ public class PatientResponseMapperTest {
@Mock
BahmniVisitLocationServiceImpl bahmniVisitLocationService;

@Mock
ConceptService conceptService;

Patient patient;

@Before
Expand Down Expand Up @@ -84,6 +91,31 @@ public void shouldMapPersonAttributes() throws Exception {
Assert.assertEquals(patientResponse.getCustomAttribute(),"{\"givenNameLocal\" : \"someName\"}");
}

@Test
public void shouldMapPersonAttributesForConceptType() throws Exception {
PersonAttributeType personAttributeType = new PersonAttributeType();
personAttributeType.setName("occupation");
personAttributeType.setFormat("org.openmrs.Concept");
patient.setAttributes(Sets.newSet(new PersonAttribute(personAttributeType,"100")));
String[] patientResultFields = {"occupation"};
Concept concept = new Concept();
ConceptName conceptName = new ConceptName();
conceptName.setName("FSN");
Locale defaultLocale = new Locale("en", "GB");
conceptName.setLocale(defaultLocale);
concept.setFullySpecifiedName(conceptName);
conceptName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED);
PowerMockito.mockStatic(Context.class);
PowerMockito.when(Context.getLocale()).thenReturn(defaultLocale);

when(Context.getConceptService()).thenReturn(conceptService);
PowerMockito.when(conceptService.getConcept("100")).thenReturn(concept);

PatientResponse patientResponse = patientResponseMapper.map(patient, null, patientResultFields, null, null);

Assert.assertEquals(patientResponse.getCustomAttribute(),"{\"occupation\" : \"FSN\"}");
}

@Test
public void shouldAddSlashToSupportSpecialCharactersInJSON() throws Exception {
PersonAttributeType personAttributeType = new PersonAttributeType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.bahmni.module.bahmnicore.contract.patient.search;

import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;


public class PatientAttributeQueryHelperTest {

@Test
public void codedPersonAttributeShouldReturnConceptName() {
PatientAttributeQueryHelper patientAttributeQueryHelper = new PatientAttributeQueryHelper("", Arrays.asList(new Integer(1), 2), Arrays.asList(1));
String updatedSelectClause = patientAttributeQueryHelper.appendToJoinClause("''");
}

@Test
public void ensureSelectClauseReturnsProperSqlForAttributes() {
PatientAttributeQueryHelper PatientAttributeQueryHelper = new PatientAttributeQueryHelper("", Arrays.asList(new Integer(1), 2), Arrays.asList(1));

String updatedSelectClause = PatientAttributeQueryHelper.selectClause("a");
assertNotNull(updatedSelectClause);
assertEquals("a,concat('{',group_concat(DISTINCT (coalesce(concat('\"',attrt_results.name,'\":\"', REPLACE(REPLACE(coalesce(cn.name, def_loc_cn.name, pattr_results.value),'\\\\','\\\\\\\\'),'\"','\\\\\"'),'\"'))) SEPARATOR ','),'}') as customAttribute", updatedSelectClause);
}

@Test
public void ensureAppendJoinClauseReturnsProperSqlForPersonAttributeConceptTypes() {
PatientAttributeQueryHelper PatientAttributeQueryHelper = new PatientAttributeQueryHelper("", Arrays.asList(new Integer(1), 2), Arrays.asList(1));

String updatedSelectClause = PatientAttributeQueryHelper.appendToJoinClause("a");
assertNotNull(updatedSelectClause);
assertEquals("a LEFT OUTER JOIN person_attribute pattrln on pattrln.person_id = p.person_id and pattrln.person_attribute_type_id in (1,2) LEFT OUTER JOIN person_attribute pattr_results on pattr_results.person_id = p.person_id and pattr_results.person_attribute_type_id in (1) LEFT OUTER JOIN person_attribute_type attrt_results on attrt_results.person_attribute_type_id = pattr_results.person_attribute_type_id and pattr_results.voided = 0 LEFT OUTER JOIN concept_name cn on cn.concept_id = pattr_results.value and cn.concept_name_type = 'FULLY_SPECIFIED' and attrt_results.format = 'org.openmrs.Concept' and cn.locale = 'en_GB' LEFT OUTER JOIN concept_name def_loc_cn on def_loc_cn.concept_id = pattr_results.value and def_loc_cn.concept_name_type = 'FULLY_SPECIFIED' and attrt_results.format = 'org.openmrs.Concept' and def_loc_cn.locale = 'en' ", updatedSelectClause);
}
}
Loading

0 comments on commit cc9074e

Please sign in to comment.