From aeb5ee004e733dc7937d942bd14ead6261e7ef94 Mon Sep 17 00:00:00 2001 From: Paulo Gomes da Cruz Junior Date: Fri, 6 Dec 2024 03:56:07 -0800 Subject: [PATCH] fix(SILVA-584): fixing queries (#526) --- .../oracle/repository/OpeningRepository.java | 193 +++++++++++------- 1 file changed, 123 insertions(+), 70 deletions(-) diff --git a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpeningRepository.java b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpeningRepository.java index 73581a7a..9628bcde 100644 --- a/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpeningRepository.java +++ b/backend/src/main/java/ca/bc/gov/restapi/results/oracle/repository/OpeningRepository.java @@ -117,27 +117,26 @@ WHERE raac.RESULTS_AUDIT_ACTION_CODE IN (?1) 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 + o.OPENING_ID AS opening_id, + MAX(o.OPENING_NUMBER) AS opening_number, + MAX(o.OPEN_CATEGORY_CODE) AS category, + MAX(o.OPENING_STATUS_CODE) AS status, + MAX(cboa.CUTTING_PERMIT_ID) AS cutting_permit_id, + MAX(cboa.TIMBER_MARK) AS timber_mark, + MAX(cboa.CUT_BLOCK_ID) AS cut_block_id, + MAX(cboa.OPENING_GROSS_AREA) AS opening_gross_area, + MAX(cboa.DISTURBANCE_START_DATE) AS disturbance_start_date, + MAX(cboa.FOREST_FILE_ID) AS forest_file_id, + MAX(ou.ORG_UNIT_CODE) AS org_unit_code, + MAX(ou.ORG_UNIT_NAME) AS org_unit_name, + MAX(res.CLIENT_NUMBER) AS client_number, + MAX(res.CLIENT_LOCN_CODE) AS client_location, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMRG.LATE_OFFSET_YEARS, 0) * 12))) AS regen_delay_date, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.EARLY_OFFSET_YEARS, 0) * 12))) AS early_free_growing_date, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.LATE_OFFSET_YEARS, 0) * 12))) AS late_free_growing_date, + MAX(o.UPDATE_TIMESTAMP) AS update_timestamp, + MAX(o.ENTRY_USERID) AS entry_user_id, + MAX(COALESCE(sra.SILV_RELIEF_APPLICATION_ID, 0)) AS submitted_to_frpa108 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) @@ -147,8 +146,9 @@ LEFT JOIN THE.SILV_RELIEF_APPLICATION sra ON (sra.ACTIVITY_TREATMENT_UNIT_ID = a 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') + GROUP BY o.OPENING_ID ) - WHERE rn = 1 AND ( + WHERE ( NVL(:#{#filter.mainSearchTerm},'NOVALUE') = 'NOVALUE' OR ( REGEXP_LIKE(:#{#filter.mainSearchTerm}, '^\\d+$') AND OPENING_ID = TO_NUMBER(:#{#filter.mainSearchTerm}) @@ -212,17 +212,41 @@ LEFT JOIN THE.STOCKING_MILESTONE smfg ON (smfg.STOCKING_STANDARD_UNIT_ID = ssu.S NVL(:#{#filter.clientLocationCode},'NOVALUE') = 'NOVALUE' OR client_location = :#{#filter.clientLocationCode} )""", 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.ACTIVITY_TREATMENT_UNIT atu ON atu.OPENING_ID = o.OPENING_ID - LEFT JOIN THE.SILV_RELIEF_APPLICATION sra ON (sra.ACTIVITY_TREATMENT_UNIT_ID = atu.ACTIVITY_TREATMENT_UNIT_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 ( + SELECT count(opening_id) as total + FROM ( + SELECT + o.OPENING_ID AS opening_id, + MAX(o.OPENING_NUMBER) AS opening_number, + MAX(o.OPEN_CATEGORY_CODE) AS category, + MAX(o.OPENING_STATUS_CODE) AS status, + MAX(cboa.CUTTING_PERMIT_ID) AS cutting_permit_id, + MAX(cboa.TIMBER_MARK) AS timber_mark, + MAX(cboa.CUT_BLOCK_ID) AS cut_block_id, + MAX(cboa.OPENING_GROSS_AREA) AS opening_gross_area, + MAX(cboa.DISTURBANCE_START_DATE) AS disturbance_start_date, + MAX(cboa.FOREST_FILE_ID) AS forest_file_id, + MAX(ou.ORG_UNIT_CODE) AS org_unit_code, + MAX(ou.ORG_UNIT_NAME) AS org_unit_name, + MAX(res.CLIENT_NUMBER) AS client_number, + MAX(res.CLIENT_LOCN_CODE) AS client_location, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMRG.LATE_OFFSET_YEARS, 0) * 12))) AS regen_delay_date, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.EARLY_OFFSET_YEARS, 0) * 12))) AS early_free_growing_date, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.LATE_OFFSET_YEARS, 0) * 12))) AS late_free_growing_date, + MAX(o.UPDATE_TIMESTAMP) AS update_timestamp, + MAX(o.ENTRY_USERID) AS entry_user_id, + MAX(COALESCE(sra.SILV_RELIEF_APPLICATION_ID, 0)) AS submitted_to_frpa108 + 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.ACTIVITY_TREATMENT_UNIT atu ON atu.OPENING_ID = o.OPENING_ID + LEFT JOIN THE.SILV_RELIEF_APPLICATION sra ON (sra.ACTIVITY_TREATMENT_UNIT_ID = atu.ACTIVITY_TREATMENT_UNIT_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') + GROUP BY o.OPENING_ID + ) + WHERE ( NVL(:#{#filter.mainSearchTerm},'NOVALUE') = 'NOVALUE' OR ( REGEXP_LIKE(:#{#filter.mainSearchTerm}, '^\\d+$') AND o.OPENING_ID = TO_NUMBER(:#{#filter.mainSearchTerm}) @@ -294,34 +318,33 @@ Page searchBy( @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 + SELECT opening_id, opening_number, category, status, cutting_permit_id, timber_mark, + cut_block_id, opening_gross_area, disturbance_start_date, forest_file_id, + org_unit_code, org_unit_name, client_number, client_location, regen_delay_date, + early_free_growing_date, late_free_growing_date, update_timestamp, entry_user_id, 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 + o.OPENING_ID AS opening_id, + MAX(o.OPENING_NUMBER) AS opening_number, + MAX(o.OPEN_CATEGORY_CODE) AS category, + MAX(o.OPENING_STATUS_CODE) AS status, + MAX(cboa.CUTTING_PERMIT_ID) AS cutting_permit_id, + MAX(cboa.TIMBER_MARK) AS timber_mark, + MAX(cboa.CUT_BLOCK_ID) AS cut_block_id, + MAX(cboa.OPENING_GROSS_AREA) AS opening_gross_area, + MAX(cboa.DISTURBANCE_START_DATE) AS disturbance_start_date, + MAX(cboa.FOREST_FILE_ID) AS forest_file_id, + MAX(ou.ORG_UNIT_CODE) AS org_unit_code, + MAX(ou.ORG_UNIT_NAME) AS org_unit_name, + MAX(res.CLIENT_NUMBER) AS client_number, + MAX(res.CLIENT_LOCN_CODE) AS client_location, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMRG.LATE_OFFSET_YEARS, 0) * 12))) AS regen_delay_date, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.EARLY_OFFSET_YEARS, 0) * 12))) AS early_free_growing_date, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.LATE_OFFSET_YEARS, 0) * 12))) AS late_free_growing_date, + MAX(o.UPDATE_TIMESTAMP) AS update_timestamp, + MAX(o.ENTRY_USERID) AS entry_user_id, + MAX(COALESCE(sra.SILV_RELIEF_APPLICATION_ID, 0)) AS submitted_to_frpa108 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) @@ -331,20 +354,50 @@ LEFT JOIN THE.SILV_RELIEF_APPLICATION sra ON (sra.ACTIVITY_TREATMENT_UNIT_ID = a 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') + GROUP BY o.OPENING_ID ) - WHERE rn = 1 AND OPENING_ID IN :openingIds""", + WHERE 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.ACTIVITY_TREATMENT_UNIT atu ON atu.OPENING_ID = o.OPENING_ID - LEFT JOIN THE.SILV_RELIEF_APPLICATION sra ON (sra.ACTIVITY_TREATMENT_UNIT_ID = atu.ACTIVITY_TREATMENT_UNIT_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""", + SELECT count(opening_id) as total + SELECT opening_id, opening_number, category, status, cutting_permit_id, timber_mark, + cut_block_id, opening_gross_area, disturbance_start_date, forest_file_id, + org_unit_code, org_unit_name, client_number, client_location, regen_delay_date, + early_free_growing_date, late_free_growing_date, update_timestamp, entry_user_id, + submitted_to_frpa108 + FROM ( + SELECT + o.OPENING_ID AS opening_id, + MAX(o.OPENING_NUMBER) AS opening_number, + MAX(o.OPEN_CATEGORY_CODE) AS category, + MAX(o.OPENING_STATUS_CODE) AS status, + MAX(cboa.CUTTING_PERMIT_ID) AS cutting_permit_id, + MAX(cboa.TIMBER_MARK) AS timber_mark, + MAX(cboa.CUT_BLOCK_ID) AS cut_block_id, + MAX(cboa.OPENING_GROSS_AREA) AS opening_gross_area, + MAX(cboa.DISTURBANCE_START_DATE) AS disturbance_start_date, + MAX(cboa.FOREST_FILE_ID) AS forest_file_id, + MAX(ou.ORG_UNIT_CODE) AS org_unit_code, + MAX(ou.ORG_UNIT_NAME) AS org_unit_name, + MAX(res.CLIENT_NUMBER) AS client_number, + MAX(res.CLIENT_LOCN_CODE) AS client_location, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMRG.LATE_OFFSET_YEARS, 0) * 12))) AS regen_delay_date, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.EARLY_OFFSET_YEARS, 0) * 12))) AS early_free_growing_date, + MAX(ADD_MONTHS(cboa.DISTURBANCE_START_DATE, (COALESCE(SMFG.LATE_OFFSET_YEARS, 0) * 12))) AS late_free_growing_date, + MAX(o.UPDATE_TIMESTAMP) AS update_timestamp, + MAX(o.ENTRY_USERID) AS entry_user_id, + MAX(COALESCE(sra.SILV_RELIEF_APPLICATION_ID, 0)) AS submitted_to_frpa108 + 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.ACTIVITY_TREATMENT_UNIT atu ON atu.OPENING_ID = o.OPENING_ID + LEFT JOIN THE.SILV_RELIEF_APPLICATION sra ON (sra.ACTIVITY_TREATMENT_UNIT_ID = atu.ACTIVITY_TREATMENT_UNIT_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') + GROUP BY o.OPENING_ID + ) + WHERE opening_id IN :openingIds""", nativeQuery = true ) Page searchByOpeningIds(