Skip to content

Commit

Permalink
fix: forest file id, duplication results and pagination (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Campos authored Sep 13, 2024
1 parent 8807620 commit 83985db
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Identifier for a cut block of a harvesting tenure (within a cutting permit for
private Boolean submittedToFrpa;

@Schema(description = "Uniquely identifies the attached file.", example = "407")
private Integer fileId;
private String forestFileId;

@Schema(
description = "Uniquely identifies an Application for Relief from obligations.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public PaginatedResult<OpeningSearchResponseDto> openingSearch(
in = ParameterIn.QUERY,
description =
"Search term representing one of Opening ID | Opening Number | Timber Mark ID |"
+ " File ID. E.g.: Opening ID 1022833, Opening Number 1012, Timber Mark"
+ " EM2184, File ID 407",
+ " Forest File ID. Eg: Opening ID 1022833, Opening Number 1012, Timber Mark"
+ " EM2184, Forest File ID TFL47",
required = false)
String mainSearchTerm,
@RequestParam(value = "orgUnit", required = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.restapi.results.oracle.repository;

import ca.bc.gov.restapi.results.common.config.ConstantsConfig;
import ca.bc.gov.restapi.results.common.pagination.PaginatedResult;
import ca.bc.gov.restapi.results.common.pagination.PaginationParameters;
import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchFiltersDto;
Expand Down Expand Up @@ -40,8 +41,8 @@ public PaginatedResult<OpeningSearchResponseDto> searchOpeningQuery(
final String sqlQuery = createNativeSqlQuery(filtersDto);
final Query query = setQueryParameters(filtersDto, sqlQuery);

// Limit to 100 records at the database
query.setMaxResults(100);
// Limit to 500 records at the database
query.setMaxResults(ConstantsConfig.MAX_PAGE_SIZE);

List<?> result = query.getResultList();
int lastPage = PaginationUtil.getLastPage(result.size(), pagination.perPage());
Expand Down Expand Up @@ -129,8 +130,8 @@ private List<OpeningSearchResponseDto> buildResultListDto(List<?> result) {
}

if (row.length > 9) {
Integer fileId = getValue(Integer.class, row[9], "fileId");
searchOpeningDto.setFileId(fileId);
String forestFileId = getValue(String.class, row[9], "forestFileId");
searchOpeningDto.setForestFileId(forestFileId);
}

if (row.length > 10) {
Expand Down Expand Up @@ -239,7 +240,7 @@ private Query setQueryParameters(OpeningSearchFiltersDto filtersDto, String nati
query.setParameter("openingOrFile", filtersDto.getMainSearchTerm());
} else {
log.info("Setting mainSearchTerm as non-numeric filter value");
// Opening number or Timber Mark
// Opening number, Timber Mark, or Forest File Id
query.setParameter("numberOrTimber", filtersDto.getMainSearchTerm());
}
}
Expand Down Expand Up @@ -324,7 +325,7 @@ private String createNativeSqlQuery(OpeningSearchFiltersDto filtersDto) {
builder.append(",cboa.CUT_BLOCK_ID AS cutBlockId");
builder.append(",cboa.OPENING_GROSS_AREA AS openingGrossArea");
builder.append(",cboa.DISTURBANCE_START_DATE AS disturbanceStartDate");
builder.append(",oa.OPENING_ATTACHMENT_FILE_ID AS fileId");
builder.append(",cboa.FOREST_FILE_ID AS forestFileId");
builder.append(",ou.ORG_UNIT_CODE AS orgUnitCode");
builder.append(",ou.ORG_UNIT_NAME AS orgUnitName");
builder.append(",res.CLIENT_NUMBER AS clientNumber");
Expand All @@ -343,7 +344,6 @@ private String createNativeSqlQuery(OpeningSearchFiltersDto filtersDto) {
builder.append(",o.ENTRY_USERID AS entryUserId");
builder.append(",COALESCE(sra.SILV_RELIEF_APPLICATION_ID, 0) AS submittedToFrpa108 ");
builder.append("FROM THE.OPENING o ");
builder.append("LEFT JOIN THE.OPENING_ATTACHMENT oa ON (oa.OPENING_ID = o.OPENING_ID)");
builder.append("LEFT JOIN THE.CUT_BLOCK_OPEN_ADMIN cboa ON (cboa.OPENING_ID = o.OPENING_ID)");
builder.append("LEFT JOIN THE.ORG_UNIT ou ON (ou.ORG_UNIT_NO = o.ADMIN_DISTRICT_NO)");
builder.append("LEFT JOIN the.RESULTS_ELECTRONIC_SUBMISSION res ON (");
Expand All @@ -370,15 +370,18 @@ private String createNativeSqlQuery(OpeningSearchFiltersDto filtersDto) {
log.info("Filter mainSearchTerm detected! mainSearchTerm={}", filtersDto.getMainSearchTerm());
boolean itsNumeric = filtersDto.getMainSearchTerm().replaceAll("[0-9]", "").isEmpty();
if (itsNumeric) {
log.info("Filter mainSearchTerm it's numeric! Looking for Opening ID or File ID");
// Opening id or File id
builder.append("AND (o.OPENING_ID = :openingOrFile or ");
builder.append("oa.OPENING_ATTACHMENT_FILE_ID = :openingOrFile) ");
log.info("Filter mainSearchTerm it's numeric! Looking for Opening ID");
// Opening id
builder.append("AND o.OPENING_ID = :openingOrFile ");
} else {
log.info("Filter mainSearchTerm NOT numeric! Looking for Opening Number or Timber Mark");
// Opening number or Timber Mark
builder.append(
"AND (o.OPENING_NUMBER = :numberOrTimber or cboa.TIMBER_MARK = :numberOrTimber) ");
log.info(
"Filter mainSearchTerm NOT numeric! Looking for Opening Number, Timber Mark, or Forest"
+ " File Id");
// Opening number, Timber Mark, or Forest File Id
builder.append("AND (");
builder.append("o.OPENING_NUMBER = :numberOrTimber ");
builder.append(" or cboa.TIMBER_MARK = :numberOrTimber ");
builder.append(" or cboa.FOREST_FILE_ID = :numberOrTimber)");
}
}

Expand Down Expand Up @@ -468,6 +471,34 @@ private String createNativeSqlQuery(OpeningSearchFiltersDto filtersDto) {
builder.append("AND o.UPDATE_TIMESTAMP <= to_timestamp(:updateEndDate, 'YYYY-MM-DD') ");
}

/* Group by - to avoid duplications */
builder.append("GROUP BY o.OPENING_ID ");
builder.append(",o.OPENING_NUMBER ");
builder.append(",o.OPEN_CATEGORY_CODE ");
builder.append(",o.OPENING_STATUS_CODE ");
builder.append(",cboa.CUTTING_PERMIT_ID ");
builder.append(",cboa.TIMBER_MARK ");
builder.append(",cboa.CUT_BLOCK_ID ");
builder.append(",cboa.OPENING_GROSS_AREA ");
builder.append(",cboa.DISTURBANCE_START_DATE ");
builder.append(",cboa.FOREST_FILE_ID ");
builder.append(",ou.ORG_UNIT_CODE ");
builder.append(",ou.ORG_UNIT_NAME ");
builder.append(",res.CLIENT_NUMBER ");

sql = ",ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMRG.LATE_OFFSET_YEARS, 0) * 12)) ";
builder.append(sql);

sql = ",ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.EARLY_OFFSET_YEARS, 0) * 12)) ";
builder.append(sql);

sql = ",ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.LATE_OFFSET_YEARS, 0) * 12)) ";
builder.append(sql);

builder.append(",o.UPDATE_TIMESTAMP ");
builder.append(",o.ENTRY_USERID ");
builder.append(",COALESCE(sra.SILV_RELIEF_APPLICATION_ID, 0) ");

// Order by
builder.append("ORDER BY o.OPENING_ID DESC");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void openingSearch_happyPath_shouldSucceed() throws Exception {
response.setEntryUserId("TEST");
response.setSubmittedToFrpa(true);
response.setSilvaReliefAppId(333L);
response.setFileId(407);
response.setForestFileId("TFL47");

paginatedResult.setData(List.of(response));

Expand All @@ -100,7 +100,7 @@ void openingSearch_happyPath_shouldSucceed() throws Exception {
.andExpect(jsonPath("$.data[0].openingGrossAreaHa").value(response.getOpeningGrossAreaHa()))
.andExpect(
jsonPath("$.data[0].disturbanceStartDate").value(response.getDisturbanceStartDate()))
.andExpect(jsonPath("$.data[0].fileId").value(response.getFileId()))
.andExpect(jsonPath("$.data[0].forestFileId").value(response.getForestFileId()))
.andExpect(jsonPath("$.data[0].orgUnitCode").value(response.getOrgUnitCode()))
.andExpect(jsonPath("$.data[0].orgUnitName").value(response.getOrgUnitName()))
.andExpect(jsonPath("$.data[0].clientNumber").value(response.getClientNumber()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void searchOpeningQuery_mainFilterNumeric_shouldSucceed() {
String cutBlockId = null;
BigDecimal openingGrossArea = new BigDecimal("11");
Timestamp disturbanceStartDate = Timestamp.valueOf(LocalDateTime.now());
Integer fileId = 407;
String forestFileId = "TFL47";
String orgUnitCode = null;
String orgUnitName = null;
String clientNumber = null;
Expand All @@ -323,7 +323,7 @@ void searchOpeningQuery_mainFilterNumeric_shouldSucceed() {
cutBlockId,
openingGrossArea,
disturbanceStartDate,
fileId,
forestFileId,
orgUnitCode,
orgUnitName,
clientNumber,
Expand Down Expand Up @@ -356,7 +356,7 @@ void searchOpeningQuery_mainFilterNumeric_shouldSucceed() {
Assertions.assertEquals(cutBlockId, dto.getCutBlockId());
Assertions.assertEquals(openingGrossArea, dto.getOpeningGrossAreaHa());
Assertions.assertEquals(disturbanceStartDate.toLocalDateTime(), dto.getDisturbanceStartDate());
Assertions.assertEquals(fileId, dto.getFileId());
Assertions.assertEquals(forestFileId, dto.getForestFileId());
Assertions.assertEquals(orgUnitCode, dto.getOrgUnitCode());
Assertions.assertEquals(orgUnitName, dto.getOrgUnitName());
Assertions.assertEquals(clientNumber, dto.getClientNumber());
Expand Down Expand Up @@ -386,7 +386,7 @@ void searchOpeningQuery_mainFilterString_shouldSucceed() {
BigDecimal openingGrossArea = new BigDecimal("11");
Timestamp disturbanceStartDate = Timestamp.valueOf(LocalDateTime.now());
;
Integer fileId = 407;
String forestFileId = "TFL47";
String orgUnitCode = null;
String orgUnitName = null;
String clientNumber = null;
Expand All @@ -408,7 +408,7 @@ void searchOpeningQuery_mainFilterString_shouldSucceed() {
cutBlockId,
openingGrossArea,
disturbanceStartDate,
fileId,
forestFileId,
orgUnitCode,
orgUnitName,
clientNumber,
Expand Down Expand Up @@ -441,7 +441,7 @@ void searchOpeningQuery_mainFilterString_shouldSucceed() {
Assertions.assertEquals(cutBlockId, dto.getCutBlockId());
Assertions.assertEquals(openingGrossArea, dto.getOpeningGrossAreaHa());
Assertions.assertEquals(disturbanceStartDate.toLocalDateTime(), dto.getDisturbanceStartDate());
Assertions.assertEquals(fileId, dto.getFileId());
Assertions.assertEquals(forestFileId, dto.getForestFileId());
Assertions.assertEquals(orgUnitCode, dto.getOrgUnitCode());
Assertions.assertEquals(orgUnitName, dto.getOrgUnitName());
Assertions.assertEquals(clientNumber, dto.getClientNumber());
Expand Down Expand Up @@ -469,7 +469,7 @@ void searchOpeningQuery_orgUnitFilter_shouldSucceed() {
String cutBlockId = null;
BigDecimal openingGrossArea = new BigDecimal("11");
Timestamp disturbanceStartDate = Timestamp.valueOf(LocalDateTime.now());
Integer fileId = 407;
String forestFileId = "TFL47";
String orgUnitCode = "DCR";
String orgUnitName = null;
String clientNumber = null;
Expand All @@ -491,7 +491,7 @@ void searchOpeningQuery_orgUnitFilter_shouldSucceed() {
cutBlockId,
openingGrossArea,
disturbanceStartDate,
fileId,
forestFileId,
orgUnitCode,
orgUnitName,
clientNumber,
Expand Down Expand Up @@ -524,7 +524,7 @@ void searchOpeningQuery_orgUnitFilter_shouldSucceed() {
Assertions.assertEquals(cutBlockId, dto.getCutBlockId());
Assertions.assertEquals(openingGrossArea, dto.getOpeningGrossAreaHa());
Assertions.assertEquals(disturbanceStartDate.toLocalDateTime(), dto.getDisturbanceStartDate());
Assertions.assertEquals(fileId, dto.getFileId());
Assertions.assertEquals(forestFileId, dto.getForestFileId());
Assertions.assertEquals(orgUnitCode, dto.getOrgUnitCode());
Assertions.assertEquals(orgUnitName, dto.getOrgUnitName());
Assertions.assertEquals(clientNumber, dto.getClientNumber());
Expand Down Expand Up @@ -552,7 +552,7 @@ void searchOpeningQuery_allFilters_shouldSucceed() {
String cutBlockId = "456";
BigDecimal openingGrossArea = new BigDecimal("11");
Timestamp disturbanceStartDate = Timestamp.valueOf(LocalDateTime.now());
Integer fileId = 407;
String forestFileId = "TFL47";
String orgUnitCode = filters.getOrgUnit();
String orgUnitName = "Org Name";
String clientNumber = "00012797";
Expand All @@ -574,7 +574,7 @@ void searchOpeningQuery_allFilters_shouldSucceed() {
cutBlockId,
openingGrossArea,
disturbanceStartDate,
fileId,
forestFileId,
orgUnitCode,
orgUnitName,
clientNumber,
Expand Down Expand Up @@ -607,7 +607,7 @@ void searchOpeningQuery_allFilters_shouldSucceed() {
Assertions.assertEquals(cutBlockId, dto.getCutBlockId());
Assertions.assertEquals(openingGrossArea, dto.getOpeningGrossAreaHa());
Assertions.assertEquals(disturbanceStartDate.toLocalDateTime(), dto.getDisturbanceStartDate());
Assertions.assertEquals(fileId, dto.getFileId());
Assertions.assertEquals(forestFileId, dto.getForestFileId());
Assertions.assertEquals(orgUnitCode, dto.getOrgUnitCode());
Assertions.assertEquals(orgUnitName, dto.getOrgUnitName());
Assertions.assertEquals(clientNumber, dto.getClientNumber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ void openingSearch_fileId_shouldSucceed() {
dto.setUpdateTimestamp(LocalDateTime.now());
dto.setEntryUserId("");
dto.setSubmittedToFrpa(false);
dto.setFileId(407);
dto.setForestFileId("TFL47");
paginated.setData(List.of(dto));

OpeningSearchFiltersDto filters = mockMainFilter("407");
Expand All @@ -296,7 +296,7 @@ void openingSearch_fileId_shouldSucceed() {
dto.getOpeningGrossAreaHa(), result.getData().get(0).getOpeningGrossAreaHa());
Assertions.assertEquals(
dto.getDisturbanceStartDate(), result.getData().get(0).getDisturbanceStartDate());
Assertions.assertEquals(dto.getFileId(), result.getData().get(0).getFileId());
Assertions.assertEquals(dto.getForestFileId(), result.getData().get(0).getForestFileId());
Assertions.assertEquals(dto.getOrgUnitCode(), result.getData().get(0).getOrgUnitCode());
Assertions.assertEquals(dto.getOrgUnitName(), result.getData().get(0).getOrgUnitName());
Assertions.assertEquals(dto.getClientNumber(), result.getData().get(0).getClientNumber());
Expand Down Expand Up @@ -340,7 +340,7 @@ void openingSearch_orgUnit_shouldSucceed() {
dto.setUpdateTimestamp(LocalDateTime.now());
dto.setEntryUserId("");
dto.setSubmittedToFrpa(false);
dto.setFileId(407);
dto.setForestFileId("TFL47");
paginated.setData(List.of(dto));

OpeningSearchFiltersDto filters = mockOrgUnit("DCR");
Expand All @@ -365,7 +365,7 @@ void openingSearch_orgUnit_shouldSucceed() {
dto.getOpeningGrossAreaHa(), result.getData().get(0).getOpeningGrossAreaHa());
Assertions.assertEquals(
dto.getDisturbanceStartDate(), result.getData().get(0).getDisturbanceStartDate());
Assertions.assertEquals(dto.getFileId(), result.getData().get(0).getFileId());
Assertions.assertEquals(dto.getForestFileId(), result.getData().get(0).getForestFileId());
Assertions.assertEquals(dto.getOrgUnitCode(), result.getData().get(0).getOrgUnitCode());
Assertions.assertEquals(dto.getOrgUnitName(), result.getData().get(0).getOrgUnitName());
Assertions.assertEquals(dto.getClientNumber(), result.getData().get(0).getClientNumber());
Expand Down

0 comments on commit 83985db

Please sign in to comment.