Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -264,27 +264,28 @@ public Long findLatestSuspectedId(Long benRegId) {
return getJdbcTemplate().query(sql, (ResultSet rs) -> rs.next() ? rs.getLong("id") : null, benRegId);
}

public void updateNikshayId(Long suspectedId, String nikshayId, String modifiedBy) {
String sql = "UPDATE tb_suspected SET nikshay_id = ?, modified_by = ?, "
public void updateNikshayId(Long suspectedId, String nikshayId, boolean createdByAmrit, String modifiedBy) {
String sql = "UPDATE tb_suspected SET nikshay_id = ?, nikshay_created_by_amrit = ?, modified_by = ?, "
+ "last_mod_date = CURRENT_TIMESTAMP WHERE id = ?";
getJdbcTemplate().update(sql, nikshayId, modifiedBy, suspectedId);
getJdbcTemplate().update(sql, nikshayId, createdByAmrit, modifiedBy, suspectedId);
}

/** Called when a beneficiary had no tb_suspected row yet — creates one to
* hold the Nikshay ID the portal generated. created_date is set explicitly
* because, unlike created_date on most other AMRIT tables, tb_suspected's
* has no DB-side default. */
public Long insertSuspectedWithNikshayId(Long benRegId, LocalDate visitDate, String nikshayId,
String createdBy) {
String sql = "INSERT INTO tb_suspected (benRegID, visit_date, nikshay_id, created_by, created_date) "
+ "VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)";
boolean createdByAmrit, String createdBy) {
String sql = "INSERT INTO tb_suspected (benRegID, visit_date, nikshay_id, nikshay_created_by_amrit, "
+ "created_by, created_date) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)";
KeyHolder keyHolder = new GeneratedKeyHolder();
getJdbcTemplate().update(connection -> {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setLong(1, benRegId);
ps.setTimestamp(2, Timestamp.valueOf(visitDate.atStartOfDay()));
ps.setString(3, nikshayId);
ps.setString(4, createdBy);
ps.setBoolean(4, createdByAmrit);
ps.setString(5, createdBy);
return ps;
}, keyHolder);
return keyHolder.getKey().longValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@
}

public record ImportSummary(int csvRowCount, int updated, int failed, int needsReview,
int createdByAmrit, int alreadyOnNikshay,
List<ImportRowResult> needsReviewRows, List<ImportRowResult> failedRows) {
}

@Autowired
private NikshayExportRepository nikshayExportRepository;

public ImportSummary importResults(LocalDate visitDate, InputStream csvInputStream, String modifiedBy)

Check failure on line 88 in src/main/java/com/iemr/mmu/service/stoptb/NikshayImportService.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 41 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=PSMRI_MMU-API&issues=AaCPTEdeAssgigl7UyjZ&open=AaCPTEdeAssgigl7UyjZ&pullRequest=185
throws Exception {
List<CSVRecord> records;
boolean hasErrorColumn;
Expand All @@ -104,6 +105,8 @@
}

int updated = 0;
int createdByAmritCount = 0;
int alreadyOnNikshayCount = 0;
List<ImportRowResult> needsReview = new ArrayList<>();
List<ImportRowResult> failedRows = new ArrayList<>();

Expand Down Expand Up @@ -141,8 +144,14 @@
if ("success".equalsIgnoreCase(status) || "skipped".equalsIgnoreCase(status)) {
String[] tokens = generatedId.isEmpty() ? new String[0] : generatedId.split("\\s+");
if (tokens.length == 1) {
writeNikshayId(visitDate, benRegId, tokens[0], modifiedBy);
boolean createdByAmrit = "success".equalsIgnoreCase(status);
writeNikshayId(visitDate, benRegId, tokens[0], createdByAmrit, modifiedBy);
updated++;
if (createdByAmrit) {
createdByAmritCount++;
} else {
alreadyOnNikshayCount++;
}
} else {
String note = tokens.length == 0 ? "Row marked " + status + " but has no generatedId."
: "Multiple possible existing Nikshay IDs (" + generatedId
Expand All @@ -157,8 +166,8 @@
}
}

return new ImportSummary(records.size(), updated, failedRows.size(), needsReview.size(), needsReview,
failedRows);
return new ImportSummary(records.size(), updated, failedRows.size(), needsReview.size(),
createdByAmritCount, alreadyOnNikshayCount, needsReview, failedRows);
}

private static Long parseBenRegId(String raw) {
Expand Down Expand Up @@ -186,12 +195,14 @@
return digits.matches("[1-9][0-9]{9}") ? digits : null;
}

private void writeNikshayId(LocalDate visitDate, Long benRegId, String nikshayId, String modifiedBy) {
private void writeNikshayId(LocalDate visitDate, Long benRegId, String nikshayId, boolean createdByAmrit,
String modifiedBy) {
Long suspectedId = nikshayExportRepository.findLatestSuspectedId(benRegId);
if (suspectedId != null) {
nikshayExportRepository.updateNikshayId(suspectedId, nikshayId, modifiedBy);
nikshayExportRepository.updateNikshayId(suspectedId, nikshayId, createdByAmrit, modifiedBy);
} else {
nikshayExportRepository.insertSuspectedWithNikshayId(benRegId, visitDate, nikshayId, modifiedBy);
nikshayExportRepository.insertSuspectedWithNikshayId(benRegId, visitDate, nikshayId, createdByAmrit,
modifiedBy);
}
}
}
Loading