From fe6fff69d9aa59a2758aecb76a5f2e2ed7f2c194 Mon Sep 17 00:00:00 2001 From: Matthijs Laan Date: Wed, 8 Jan 2025 19:30:16 +0100 Subject: [PATCH] Change knelpunten_meerkeuze to array --- .../api/PlanmonitorWonenDatabaseService.java | 29 +++++++++++++++---- .../api/configuration/PopulateTestData.java | 2 +- .../api/model/Planregistratie.java | 6 ++-- .../V2__knelpunten_meerkeuze_array.sql | 3 ++ 4 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/main/resources/db/migration/V2__knelpunten_meerkeuze_array.sql diff --git a/src/main/java/nl/b3p/planmonitorwonen/api/PlanmonitorWonenDatabaseService.java b/src/main/java/nl/b3p/planmonitorwonen/api/PlanmonitorWonenDatabaseService.java index b054b2d..9080a37 100644 --- a/src/main/java/nl/b3p/planmonitorwonen/api/PlanmonitorWonenDatabaseService.java +++ b/src/main/java/nl/b3p/planmonitorwonen/api/PlanmonitorWonenDatabaseService.java @@ -22,10 +22,15 @@ import org.locationtech.jts.io.WKBWriter; import org.locationtech.jts.io.WKTReader; import org.locationtech.jts.io.WKTWriter; +import org.postgresql.jdbc.PgArray; import org.springframework.context.annotation.Profile; +import org.springframework.core.convert.converter.Converter; +import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.core.convert.support.GenericConversionService; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.SimplePropertyRowMapper; import org.springframework.jdbc.core.simple.JdbcClient; +import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -38,12 +43,24 @@ public class PlanmonitorWonenDatabaseService { public PlanmonitorWonenDatabaseService(JdbcClient jdbcClient) { this.jdbcClient = jdbcClient; + final GenericConversionService conversionService = new GenericConversionService(); + DefaultConversionService.addDefaultConverters(conversionService); + conversionService.addConverter( + (Converter) + source -> { + try { + return (String[]) source.getArray(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + }); + planregistratieRowMapper = - new SimplePropertyRowMapper<>(Planregistratie.class) { + new SimplePropertyRowMapper<>(Planregistratie.class, conversionService) { @Override - public Planregistratie mapRow(ResultSet rs, int rowNumber) throws SQLException { + @NonNull + public Planregistratie mapRow(@NonNull ResultSet rs, int rowNumber) throws SQLException { Planregistratie planregistratie = super.mapRow(rs, rowNumber); - assert planregistratie != null; planregistratie.setGeometrie(wkbToWkt(planregistratie.getGeometrie())); return planregistratie; } @@ -126,8 +143,8 @@ insert into planregistratie( aantal_studentenwoningen, sleutelproject ) -values (%s)""" - .formatted(sqlQuestionMarks(23)); +values (%s, ?::pmw_knelpunten_meerkeuze[], %s)""" + .formatted(sqlQuestionMarks(19), sqlQuestionMarks(3)); this.jdbcClient .sql(insertPlanregistratie) .param(1, planregistratie.getId(), Types.OTHER) @@ -149,7 +166,7 @@ insert into planregistratie( .param(planregistratie.getBestemmingsplan()) .param(18, planregistratie.getStatusProject(), Types.OTHER) .param(19, planregistratie.getStatusPlanologisch(), Types.OTHER) - .param(20, planregistratie.getKnelpuntenMeerkeuze(), Types.OTHER) + .param(20, planregistratie.getKnelpuntenMeerkeuze(), Types.ARRAY) .param(21, planregistratie.getBeoogdWoonmilieuAbf13(), Types.OTHER) .param(planregistratie.getAantalStudentenwoningen()) .param(planregistratie.isSleutelproject()) diff --git a/src/main/java/nl/b3p/planmonitorwonen/api/configuration/PopulateTestData.java b/src/main/java/nl/b3p/planmonitorwonen/api/configuration/PopulateTestData.java index 618c960..44620e3 100644 --- a/src/main/java/nl/b3p/planmonitorwonen/api/configuration/PopulateTestData.java +++ b/src/main/java/nl/b3p/planmonitorwonen/api/configuration/PopulateTestData.java @@ -83,7 +83,7 @@ public void init() throws ParseException { .setBestemmingsplan("Een bestemmingsplan") .setStatusProject("Voorbereiding") .setStatusPlanologisch("3. In voorbereiding") - .setKnelpuntenMeerkeuze("Bereikbaarheid") + .setKnelpuntenMeerkeuze(new String[] {"Bereikbaarheid"}) .setBeoogdWoonmilieuAbf13("Centrum") .setAantalStudentenwoningen(70) .setSleutelproject(false); diff --git a/src/main/java/nl/b3p/planmonitorwonen/api/model/Planregistratie.java b/src/main/java/nl/b3p/planmonitorwonen/api/model/Planregistratie.java index 8699247..dacb58b 100644 --- a/src/main/java/nl/b3p/planmonitorwonen/api/model/Planregistratie.java +++ b/src/main/java/nl/b3p/planmonitorwonen/api/model/Planregistratie.java @@ -30,7 +30,7 @@ public final class Planregistratie { private String bestemmingsplan; private String statusProject; private String statusPlanologisch; - private String knelpuntenMeerkeuze; + private String[] knelpuntenMeerkeuze; private String beoogdWoonmilieuAbf13; private Integer aantalStudentenwoningen; private boolean sleutelproject; @@ -209,11 +209,11 @@ public Planregistratie setStatusPlanologisch(String statusPlanologisch) { return this; } - public String getKnelpuntenMeerkeuze() { + public String[] getKnelpuntenMeerkeuze() { return knelpuntenMeerkeuze; } - public Planregistratie setKnelpuntenMeerkeuze(String knelpuntenMeerkeuze) { + public Planregistratie setKnelpuntenMeerkeuze(String[] knelpuntenMeerkeuze) { this.knelpuntenMeerkeuze = knelpuntenMeerkeuze; return this; } diff --git a/src/main/resources/db/migration/V2__knelpunten_meerkeuze_array.sql b/src/main/resources/db/migration/V2__knelpunten_meerkeuze_array.sql new file mode 100644 index 0000000..beb3f77 --- /dev/null +++ b/src/main/resources/db/migration/V2__knelpunten_meerkeuze_array.sql @@ -0,0 +1,3 @@ +alter table planregistratie alter column knelpunten_meerkeuze + type pmw_knelpunten_meerkeuze[] + using case when knelpunten_meerkeuze is null then array[]::pmw_knelpunten_meerkeuze[] else array[knelpunten_meerkeuze] end; \ No newline at end of file