diff --git a/pom.xml b/pom.xml index e4d6d0b..31834fc 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ com.milmove.trdmlambda trdm-lambda - 1.0.3.7 + 1.0.3.8 trdm java spring interface Project for deploying a Java TRDM interfacer for TGET data. diff --git a/src/main/java/com/milmove/trdmlambda/milmove/service/DatabaseService.java b/src/main/java/com/milmove/trdmlambda/milmove/service/DatabaseService.java index 7bbc5c6..cc68328 100644 --- a/src/main/java/com/milmove/trdmlambda/milmove/service/DatabaseService.java +++ b/src/main/java/com/milmove/trdmlambda/milmove/service/DatabaseService.java @@ -76,12 +76,10 @@ public void updateTransportationAccountingCodes(List codes) throws SQ pstmt.setTimestamp(57, java.sql.Timestamp.valueOf(LocalDateTime.now())); pstmt.setObject(58, code.getId()); - pstmt.addBatch(); // Execute every 10000 rows or when finished with the provided LOAs @@ -411,7 +408,8 @@ public ArrayList dbLoasToModel(ResultSet rs) throws SQLExcepti ArrayList loas = new ArrayList(); - // Loas created times are in 2 different formats. some have 25 characters and some have 26. Based on their character length will choose the formatter + // Loas created times are in 2 different formats. some have 25 characters and + // some have 26. Based on their character length will choose the formatter DateTimeFormatter timeFormatterLen25 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSS"); DateTimeFormatter timeFormatterLen26 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"); @@ -422,9 +420,11 @@ public ArrayList dbLoasToModel(ResultSet rs) throws SQLExcepti loa.setLoaDptID(rs.getString(LinesOfAccountingDatabaseColumns.loaDptId)); if (rs.getString(LinesOfAccountingDatabaseColumns.updatedAt).length() == 25) { - loa.setUpdatedAt(LocalDateTime.parse(rs.getString(LinesOfAccountingDatabaseColumns.updatedAt), timeFormatterLen25)); + loa.setUpdatedAt(LocalDateTime.parse(rs.getString(LinesOfAccountingDatabaseColumns.updatedAt), + timeFormatterLen25)); } else if (rs.getString(LinesOfAccountingDatabaseColumns.updatedAt).length() == 26) { - loa.setUpdatedAt(LocalDateTime.parse(rs.getString(LinesOfAccountingDatabaseColumns.updatedAt), timeFormatterLen26)); + loa.setUpdatedAt(LocalDateTime.parse(rs.getString(LinesOfAccountingDatabaseColumns.updatedAt), + timeFormatterLen26)); } loas.add(loa); @@ -449,10 +449,10 @@ public ArrayList getCurrentTacInformation() throws TransportationAccountingCode tac = new TransportationAccountingCode(); tac.setId(UUID.fromString(rs.getString(TransportationAccountingCodesDatabaseColumns.id))); if (rs.getString(TransportationAccountingCodesDatabaseColumns.loaId) != null - && rs.getString(TransportationAccountingCodesDatabaseColumns.loaId) != "null") { + && rs.getString(TransportationAccountingCodesDatabaseColumns.loaId) != "null") { tac.setLoaID(UUID.fromString(rs.getString(TransportationAccountingCodesDatabaseColumns.loaId))); } else { - UUID nilUUID = new UUID(0,0); // represents a nil UUID + UUID nilUUID = new UUID(0, 0); // represents a nil UUID tac.setLoaID(nilUUID); } tac.setTac(rs.getString(TransportationAccountingCodesDatabaseColumns.tac)); @@ -464,4 +464,28 @@ public ArrayList getCurrentTacInformation() throws } } + + // Get LOA loa_sys_ids with a count greater than 1 + public ArrayList getLoaSysIdCountGreaterThan1() throws SQLException { + + logger.info("retrieving loa_sys_ids with a count greater than 1"); + + ArrayList loaSysIds = new ArrayList(); + + // Select loa_sys_ids with a count greater than 1 + String sql = "SELECT loa_sys_id, COUNT(*) FROM lines_of_accounting GROUP BY loa_sys_id HAVING count(loa_sys_id) > 1"; + + try (Connection conn = this.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) { + ResultSet rs = pstmt.executeQuery(); + while (rs.next()) { + loaSysIds.add(rs.getString(LinesOfAccountingDatabaseColumns.loaSysId)); + } + + logger.info("finished retrieving loa_sys_ids with a count greater than 1. " + loaSysIds.size() + + " non-unique loaSysIds."); + + return loaSysIds; + } + + } } diff --git a/src/main/java/com/milmove/trdmlambda/milmove/util/Trdm.java b/src/main/java/com/milmove/trdmlambda/milmove/util/Trdm.java index 7ff26b0..61d0185 100644 --- a/src/main/java/com/milmove/trdmlambda/milmove/util/Trdm.java +++ b/src/main/java/com/milmove/trdmlambda/milmove/util/Trdm.java @@ -166,8 +166,11 @@ public void UpdateTGETData(XMLGregorianCalendar ourLastUpdate, String trdmTable, // Get all loas ArrayList currentLoas = databaseService.getCurrentLoaInformation(); + // Get all loasSysIds that occur more than once + ArrayList duplicateLoaSysIds = databaseService.getLoaSysIdCountGreaterThan1(); + // Identify Loas to delete based on if their loaSysId is not unique, their id/primary key is not referenced in TACS loa_id and the loa is the latest - ArrayList loasToDelete = identifyDuplicateLoasToDelete(currentLoas, currentTacs); + ArrayList loasToDelete = identifyDuplicateLoasToDelete(currentLoas, currentTacs, duplicateLoaSysIds); // Delete duplicate Loas databaseService.deleteLoas(loasToDelete); @@ -280,42 +283,22 @@ public List identifyLoasToCreate(List newLoa // Identify Loas to delete based on if their loaSysId is not unique, their id/primary key is not referenced in TACS loa_id and the loa created_at is the latest - public ArrayList identifyDuplicateLoasToDelete(ArrayList loas, ArrayList tacs) throws SQLException { + public ArrayList identifyDuplicateLoasToDelete(ArrayList loas, ArrayList tacs, ArrayList duplicateLoaSysIds) throws SQLException { logger.info("identifying duplicate Line of Accounting codes to delete"); logger.info("LOA codes count: " + loas.size()); logger.info("TAC codes count: " + tacs.size()); + logger.info("Duplicate LOA codes loa_sys_ids count: " + duplicateLoaSysIds.size()); // Store loas that needs to be checked for deletion ArrayList duplicateLoas = new ArrayList(); - // Store duplicate loa_sys_id - ArrayList duplicateLoaSysIds = new ArrayList(); - - logger.info("starting to identify LOA codes with non unique loa_sys_ids"); + logger.info("starting to identify duplicate LOA codes"); for (LineOfAccounting loa : loas) { - - // If already confirmed a duplicate then no need to check if it is - boolean alreadyConfirmedDupe = false; if (duplicateLoaSysIds.contains(loa.getLoaSysID())) { - alreadyConfirmedDupe = true; duplicateLoas.add(loa); } - - // If not already confirmed a duplicate check to see if it is a duplicate - if (!alreadyConfirmedDupe) { - - // Check if there are more than 1 loa with this loa_sys_id if so enter if logic - List l1 = loas.stream().filter(l -> l.getLoaSysID().equals(loa.getLoaSysID())).collect(Collectors.toList()); - if (l1.size() > 1) { - - // Add loaSysId to duplicateLoaSysIds another loa with the same sysId is being checked it will not have to run through this logic - duplicateLoaSysIds.add(loa.getLoaSysID()); - duplicateLoas.add(loa); - } - } } - - logger.info("finished identifying LOA codes with non unique loa_sys_ids. Count: " + duplicateLoas.size()); + logger.info("finished identifying duplicate LOA codes. Count: " + duplicateLoas.size()); logger.info("starting to identify duplicate LOA codes that are not referenced by a TAC code"); // Duplicate loas not referenced in TACS diff --git a/src/test/java/com/milmove/trdmlambda/milmove/util/DatabaseServiceTest.java b/src/test/java/com/milmove/trdmlambda/milmove/util/DatabaseServiceTest.java index f56b3df..ec42f66 100644 --- a/src/test/java/com/milmove/trdmlambda/milmove/util/DatabaseServiceTest.java +++ b/src/test/java/com/milmove/trdmlambda/milmove/util/DatabaseServiceTest.java @@ -262,6 +262,49 @@ void testUpdateTransportationAccountingCodes() throws Exception { assertEquals(testTacs.get(0).getTac(), codes.get(0).getTac()); } + // Test that we get a list of loa_sys_ids that occur more than once + // DatabaseService.getLoaSysIdCountGreaterThan1() + @Test + void testGetLoaSysIdCountGreaterThan1() throws Exception { + + setUpTests(); + + LocalDateTime time = LocalDateTime.now(); + int mo = time.getMonthValue(); + int day = time.getDayOfMonth(); + int year = time.getYear(); + int hr = time.getHour(); + int min = time.getMinute(); + int sec = time.getSecond(); + + String dayTime = Integer.toString(mo) + Integer.toString(day) + Integer.toString(year) + Integer.toString(hr) + Integer.toString(hr) + Integer.toString(min) + Integer.toString(sec); + + String testLoaSysId = "dum" + dayTime; + String nonDupLoaSysId = testLoaSysId + "a"; + + ArrayList testLoas = createMockLoas(3); + + testLoas.get(0).setLoaSysID(testLoaSysId); + testLoas.get(1).setLoaSysID(testLoaSysId); + testLoas.get(2).setLoaSysID(nonDupLoaSysId); + + // Invoke insertLineOfAccountingCodes() with test LOA(s) + spyDatabaseService.insertLinesOfAccounting(testLoas); + + Connection conn1 = createTestDbConnection(); + + // Mock the DatabaseService.getConnection() to return the test_db connection + doReturn(conn1).when(spyDatabaseService).getConnection(); + + // Make sure the test_db connection is returned when .getConnection is called + assertEquals(conn1, spyDatabaseService.getConnection()); + + ArrayList loaSysIds = spyDatabaseService.getLoaSysIdCountGreaterThan1(); + + assertTrue(loaSysIds.contains(testLoaSysId)); + assertFalse(loaSysIds.contains(nonDupLoaSysId)); + } + // Test that we can delete a list of loas // DatabaseService.deleteLoas() @Test diff --git a/src/test/java/com/milmove/trdmlambda/milmove/util/TrdmTest.java b/src/test/java/com/milmove/trdmlambda/milmove/util/TrdmTest.java index 7dd008a..8cfdfcc 100644 --- a/src/test/java/com/milmove/trdmlambda/milmove/util/TrdmTest.java +++ b/src/test/java/com/milmove/trdmlambda/milmove/util/TrdmTest.java @@ -292,11 +292,19 @@ void identifyDuplicateLoasToDeleteTest() throws SQLException { currentLoas.get(5).setLoaSysID("NoDupe" + rand.nextInt(1000) + rand.nextInt(1000)); // No delete currentLoas.get(6).setLoaSysID("NoDupe" + rand.nextInt(1000) + rand.nextInt(1000)); // No Delete + // List of duplicateLoas + ArrayList duplicateLoaSysIds = new ArrayList(); + duplicateLoaSysIds.add(currentLoas.get(0).getLoaSysID()); + duplicateLoaSysIds.add(currentLoas.get(1).getLoaSysID()); + duplicateLoaSysIds.add(currentLoas.get(2).getLoaSysID()); + duplicateLoaSysIds.add(currentLoas.get(3).getLoaSysID()); + duplicateLoaSysIds.add(currentLoas.get(4).getLoaSysID()); - ArrayList loasToDelete = trdm.identifyDuplicateLoasToDelete(currentLoas, currentTacs); + + ArrayList loasToDelete = trdm.identifyDuplicateLoasToDelete(currentLoas, currentTacs, duplicateLoaSysIds); List loaIds = loasToDelete.stream().map(loa -> loa.getId()).collect(Collectors.toList()); - // Loas that should be deleted should be returned for deletetion + // Loas that should be deleted should be returned for deletion assertTrue(loaIds.contains(currentLoas.get(2).getId())); assertTrue(loaIds.contains(currentLoas.get(3).getId()));