Skip to content

Commit

Permalink
added tac batch insert. reinstated secret fetcher params for db service.
Browse files Browse the repository at this point in the history
  • Loading branch information
cameroncaci committed Dec 9, 2023
1 parent f009b85 commit 161ff32
Showing 1 changed file with 50 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import software.amazon.awssdk.services.rds.RdsUtilities;
import software.amazon.awssdk.services.rds.model.GenerateAuthenticationTokenRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

Expand All @@ -20,33 +20,17 @@
@Service
public class DatabaseService {

// TODO: Reinstate
// private String hostname;
// private Integer port;
// private String dbName;
// private String username;
private RdsClient rdsClient;

// TODO: Remove spring @Value and replace with secret fetcher usage

@Value("${myproperty.hostname}")
private String hostname;

@Value("${myproperty.port}")
private int port;

@Value("${myproperty.dbname}")
private Integer port;
private String dbName;

@Value("${myproperty.username}")
private String username;
private RdsClient rdsClient;

public DatabaseService(SecretFetcher secretFetcher) {
// TODO: Reinstate
// this.hostname = secretFetcher.getSecret("rds_hostname");
// this.port = Integer.parseInt(secretFetcher.getSecret("rds_port"));
// this.dbName = secretFetcher.getSecret("rds_db_name");
// this.username = secretFetcher.getSecret("rds_username");
this.hostname = secretFetcher.getSecret("rds_hostname");
this.port = Integer.parseInt(secretFetcher.getSecret("rds_port"));
this.dbName = secretFetcher.getSecret("rds_db_name");
this.username = secretFetcher.getSecret("rds_username");
rdsClient = RdsClient.builder()
.region(Region.of("us-gov-west-1"))
.credentialsProvider(DefaultCredentialsProvider.create())
Expand All @@ -71,7 +55,48 @@ public Connection getConnection() throws SQLException {
return DriverManager.getConnection(jdbcUrl, username, authToken);
}

public void insertTransportationAccountingCodes(List<TransportationAccountingCode> codes) {
// TODO: jdbc connection insert the TAC data into RDS db
// Batch insert 10k TACs at a time
public void insertTransportationAccountingCodes(List<TransportationAccountingCode> codes) throws SQLException {

String sql = "INSERT INTO transportation_accounting_codes (tac, tac_sys_id, loa_sys_id, tac_fy_txt, tac_fn_bl_mod_cd, org_grp_dfas_cd, tac_mvt_dsg_id, tac_ty_cd, tac_use_cd, tac_maj_clmt_id, tac_bill_act_txt, tac_cost_ctr_nm, buic, tac_hist_cd, tac_stat_cd, trnsprtn_acnt_tx, trnsprtn_acnt_bgn_dt, trnsprtn_acnt_end_dt, dd_actvty_adrs_id, tac_blld_add_frst_ln_tx, tac_blld_add_scnd_ln_tx, tac_blld_add_thrd_ln_tx, tac_blld_add_frth_ln_tx, tac_fnct_poc_nm) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

try (Connection conn = this.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) {

int count = 0;

for (TransportationAccountingCode code : codes) {

pstmt.setString(1, code.getTac());
pstmt.setString(2, code.getTacSysID());
pstmt.setString(3, code.getLoaSysID());
pstmt.setString(4, code.getTacFyTxt());
pstmt.setString(5, code.getTacFnBlModCd());
pstmt.setString(6, code.getOrgGrpDfasCd());
pstmt.setString(7, code.getTacMvtDsgID());
pstmt.setString(8, code.getTacTyCd());
pstmt.setString(9, code.getTacUseCd());
pstmt.setString(10, code.getTacMajClmtID());
pstmt.setString(11, code.getTacBillActTxt());
pstmt.setString(12, code.getTacCostCtrNm());
pstmt.setString(13, code.getBuic());
pstmt.setString(14, code.getTacHistCd());
pstmt.setString(15, code.getTacStatCd());
pstmt.setString(16, code.getTrnsprtnAcntTx());
pstmt.setDate(17, java.sql.Date.valueOf(code.getTrnsprtnAcntBgnDt().toLocalDate()));
pstmt.setDate(18, java.sql.Date.valueOf(code.getTrnsprtnAcntEndDt().toLocalDate()));
pstmt.setString(19, code.getDdActvtyAdrsID());
pstmt.setString(20, code.getTacBlldAddFrstLnTx());
pstmt.setString(21, code.getTacBlldAddScndLnTx());
pstmt.setString(22, code.getTacBlldAddThrdLnTx());
pstmt.setString(23, code.getTacBlldAddFrthLnTx());
pstmt.setString(24, code.getTacFnctPocNm());
pstmt.addBatch();

// Execute every 10000 rows or when finished with the provided TACs
if (count++ % 10000 == 0 || count == codes.size()) {
pstmt.executeBatch();
}
}
}
}
}

0 comments on commit 161ff32

Please sign in to comment.