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

test: updating tests and some code refactor #428

Merged
merged 17 commits into from
Oct 29, 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
@@ -0,0 +1,26 @@
package ca.bc.gov.restapi.results.oracle;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SilvaOracleConstants {

public static final String ORG_UNIT = "orgUnit";
public static final String CATEGORY = "category";
public static final String STATUS_LIST = "statusList";
public static final String MY_OPENINGS = "myOpenings";
public static final String SUBMITTED_TO_FRPA = "submittedToFrpa";
public static final String DISTURBANCE_DATE_START = "disturbanceDateStart";
public static final String DISTURBANCE_DATE_END = "disturbanceDateEnd";
public static final String REGEN_DELAY_DATE_START = "regenDelayDateStart";
public static final String REGEN_DELAY_DATE_END = "regenDelayDateEnd";
public static final String FREE_GROWING_DATE_START = "freeGrowingDateStart";
public static final String FREE_GROWING_DATE_END = "freeGrowingDateEnd";
public static final String UPDATE_DATE_START = "updateDateStart";
public static final String UPDATE_DATE_END = "updateDateEnd";
public static final String CUTTING_PERMIT_ID = "cuttingPermitId";
public static final String CUT_BLOCK_ID = "cutBlockId";
public static final String TIMBER_MARK = "timberMark";
public static final String MAIN_SEARCH_TERM = "mainSearchTerm";
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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;

/** This record contains all possible filters when using the Opening Search API. */
@Slf4j
@Getter
@ToString
public class OpeningSearchFiltersDto {
private final String orgUnit;
private final String category;
Expand All @@ -29,24 +33,7 @@ public class OpeningSearchFiltersDto {
// Main input, it can be one of Opening ID, Opening Number, Timber Mark ID, or File ID
private final String mainSearchTerm;

public static final String ORG_UNIT = "orgUnit";
public static final String CATEGORY = "category";
public static final String STATUS_LIST = "statusList";
public static final String MY_OPENINGS = "myOpenings";
public static final String SUBMITTED_TO_FRPA = "submittedToFrpa";
public static final String DISTURBANCE_DATE_START = "disturbanceDateStart";
public static final String DISTURBANCE_DATE_END = "disturbanceDateEnd";
public static final String REGEN_DELAY_DATE_START = "regenDelayDateStart";
public static final String REGEN_DELAY_DATE_END = "regenDelayDateEnd";
public static final String FREE_GROWING_DATE_START = "freeGrowingDateStart";
public static final String FREE_GROWING_DATE_END = "freeGrowingDateEnd";
public static final String UPDATE_DATE_START = "updateDateStart";
public static final String UPDATE_DATE_END = "updateDateEnd";
public static final String CUTTING_PERMIT_ID = "cuttingPermitId";
public static final String CUT_BLOCK_ID = "cutBlockId";
public static final String TIMBER_MARK = "timberMark";
public static final String MAIN_SEARCH_TERM = "mainSearchTerm";

@Setter
private String requestUserId;

/** Creates an instance of the search opening filter dto. */
Expand Down Expand Up @@ -95,61 +82,37 @@ public OpeningSearchFiltersDto(
Objects.isNull(mainSearchTerm) ? null : mainSearchTerm.toUpperCase().trim();
}

/**
* Set the Request User Id.
*
* @param requestUserId The id to be set.
*/
public void setRequestUserId(String requestUserId) {
this.requestUserId = requestUserId;
}

/**
* Define if a property has value.
*
* @param prop The property to be checked.
* @return True if it has, false otherwise.
*/
public boolean hasValue(String prop) {
switch (prop) {
case ORG_UNIT:
return !Objects.isNull(this.orgUnit);
case CATEGORY:
return !Objects.isNull(this.category);
case STATUS_LIST:
return !this.statusList.isEmpty();
case MY_OPENINGS:
return !Objects.isNull(this.myOpenings);
case SUBMITTED_TO_FRPA:
return !Objects.isNull(this.submittedToFrpa);
case DISTURBANCE_DATE_START:
return !Objects.isNull(this.disturbanceDateStart);
case DISTURBANCE_DATE_END:
return !Objects.isNull(this.disturbanceDateEnd);
case REGEN_DELAY_DATE_START:
return !Objects.isNull(this.regenDelayDateStart);
case REGEN_DELAY_DATE_END:
return !Objects.isNull(this.regenDelayDateEnd);
case FREE_GROWING_DATE_START:
return !Objects.isNull(this.freeGrowingDateStart);
case FREE_GROWING_DATE_END:
return !Objects.isNull(this.freeGrowingDateEnd);
case UPDATE_DATE_START:
return !Objects.isNull(this.updateDateStart);
case UPDATE_DATE_END:
return !Objects.isNull(this.updateDateEnd);
case CUTTING_PERMIT_ID:
return !Objects.isNull(this.cuttingPermitId);
case CUT_BLOCK_ID:
return !Objects.isNull(this.cutBlockId);
case TIMBER_MARK:
return !Objects.isNull(this.timberMark);
case MAIN_SEARCH_TERM:
return !Objects.isNull(this.mainSearchTerm);
default: {
return switch (prop) {
case SilvaOracleConstants.ORG_UNIT -> !Objects.isNull(this.orgUnit);
case SilvaOracleConstants.CATEGORY -> !Objects.isNull(this.category);
case SilvaOracleConstants.STATUS_LIST -> !this.statusList.isEmpty();
case SilvaOracleConstants.MY_OPENINGS -> !Objects.isNull(this.myOpenings);
case SilvaOracleConstants.SUBMITTED_TO_FRPA -> !Objects.isNull(this.submittedToFrpa);
case SilvaOracleConstants.DISTURBANCE_DATE_START ->
!Objects.isNull(this.disturbanceDateStart);
case SilvaOracleConstants.DISTURBANCE_DATE_END -> !Objects.isNull(this.disturbanceDateEnd);
case SilvaOracleConstants.REGEN_DELAY_DATE_START -> !Objects.isNull(this.regenDelayDateStart);
case SilvaOracleConstants.REGEN_DELAY_DATE_END -> !Objects.isNull(this.regenDelayDateEnd);
case SilvaOracleConstants.FREE_GROWING_DATE_START ->
!Objects.isNull(this.freeGrowingDateStart);
case SilvaOracleConstants.FREE_GROWING_DATE_END -> !Objects.isNull(this.freeGrowingDateEnd);
case SilvaOracleConstants.UPDATE_DATE_START -> !Objects.isNull(this.updateDateStart);
case SilvaOracleConstants.UPDATE_DATE_END -> !Objects.isNull(this.updateDateEnd);
case SilvaOracleConstants.CUTTING_PERMIT_ID -> !Objects.isNull(this.cuttingPermitId);
case SilvaOracleConstants.CUT_BLOCK_ID -> !Objects.isNull(this.cutBlockId);
case SilvaOracleConstants.TIMBER_MARK -> !Objects.isNull(this.timberMark);
case SilvaOracleConstants.MAIN_SEARCH_TERM -> !Objects.isNull(this.mainSearchTerm);
default -> {
log.warn("Prop not found {}", prop);
return false;
yield false;
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ca.bc.gov.restapi.results.common.SilvaConstants;
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.SilvaOracleConstants;
import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchFiltersDto;
import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto;
import ca.bc.gov.restapi.results.oracle.enums.OpeningCategoryEnum;
Expand Down Expand Up @@ -241,7 +242,7 @@ private Query setQueryParameters(OpeningSearchFiltersDto filtersDto, String nati
Query query = em.createNativeQuery(nativeQuery);

// set parameters
if (filtersDto.hasValue(OpeningSearchFiltersDto.MAIN_SEARCH_TERM)) {
if (filtersDto.hasValue(SilvaOracleConstants.MAIN_SEARCH_TERM)) {
boolean itsNumeric = filtersDto.getMainSearchTerm().replaceAll("[0-9]", "").isEmpty();
if (itsNumeric) {
log.info("Setting mainSearchTerm as numeric filter value");
Expand All @@ -255,83 +256,83 @@ private Query setQueryParameters(OpeningSearchFiltersDto filtersDto, String nati
}

// 1. Org Unit code
if (filtersDto.hasValue(OpeningSearchFiltersDto.ORG_UNIT)) {
if (filtersDto.hasValue(SilvaOracleConstants.ORG_UNIT)) {
log.info("Setting orgUnit filter value");
query.setParameter("orgUnit", filtersDto.getOrgUnit());
}
// 2. Category code
if (filtersDto.hasValue(OpeningSearchFiltersDto.CATEGORY)) {
if (filtersDto.hasValue(SilvaOracleConstants.CATEGORY)) {
log.info("Setting category filter value");
query.setParameter("category", filtersDto.getCategory());
}
// 3. Status list codes
if (filtersDto.hasValue(OpeningSearchFiltersDto.STATUS_LIST)) {
if (filtersDto.hasValue(SilvaOracleConstants.STATUS_LIST)) {

log.info("Setting statusList filter values");
// No need to set value since the query already dit it. Didn't work set through named param
}
// 4. User entry id
if (filtersDto.hasValue(OpeningSearchFiltersDto.MY_OPENINGS)) {
if (filtersDto.hasValue(SilvaOracleConstants.MY_OPENINGS)) {
log.info("Setting myOpenings filter value");
query.setParameter("entryUserId", filtersDto.getRequestUserId());
}
// 5. Submitted to FRPA Section 108
if (filtersDto.hasValue(OpeningSearchFiltersDto.SUBMITTED_TO_FRPA)) {
if (filtersDto.hasValue(SilvaOracleConstants.SUBMITTED_TO_FRPA)) {
log.info("Setting submitted to FRPA filter!");
// No need to set value since the query already dit it.
}
// 6. Disturbance start date
if (filtersDto.hasValue(OpeningSearchFiltersDto.DISTURBANCE_DATE_START)) {
if (filtersDto.hasValue(SilvaOracleConstants.DISTURBANCE_DATE_START)) {
log.info("Setting disturbanceDateStart filter value");
query.setParameter("disturbStartDate", filtersDto.getDisturbanceDateStart());
}
// 7. Disturbance end date
if (filtersDto.hasValue(OpeningSearchFiltersDto.DISTURBANCE_DATE_END)) {
if (filtersDto.hasValue(SilvaOracleConstants.DISTURBANCE_DATE_END)) {
log.info("Setting disturbanceDateEnd filter value");
query.setParameter("disturbEndDate", filtersDto.getDisturbanceDateEnd());
}
// 8. Regen delay start date
if (filtersDto.hasValue(OpeningSearchFiltersDto.REGEN_DELAY_DATE_START)) {
if (filtersDto.hasValue(SilvaOracleConstants.REGEN_DELAY_DATE_START)) {
log.info("Setting regenDelayDateStart filter value");
query.setParameter("regenDelayDateStart", filtersDto.getRegenDelayDateStart());
}
// 9. Regen delay end date
if (filtersDto.hasValue(OpeningSearchFiltersDto.REGEN_DELAY_DATE_END)) {
if (filtersDto.hasValue(SilvaOracleConstants.REGEN_DELAY_DATE_END)) {
log.info("Setting regenDelayDateEnd filter value");
query.setParameter("regenDelayDateEnd", filtersDto.getRegenDelayDateEnd());
}
// 10. Free growing start date
if (filtersDto.hasValue(OpeningSearchFiltersDto.FREE_GROWING_DATE_START)) {
if (filtersDto.hasValue(SilvaOracleConstants.FREE_GROWING_DATE_START)) {
log.info("Setting freeGrowingDateStart filter value");
query.setParameter("freeGrowingDateStart", filtersDto.getFreeGrowingDateStart());
}
// 11. Free growing end date
if (filtersDto.hasValue(OpeningSearchFiltersDto.FREE_GROWING_DATE_END)) {
if (filtersDto.hasValue(SilvaOracleConstants.FREE_GROWING_DATE_END)) {
log.info("Setting freeGrowingDateEnd filter value");
query.setParameter("freeGrowingDateEnd", filtersDto.getFreeGrowingDateEnd());
}
// 12. Update date start
if (filtersDto.hasValue(OpeningSearchFiltersDto.UPDATE_DATE_START)) {
if (filtersDto.hasValue(SilvaOracleConstants.UPDATE_DATE_START)) {
log.info("Setting updateDateStart filter value");
query.setParameter("updateStartDate", filtersDto.getUpdateDateStart());
}
// 13. Update date end
if (filtersDto.hasValue(OpeningSearchFiltersDto.UPDATE_DATE_END)) {
if (filtersDto.hasValue(SilvaOracleConstants.UPDATE_DATE_END)) {
log.info("Setting updateDateEnd filter value");
query.setParameter("updateEndDate", filtersDto.getUpdateDateEnd());
}
// 14. Cutting permit id
if (filtersDto.hasValue(OpeningSearchFiltersDto.CUTTING_PERMIT_ID)) {
if (filtersDto.hasValue(SilvaOracleConstants.CUTTING_PERMIT_ID)) {
log.info("Setting cuttingPermitId filter value");
query.setParameter("cuttingPermitId", filtersDto.getCuttingPermitId());
}
// 15. Cut block id
if (filtersDto.hasValue(OpeningSearchFiltersDto.CUT_BLOCK_ID)) {
if (filtersDto.hasValue(SilvaOracleConstants.CUT_BLOCK_ID)) {
log.info("Setting cutBlockId filter value");
query.setParameter("cutBlockId", filtersDto.getCutBlockId());
}
// 16. Timber mark
if (filtersDto.hasValue(OpeningSearchFiltersDto.TIMBER_MARK)) {
if (filtersDto.hasValue(SilvaOracleConstants.TIMBER_MARK)) {
log.info("Setting timberMark filter value");
query.setParameter("timberMark", filtersDto.getTimberMark());
}
Expand Down Expand Up @@ -392,7 +393,7 @@ private String createNativeSqlQuery(OpeningSearchFiltersDto filtersDto) {
// 0. Main number filter [opening_id, opening_number, timber_mark, file_id]
// if it's a number, filter by openingId or fileId, otherwise filter by timber mark and opening
// number
if (filtersDto.hasValue(OpeningSearchFiltersDto.MAIN_SEARCH_TERM)) {
if (filtersDto.hasValue(SilvaOracleConstants.MAIN_SEARCH_TERM)) {
log.info("Filter mainSearchTerm detected! mainSearchTerm={}", filtersDto.getMainSearchTerm());
boolean itsNumeric = filtersDto.getMainSearchTerm().replaceAll("[0-9]", "").isEmpty();
if (itsNumeric) {
Expand All @@ -412,28 +413,28 @@ private String createNativeSqlQuery(OpeningSearchFiltersDto filtersDto) {
}

// 1. Org Unit code
if (filtersDto.hasValue(OpeningSearchFiltersDto.ORG_UNIT)) {
if (filtersDto.hasValue(SilvaOracleConstants.ORG_UNIT)) {
log.info("Filter orgUnit detected! orgUnit={}", filtersDto.getOrgUnit());
builder.append("AND ou.ORG_UNIT_CODE = :orgUnit ");
}
// 2. Category code
if (filtersDto.hasValue(OpeningSearchFiltersDto.CATEGORY)) {
if (filtersDto.hasValue(SilvaOracleConstants.CATEGORY)) {
log.info("Filter category detected! category={}", filtersDto.getCategory());
builder.append("AND o.OPEN_CATEGORY_CODE = :category ");
}
// 3. Status code
if (filtersDto.hasValue(OpeningSearchFiltersDto.STATUS_LIST)) {
if (filtersDto.hasValue(SilvaOracleConstants.STATUS_LIST)) {
String statuses = String.join(",", filtersDto.getStatusList());
log.info("Filter statusList detected! statusList={}", statuses);
builder.append(String.format("AND o.OPENING_STATUS_CODE IN (%s) ", statuses));
}
// 4. My openings
if (filtersDto.hasValue(OpeningSearchFiltersDto.MY_OPENINGS)) {
if (filtersDto.hasValue(SilvaOracleConstants.MY_OPENINGS)) {
log.info("Filter myOpenings detected! entryUserId={}", filtersDto.getRequestUserId());
builder.append("AND o.ENTRY_USERID = :entryUserId ");
}
// 5. Submitted to FRPA
if (filtersDto.hasValue(OpeningSearchFiltersDto.SUBMITTED_TO_FRPA)) {
if (filtersDto.hasValue(SilvaOracleConstants.SUBMITTED_TO_FRPA)) {
Boolean value = filtersDto.getSubmittedToFrpa();
if (Boolean.FALSE.equals(value)) {
log.info(
Expand All @@ -446,70 +447,70 @@ private String createNativeSqlQuery(OpeningSearchFiltersDto filtersDto) {
}
}
// 6. Disturbance start date
if (filtersDto.hasValue(OpeningSearchFiltersDto.DISTURBANCE_DATE_START)) {
if (filtersDto.hasValue(SilvaOracleConstants.DISTURBANCE_DATE_START)) {
log.info(
"Filter disturbanceDateStart detected! date={}", filtersDto.getDisturbanceDateStart());
builder.append(
"AND cboa.DISTURBANCE_START_DATE >= to_timestamp(:disturbStartDate, 'YYYY-MM-DD') ");
}
// 7. Disturbance end date
if (filtersDto.hasValue(OpeningSearchFiltersDto.DISTURBANCE_DATE_END)) {
if (filtersDto.hasValue(SilvaOracleConstants.DISTURBANCE_DATE_END)) {
log.info("Filter disturbanceDateEnd detected! date={}", filtersDto.getDisturbanceDateEnd());
builder.append(
"AND cboa.DISTURBANCE_START_DATE <= to_timestamp(:disturbEndDate, 'YYYY-MM-DD') ");
}
// 8. Regen delay start date
if (filtersDto.hasValue(OpeningSearchFiltersDto.REGEN_DELAY_DATE_START)) {
if (filtersDto.hasValue(SilvaOracleConstants.REGEN_DELAY_DATE_START)) {
log.info("Filter regenDelayDateStart detected! date={}", filtersDto.getRegenDelayDateStart());
builder.append("AND ADD_MONTHS(cboa.DISTURBANCE_START_DATE, ");
builder.append("COALESCE(SMRG.LATE_OFFSET_YEARS,0)*12) ");
builder.append("> to_timestamp(:regenDelayDateStart,'YYYY-MM-DD')");
}
// 9. Regen delay end date
if (filtersDto.hasValue(OpeningSearchFiltersDto.REGEN_DELAY_DATE_END)) {
if (filtersDto.hasValue(SilvaOracleConstants.REGEN_DELAY_DATE_END)) {
log.info("Filter regenDelayDateEnd detected! date={}", filtersDto.getRegenDelayDateEnd());
builder.append("AND ADD_MONTHS(cboa.DISTURBANCE_START_DATE, ");
builder.append("COALESCE(SMRG.LATE_OFFSET_YEARS,0)*12) ");
builder.append("< to_timestamp(:regenDelayDateEnd,'YYYY-MM-DD')");
}
// 10. Free growing start date
if (filtersDto.hasValue(OpeningSearchFiltersDto.FREE_GROWING_DATE_START)) {
if (filtersDto.hasValue(SilvaOracleConstants.FREE_GROWING_DATE_START)) {
log.info(
"Filter freeGrowingDateStart detected! date={}", filtersDto.getFreeGrowingDateStart());
builder.append("AND ADD_MONTHS(cboa.DISTURBANCE_START_DATE, ");
builder.append("COALESCE(SMFG.EARLY_OFFSET_YEARS,0)*12) ");
builder.append("> to_timestamp(:freeGrowingDateStart,'YYYY-MM-DD')");
}
// 11. Free growing end date
if (filtersDto.hasValue(OpeningSearchFiltersDto.FREE_GROWING_DATE_END)) {
if (filtersDto.hasValue(SilvaOracleConstants.FREE_GROWING_DATE_END)) {
log.info("Filter freeGrowingDateEnd detected! date={}", filtersDto.getFreeGrowingDateEnd());
builder.append("AND ADD_MONTHS(cboa.DISTURBANCE_START_DATE, ");
builder.append("COALESCE(SMFG.LATE_OFFSET_YEARS,0)*12) ");
builder.append("< to_timestamp(:freeGrowingDateEnd, 'YYYY-MM-DD')");
}
// 12. Update date start
if (filtersDto.hasValue(OpeningSearchFiltersDto.UPDATE_DATE_START)) {
if (filtersDto.hasValue(SilvaOracleConstants.UPDATE_DATE_START)) {
log.info("Filter updateDateStart detected! date={}", filtersDto.getUpdateDateStart());
builder.append("AND o.UPDATE_TIMESTAMP >= to_timestamp(:updateStartDate, 'YYYY-MM-DD') ");
}
// 13. Update date end
if (filtersDto.hasValue(OpeningSearchFiltersDto.UPDATE_DATE_END)) {
if (filtersDto.hasValue(SilvaOracleConstants.UPDATE_DATE_END)) {
log.info("Filter updateDateEnd detected! date={}", filtersDto.getUpdateDateEnd());
builder.append("AND o.UPDATE_TIMESTAMP <= to_timestamp(:updateEndDate, 'YYYY-MM-DD') ");
}
// 14. Cutting permit id
if (filtersDto.hasValue(OpeningSearchFiltersDto.CUTTING_PERMIT_ID)) {
if (filtersDto.hasValue(SilvaOracleConstants.CUTTING_PERMIT_ID)) {
log.info(
"Filter cuttingPermitId detected! cuttingPermitId={}", filtersDto.getCuttingPermitId());
builder.append("AND cboa.CUTTING_PERMIT_ID = :cuttingPermitId ");
}
// 15. Cut block id
if (filtersDto.hasValue(OpeningSearchFiltersDto.CUT_BLOCK_ID)) {
if (filtersDto.hasValue(SilvaOracleConstants.CUT_BLOCK_ID)) {
log.info("Filter cutBlockId detected! cutBlockId={}", filtersDto.getCutBlockId());
builder.append("AND cboa.CUT_BLOCK_ID = :cutBlockId ");
}
// 16. Timber mark
if (filtersDto.hasValue(OpeningSearchFiltersDto.TIMBER_MARK)) {
if (filtersDto.hasValue(SilvaOracleConstants.TIMBER_MARK)) {
log.info("Filter timberMark detected! timberMark={}", filtersDto.getTimberMark());
builder.append("AND cboa.TIMBER_MARK = :timberMark ");
}
Expand Down
Loading
Loading