Skip to content

Commit

Permalink
#75 | Delete uncancelled encounters from cancel tables
Browse files Browse the repository at this point in the history
  • Loading branch information
1t5j0y committed Aug 21, 2024
1 parent 8fdc32d commit e927a1a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.avniproject.etl.repository.sync;

import org.avniproject.etl.domain.NullObject;
import org.avniproject.etl.domain.OrgIdentityContextHolder;
import org.avniproject.etl.domain.metadata.SchemaMetadata;
import org.avniproject.etl.domain.metadata.TableMetadata;
import org.avniproject.etl.repository.sql.SqlFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.stringtemplate.v4.ST;

import java.util.Arrays;
import java.util.Date;

import static org.avniproject.etl.repository.JdbcContextWrapper.runInOrgContext;

@Repository
public class CleanEncounterCancelTableAction implements EntitySyncAction {
private final JdbcTemplate jdbcTemplate;
private static final String deleteUncancelledEncountersSqlTemplate = SqlFile.readSqlFile("deleteUncancelledEncounters.sql.st");
@Autowired
public CleanEncounterCancelTableAction(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Override
public void perform(TableMetadata tableMetadata, Date lastSyncTime, Date dataSyncBoundaryTime, SchemaMetadata currentSchemaMetadata) {
if (this.doesntSupport(tableMetadata)) {
return;
}
cleanUncancelledEncounters(tableMetadata);
}

private void cleanUncancelledEncounters(TableMetadata tableMetadata) {
String schema = OrgIdentityContextHolder.getDbSchema();
String encounterCancelTableName = tableMetadata.getName();
String primaryTableName = encounterCancelTableName.substring(0, encounterCancelTableName.length() - 7);
String sql = new ST(deleteUncancelledEncountersSqlTemplate)
.add("schemaName", wrapInQuotes(schema))
.add("encounterCancelTableName", wrapInQuotes(encounterCancelTableName))
.add("primaryTableName", wrapInQuotes(primaryTableName))
.render();
runInOrgContext(() -> {
jdbcTemplate.execute(sql);
return NullObject.instance();
}, jdbcTemplate);
}

private String wrapInQuotes(String parameter) {
return parameter == null ? "null" : "\"" + parameter + "\"";
}

private boolean supports(TableMetadata tableMetadata) {
return Arrays.asList(TableMetadata.Type.IndividualEncounterCancellation, TableMetadata.Type.ProgramEncounterCancellation).contains(tableMetadata.getType());
}

public boolean doesntSupport(TableMetadata tableMetadata) {
return !supports(tableMetadata);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public EntityRepository(TransactionalTablesSyncAction transactionalTablesSyncAct
SyncTelemetryTableSyncAction syncTelemetryTableSyncAction,
UserTableSyncAction userTableSyncAction,
CleanEnrolmentExitTableAction cleanEnrolmentExitTableAction,
CleanEncounterCancelTableAction cleanEncounterCancelTableAction,
RepeatableQGTransactionTablesSyncAction repeatableQGTransactionTablesSyncAction) {
entitySyncRepositories.add(transactionalTablesSyncAction);
entitySyncRepositories.add(addressTableSyncAction);
Expand All @@ -34,6 +35,7 @@ public EntityRepository(TransactionalTablesSyncAction transactionalTablesSyncAct
entitySyncRepositories.add(syncTelemetryTableSyncAction);
entitySyncRepositories.add(userTableSyncAction);
entitySyncRepositories.add(duplicateRowDeleteAction);
entitySyncRepositories.add(cleanEncounterCancelTableAction);
entitySyncRepositories.add(cleanEnrolmentExitTableAction);
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/sql/etl/deleteUncancelledEncounters.sql.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
delete from <schemaName>.<encounterCancelTableName>
using <schemaName>.<primaryTableName>
where <schemaName>.<encounterCancelTableName>.id = <schemaName>.<primaryTableName>.id
and <schemaName>.<primaryTableName>.cancel_date_time is null;

0 comments on commit e927a1a

Please sign in to comment.