diff --git a/src/main/java/org/avniproject/etl/domain/metadata/SchemaMetadata.java b/src/main/java/org/avniproject/etl/domain/metadata/SchemaMetadata.java index cfd6438..7a61966 100644 --- a/src/main/java/org/avniproject/etl/domain/metadata/SchemaMetadata.java +++ b/src/main/java/org/avniproject/etl/domain/metadata/SchemaMetadata.java @@ -88,6 +88,11 @@ public List getAllEncounterTables() { return tableMetadata.stream().filter(table -> table.getType() == TableMetadata.Type.Encounter).toList(); } + public List getAllIndividualAndProgramEncounterTables() { + return tableMetadata.stream().filter(table -> table.getType() == TableMetadata.Type.Encounter + || table.getType() == TableMetadata.Type.ProgramEncounter).toList(); + } + public List getAllEncounterTableNames() { List encounterTables = getAllEncounterTables(); List encounterTableNames = new ArrayList<>(); diff --git a/src/main/java/org/avniproject/etl/repository/sync/CleanEncounterCancelTableAction.java b/src/main/java/org/avniproject/etl/repository/sync/CleanEncounterCancelTableAction.java index 914d567..d04fe62 100644 --- a/src/main/java/org/avniproject/etl/repository/sync/CleanEncounterCancelTableAction.java +++ b/src/main/java/org/avniproject/etl/repository/sync/CleanEncounterCancelTableAction.java @@ -12,6 +12,8 @@ import java.util.Arrays; import java.util.Date; +import java.util.Objects; +import java.util.Optional; import static org.avniproject.etl.repository.JdbcContextWrapper.runInOrgContext; @@ -29,13 +31,13 @@ public void perform(TableMetadata tableMetadata, Date lastSyncTime, Date dataSyn if (this.doesntSupport(tableMetadata)) { return; } - cleanUncancelledEncounters(tableMetadata); + cleanUncancelledEncounters(tableMetadata, currentSchemaMetadata); } - private void cleanUncancelledEncounters(TableMetadata tableMetadata) { + private void cleanUncancelledEncounters(TableMetadata tableMetadata, SchemaMetadata currentSchemaMetadata) { String schema = OrgIdentityContextHolder.getDbSchema(); String encounterCancelTableName = tableMetadata.getName(); - String primaryTableName = encounterCancelTableName.substring(0, encounterCancelTableName.length() - 7); + String primaryTableName = getPrimaryTableName(tableMetadata, currentSchemaMetadata); String sql = new ST(deleteUncancelledEncountersSqlTemplate) .add("schemaName", wrapInQuotes(schema)) .add("encounterCancelTableName", wrapInQuotes(encounterCancelTableName)) @@ -47,6 +49,18 @@ private void cleanUncancelledEncounters(TableMetadata tableMetadata) { }, jdbcTemplate); } + private String getPrimaryTableName(TableMetadata tableMetadata, SchemaMetadata currentSchemaMetadata) { + Optional primaryTableMetadata = currentSchemaMetadata.getAllIndividualAndProgramEncounterTables() + .stream().filter(tm -> tm.getEncounterTypeUuid().equals(tableMetadata.getEncounterTypeUuid()) + && (Objects.isNull(tm.getProgramUuid()) || (Objects.nonNull(tableMetadata.getProgramUuid()) && tableMetadata.getProgramUuid().equals(tm.getProgramUuid()))) + && tm.getSubjectTypeUuid().equals(tableMetadata.getSubjectTypeUuid())).findAny(); + + if (primaryTableMetadata.isPresent()) { + return primaryTableMetadata.get().getName(); + } + throw new RuntimeException(String.format("Corresponding Primary table not found for cancel table: %s", tableMetadata.getName())); + } + private String wrapInQuotes(String parameter) { return parameter == null ? "null" : "\"" + parameter + "\""; } diff --git a/src/main/java/org/avniproject/etl/repository/sync/CleanEnrolmentExitTableAction.java b/src/main/java/org/avniproject/etl/repository/sync/CleanEnrolmentExitTableAction.java index 69c89d3..93f5c32 100644 --- a/src/main/java/org/avniproject/etl/repository/sync/CleanEnrolmentExitTableAction.java +++ b/src/main/java/org/avniproject/etl/repository/sync/CleanEnrolmentExitTableAction.java @@ -11,6 +11,7 @@ import org.stringtemplate.v4.ST; import java.util.Date; +import java.util.Optional; import static org.avniproject.etl.repository.JdbcContextWrapper.runInOrgContext; @@ -28,13 +29,13 @@ public void perform(TableMetadata tableMetadata, Date lastSyncTime, Date dataSyn if (this.doesntSupport(tableMetadata)) { return; } - cleanInvalidExits(tableMetadata); + cleanInvalidExits(tableMetadata, currentSchemaMetadata); } - private void cleanInvalidExits(TableMetadata tableMetadata) { + private void cleanInvalidExits(TableMetadata tableMetadata, SchemaMetadata currentSchemaMetadata) { String schema = OrgIdentityContextHolder.getDbSchema(); String exitTableName = tableMetadata.getName(); - String primaryTableName = exitTableName.substring(0, exitTableName.length() - 5); + String primaryTableName = getPrimaryTableName(tableMetadata, currentSchemaMetadata); String sql = new ST(deleteInvalidExitsSqlTemplate) .add("schemaName", wrapInQuotes(schema)) .add("exitTableName", wrapInQuotes(exitTableName)) @@ -46,6 +47,18 @@ private void cleanInvalidExits(TableMetadata tableMetadata) { }, jdbcTemplate); } + + private String getPrimaryTableName(TableMetadata tableMetadata, SchemaMetadata currentSchemaMetadata) { + Optional primaryTableMetadata = currentSchemaMetadata.getAllProgramEnrolmentTables() + .stream().filter(tm -> tm.getProgramUuid().equals(tableMetadata.getProgramUuid()) + && tm.getSubjectTypeUuid().equals(tableMetadata.getSubjectTypeUuid())).findAny(); + + if (primaryTableMetadata.isPresent()) { + return primaryTableMetadata.get().getName(); + } + throw new RuntimeException(String.format("Corresponding Primary table not found for cancel table: %s", tableMetadata.getName())); + } + private String wrapInQuotes(String parameter) { return parameter == null ? "null" : "\"" + parameter + "\""; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f4ef015..14cc23d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,6 @@ avni.database=${OPENCHS_DATABASE_NAME:openchs} +avni.database.port=${OPENCHS_DATABASE_PORT:5432} +avni.database.server=${OPENCHS_DATABASE_SERVER:localhost} spring.config.import=classpath:/main-application.properties debug=${ETL_DEBUG_MODE:false} avni.current.time.offset.seconds=${AVNI_CURRENT_TIME_OFFSET_SECONDS:10} diff --git a/src/main/resources/main-application.properties b/src/main/resources/main-application.properties index aade73e..1d9c5c1 100644 --- a/src/main/resources/main-application.properties +++ b/src/main/resources/main-application.properties @@ -1,7 +1,7 @@ app.name=Avni ETL app.description=${app.name} is used to create and update the analytics schema for organisations. server.port=${ETL_SERVER_PORT:8022} -spring.datasource.url=${OPENCHS_DATABASE_URL:jdbc:postgresql://localhost:5432/${avni.database}} +spring.datasource.url=${OPENCHS_DATABASE_URL:jdbc:postgresql://${avni.database.server}:${avni.database.port}/${avni.database}} spring.datasource.username=${OPENCHS_DATABASE_USER:openchs} spring.datasource.password=${OPENCHS_DATABASE_PASSWORD:password} spring.datasource.driverClassName=org.postgresql.Driver diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index c393a95..bded3ac 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,4 +1,6 @@ avni.database=openchs_test +avni.database.port=${OPENCHS_DATABASE_PORT:5432} +avni.database.server=${OPENCHS_DATABASE_SERVER:localhost} spring.config.import=classpath:/main-application.properties command.line.runner.enabled=false avni.current.time.offset.seconds=0