Skip to content

Commit

Permalink
#96 - build fix. introduce a new module to keep code common to all in…
Browse files Browse the repository at this point in the history
…tegrations.
  • Loading branch information
petmongrels committed Nov 7, 2023
1 parent b5f4f2e commit 266fdcd
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected <T extends AmritBaseResponse> T getSingleEntityResponse(String resourc
throw getRestClientResponseException(responseEntity.getHeaders(), responseEntity.getStatusCode(), null);
}

protected <T extends AmritBaseResponse> T createSingleEntity(String resource, HttpEntity<?> requestEntity,Class<T> returnType) throws RestClientResponseException {
protected <T extends AmritBaseResponse> T createSingleEntity(String resource, HttpEntity<?> requestEntity, Class<T> returnType) throws RestClientResponseException {
logger.info("Request body:" + ObjectJsonMapper.writeValueAsString(requestEntity.getBody()));
URI uri = URI.create(String.format("%s/%s", amritApplicationConfig.getAmritServerUrl(), resource));
ResponseEntity<T> responseEntity = amritRestTemplate.exchange(uri, HttpMethod.POST, requestEntity, returnType);
Expand All @@ -122,7 +122,7 @@ private <T extends AmritBaseResponse> T processResponse(String resource, Respons

protected RestClientException handleError(ResponseEntity<? extends AmritBaseResponse> 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();
}
Expand All @@ -139,7 +139,7 @@ protected void populateObservations(Map<String, Object> 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;
}
Expand All @@ -150,7 +150,7 @@ protected void populateObservations(Map<String, Object> 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()));
Expand All @@ -159,14 +159,14 @@ protected void populateObservations(Map<String, Object> 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<String> answers = (List<String>) answer;
answer = String.join(",", answers);
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@Disabled
public class ObservationMapperExternalTest {
@Autowired
private ObservationMapper observationMapper;
private BahmniModuleObservationMapper observationMapper;

@Autowired
private MappingService mappingService;
Expand Down
48 changes: 48 additions & 0 deletions integration-common/build.gradle
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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());
}
});
}
}
1 change: 1 addition & 0 deletions integrator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 1 addition & 0 deletions lahi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ repositories {
}

dependencies {
implementation project(':integration-common')
implementation project(':integration-data')
implementation project(':glific')
implementation project(':avni')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public String getFlowResultId() {
return flowResult.getFlowResultId();
}

public Map<String, String> getObservations() {
HashMap<String, String> observations = new HashMap<>();
public Map<String, Object> getObservations() {
HashMap<String, Object> observations = new HashMap<>();

PRIMITIVE_OBS_FIELDS.forEach(fieldName -> observations.put(fieldName, getInput(fieldName)));
CODED_OBS_FIELDS.forEach(fieldName -> observations.put(fieldName, getCategory(fieldName)));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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) {
Expand All @@ -38,26 +33,7 @@ public Subject mapToSubject(LahiStudent lahiStudent) {
}

private void populateObservations(ObservationHolder observationHolder, LahiStudent student) {
Map<String, String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
12 changes: 12 additions & 0 deletions lahi/src/test/resources/gcp.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ include 'amrit'
include 'power'
include 'lahi'
include 'glific'
include 'integration-common'

0 comments on commit 266fdcd

Please sign in to comment.