diff --git a/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Controller.kt b/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Controller.kt index ef2dfc0b..f1a54bff 100644 --- a/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Controller.kt +++ b/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Controller.kt @@ -1,14 +1,14 @@ package nz.govt.eop.plan_limits -import mu.KotlinLogging import java.time.LocalDate import java.time.temporal.ChronoUnit.YEARS import java.util.concurrent.TimeUnit +import mu.KotlinLogging import org.jooq.* import org.springframework.http.CacheControl +import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.http.ResponseEntity -import org.springframework.http.HttpStatus import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping @@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.ResponseBody @Controller class Controller(val context: DSLContext, val queries: Queries, val manifest: Manifest) { - private val logger = KotlinLogging.logger {} + private val logger = KotlinLogging.logger {} @RequestMapping("/plan-limits/manifest", produces = [MediaType.APPLICATION_JSON_VALUE]) @ResponseBody @@ -71,42 +71,42 @@ class Controller(val context: DSLContext, val queries: Queries, val manifest: Ma @GetMapping("/plan-limits/surface-water-pnrp", produces = [MediaType.APPLICATION_JSON_VALUE]) @ResponseBody fun surfaceWaterPNRP(@RequestParam(name = "councilId") councilId: Int): ResponseEntity { - return try { - val result = queries.surfaceWaterPNRP(councilId) - - // Check if the result is empty or null and handle it - if (result.isEmpty()) { - ResponseEntity.status(HttpStatus.NOT_FOUND).body("No data found for the given council ID.") - } else { - ResponseEntity.ok() - .cacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) - .body(result) - } - } catch (e: Exception) { - logger.error("Error fetching surface water PNRP data", e) - // Return a generic Internal Server Error - ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An unexpected error occurred while processing your request. Please try again later.") + return try { + val result = queries.surfaceWaterPNRP(councilId) + + // Check if the result is empty or null and handle it + if (result.isEmpty()) { + ResponseEntity.status(HttpStatus.NOT_FOUND).body("No data found for the given council ID.") + } else { + ResponseEntity.ok().cacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)).body(result) } + } catch (e: Exception) { + logger.error("Error fetching surface water PNRP data", e) + // Return a generic Internal Server Error + ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body( + "An unexpected error occurred while processing your request. Please try again later.") + } } @GetMapping("/plan-limits/ground-water-pnrp", produces = [MediaType.APPLICATION_JSON_VALUE]) @ResponseBody fun groundWaterPNRP(@RequestParam(name = "councilId") councilId: Int): ResponseEntity { return try { - val result = queries.groundWaterPNRP(councilId) - - // Check if the result is empty or null and handle it - if (result.isEmpty()) { - ResponseEntity.status(HttpStatus.NOT_FOUND).body("No data found for the given council ID.") - } else { - ResponseEntity.ok() - .cacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) - .body(result) - } + val result = queries.groundWaterPNRP(councilId) + + // Check if the result is empty or null and handle it + if (result.isEmpty()) { + ResponseEntity.status(HttpStatus.NOT_FOUND).body("No data found for the given council ID.") + } else { + ResponseEntity.ok().cacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)).body(result) + } } catch (e: Exception) { - logger.error("Error fetching ground water PNRP data", e) - // Return a generic Internal Server Error - ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An unexpected error occurred while processing your request. Please try again later.") + logger.error("Error fetching ground water PNRP data", e) + // Return a generic Internal Server Error + ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body( + "An unexpected error occurred while processing your request. Please try again later.") } } diff --git a/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Manifest.kt b/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Manifest.kt index d3a1f92e..d86e15c6 100644 --- a/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Manifest.kt +++ b/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Manifest.kt @@ -38,10 +38,8 @@ class Manifest(val queries: Queries, val context: DSLContext) { "/plan-limits/surface-water-limits" to generateHash(queries.surfaceWaterLimits(councilId)), "/plan-limits/ground-water-limits" to generateHash(queries.groundwaterWaterLimits(councilId)), - "/plan-limits/surface-water-pnrp" to - generateHash(queries.surfaceWaterPNRP(councilId)), - "/plan-limits/ground-water-pnrp" to - generateHash(queries.groundWaterPNRP(councilId)), + "/plan-limits/surface-water-pnrp" to generateHash(queries.surfaceWaterPNRP(councilId)), + "/plan-limits/ground-water-pnrp" to generateHash(queries.groundWaterPNRP(councilId)), "/plan-limits/flow-measurement-sites" to generateHash(queries.flowMeasurementSites(councilId)), "/plan-limits/flow-limits" to generateHash(queries.flowLimits(councilId)), diff --git a/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Queries.kt b/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Queries.kt index 36552c05..c521c188 100644 --- a/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Queries.kt +++ b/packages/Manager/src/main/kotlin/nz/govt/eop/plan_limits/Queries.kt @@ -4,15 +4,15 @@ import java.time.LocalDate import nz.govt.eop.si.jooq.tables.Councils.Companion.COUNCILS import nz.govt.eop.si.jooq.tables.FlowLimits.Companion.FLOW_LIMITS import nz.govt.eop.si.jooq.tables.FlowMeasurementSites.Companion.FLOW_MEASUREMENT_SITES +import nz.govt.eop.si.jooq.tables.GroundwaterAllocationLimitsByAreaAndCategory.Companion.GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY import nz.govt.eop.si.jooq.tables.GroundwaterAreas.Companion.GROUNDWATER_AREAS import nz.govt.eop.si.jooq.tables.GroundwaterLimits.Companion.GROUNDWATER_LIMITS import nz.govt.eop.si.jooq.tables.PlanRegions.Companion.PLAN_REGIONS import nz.govt.eop.si.jooq.tables.Plans.Companion.PLANS import nz.govt.eop.si.jooq.tables.SurfaceWaterLimits.Companion.SURFACE_WATER_LIMITS +import nz.govt.eop.si.jooq.tables.SurfacewaterAllocationLimitsByAreaAndCategory.Companion.SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY import nz.govt.eop.si.jooq.tables.WaterAllocationAndUsageByArea.Companion.WATER_ALLOCATION_AND_USAGE_BY_AREA import nz.govt.eop.si.jooq.tables.WaterAllocationsByArea.Companion.WATER_ALLOCATIONS_BY_AREA -import nz.govt.eop.si.jooq.tables.GroundwaterAllocationLimitsByAreaAndCategory.Companion.GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY -import nz.govt.eop.si.jooq.tables.SurfacewaterAllocationLimitsByAreaAndCategory.Companion.SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY import org.jooq.* import org.jooq.impl.DSL import org.jooq.impl.DSL.* @@ -122,59 +122,59 @@ class Queries(@Autowired val context: DSLContext) { return buildFeatureCollection(context, innerQuery) } - fun surfaceWaterPNRP(councilId: Int): String { - val innerQuery = - select( - SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.AREA_ID, - SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PLAN_REGION_ID, - SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.CATEGORY_A, - SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.CATEGORY_B, - SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.SURFACE_TAKE, - SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.TOTAL_ALLOCATION, - SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.ALLOCATION_LIMIT, - SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PNRP_ALLOCATION_PERCENTAGE, - SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.NAME) - .from(SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY) - .where( - SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PLAN_REGION_ID.`in`( - select(PLAN_REGIONS.ID) - .from(PLAN_REGIONS) - .join(PLANS) - .on(PLAN_REGIONS.PLAN_ID.eq(PLANS.ID)) - .where(PLANS.COUNCIL_ID.eq(councilId)))) - val featureCollection: Field = - function("to_jsonb", JSONB::class.java, field("to_jsonb(inputs)")) + fun surfaceWaterPNRP(councilId: Int): String { + val innerQuery = + select( + SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.AREA_ID, + SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PLAN_REGION_ID, + SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.CATEGORY_A, + SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.CATEGORY_B, + SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.SURFACE_TAKE, + SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.TOTAL_ALLOCATION, + SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.ALLOCATION_LIMIT, + SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PNRP_ALLOCATION_PERCENTAGE, + SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.NAME) + .from(SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY) + .where( + SURFACEWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PLAN_REGION_ID.`in`( + select(PLAN_REGIONS.ID) + .from(PLAN_REGIONS) + .join(PLANS) + .on(PLAN_REGIONS.PLAN_ID.eq(PLANS.ID)) + .where(PLANS.COUNCIL_ID.eq(councilId)))) + val featureCollection: Field = + function("to_jsonb", JSONB::class.java, field("to_jsonb(inputs)")) - val result = context.select(featureCollection).from(innerQuery.asTable("inputs")).fetch() - return result.firstNotNullOf { it.value1().toString() } - } + val result = context.select(featureCollection).from(innerQuery.asTable("inputs")).fetch() + return result.firstNotNullOf { it.value1().toString() } + } - fun groundWaterPNRP(councilId: Int): String { - val innerQuery = - select( - GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.AREA_ID, - GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PLAN_REGION_ID, - GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.CATEGORY_B, - GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.CATEGORY_BC, - GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.CATEGORY_C, - GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.TOTAL_ALLOCATION, - GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.ALLOCATION_LIMIT, - GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PNRP_ALLOCATION_PERCENTAGE, - GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.NAME) - .from(GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY) - .where( - GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PLAN_REGION_ID.`in`( - select(PLAN_REGIONS.ID) - .from(PLAN_REGIONS) - .join(PLANS) - .on(PLAN_REGIONS.PLAN_ID.eq(PLANS.ID)) - .where(PLANS.COUNCIL_ID.eq(councilId)))) - val featureCollection: Field = - function("to_jsonb", JSONB::class.java, field("to_jsonb(inputs)")) + fun groundWaterPNRP(councilId: Int): String { + val innerQuery = + select( + GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.AREA_ID, + GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PLAN_REGION_ID, + GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.CATEGORY_B, + GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.CATEGORY_BC, + GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.CATEGORY_C, + GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.TOTAL_ALLOCATION, + GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.ALLOCATION_LIMIT, + GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PNRP_ALLOCATION_PERCENTAGE, + GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.NAME) + .from(GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY) + .where( + GROUNDWATER_ALLOCATION_LIMITS_BY_AREA_AND_CATEGORY.PLAN_REGION_ID.`in`( + select(PLAN_REGIONS.ID) + .from(PLAN_REGIONS) + .join(PLANS) + .on(PLAN_REGIONS.PLAN_ID.eq(PLANS.ID)) + .where(PLANS.COUNCIL_ID.eq(councilId)))) + val featureCollection: Field = + function("to_jsonb", JSONB::class.java, field("to_jsonb(inputs)")) - val result = context.select(featureCollection).from(innerQuery.asTable("inputs")).fetch() - return result.firstNotNullOf { it.value1().toString() } - } + val result = context.select(featureCollection).from(innerQuery.asTable("inputs")).fetch() + return result.firstNotNullOf { it.value1().toString() } + } fun flowMeasurementSites(councilId: Int): String { val innerQuery =