Skip to content

Commit

Permalink
Merge branch '10.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
himeshr committed Nov 26, 2024
2 parents 127b551 + 7cf5560 commit f3bc7dd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public List<TableMetadata> getAllEncounterTables() {
return tableMetadata.stream().filter(table -> table.getType() == TableMetadata.Type.Encounter).toList();
}

public List<TableMetadata> getAllIndividualAndProgramEncounterTables() {
return tableMetadata.stream().filter(table -> table.getType() == TableMetadata.Type.Encounter
|| table.getType() == TableMetadata.Type.ProgramEncounter).toList();
}

public List<String> getAllEncounterTableNames() {
List<TableMetadata> encounterTables = getAllEncounterTables();
List<String> encounterTableNames = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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))
Expand All @@ -47,6 +49,18 @@ private void cleanUncancelledEncounters(TableMetadata tableMetadata) {
}, jdbcTemplate);
}

private String getPrimaryTableName(TableMetadata tableMetadata, SchemaMetadata currentSchemaMetadata) {
Optional<TableMetadata> 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 + "\"";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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))
Expand All @@ -46,6 +47,18 @@ private void cleanInvalidExits(TableMetadata tableMetadata) {
}, jdbcTemplate);
}


private String getPrimaryTableName(TableMetadata tableMetadata, SchemaMetadata currentSchemaMetadata) {
Optional<TableMetadata> 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 + "\"";
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/main-application.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f3bc7dd

Please sign in to comment.