Skip to content

Commit

Permalink
fix(SILVA-511): fixing opening map popup (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj authored Nov 22, 2024
1 parent ce6c3ef commit 3ed7e91
Show file tree
Hide file tree
Showing 15 changed files with 392 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class OpeningSearchFiltersDto {

@Setter
private String requestUserId;
private List<Long> openingIds;

/** Creates an instance of the search opening filter dto. */
public OpeningSearchFiltersDto(
Expand All @@ -59,7 +58,6 @@ public OpeningSearchFiltersDto(
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 =
BooleanUtils
Expand Down Expand Up @@ -88,27 +86,6 @@ public OpeningSearchFiltersDto(
Objects.isNull(mainSearchTerm) ? null : mainSearchTerm.toUpperCase().trim();
}

// Create a constructor with only the List<String> openingIds
public OpeningSearchFiltersDto(List<Long> openingIds) {
this.orgUnit = null;
this.category = null;
this.statusList = null;
this.openingIds = openingIds;
this.myOpenings = null;
this.submittedToFrpa = "NO";
this.disturbanceDateStart = null;
this.disturbanceDateEnd = null;
this.regenDelayDateStart = null;
this.regenDelayDateEnd = null;
this.freeGrowingDateStart = null;
this.freeGrowingDateEnd = null;
this.updateDateStart = null;
this.updateDateEnd = null;
this.cuttingPermitId = null;
this.cutBlockId = null;
this.timberMark = null;
this.mainSearchTerm = null;
}
/**
* Define if a property has value.
*
Expand All @@ -120,7 +97,6 @@ public boolean hasValue(String prop) {
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
Expand Up @@ -206,9 +206,6 @@ LEFT JOIN THE.STOCKING_MILESTONE smfg ON (smfg.STOCKING_STANDARD_UNIT_ID = ssu.S
)
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
Expand Down Expand Up @@ -279,14 +276,69 @@ LEFT JOIN THE.STOCKING_MILESTONE smfg ON (smfg.STOCKING_STANDARD_UNIT_ID = ssu.S
)
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
);

@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 OPENING_ID IN :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 o.OPENING_ID IN :openingIds""",
nativeQuery = true
)
Page<SilvicultureSearchProjection> searchByOpeningIds(
List<Long> openingIds,
Pageable pageable
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public PaginatedResult<OpeningSearchResponseDto> openingSearch(
pagination.toPageable(Sort.by("opening_id").descending())
);

return parsePageResult(pagination, searchResultPage);
}

public PaginatedResult<OpeningSearchResponseDto> parsePageResult(
PaginationParameters pagination, Page<SilvicultureSearchProjection> searchResultPage) {
PaginatedResult<OpeningSearchResponseDto> result = new PaginatedResult<>();
result.setTotalItems(searchResultPage.getTotalElements());
result.setPageIndex(pagination.page());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public List<Long> getFavorites() {
return userOpeningService.listUserFavoriteOpenings();
}

@GetMapping("/{id}")
public boolean checkFavorite(@PathVariable Long id) {
return !userOpeningService.checkForFavorites(List.of(id)).isEmpty();
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
public void addToFavorites(@PathVariable Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import ca.bc.gov.restapi.results.common.pagination.PaginatedResult;
import ca.bc.gov.restapi.results.common.pagination.PaginationParameters;
import ca.bc.gov.restapi.results.common.security.LoggedUserService;
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.repository.OpeningRepository;
import ca.bc.gov.restapi.results.oracle.service.OpeningService;
Expand All @@ -30,87 +29,91 @@
@RequiredArgsConstructor
public class UserRecentOpeningService {

private final LoggedUserService loggedUserService;
private final UserRecentOpeningRepository userRecentOpeningRepository;
private final OpeningService openingService;
private final OpeningRepository openingRepository;
private final LoggedUserService loggedUserService;
private final UserRecentOpeningRepository userRecentOpeningRepository;
private final OpeningService openingService;
private final OpeningRepository openingRepository;

@Transactional
public UserRecentOpeningDto storeViewedOpening(Long openingId) {
log.info("Adding opening ID {} as recently viewed for user {}", openingId,
loggedUserService.getLoggedUserId());
@Transactional
public UserRecentOpeningDto storeViewedOpening(Long openingId) {
log.info("Adding opening ID {} as recently viewed for user {}", openingId,
loggedUserService.getLoggedUserId());

if(openingId == null) {
log.info("Opening ID is null");
throw new IllegalArgumentException("Opening ID must contain numbers only!");
}
if (openingId == null) {
log.info("Opening ID is null");
throw new IllegalArgumentException("Opening ID must contain numbers only!");
}

if (!openingRepository.existsById(openingId)) {
log.info("Opening ID not found: {}", openingId);
throw new OpeningNotFoundException();
}
if (!openingRepository.existsById(openingId)) {
log.info("Opening ID not found: {}", openingId);
throw new OpeningNotFoundException();
}

LocalDateTime lastViewed = LocalDateTime.now();
LocalDateTime lastViewed = LocalDateTime.now();

userRecentOpeningRepository.saveAndFlush(
userRecentOpeningRepository.saveAndFlush(
userRecentOpeningRepository
.findByUserIdAndOpeningId(loggedUserService.getLoggedUserId(), openingId)
.map(entity -> entity.withLastViewed(lastViewed))
.orElse(
new UserRecentOpeningEntity(null,loggedUserService.getLoggedUserId(),openingId,lastViewed)
new UserRecentOpeningEntity(null, loggedUserService.getLoggedUserId(), openingId,
lastViewed)
)
);

// Return the DTO
return new UserRecentOpeningDto(
loggedUserService.getLoggedUserId(),
openingId,
lastViewed
);
}

/**
* Retrieves the recent openings viewed by the logged-in user, limited by the provided limit.
*
* @param limit The maximum number of recent openings to retrieve.
* @return A list of opening IDs the user has viewed, sorted by last viewed in descending order.
*/
public PaginatedResult<OpeningSearchResponseDto> getAllRecentOpeningsForUser(int limit) {
String userId = loggedUserService.getLoggedUserId();
Pageable pageable = PageRequest.of(0, limit); // PageRequest object to apply limit

// Fetch recent openings for the user
Page<UserRecentOpeningEntity> recentOpenings = userRecentOpeningRepository
.findByUserIdOrderByLastViewedDesc(userId, pageable);

// Extract opening IDs as String
Map<Long, LocalDateTime> openingIds = recentOpenings.getContent().stream()
.collect(Collectors.toMap(UserRecentOpeningEntity::getOpeningId,
UserRecentOpeningEntity::getLastViewed));
log.info("User with the userId {} has the following openingIds {}", userId, openingIds);

if (openingIds.isEmpty()) {
// Ensure an empty data list instead of null
return new PaginatedResult<OpeningSearchResponseDto>().withData(Collections.emptyList());
}

PaginatedResult<OpeningSearchResponseDto> pageResult =
openingService.parsePageResult(
new PaginationParameters(0, 10),
openingRepository
.searchByOpeningIds(new ArrayList<>(openingIds.keySet()),
PageRequest.of(0, 10)
)
);

// Return the DTO
return new UserRecentOpeningDto(
loggedUserService.getLoggedUserId(),
openingId,
lastViewed

return pageResult
.withData(
pageResult
.getData()
.stream()
.map(result -> result.withLastViewDate(
openingIds.get(result.getOpeningId().longValue())))
.sorted(Comparator.comparing(OpeningSearchResponseDto::getLastViewDate).reversed())
.toList()
);
}
}


/**
* Retrieves the recent openings viewed by the logged-in user, limited by the provided limit.
*
* @param limit The maximum number of recent openings to retrieve.
* @return A list of opening IDs the user has viewed, sorted by last viewed in descending order.
*/
public PaginatedResult<OpeningSearchResponseDto> getAllRecentOpeningsForUser(int limit) {
String userId = loggedUserService.getLoggedUserId();
Pageable pageable = PageRequest.of(0, limit); // PageRequest object to apply limit

// Fetch recent openings for the user
Page<UserRecentOpeningEntity> recentOpenings = userRecentOpeningRepository
.findByUserIdOrderByLastViewedDesc(userId, pageable);

// Extract opening IDs as String
Map<Long, LocalDateTime> openingIds = recentOpenings.getContent().stream()
.collect(Collectors.toMap(UserRecentOpeningEntity::getOpeningId, UserRecentOpeningEntity::getLastViewed));
log.info("User with the userId {} has the following openingIds {}", userId, openingIds);

if (openingIds.isEmpty()) {
// Ensure an empty data list instead of null
return new PaginatedResult<OpeningSearchResponseDto>()
.withData(Collections.emptyList());
}

PaginatedResult<OpeningSearchResponseDto> pageResult =
openingService
.openingSearch(
new OpeningSearchFiltersDto(new ArrayList<>(openingIds.keySet())),
new PaginationParameters(0, 10)
);

return pageResult
.withData(
pageResult
.getData()
.stream()
.map(result -> result.withLastViewDate(openingIds.get(result.getOpeningId().longValue())))
.sorted(Comparator.comparing(OpeningSearchResponseDto::getLastViewDate).reversed())
.toList()
);
}


}
Loading

0 comments on commit 3ed7e91

Please sign in to comment.