Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: changing query to prevent connection drop #499

Merged
merged 3 commits into from
Nov 22, 2024
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 @@ -20,6 +20,7 @@ public class PaginatedResult<T> {
private int pageIndex;
private int perPage;
private int totalPages;
private long totalItems;
private boolean hasNextPage;
private List<T> data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.validation.constraints.PositiveOrZero;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
* Pagination parameters to be used in the processing of HTTP GET requests.
Expand Down Expand Up @@ -34,4 +35,8 @@ public record PaginationParameters(
public Pageable toPageable(int maxPageSize) {
return PageRequest.of(page, maxPageSize);
}

public Pageable toPageable(Sort sort) {
return PageRequest.of(page, perPage,sort);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package ca.bc.gov.restapi.results.oracle.dto;

import ca.bc.gov.restapi.results.oracle.SilvaOracleConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;

/** This record contains all possible filters when using the Opening Search API. */
@Slf4j
Expand All @@ -18,7 +18,7 @@ public class OpeningSearchFiltersDto {
private final List<String> category;
private final List<String> statusList;
private final Boolean myOpenings;
private final Boolean submittedToFrpa;
private final String submittedToFrpa;
private final String disturbanceDateStart;
private final String disturbanceDateEnd;
private final String regenDelayDateStart;
Expand Down Expand Up @@ -56,25 +56,19 @@ public OpeningSearchFiltersDto(
String cutBlockId,
String timberMark,
String mainSearchTerm) {
this.orgUnit = new ArrayList<>();
if (!Objects.isNull(orgUnit)) {
this.orgUnit.addAll(orgUnit.stream()
.map(s -> String.format("'%s'", s.toUpperCase().trim()))
.toList());
}
this.category = new ArrayList<>();
if (!Objects.isNull(category)) {
this.category.addAll(category.stream()
.map(s -> String.format("'%s'", s.toUpperCase().trim()))
.toList());
}
this.statusList = new ArrayList<>();
this.openingIds = new ArrayList<>();
if (!Objects.isNull(statusList)) {
this.statusList.addAll(statusList.stream().map(s -> String.format("'%s'", s)).toList());
}
this.orgUnit = !Objects.isNull(orgUnit) ? orgUnit : null;
this.category = !Objects.isNull(category) ? category : null;
this.statusList = !Objects.isNull(statusList) ? statusList : null;
this.openingIds = null;
this.myOpenings = myOpenings;
this.submittedToFrpa = submittedToFrpa;
this.submittedToFrpa =
BooleanUtils
.toString(
submittedToFrpa,
"YES",
"NO",
"NO"
);
this.disturbanceDateStart =
Objects.isNull(disturbanceDateStart) ? null : disturbanceDateStart.trim();
this.disturbanceDateEnd = Objects.isNull(disturbanceDateEnd) ? null : disturbanceDateEnd.trim();
Expand All @@ -95,14 +89,13 @@ public OpeningSearchFiltersDto(
}

// Create a constructor with only the List<String> openingIds
public OpeningSearchFiltersDto(
List<Long> openingIds) {
this.orgUnit = new ArrayList<>();
this.category = new ArrayList<>();
this.statusList = new ArrayList<>();
public OpeningSearchFiltersDto(List<Long> openingIds) {
this.orgUnit = null;
this.category = null;
this.statusList = null;
this.openingIds = openingIds;
this.myOpenings = null;
this.submittedToFrpa = null;
this.submittedToFrpa = "NO";
this.disturbanceDateStart = null;
this.disturbanceDateEnd = null;
this.regenDelayDateStart = null;
Expand All @@ -124,10 +117,10 @@ public OpeningSearchFiltersDto(
*/
public boolean hasValue(String prop) {
return switch (prop) {
case SilvaOracleConstants.ORG_UNIT -> !this.orgUnit.isEmpty();
case SilvaOracleConstants.CATEGORY -> !this.category.isEmpty();
case SilvaOracleConstants.STATUS_LIST -> !this.statusList.isEmpty();
case SilvaOracleConstants.OPENING_IDS -> !this.openingIds.isEmpty();
case SilvaOracleConstants.ORG_UNIT -> !Objects.isNull(this.orgUnit) && !this.orgUnit.isEmpty();
case SilvaOracleConstants.CATEGORY -> !Objects.isNull(this.category) && !this.category.isEmpty();
case SilvaOracleConstants.STATUS_LIST -> !Objects.isNull(this.statusList) && !this.statusList.isEmpty();
case SilvaOracleConstants.OPENING_IDS -> !Objects.isNull(this.openingIds) && !this.openingIds.isEmpty();
case SilvaOracleConstants.MY_OPENINGS -> !Objects.isNull(this.myOpenings);
case SilvaOracleConstants.SUBMITTED_TO_FRPA -> !Objects.isNull(this.submittedToFrpa);
case SilvaOracleConstants.DISTURBANCE_DATE_START ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ca.bc.gov.restapi.results.oracle.entity;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public interface SilvicultureSearchProjection {
Long getOpeningId();
String getOpeningNumber();
String getCategory();
String getStatus();
String getCuttingPermitId();
String getTimberMark();
String getCutBlockId();
BigDecimal getOpeningGrossArea();
LocalDateTime getDisturbanceStartDate();
String getForestFileId();
String getOrgUnitCode();
String getOrgUnitName();
String getClientNumber();
String getClientLocation();
LocalDateTime getRegenDelayDate();
LocalDateTime getEarlyFreeGrowingDate();
LocalDateTime getLateFreeGrowingDate();
LocalDateTime getUpdateTimestamp();
String getEntryUserId();
Long getSubmittedToFrpa108();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import ca.bc.gov.restapi.results.oracle.dto.DashboardOrgUnitDto;
import ca.bc.gov.restapi.results.oracle.dto.DashboardResultsAuditDto;
import ca.bc.gov.restapi.results.oracle.dto.DashboardStockingEventDto;
import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchFiltersDto;
import ca.bc.gov.restapi.results.oracle.entity.OpeningEntity;
import ca.bc.gov.restapi.results.oracle.entity.SilvicultureSearchProjection;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -105,4 +107,186 @@ WHERE raac.RESULTS_AUDIT_ACTION_CODE IN (?1)
""",
nativeQuery = true)
List<DashboardActionCodeDto> findAllDashboardActionCodes(List<String> codes);

@Query(
value = """
SELECT opening_id, opening_number, category, status, cutting_permit_id, timber_mark,\s
cut_block_id, opening_gross_area, disturbance_start_date, forest_file_id,\s
org_unit_code, org_unit_name, client_number, client_location, regen_delay_date,\s
early_free_growing_date, late_free_growing_date, update_timestamp, entry_user_id,\s
submitted_to_frpa108
FROM (
SELECT
o.OPENING_ID AS opening_id,
o.OPENING_NUMBER AS opening_number,
o.OPEN_CATEGORY_CODE AS category,
o.OPENING_STATUS_CODE AS status,
cboa.CUTTING_PERMIT_ID AS cutting_permit_id,
cboa.TIMBER_MARK AS timber_mark,
cboa.CUT_BLOCK_ID AS cut_block_id,
cboa.OPENING_GROSS_AREA AS opening_gross_area,
cboa.DISTURBANCE_START_DATE AS disturbance_start_date,
cboa.FOREST_FILE_ID AS forest_file_id,
ou.ORG_UNIT_CODE AS org_unit_code,
ou.ORG_UNIT_NAME AS org_unit_name,
res.CLIENT_NUMBER AS client_number,
res.CLIENT_LOCN_CODE AS client_location,
ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMRG.LATE_OFFSET_YEARS, 0) * 12)) AS regen_delay_date,
ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.EARLY_OFFSET_YEARS, 0) * 12)) AS early_free_growing_date,
ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.LATE_OFFSET_YEARS, 0) * 12)) AS late_free_growing_date,
o.UPDATE_TIMESTAMP AS update_timestamp,
o.ENTRY_USERID AS entry_user_id,
COALESCE(sra.SILV_RELIEF_APPLICATION_ID, 0) AS submitted_to_frpa108,
ROW_NUMBER() OVER (PARTITION BY o.OPENING_ID ORDER BY o.UPDATE_TIMESTAMP DESC) AS rn
FROM THE.OPENING o
LEFT JOIN THE.CUT_BLOCK_OPEN_ADMIN cboa ON (cboa.OPENING_ID = o.OPENING_ID)
LEFT JOIN THE.ORG_UNIT ou ON (ou.ORG_UNIT_NO = o.ADMIN_DISTRICT_NO)
LEFT JOIN THE.RESULTS_ELECTRONIC_SUBMISSION res ON (res.RESULTS_SUBMISSION_ID = o.RESULTS_SUBMISSION_ID)
LEFT JOIN THE.SILV_RELIEF_APPLICATION sra ON (sra.ACTIVITY_TREATMENT_UNIT_ID = o.OPENING_ID AND sra.SILV_RELIEF_APPL_STATUS_CODE = 'APP')
LEFT JOIN THE.STOCKING_STANDARD_UNIT ssu ON (ssu.OPENING_ID = o.OPENING_ID)
LEFT JOIN THE.STOCKING_MILESTONE smrg ON (smrg.STOCKING_STANDARD_UNIT_ID = ssu.STOCKING_STANDARD_UNIT_ID AND SMRG.SILV_MILESTONE_TYPE_CODE = 'RG')
LEFT JOIN THE.STOCKING_MILESTONE smfg ON (smfg.STOCKING_STANDARD_UNIT_ID = ssu.STOCKING_STANDARD_UNIT_ID AND smfg.SILV_MILESTONE_TYPE_CODE = 'FG')
)
WHERE rn = 1 AND (
NVL(:#{#filter.mainSearchTerm},'NOVALUE') = 'NOVALUE' OR (
REGEXP_LIKE(:#{#filter.mainSearchTerm}, '^\\d+$')
AND OPENING_ID = TO_NUMBER(:#{#filter.mainSearchTerm})
) OR (
OPENING_NUMBER = :#{#filter.mainSearchTerm} OR
TIMBER_MARK = :#{#filter.mainSearchTerm} OR
FOREST_FILE_ID = :#{#filter.mainSearchTerm}
)
)
AND (
NVL(:#{#filter.orgUnit},'NOVALUE') = 'NOVALUE' OR ORG_UNIT_CODE IN (:#{#filter.orgUnit})
)
AND (
NVL(:#{#filter.category},'NOVALUE') = 'NOVALUE' OR CATEGORY IN (:#{#filter.category})
)
AND (
NVL(:#{#filter.statusList},'NOVALUE') = 'NOVALUE' OR STATUS IN (:#{#filter.statusList})
)
AND (
NVL(:#{#filter.requestUserId},'NOVALUE') = 'NOVALUE' OR ENTRY_USER_ID = :#{#filter.requestUserId}
)
AND (
NVL(:#{#filter.submittedToFrpa},'NO') = 'NO' OR (
NVL(:#{#filter.submittedToFrpa},'NO') = 'YES' AND submitted_to_frpa108 > 0
)
)
AND (
NVL(:#{#filter.disturbanceDateStart},'NOVALUE') = 'NOVALUE' OR DISTURBANCE_START_DATE >= TO_TIMESTAMP(:#{#filter.disturbanceDateStart},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.disturbanceDateEnd},'NOVALUE') = 'NOVALUE' OR DISTURBANCE_START_DATE <= TO_TIMESTAMP(:#{#filter.disturbanceDateEnd},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.regenDelayDateStart},'NOVALUE') = 'NOVALUE' OR regen_delay_date > TO_TIMESTAMP(:#{#filter.regenDelayDateStart},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.regenDelayDateEnd},'NOVALUE') = 'NOVALUE' OR regen_delay_date < TO_TIMESTAMP(:#{#filter.regenDelayDateEnd},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.freeGrowingDateStart},'NOVALUE') = 'NOVALUE' OR early_free_growing_date > TO_TIMESTAMP(:#{#filter.freeGrowingDateStart},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.freeGrowingDateEnd},'NOVALUE') = 'NOVALUE' OR late_free_growing_date < TO_TIMESTAMP(:#{#filter.freeGrowingDateEnd},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.updateDateStart},'NOVALUE') = 'NOVALUE' OR UPDATE_TIMESTAMP >= TO_TIMESTAMP(:#{#filter.updateDateStart}, 'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.updateDateEnd},'NOVALUE') = 'NOVALUE' OR UPDATE_TIMESTAMP <= TO_TIMESTAMP(:#{#filter.updateDateEnd}, 'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.cuttingPermitId},'NOVALUE') = 'NOVALUE' OR CUTTING_PERMIT_ID = :#{#filter.cuttingPermitId}
)
AND (
NVL(:#{#filter.cutBlockId},'NOVALUE') = 'NOVALUE' OR CUT_BLOCK_ID = :#{#filter.cutBlockId}
)
AND (
NVL(:#{#filter.timberMark},'NOVALUE') = 'NOVALUE' OR TIMBER_MARK = :#{#filter.timberMark}
)
AND (
NVL(:#{#filter.openingIds},'NOVALUE') = 'NOVALUE' OR OPENING_ID IN (:#{#filter.openingIds})
)""",
countQuery = """
SELECT count(o.OPENING_ID) as total
FROM THE.OPENING o
LEFT JOIN THE.CUT_BLOCK_OPEN_ADMIN cboa ON (cboa.OPENING_ID = o.OPENING_ID)
LEFT JOIN THE.ORG_UNIT ou ON (ou.ORG_UNIT_NO = o.ADMIN_DISTRICT_NO)
LEFT JOIN THE.RESULTS_ELECTRONIC_SUBMISSION res ON (res.RESULTS_SUBMISSION_ID = o.RESULTS_SUBMISSION_ID)
LEFT JOIN THE.SILV_RELIEF_APPLICATION sra ON (sra.ACTIVITY_TREATMENT_UNIT_ID = o.OPENING_ID AND sra.SILV_RELIEF_APPL_STATUS_CODE = 'APP')
LEFT JOIN THE.STOCKING_STANDARD_UNIT ssu ON (ssu.OPENING_ID = o.OPENING_ID)
LEFT JOIN THE.STOCKING_MILESTONE smrg ON (smrg.STOCKING_STANDARD_UNIT_ID = ssu.STOCKING_STANDARD_UNIT_ID AND SMRG.SILV_MILESTONE_TYPE_CODE = 'RG')
LEFT JOIN THE.STOCKING_MILESTONE smfg ON (smfg.STOCKING_STANDARD_UNIT_ID = ssu.STOCKING_STANDARD_UNIT_ID AND smfg.SILV_MILESTONE_TYPE_CODE = 'FG')
WHERE (
NVL(:#{#filter.mainSearchTerm},'NOVALUE') = 'NOVALUE' OR (
REGEXP_LIKE(:#{#filter.mainSearchTerm}, '^\\d+$')
AND o.OPENING_ID = TO_NUMBER(:#{#filter.mainSearchTerm})
) OR (
o.OPENING_NUMBER = :#{#filter.mainSearchTerm} OR
cboa.TIMBER_MARK = :#{#filter.mainSearchTerm} OR
cboa.FOREST_FILE_ID = :#{#filter.mainSearchTerm}
)
)
AND (
NVL(:#{#filter.orgUnit},'NOVALUE') = 'NOVALUE' OR ou.ORG_UNIT_CODE IN (:#{#filter.orgUnit})
)
AND (
NVL(:#{#filter.category},'NOVALUE') = 'NOVALUE' OR o.OPEN_CATEGORY_CODE IN (:#{#filter.category})
)
AND (
NVL(:#{#filter.statusList},'NOVALUE') = 'NOVALUE' OR o.OPENING_STATUS_CODE IN (:#{#filter.statusList})
)
AND (
NVL(:#{#filter.requestUserId},'NOVALUE') = 'NOVALUE' OR o.ENTRY_USERID = :#{#filter.requestUserId}
)
AND (
NVL(:#{#filter.submittedToFrpa},'NO') = 'NO' OR (
NVL(:#{#filter.submittedToFrpa},'NO') = 'YES' AND COALESCE(sra.SILV_RELIEF_APPLICATION_ID, 0) > 0
)
)
AND (
NVL(:#{#filter.disturbanceDateStart},'NOVALUE') = 'NOVALUE' OR cboa.DISTURBANCE_START_DATE >= TO_TIMESTAMP(:#{#filter.disturbanceDateStart},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.disturbanceDateEnd},'NOVALUE') = 'NOVALUE' OR cboa.DISTURBANCE_START_DATE <= TO_TIMESTAMP(:#{#filter.disturbanceDateEnd},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.regenDelayDateStart},'NOVALUE') = 'NOVALUE' OR ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMRG.LATE_OFFSET_YEARS, 0) * 12)) > TO_TIMESTAMP(:#{#filter.regenDelayDateStart},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.regenDelayDateEnd},'NOVALUE') = 'NOVALUE' OR ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMRG.LATE_OFFSET_YEARS, 0) * 12)) < TO_TIMESTAMP(:#{#filter.regenDelayDateEnd},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.freeGrowingDateStart},'NOVALUE') = 'NOVALUE' OR ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.EARLY_OFFSET_YEARS, 0) * 12)) > TO_TIMESTAMP(:#{#filter.freeGrowingDateStart},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.freeGrowingDateEnd},'NOVALUE') = 'NOVALUE' OR ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.LATE_OFFSET_YEARS, 0) * 12)) < TO_TIMESTAMP(:#{#filter.freeGrowingDateEnd},'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.updateDateStart},'NOVALUE') = 'NOVALUE' OR ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.LATE_OFFSET_YEARS, 0) * 12)) >= TO_TIMESTAMP(:#{#filter.updateDateStart}, 'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.updateDateEnd},'NOVALUE') = 'NOVALUE' OR ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.LATE_OFFSET_YEARS, 0) * 12)) <= TO_TIMESTAMP(:#{#filter.updateDateEnd}, 'YYYY-MM-DD')
)
AND (
NVL(:#{#filter.cuttingPermitId},'NOVALUE') = 'NOVALUE' OR cboa.CUTTING_PERMIT_ID = :#{#filter.cuttingPermitId}
)
AND (
NVL(:#{#filter.cutBlockId},'NOVALUE') = 'NOVALUE' OR cboa.CUT_BLOCK_ID = :#{#filter.cutBlockId}
)
AND (
NVL(:#{#filter.timberMark},'NOVALUE') = 'NOVALUE' OR cboa.TIMBER_MARK = :#{#filter.timberMark}
)
AND (
NVL(:#{#filter.openingIds},'NOVALUE') = 'NOVALUE' OR o.OPENING_ID IN (:#{#filter.openingIds})
)""",
nativeQuery = true
)
Page<SilvicultureSearchProjection> searchBy(
OpeningSearchFiltersDto filter,
Pageable pageable
);
}
Loading
Loading