diff --git a/src/main/java/org/avniproject/etl/repository/sync/CleanEncounterCancelTableAction.java b/src/main/java/org/avniproject/etl/repository/sync/CleanEncounterCancelTableAction.java new file mode 100644 index 0000000..914d567 --- /dev/null +++ b/src/main/java/org/avniproject/etl/repository/sync/CleanEncounterCancelTableAction.java @@ -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); + } + +} diff --git a/src/main/java/org/avniproject/etl/repository/sync/EntityRepository.java b/src/main/java/org/avniproject/etl/repository/sync/EntityRepository.java index 96e3f8e..d3f65e1 100644 --- a/src/main/java/org/avniproject/etl/repository/sync/EntityRepository.java +++ b/src/main/java/org/avniproject/etl/repository/sync/EntityRepository.java @@ -24,6 +24,7 @@ public EntityRepository(TransactionalTablesSyncAction transactionalTablesSyncAct SyncTelemetryTableSyncAction syncTelemetryTableSyncAction, UserTableSyncAction userTableSyncAction, CleanEnrolmentExitTableAction cleanEnrolmentExitTableAction, + CleanEncounterCancelTableAction cleanEncounterCancelTableAction, RepeatableQGTransactionTablesSyncAction repeatableQGTransactionTablesSyncAction) { entitySyncRepositories.add(transactionalTablesSyncAction); entitySyncRepositories.add(addressTableSyncAction); @@ -34,6 +35,7 @@ public EntityRepository(TransactionalTablesSyncAction transactionalTablesSyncAct entitySyncRepositories.add(syncTelemetryTableSyncAction); entitySyncRepositories.add(userTableSyncAction); entitySyncRepositories.add(duplicateRowDeleteAction); + entitySyncRepositories.add(cleanEncounterCancelTableAction); entitySyncRepositories.add(cleanEnrolmentExitTableAction); } diff --git a/src/main/resources/sql/etl/deleteUncancelledEncounters.sql.st b/src/main/resources/sql/etl/deleteUncancelledEncounters.sql.st new file mode 100644 index 0000000..2ace362 --- /dev/null +++ b/src/main/resources/sql/etl/deleteUncancelledEncounters.sql.st @@ -0,0 +1,4 @@ +delete from . +using . +where ..id = ..id +and ..cancel_date_time is null; \ No newline at end of file