Skip to content

Commit

Permalink
#112 | Obtain enrolmentTableName from SchemaMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
himeshr committed Nov 21, 2024
1 parent a4d91e9 commit 7e0fcd8
Showing 1 changed file with 16 additions and 3 deletions.
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() == tableMetadata.getProgramUuid()
&& tm.getSubjectTypeUuid() == 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

0 comments on commit 7e0fcd8

Please sign in to comment.