From dddd2fac9a23648965ac097fdce52977b894ff08 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Thu, 21 Mar 2024 15:43:21 +0530 Subject: [PATCH] avniproject/avni-product#1574 | Cherry pick avniproject/avni-server#702 --- .../avni/server/dao/ConceptRepository.java | 17 ------- .../avni/server/service/ConceptService.java | 46 +++++++++---------- .../response/ConceptNameUuidAndDatatype.java | 5 ++ .../avni/server/web/response/Response.java | 17 ++----- .../server/web/response/ResponseUnitTest.java | 33 ------------- makefiles/externalDB.mk | 16 +++++-- 6 files changed, 45 insertions(+), 89 deletions(-) diff --git a/avni-server-api/src/main/java/org/avni/server/dao/ConceptRepository.java b/avni-server-api/src/main/java/org/avni/server/dao/ConceptRepository.java index 2a3e76b02..842fc46bc 100644 --- a/avni-server-api/src/main/java/org/avni/server/dao/ConceptRepository.java +++ b/avni-server-api/src/main/java/org/avni/server/dao/ConceptRepository.java @@ -73,23 +73,6 @@ default Concept findByUuid(String uuid) { @Query("select c.name from Concept c where c.isVoided = false") List getAllNames(); List getAllConceptByUuidIn(List uuid); - List getAllConceptByNameIn(List names); - - @Query(value = "SELECT DISTINCT c.uuid, c.name, c.data_type\n" + - " FROM concept c\n" + - " INNER JOIN (\n" + - " SELECT unnest(ARRAY [to_jsonb(key), value]) conecpt_uuid\n" + - " FROM jsonb_each(cast( :observations as jsonb))\n" + - " ) obs ON obs.conecpt_uuid @> to_jsonb(c.uuid)", nativeQuery = true) - @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) - List> getConceptUuidToNameMapList(String observations); - - default List findAllConceptsInObs(String observations) { - return getConceptUuidToNameMapList(observations) - .stream() - .map(resultMap -> new ConceptNameUuidAndDatatype(resultMap.get("uuid"), resultMap.get("name"), ConceptDataType.valueOf(resultMap.get("data_type")))) - .collect(Collectors.toList()); - } Page findAllByUuidIn(String [] uuids, Pageable pageable); List findAllByUuidInAndDataTypeIn(String[] uuids, String[] dataTypes); diff --git a/avni-server-api/src/main/java/org/avni/server/service/ConceptService.java b/avni-server-api/src/main/java/org/avni/server/service/ConceptService.java index dff6cdd07..995be3206 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/ConceptService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/ConceptService.java @@ -16,11 +16,9 @@ import org.avni.server.web.request.ReferenceDataContract; import org.avni.server.web.request.application.ConceptUsageContract; import org.avni.server.web.request.application.FormUsageContract; -import org.avni.server.web.response.ConceptNameUuidAndDatatype; import org.avni.server.web.response.Response; import org.avni.server.web.validation.ValidationException; import org.joda.time.DateTime; -import org.joda.time.Instant; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -114,7 +112,7 @@ private ConceptAnswer fetchOrCreateConceptAnswer(Concept concept, ConceptContrac return conceptAnswer; } - private Concept createCodedConcept(Concept concept, ConceptContract conceptRequest) throws AnswerConceptNotFoundException { + private void createCodedConcept(Concept concept, ConceptContract conceptRequest) throws AnswerConceptNotFoundException { List answers = (List) O.coalesce(conceptRequest.getAnswers(), new ArrayList<>()); AtomicInteger index = new AtomicInteger(0); List conceptAnswers = new ArrayList<>(); @@ -123,7 +121,6 @@ private Concept createCodedConcept(Concept concept, ConceptContract conceptReque conceptAnswers.add(conceptAnswer); } concept.addAll(conceptAnswers); - return concept; } private Concept createNumericConcept(Concept concept, ConceptContract conceptRequest) { @@ -155,10 +152,10 @@ private Concept map(@NotNull ConceptContract conceptRequest) throws AnswerConcep concept.updateAudit(); switch (ConceptDataType.valueOf(impliedDataType)) { case Coded: - concept = createCodedConcept(concept, conceptRequest); + createCodedConcept(concept, conceptRequest); break; case Numeric: - concept = createNumericConcept(concept, conceptRequest); + createNumericConcept(concept, conceptRequest); break; } return concept; @@ -251,8 +248,8 @@ public ConceptAnswer getAnswer(String conceptUUID, String conceptAnswerUUID) { /** * Important: Not to be used in any Internal API calls */ - public Object getObservationValue(ConceptNameUuidAndDatatype concept, Map conceptMap, Object value) { - if (concept.getConceptDataType().equals(ConceptDataType.Date)) { + public Object getObservationValue(Concept questionConcept, Object value) { + if (questionConcept.getDataType().equals(ConceptDataType.Date.toString())) { return ApiRequestContextHolder.isVersionGreaterThan(1)? DateTimeUtil.toDateString((String) value): value; } @@ -260,22 +257,23 @@ public Object getObservationValue(ConceptNameUuidAndDatatype concept, Map elements = (List) value; - return elements.stream().map(element -> { - if (element != null && element instanceof String) { // Multicoded concept - return getNameWithDefaultValue(matchingConcept(conceptMap, element), element); - } else if (element != null && element instanceof HashMap) { // Repeatable question group + List answerElements = (List) value; + return answerElements.stream().map(answersItem -> { + if (answersItem instanceof String) { // Multi coded concept + Concept answerConcept = conceptRepository.findByUuid((String) answersItem); + return getNameWithDefaultValue(answerConcept, answersItem); + } else if (answersItem instanceof HashMap) { // Repeatable question group LinkedHashMap observationResponse = new LinkedHashMap<>(); Response.mapObservations(conceptRepository, this, observationResponse, - new ObservationCollection((HashMap) element)); + new ObservationCollection((HashMap) answersItem)); return observationResponse; } else { - return getNameWithDefaultValue(matchingConcept(conceptMap, element), element); + return answersItem; } }).toArray(); } @@ -286,19 +284,21 @@ public Object getObservationValue(ConceptNameUuidAndDatatype concept, Map conceptMap, Object element) { - return conceptMap.get(element); + return value; } - private Object getNameWithDefaultValue(ConceptNameUuidAndDatatype value, Object defaultValue) { - return value == null ? defaultValue : value.getName(); + private Object getNameWithDefaultValue(Concept answerConcept, Object obsRawValue) { + return answerConcept == null ? obsRawValue : answerConcept.getName(); } private Object checkAndReturnLocationAddress(Object value) { diff --git a/avni-server-api/src/main/java/org/avni/server/web/response/ConceptNameUuidAndDatatype.java b/avni-server-api/src/main/java/org/avni/server/web/response/ConceptNameUuidAndDatatype.java index 61180cffc..b396f0f5e 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/response/ConceptNameUuidAndDatatype.java +++ b/avni-server-api/src/main/java/org/avni/server/web/response/ConceptNameUuidAndDatatype.java @@ -1,5 +1,6 @@ package org.avni.server.web.response; +import org.avni.server.domain.Concept; import org.avni.server.domain.ConceptDataType; public class ConceptNameUuidAndDatatype { @@ -13,6 +14,10 @@ public ConceptNameUuidAndDatatype(String uuid, String name, ConceptDataType conc this.conceptDataType = conceptDataType; } + public ConceptNameUuidAndDatatype(Concept concept) { + this(concept.getUuid(), concept.getName(), ConceptDataType.valueOf(concept.getDataType())); + } + public String getUuid() { return uuid; } diff --git a/avni-server-api/src/main/java/org/avni/server/web/response/Response.java b/avni-server-api/src/main/java/org/avni/server/web/response/Response.java index 9f7f11124..b2afc355d 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/response/Response.java +++ b/avni-server-api/src/main/java/org/avni/server/web/response/Response.java @@ -2,9 +2,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import org.avni.server.dao.ConceptRepository; -import org.avni.server.domain.CHSBaseEntity; -import org.avni.server.domain.CHSEntity; -import org.avni.server.domain.ObservationCollection; +import org.avni.server.domain.*; import org.avni.server.service.ConceptService; import org.avni.server.util.ObjectMapperSingleton; @@ -24,15 +22,10 @@ public static void putObservations(ConceptRepository conceptRepository, ConceptS public static void mapObservations(ConceptRepository conceptRepository, ConceptService conceptService, Map observationsResponse, ObservationCollection observations) { ObservationCollection obs = Optional.ofNullable(observations).orElse(new ObservationCollection()); - String stringObservations; - try { - stringObservations = ObjectMapperSingleton.getObjectMapper().writeValueAsString(obs); - } catch (JsonProcessingException e) { - throw new RuntimeException(String.format("Error while processing observation %s", obs.toString())); - } - List conceptMaps = conceptRepository.findAllConceptsInObs(stringObservations); - Map conceptMap = conceptMaps.stream().collect(Collectors.toMap(s -> s.getUuid(), s -> s)); - obs.forEach((key, value) -> observationsResponse.put(conceptMap.get(key).getName(), conceptService.getObservationValue(conceptMap.get(key), conceptMap, value))); + obs.forEach((key, value) -> { + Concept concept = conceptRepository.findByUuid(key); + observationsResponse.put(concept.getName(), conceptService.getObservationValue(concept, value)); + }); } static void putObservations(ConceptRepository conceptRepository, ConceptService conceptService, Map parentMap, LinkedHashMap observationsResponse, ObservationCollection observations) { diff --git a/avni-server-api/src/test/java/org/avni/server/web/response/ResponseUnitTest.java b/avni-server-api/src/test/java/org/avni/server/web/response/ResponseUnitTest.java index 66a30d816..61d3059ff 100644 --- a/avni-server-api/src/test/java/org/avni/server/web/response/ResponseUnitTest.java +++ b/avni-server-api/src/test/java/org/avni/server/web/response/ResponseUnitTest.java @@ -54,37 +54,4 @@ public void shouldNotAlterExistingObsWhenPassedEmptyObservations() throws Except assertThat(observations.size(), is(1)); assertThat(observations.get("First Name"), is("Test")); } - - @Test() - public void shouldAddObservationsWhenPassedObsAreNotEmpty() { - String questionConceptName = "ABC"; - String answerValue = "XYZ"; - LinkedHashMap parentMap = new LinkedHashMap<>(); - LinkedHashMap observationsResponse = new LinkedHashMap<>(); - observationsResponse.put("First Name", "Test"); - ObservationCollection observations = new ObservationCollection(); - String questionConceptUuid = "55f3e0cc-a9bc-45d6-a42c-a4fd3d90465f"; - String answerConceptUuid = "a33da2f8-7329-4e2a-8c27-046ee4082524"; - Concept questionConcept = new Concept(); - questionConcept.setName(questionConceptName); - Concept answerConcept = new Concept(); - answerConcept.setUuid(answerConceptUuid); - answerConcept.setName(answerValue); - ConceptAnswer conceptAnswer = new ConceptAnswer(); - conceptAnswer.setConcept(answerConcept); - Set answers = new HashSet<>(); - answers.add(conceptAnswer); - questionConcept.setConceptAnswers(answers); - observations.put(questionConceptUuid, answerConceptUuid); - ConceptNameUuidAndDatatype conceptMap1 = new ConceptNameUuidAndDatatype(questionConceptUuid, questionConceptName, ConceptDataType.Coded); - ConceptNameUuidAndDatatype conceptMap2 = new ConceptNameUuidAndDatatype(answerConceptUuid, answerValue, ConceptDataType.NA); - List conceptMapList = Arrays.asList(conceptMap1, conceptMap2); - when(conceptRepository.findAllConceptsInObs(anyString())).thenReturn(conceptMapList); - when(conceptService.getObservationValue(any(), anyMap(), anyString())).thenReturn(answerValue); - Response.putObservations(conceptRepository, conceptService, parentMap, observationsResponse, observations); - LinkedHashMap result = (LinkedHashMap) parentMap.get("observations"); - - assertThat(result.get("First Name"), is("Test")); - assertThat(result.get(questionConceptName), is(answerValue)); - } } diff --git a/makefiles/externalDB.mk b/makefiles/externalDB.mk index 2b7e3c630..6c823bc9a 100644 --- a/makefiles/externalDB.mk +++ b/makefiles/externalDB.mk @@ -115,7 +115,15 @@ endif --exclude-table-data='scheduled_job_run' \ --exclude-table-data='qrtz_*' \ --exclude-table-data='batch_*' \ - --exclude-table-data='public.individual_copy' \ - --exclude-table-data='public.program_enrolment_copy' \ - --exclude-table-data='public.encounter_copy' \ - --exclude-table-data='public.program_encounter_copy' + --exclude-table='public.individual_copy' \ + --exclude-table='public.program_enrolment_copy' \ + --exclude-table='public.encounter_copy' \ + --exclude-table='public.program_encounter_copy' \ + --exclude-table='public.individual_copy_ck' \ + --exclude-table='public.program_enrolment_ck' \ + --exclude-table='public.encounter_ck' \ + --exclude-table='public.program_encounter_ck' \ + --exclude-table='public.individual_copy_ihmp' \ + --exclude-table='public.program_enrolment_ihmp' \ + --exclude-table-data='public.individual_02_24' \ + --exclude-table-data='public.program_enrolment_02_24'