-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#75 | Delete uncancelled encounters from cancel tables
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/org/avniproject/etl/repository/sync/CleanEncounterCancelTableAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/resources/sql/etl/deleteUncancelledEncounters.sql.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |