diff --git a/amrit/src/main/java/org/avni_integration_service/amrit/repository/AmritBaseRepository.java b/amrit/src/main/java/org/avni_integration_service/amrit/repository/AmritBaseRepository.java index 15ef76df..d94da76e 100644 --- a/amrit/src/main/java/org/avni_integration_service/amrit/repository/AmritBaseRepository.java +++ b/amrit/src/main/java/org/avni_integration_service/amrit/repository/AmritBaseRepository.java @@ -99,7 +99,7 @@ protected T getSingleEntityResponse(String resourc throw getRestClientResponseException(responseEntity.getHeaders(), responseEntity.getStatusCode(), null); } - protected T createSingleEntity(String resource, HttpEntity requestEntity,Class returnType) throws RestClientResponseException { + protected T createSingleEntity(String resource, HttpEntity requestEntity, Class returnType) throws RestClientResponseException { logger.info("Request body:" + ObjectJsonMapper.writeValueAsString(requestEntity.getBody())); URI uri = URI.create(String.format("%s/%s", amritApplicationConfig.getAmritServerUrl(), resource)); ResponseEntity responseEntity = amritRestTemplate.exchange(uri, HttpMethod.POST, requestEntity, returnType); @@ -122,7 +122,7 @@ private T processResponse(String resource, Respons protected RestClientException handleError(ResponseEntity responseEntity, HttpStatus statusCode) { AmritBaseResponse responseBody = responseEntity.getBody(); - if(statusCode.equals(HttpStatus.BAD_REQUEST) && responseBody.getErrorMessage() + if (statusCode.equals(HttpStatus.BAD_REQUEST) && responseBody.getErrorMessage() .equalsIgnoreCase(INVALID_LOGIN_KEY_OR_SESSION_IS_EXPIRED)) { amritTokenService.loginAndGenerateToken(); } @@ -139,7 +139,7 @@ protected void populateObservations(Map observationHolder, AvniB for (MappingMetaData amritField : amritFields) { MappingMetaData mapping = mappingMetaDataRepository .getAvniMappingIfPresent(mappingGroup, mappingType, amritField.getIntSystemValue(), integrationSystem); - if(mapping == null) { + if (mapping == null) { logger.warn("Mapping entry not found for amrit field: " + amritField.getIntSystemValue()); continue; } @@ -150,7 +150,7 @@ protected void populateObservations(Map observationHolder, AvniB } else if (dataTypeHint == ObsDataType.Coded && getValue(avniEntity, obsField) != null) { MappingMetaData answerMapping = mappingMetaDataRepository.getIntSystemMappingIfPresent(mappingGroup, mappingTypeCodedObservations, getValue(avniEntity, obsField).toString(), integrationSystem); - if(answerMapping != null) { + if (answerMapping != null) { observationHolder.put(mapping.getIntSystemValue(), answerMapping.getIntSystemValue()); } else { logger.error(String.format("Unable to find coded mapping for attribute %s", mapping.getIntSystemValue())); @@ -159,14 +159,14 @@ protected void populateObservations(Map observationHolder, AvniB //Fetch corresponding ID from group MAPPING_GROUP_MASTER_IDS for the same mappingType MappingMetaData answerMapping = mappingMetaDataRepository.getIntSystemMappingIfPresent(MAPPING_GROUP_MASTER_IDS, mapping.getIntSystemValue(), getValue(avniEntity, obsField).toString(), integrationSystem); - if(answerMapping != null) { + if (answerMapping != null) { observationHolder.put(mapping.getIntSystemValue(), answerMapping.getIntSystemValue()); } else { logger.error(String.format("Unable to find numeric mapping for attribute %s", mapping.getIntSystemValue())); } } else if (dataTypeHint == ObsDataType.Text && getValue(avniEntity, obsField) != null) { Object answer = getValue(avniEntity, obsField); - if(answer instanceof List) { + if (answer instanceof List) { //Convert string array to single string List answers = (List) answer; answer = String.join(",", answers); @@ -215,7 +215,7 @@ public Object deleteEvent(String resourceType, GeneralEncounter encounter) { public AmritFetchIdentityResponse getAmritId(String individualUUID) { return getSingleEntityResponse(amritApplicationConfig.getIdentityApiPrefix() + FETCH_AMRIT_ID_RESOURCE_PATH, - new HttpEntity<>(new String[] {individualUUID}), AmritFetchIdentityResponse.class); + new HttpEntity<>(new String[]{individualUUID}), AmritFetchIdentityResponse.class); } } diff --git a/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/ObservationMapper.java b/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/BahmniModuleObservationMapper.java similarity index 98% rename from bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/ObservationMapper.java rename to bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/BahmniModuleObservationMapper.java index f8ff31be..3386f3ae 100644 --- a/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/ObservationMapper.java +++ b/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/BahmniModuleObservationMapper.java @@ -15,13 +15,13 @@ import static org.avni_integration_service.bahmni.contract.OpenMRSSaveObservation.createVoidedObs; @Component -public class ObservationMapper { +public class BahmniModuleObservationMapper { private final MappingService mappingService; private final BahmniMappingGroup bahmniMappingGroup; private final BahmniMappingType bahmniMappingType; @Autowired - public ObservationMapper(MappingService mappingService, BahmniMappingGroup bahmniMappingGroup, BahmniMappingType bahmniMappingType) { + public BahmniModuleObservationMapper(MappingService mappingService, BahmniMappingGroup bahmniMappingGroup, BahmniMappingType bahmniMappingType) { this.mappingService = mappingService; this.bahmniMappingGroup = bahmniMappingGroup; this.bahmniMappingType = bahmniMappingType; diff --git a/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/EncounterMapper.java b/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/EncounterMapper.java index 1a64140f..a8c7d76a 100644 --- a/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/EncounterMapper.java +++ b/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/EncounterMapper.java @@ -18,11 +18,11 @@ @Component public class EncounterMapper { private final MappingService mappingService; - private final ObservationMapper observationMapper; + private final BahmniModuleObservationMapper observationMapper; private final BahmniMappingGroup bahmniMappingGroup; private final BahmniMappingType bahmniMappingType; - public EncounterMapper(MappingService mappingService, ObservationMapper observationMapper, + public EncounterMapper(MappingService mappingService, BahmniModuleObservationMapper observationMapper, BahmniMappingGroup bahmniMappingGroup, BahmniMappingType bahmniMappingType) { this.mappingService = mappingService; this.observationMapper = observationMapper; diff --git a/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/EnrolmentMapper.java b/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/EnrolmentMapper.java index bbe94311..004581cd 100644 --- a/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/EnrolmentMapper.java +++ b/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/EnrolmentMapper.java @@ -17,13 +17,13 @@ @Component public class EnrolmentMapper { - private final ObservationMapper observationMapper; + private final BahmniModuleObservationMapper observationMapper; private final MappingService mappingService; private final BahmniMappingGroup bahmniMappingGroup; private final BahmniMappingType bahmniMappingType; @Autowired - public EnrolmentMapper(MappingService mappingService, ObservationMapper observationMapper, + public EnrolmentMapper(MappingService mappingService, BahmniModuleObservationMapper observationMapper, BahmniMappingGroup bahmniMappingGroup, BahmniMappingType bahmniMappingType) { this.mappingService = mappingService; this.observationMapper = observationMapper; diff --git a/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/SubjectMapper.java b/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/SubjectMapper.java index d10650d4..076c9239 100644 --- a/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/SubjectMapper.java +++ b/bahmni/src/main/java/org/avni_integration_service/bahmni/mapper/avni/SubjectMapper.java @@ -17,12 +17,12 @@ @Component public class SubjectMapper { private final MappingService mappingService; - private final ObservationMapper observationMapper; + private final BahmniModuleObservationMapper observationMapper; private final BahmniMappingGroup bahmniMappingGroup; private final BahmniMappingType bahmniMappingType; @Autowired - public SubjectMapper(MappingService mappingService, ObservationMapper observationMapper, - BahmniMappingGroup bahmniMappingGroup, BahmniMappingType bahmniMappingType) { + public SubjectMapper(MappingService mappingService, BahmniModuleObservationMapper observationMapper, + BahmniMappingGroup bahmniMappingGroup, BahmniMappingType bahmniMappingType) { this.mappingService = mappingService; this.observationMapper = observationMapper; this.bahmniMappingGroup = bahmniMappingGroup; diff --git a/bahmni/src/test/java/org/avni_integration_service/bahmni/mapper/avni/ObservationMapperExternalTest.java b/bahmni/src/test/java/org/avni_integration_service/bahmni/mapper/avni/ObservationMapperExternalTest.java index 8f22c3e1..f7a054c1 100644 --- a/bahmni/src/test/java/org/avni_integration_service/bahmni/mapper/avni/ObservationMapperExternalTest.java +++ b/bahmni/src/test/java/org/avni_integration_service/bahmni/mapper/avni/ObservationMapperExternalTest.java @@ -23,7 +23,7 @@ @Disabled public class ObservationMapperExternalTest { @Autowired - private ObservationMapper observationMapper; + private BahmniModuleObservationMapper observationMapper; @Autowired private MappingService mappingService; diff --git a/integration-common/build.gradle b/integration-common/build.gradle new file mode 100644 index 00000000..493683c2 --- /dev/null +++ b/integration-common/build.gradle @@ -0,0 +1,48 @@ +plugins { + id 'org.springframework.boot' version '2.5.6' + id 'io.spring.dependency-management' version '1.0.10.RELEASE' + id 'java' +} + +group = 'org.avni_integration_service' +version = '0.0.2-SNAPSHOT' +sourceCompatibility = "17" + +bootJar { + enabled = false +} + +jar { + enabled = true +} + +tasks.withType(JavaCompile).all { + options.compilerArgs += ['--enable-preview'] +} + +tasks.withType(Test).all { + jvmArgs += '--enable-preview' +} + +tasks.withType(JavaExec) { + jvmArgs += '--enable-preview' +} + +repositories { + mavenCentral() +} + +dependencies { + implementation project(':util') + implementation project(':avni') + implementation project(':integration-data') + implementation 'org.springframework.boot:spring-boot-starter' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation "log4j:log4j:1.2.17" + testImplementation 'org.springframework.boot:spring-boot-starter-test' +} + +test { + useJUnitPlatform() +} +targetCompatibility = JavaVersion.VERSION_17 diff --git a/integration-common/src/main/java/org/avni_integration_service/common/ObservationMapper.java b/integration-common/src/main/java/org/avni_integration_service/common/ObservationMapper.java new file mode 100644 index 00000000..c13baeab --- /dev/null +++ b/integration-common/src/main/java/org/avni_integration_service/common/ObservationMapper.java @@ -0,0 +1,45 @@ +package org.avni_integration_service.common; + +import org.avni_integration_service.avni.domain.ObservationHolder; +import org.avni_integration_service.integration_data.domain.MappingMetaData; +import org.avni_integration_service.integration_data.domain.framework.MappingException; +import org.avni_integration_service.integration_data.repository.MappingMetaDataRepository; +import org.avni_integration_service.util.ObsDataType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.apache.log4j.Logger; + +import java.util.Map; + +@Component +public class ObservationMapper { + private static final Logger logger = Logger.getLogger(ObservationMapper.class); + private final MappingMetaDataRepository mappingMetaDataRepository; + + @Autowired + public ObservationMapper(MappingMetaDataRepository mappingMetaDataRepository) { + this.mappingMetaDataRepository = mappingMetaDataRepository; + } + + public void mapObservations(ObservationHolder observationHolder, Map externalSystemObservations, String mappingGroupName, String mappingTypeName) { + externalSystemObservations.forEach((key, value) -> { + MappingMetaData mapping = mappingMetaDataRepository.getAvniMappingIfPresent(mappingGroupName, mappingTypeName, key); + if (mapping == null) { + logger.error("Mapping entry not found for observation field: " + key); + return; + } + ObsDataType dataTypeHint = mapping.getDataTypeHint(); + if (dataTypeHint == null) + observationHolder.addObservation(mapping.getAvniValue(), value); + else if (dataTypeHint == ObsDataType.Coded && value != null) { + MappingMetaData answerMapping = mappingMetaDataRepository.getAvniMappingIfPresent(mappingGroupName, mappingTypeName, (String) value); + if (answerMapping == null) { + String errorMessage = "Answer Mapping entry not found for coded concept answer field: " + value; + logger.error(errorMessage); + throw new MappingException(errorMessage); + } + observationHolder.addObservation(mapping.getAvniValue(), answerMapping.getAvniValue()); + } + }); + } +} diff --git a/integrator/build.gradle b/integrator/build.gradle index 8d4373f1..f594ea18 100644 --- a/integrator/build.gradle +++ b/integrator/build.gradle @@ -35,6 +35,7 @@ dependencies { implementation project(':util') implementation project(':avni') implementation project(':integration-data') + implementation project(':integration-common') implementation project(':bahmni') implementation project(':goonj') implementation project(':power') diff --git a/integrator/src/test/java/org/avni_integration_service/AvniIntegrationApplicationTests.java b/integrator/src/test/java/org/avni_integration_service/AvniIntegrationApplicationTests.java index c739b2e7..24ad8b09 100644 --- a/integrator/src/test/java/org/avni_integration_service/AvniIntegrationApplicationTests.java +++ b/integrator/src/test/java/org/avni_integration_service/AvniIntegrationApplicationTests.java @@ -1,5 +1,6 @@ package org.avni_integration_service; +import org.avni_integration_service.common.ObservationMapper; import org.avni_integration_service.web.ErrorRecordLogController; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; diff --git a/lahi/build.gradle b/lahi/build.gradle index fc21e301..acb774f7 100644 --- a/lahi/build.gradle +++ b/lahi/build.gradle @@ -32,6 +32,7 @@ repositories { } dependencies { + implementation project(':integration-common') implementation project(':integration-data') implementation project(':glific') implementation project(':avni') diff --git a/lahi/src/main/java/org/avni_integration_service/lahi/domain/LahiStudent.java b/lahi/src/main/java/org/avni_integration_service/lahi/domain/LahiStudent.java index dc4e83e3..b7d48486 100644 --- a/lahi/src/main/java/org/avni_integration_service/lahi/domain/LahiStudent.java +++ b/lahi/src/main/java/org/avni_integration_service/lahi/domain/LahiStudent.java @@ -70,8 +70,8 @@ public String getFlowResultId() { return flowResult.getFlowResultId(); } - public Map getObservations() { - HashMap observations = new HashMap<>(); + public Map getObservations() { + HashMap observations = new HashMap<>(); PRIMITIVE_OBS_FIELDS.forEach(fieldName -> observations.put(fieldName, getInput(fieldName))); CODED_OBS_FIELDS.forEach(fieldName -> observations.put(fieldName, getCategory(fieldName))); diff --git a/lahi/src/main/java/org/avni_integration_service/lahi/service/StudentMapper.java b/lahi/src/main/java/org/avni_integration_service/lahi/service/StudentMapper.java index 21cd4721..f4d0a2e4 100644 --- a/lahi/src/main/java/org/avni_integration_service/lahi/service/StudentMapper.java +++ b/lahi/src/main/java/org/avni_integration_service/lahi/service/StudentMapper.java @@ -1,17 +1,13 @@ package org.avni_integration_service.lahi.service; -import org.apache.log4j.Logger; import org.apache.logging.log4j.util.Strings; import org.avni_integration_service.avni.domain.ObservationHolder; import org.avni_integration_service.avni.domain.Subject; -import org.avni_integration_service.integration_data.domain.MappingMetaData; -import org.avni_integration_service.integration_data.domain.framework.MappingException; -import org.avni_integration_service.integration_data.repository.MappingMetaDataRepository; import org.avni_integration_service.lahi.config.LahiMappingDbConstants; import org.avni_integration_service.lahi.domain.LahiStudentConstants; import org.avni_integration_service.lahi.domain.LahiStudent; import org.avni_integration_service.lahi.util.DateTimeUtil; -import org.avni_integration_service.util.ObsDataType; +import org.avni_integration_service.common.ObservationMapper; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -20,11 +16,10 @@ @Service public class StudentMapper implements LahiStudentConstants { - private static final Logger logger = Logger.getLogger(StudentMapper.class); - private final MappingMetaDataRepository mappingMetaDataRepository; + private final ObservationMapper observationMapper; - public StudentMapper(MappingMetaDataRepository mappingMetaDataRepository) { - this.mappingMetaDataRepository = mappingMetaDataRepository; + public StudentMapper(ObservationMapper observationMapper) { + this.observationMapper = observationMapper; } public Subject mapToSubject(LahiStudent lahiStudent) { @@ -38,26 +33,7 @@ public Subject mapToSubject(LahiStudent lahiStudent) { } private void populateObservations(ObservationHolder observationHolder, LahiStudent student) { - Map observationFields = student.getObservations(); - observationFields.forEach((key, value) -> { - MappingMetaData mapping = mappingMetaDataRepository.getAvniMappingIfPresent(LahiMappingDbConstants.MAPPING_GROUP_STUDENT, LahiMappingDbConstants.MAPPING_TYPE_OBS, key); - if (mapping == null) { - logger.error("Mapping entry not found for observation field: " + key); - return; - } - ObsDataType dataTypeHint = mapping.getDataTypeHint(); - if (dataTypeHint == null) - observationHolder.addObservation(mapping.getAvniValue(), value); - else if (dataTypeHint == ObsDataType.Coded && value != null) { - MappingMetaData answerMapping = mappingMetaDataRepository.getAvniMappingIfPresent(LahiMappingDbConstants.MAPPING_GROUP_STUDENT, LahiMappingDbConstants.MAPPING_TYPE_OBS, value); - if (answerMapping == null) { - String errorMessage = "Answer Mapping entry not found for coded concept answer field: " + value; - logger.error(errorMessage); - throw new MappingException(errorMessage); - } - observationHolder.addObservation(mapping.getAvniValue(), answerMapping.getAvniValue()); - } - }); + observationMapper.mapObservations(observationHolder, student.getObservations(), LahiMappingDbConstants.MAPPING_GROUP_STUDENT, LahiMappingDbConstants.MAPPING_TYPE_OBS); } private void setOtherAddress(Subject subject, LahiStudent student) { diff --git a/lahi/src/test/java/org/avni_integration_service/lahi/BaseLahiSpringTest.java b/lahi/src/test/java/org/avni_integration_service/lahi/BaseLahiSpringTest.java index 9a4c340d..39ffd797 100644 --- a/lahi/src/test/java/org/avni_integration_service/lahi/BaseLahiSpringTest.java +++ b/lahi/src/test/java/org/avni_integration_service/lahi/BaseLahiSpringTest.java @@ -8,6 +8,6 @@ @EnableJpaRepositories(basePackages = {"org.avni_integration_service.lahi", "org.avni_integration_service.integration_data", "org.avni_integration_service.avni"}) @EntityScan(basePackages = {"org.avni_integration_service.lahi", "org.avni_integration_service.integration_data", "org.avni_integration_service.avni"}) @EnableAutoConfiguration -@ComponentScan(basePackages = {"org.avni_integration_service.lahi", "org.avni_integration_service.integration_data", "org.avni_integration_service.avni", "org.avni_integration_service.util"}) +@ComponentScan(basePackages = {"org.avni_integration_service.lahi", "org.avni_integration_service.integration_data", "org.avni_integration_service.common", "org.avni_integration_service.avni", "org.avni_integration_service.util"}) public abstract class BaseLahiSpringTest { } diff --git a/lahi/src/test/resources/gcp.json b/lahi/src/test/resources/gcp.json new file mode 100644 index 00000000..d19e1ff1 --- /dev/null +++ b/lahi/src/test/resources/gcp.json @@ -0,0 +1,12 @@ +{ + "type": "service_account", + "project_id": "big-query-integration-poc", + "private_key_id": "dummy_private_key_id", + "private_key": "-----BEGIN PRIVATE KEY-----\nMIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqPfgaTEWEP3S9w0t\ngsicURfo+nLW09/0KfOPinhYZ4ouzU+3xC4pSlEp8Ut9FgL0AgqNslNaK34Kq+NZ\njO9DAQIDAQABAkAgkuLEHLaqkWhLgNKagSajeobLS3rPT0Agm0f7k55FXVt743hw\nNgkp98bMNrzy9AQ1mJGbQZGrpr4c8ZAx3aRNAiEAoxK/MgGeeLui385KJ7ZOYktj\nhLBNAB69fKwTZFsUNh0CIQEJQRpFCcydunv2bENcN/oBTRw39E8GNv2pIcNxZkcb\nNQIgbYSzn3Py6AasNj6nEtCfB+i1p3F35TK/87DlPSrmAgkCIQDJLhFoj1gbwRbH\n/bDRPrtlRUDDx44wHoEhSDRdy77eiQIgE6z/k6I+ChN1LLttwX0galITxmAYrOBh\nBVl433tgTTQ=\n-----END PRIVATE KEY-----", + "client_email": "dummy email id", + "client_id": "dummy client id", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token", + "auth_provider_x509_cert_url": "dummy auth provider cert url", + "client_x509_cert_url": "dummy cert url" +} diff --git a/settings.gradle b/settings.gradle index a2546dc7..afd37032 100644 --- a/settings.gradle +++ b/settings.gradle @@ -10,3 +10,4 @@ include 'amrit' include 'power' include 'lahi' include 'glific' +include 'integration-common'