|
| 1 | +package ca.bc.gov.nrs.ilcr.reporting; |
| 2 | + |
| 3 | +import ca.bc.gov.nrs.ilcr.dto.base.CodeDescriptionDto; |
| 4 | +import ca.bc.gov.nrs.ilcr.schedule10.dto.ConstructionPage; |
| 5 | +import ca.bc.gov.nrs.ilcr.schedule10.dto.MaterialComposition; |
| 6 | +import ca.bc.gov.nrs.ilcr.schedule10.dto.RoadDetail; |
| 7 | +import ca.bc.gov.nrs.ilcr.schedule10.dto.Schedule10CodeLists; |
| 8 | +import ca.bc.gov.nrs.ilcr.schedule10.dto.Schedule10Response; |
| 9 | +import ca.bc.gov.nrs.ilcr.schedule10.dto.Stabilizing; |
| 10 | +import ca.bc.gov.nrs.ilcr.schedule10.dto.SubGrade; |
| 11 | +import java.math.BigDecimal; |
| 12 | +import java.math.RoundingMode; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.LinkedHashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +/** |
| 20 | + * Maps the Schedule 10 (New Road Construction Costs) read document to the print section datasource: |
| 21 | + * ONE detail row per road detail (flattened across construction pages), each carrying BOTH the road |
| 22 | + * detail's own fields AND its construction-page/project context (division, period, region, |
| 23 | + * TSA-or-TFL, supply block, TFL, road group) plus the page/road ordinals — so {@code |
| 24 | + * schedule10.jrxml} can group by construction page (printing the project header once) and |
| 25 | + * page-break per road detail, and the footer can read {@code Page {pageNumber} - Road {rowNumber} |
| 26 | + * of {roadDetailCount}} (the legacy form layout). The service ({@code Schedule10Service}) has |
| 27 | + * already derived the road group, the sub-grade / stabilizing totals and $/km, and the material |
| 28 | + * percentages — this mapper only formats and resolves code → label for the header. |
| 29 | + * |
| 30 | + * <p>Two deliberate deviations from the legacy report, both following the rebuilt Schedule 10 model |
| 31 | + * (Story 20.4 decisions): |
| 32 | + * |
| 33 | + * <ul> |
| 34 | + * <li>The LD-1/LD-2/LD-3 fields the legacy report showed — Moisture/ASM Code, Soil Moisture Code, |
| 35 | + * and Boulder Area % — are OMITTED: the rebuilt Schedule 10 removed them by business |
| 36 | + * direction and does not expose them on read, so the print does not show them either. |
| 37 | + * <li>End Haul / Overland {@code $/m3/km} is not carried by the read DTO, so it is computed here |
| 38 | + * for display: {@code lessEndHaul / (volume × distance)} (and the overland pair), dashed when |
| 39 | + * the volume or distance is absent/zero. |
| 40 | + * </ul> |
| 41 | + */ |
| 42 | +final class Schedule10SectionMapper { |
| 43 | + |
| 44 | + /** The sentinel the frontend/legacy shows in the "TSA or TFL" field for a TFL-located page. */ |
| 45 | + private static final String TFL_SENTINEL = "Tree Farm Licensee"; |
| 46 | + |
| 47 | + private Schedule10SectionMapper() {} |
| 48 | + |
| 49 | + static SectionData map(Schedule10Response response) { |
| 50 | + if (response == null || response.pages() == null || response.pages().isEmpty()) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + Schedule10CodeLists codeLists = response.codeLists(); |
| 54 | + List<Map<String, ?>> rows = new ArrayList<>(); |
| 55 | + for (ConstructionPage page : response.pages()) { |
| 56 | + List<RoadDetail> details = page.roadDetails() == null ? List.of() : page.roadDetails(); |
| 57 | + for (RoadDetail detail : details) { |
| 58 | + rows.add(toRow(page, detail, codeLists)); |
| 59 | + } |
| 60 | + } |
| 61 | + if (rows.isEmpty()) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + return new SectionData(rows, new HashMap<>()); |
| 65 | + } |
| 66 | + |
| 67 | + private static Map<String, ?> toRow( |
| 68 | + ConstructionPage page, RoadDetail detail, Schedule10CodeLists codeLists) { |
| 69 | + boolean tflLocated = page.tflNumberCode() != null && !page.tflNumberCode().isBlank(); |
| 70 | + Map<String, Object> row = new LinkedHashMap<>(); |
| 71 | + |
| 72 | + // Construction-page / project context (drives the Jasper group header + the page/road footer). |
| 73 | + row.put("pageNumber", page.pageNumber()); |
| 74 | + row.put("rowNumber", detail.rowNumber()); |
| 75 | + row.put("roadDetailCount", page.roadDetailCount()); |
| 76 | + row.put("division", SectionFormat.text(page.divisionName())); |
| 77 | + row.put("period", SectionFormat.text(page.constructionPeriod())); |
| 78 | + row.put( |
| 79 | + "region", |
| 80 | + describe(codeLists == null ? null : codeLists.forestRegions(), page.forestRegionCode())); |
| 81 | + row.put( |
| 82 | + "tsaOrTfl", |
| 83 | + tflLocated |
| 84 | + ? TFL_SENTINEL |
| 85 | + : describe(codeLists == null ? null : codeLists.tsaNumbers(), page.tsaNumber())); |
| 86 | + row.put( |
| 87 | + "supplyBlock", |
| 88 | + tflLocated |
| 89 | + ? "" |
| 90 | + : describe(codeLists == null ? null : codeLists.supplyBlocks(), page.tsbNumberCode())); |
| 91 | + row.put("tfl", tflLocated ? SectionFormat.text(page.tflNumberCode()) : ""); |
| 92 | + row.put("roadGroup", page.roadGroup() == null ? "" : page.roadGroup()); |
| 93 | + |
| 94 | + // Road information. |
| 95 | + row.put("roadName", SectionFormat.text(detail.roadName())); |
| 96 | + row.put( |
| 97 | + "roadType", |
| 98 | + describe(codeLists == null ? null : codeLists.roadLifetimes(), detail.roadLifetimeCode())); |
| 99 | + row.put( |
| 100 | + "biogeoVariant", |
| 101 | + SectionFormat.text( |
| 102 | + detail.becClassification() == null ? null : detail.becClassification().label())); |
| 103 | + row.put( |
| 104 | + "rsmsClass", |
| 105 | + describeRsmr( |
| 106 | + codeLists == null ? null : codeLists.rsmrClasses(), detail.relSoilMoistRgmClsCode())); |
| 107 | + row.put("sideSlope", SectionFormat.percent(detail.sideSlopePct())); |
| 108 | + |
| 109 | + // Material composition percentages. |
| 110 | + MaterialComposition material = detail.materialComposition(); |
| 111 | + row.put( |
| 112 | + "matSolidRock", SectionFormat.percent(material == null ? null : material.solidRockPct())); |
| 113 | + row.put( |
| 114 | + "matRippableRock", |
| 115 | + SectionFormat.percent(material == null ? null : material.rippableRockPct())); |
| 116 | + row.put("matCoarse", SectionFormat.percent(material == null ? null : material.coarsePct())); |
| 117 | + row.put("matFine", SectionFormat.percent(material == null ? null : material.finePct())); |
| 118 | + row.put("matOrganic", SectionFormat.percent(material == null ? null : material.organicPct())); |
| 119 | + row.put("matTotal", SectionFormat.percent(material == null ? null : material.totalPct())); |
| 120 | + |
| 121 | + // Sub-grade. |
| 122 | + SubGrade sg = detail.subGrade(); |
| 123 | + row.put("sgLength", SectionFormat.measure(sg == null ? null : sg.length(), "km")); |
| 124 | + row.put("sgSurfaceWidth", SectionFormat.measure(sg == null ? null : sg.surfaceWidth(), "m")); |
| 125 | + row.put("sgActualCost", SectionFormat.money(sg == null ? null : sg.actualCost())); |
| 126 | + row.put("sgTtTransfer", SectionFormat.money(sg == null ? null : sg.ttTransfer())); |
| 127 | + row.put("sgOtherTransfer", SectionFormat.money(sg == null ? null : sg.otherTransfer())); |
| 128 | + row.put("sgTotalCosts", SectionFormat.money(sg == null ? null : sg.totalCosts())); |
| 129 | + row.put("sgLessBridges", SectionFormat.money(sg == null ? null : sg.lessBridges())); |
| 130 | + row.put("sgLessCulverts", SectionFormat.money(sg == null ? null : sg.lessCulverts())); |
| 131 | + row.put("sgLessLandings", SectionFormat.money(sg == null ? null : sg.lessLandings())); |
| 132 | + row.put("sgLessEndHaul", SectionFormat.money(sg == null ? null : sg.lessEndHaul())); |
| 133 | + row.put("sgLessOverland", SectionFormat.money(sg == null ? null : sg.lessOverland())); |
| 134 | + row.put("sgLessOtherEng", SectionFormat.money(sg == null ? null : sg.lessOtherEng())); |
| 135 | + row.put("sgTotal", SectionFormat.money(sg == null ? null : sg.total())); |
| 136 | + row.put("sgCostPerLength", SectionFormat.decimal(sg == null ? null : sg.costPerLength())); |
| 137 | + |
| 138 | + // Additional stabilizing. |
| 139 | + Stabilizing st = detail.stabilizing(); |
| 140 | + row.put( |
| 141 | + "stCode", |
| 142 | + describe( |
| 143 | + codeLists == null ? null : codeLists.ballastMethods(), |
| 144 | + st == null ? null : st.ballastMethodCode())); |
| 145 | + row.put("stLength", SectionFormat.measure(st == null ? null : st.length(), "km")); |
| 146 | + row.put("stSurfaceWidth", SectionFormat.measure(st == null ? null : st.surfaceWidth(), "m")); |
| 147 | + row.put( |
| 148 | + "stType", |
| 149 | + describe( |
| 150 | + codeLists == null ? null : codeLists.ballastMaterials(), |
| 151 | + st == null ? null : st.ballastMaterialCode())); |
| 152 | + row.put("stDepth", SectionFormat.measure(st == null ? null : st.depth(), "m")); |
| 153 | + row.put( |
| 154 | + "stDistanceToSource", |
| 155 | + SectionFormat.measure(st == null ? null : st.distanceToSource(), "km")); |
| 156 | + row.put("stActualCost", SectionFormat.money(st == null ? null : st.actualCost())); |
| 157 | + row.put("stTtTransfer", SectionFormat.money(st == null ? null : st.ttTransfer())); |
| 158 | + row.put("stOtherTransfer", SectionFormat.money(st == null ? null : st.otherTransfer())); |
| 159 | + row.put("stTotal", SectionFormat.money(st == null ? null : st.total())); |
| 160 | + row.put("stCostPerLength", SectionFormat.decimal(st == null ? null : st.costPerLength())); |
| 161 | + |
| 162 | + // Include-detail-engineering flag + end-haul / overland detail table. |
| 163 | + row.put("includeDetailEng", SectionFormat.text(detail.detailedEngineeringCostInd())); |
| 164 | + row.put("endHaulDistance", SectionFormat.plain(detail.endHaulDistance())); |
| 165 | + row.put("endHaulVolume", SectionFormat.plain(detail.endHaulVolume())); |
| 166 | + row.put( |
| 167 | + "endHaulPerUnit", |
| 168 | + SectionFormat.decimal( |
| 169 | + perM3PerKm( |
| 170 | + sg == null ? null : sg.lessEndHaul(), |
| 171 | + detail.endHaulVolume(), |
| 172 | + detail.endHaulDistance()))); |
| 173 | + row.put("overlandDistance", SectionFormat.plain(detail.overlandDistance())); |
| 174 | + row.put("overlandVolume", SectionFormat.plain(detail.overlandVolume())); |
| 175 | + row.put( |
| 176 | + "overlandPerUnit", |
| 177 | + SectionFormat.decimal( |
| 178 | + perM3PerKm( |
| 179 | + sg == null ? null : sg.lessOverland(), |
| 180 | + detail.overlandVolume(), |
| 181 | + detail.overlandDistance()))); |
| 182 | + |
| 183 | + row.put("comments", SectionFormat.text(detail.comments())); |
| 184 | + return row; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * The legacy {@code $/m3/km} for an end-haul / overland row: the deducted cost spread over the |
| 189 | + * hauled volume-kilometres, {@code cost / (volume × distance)} at two decimals. Returns {@code |
| 190 | + * null} (rendered as a dash) when the cost, volume, or distance is absent or the denominator is |
| 191 | + * zero — so a road with no end-haul / overland leaves the column dashed, matching the legacy |
| 192 | + * report. |
| 193 | + */ |
| 194 | + private static BigDecimal perM3PerKm(BigDecimal cost, BigDecimal volume, BigDecimal distance) { |
| 195 | + if (cost == null || volume == null || distance == null) { |
| 196 | + return null; |
| 197 | + } |
| 198 | + BigDecimal denominator = volume.multiply(distance); |
| 199 | + if (denominator.signum() <= 0) { |
| 200 | + return null; |
| 201 | + } |
| 202 | + return cost.divide(denominator, 2, RoundingMode.HALF_UP); |
| 203 | + } |
| 204 | + |
| 205 | + /** Resolve an RSMR class code to `{code} - {desc}` via the code list. */ |
| 206 | + private static String describeRsmr(List<CodeDescriptionDto> options, String code) { |
| 207 | + if (code == null || code.isBlank()) { |
| 208 | + return "-"; |
| 209 | + } |
| 210 | + if (options != null) { |
| 211 | + for (CodeDescriptionDto option : options) { |
| 212 | + if (code.equals(option.code())) { |
| 213 | + return code + " - " + option.description(); |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + return code; |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Resolve a code to its description via the code list, falling back to the raw code (never |
| 222 | + * dashed). |
| 223 | + */ |
| 224 | + private static String describe(List<CodeDescriptionDto> options, String code) { |
| 225 | + if (code == null || code.isBlank()) { |
| 226 | + return "-"; |
| 227 | + } |
| 228 | + if (options != null) { |
| 229 | + for (CodeDescriptionDto option : options) { |
| 230 | + if (code.equals(option.code())) { |
| 231 | + return option.description(); |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + return code; |
| 236 | + } |
| 237 | +} |
0 commit comments