Skip to content

Commit 8cf91af

Browse files
committed
add DB migration and repository impl changes
1 parent 114c90c commit 8cf91af

File tree

8 files changed

+61
-13
lines changed

8 files changed

+61
-13
lines changed

src/main/java/de/rwth/idsg/steve/repository/impl/AddressRepositoryImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ private Integer insert(DSLContext ctx, Address ad) {
9999
.set(ADDRESS.ZIP_CODE, ad.getZipCode())
100100
.set(ADDRESS.CITY, ad.getCity())
101101
.set(ADDRESS.COUNTRY, ad.getCountryAlpha2OrNull())
102+
.set(ADDRESS.LATITUDE, ad.getLatitude())
103+
.set(ADDRESS.LONGITUDE, ad.getLongitude())
102104
.returning(ADDRESS.ADDRESS_PK)
103105
.fetchOne()
104106
.getAddressPk();
@@ -114,6 +116,8 @@ private void update(DSLContext ctx, Address ad) {
114116
.set(ADDRESS.ZIP_CODE, ad.getZipCode())
115117
.set(ADDRESS.CITY, ad.getCity())
116118
.set(ADDRESS.COUNTRY, ad.getCountryAlpha2OrNull())
119+
.set(ADDRESS.LATITUDE, ad.getLatitude())
120+
.set(ADDRESS.LONGITUDE, ad.getLongitude())
117121
.where(ADDRESS.ADDRESS_PK.eq(ad.getAddressPk()))
118122
.execute();
119123

src/main/java/de/rwth/idsg/steve/repository/impl/ChargePointRepositoryImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,6 @@ private int addChargePointInternal(DSLContext ctx, ChargePointForm form, Integer
347347
return ctx.insertInto(CHARGE_BOX)
348348
.set(CHARGE_BOX.CHARGE_BOX_ID, form.getChargeBoxId())
349349
.set(CHARGE_BOX.DESCRIPTION, form.getDescription())
350-
.set(CHARGE_BOX.LOCATION_LATITUDE, form.getLocationLatitude())
351-
.set(CHARGE_BOX.LOCATION_LONGITUDE, form.getLocationLongitude())
352350
.set(CHARGE_BOX.INSERT_CONNECTOR_STATUS_AFTER_TRANSACTION_MSG, form.getInsertConnectorStatusAfterTransactionMsg())
353351
.set(CHARGE_BOX.REGISTRATION_STATUS, form.getRegistrationStatus())
354352
.set(CHARGE_BOX.NOTE, form.getNote())
@@ -362,8 +360,6 @@ private int addChargePointInternal(DSLContext ctx, ChargePointForm form, Integer
362360
private void updateChargePointInternal(DSLContext ctx, ChargePointForm form, Integer addressPk) {
363361
ctx.update(CHARGE_BOX)
364362
.set(CHARGE_BOX.DESCRIPTION, form.getDescription())
365-
.set(CHARGE_BOX.LOCATION_LATITUDE, form.getLocationLatitude())
366-
.set(CHARGE_BOX.LOCATION_LONGITUDE, form.getLocationLongitude())
367363
.set(CHARGE_BOX.INSERT_CONNECTOR_STATUS_AFTER_TRANSACTION_MSG, form.getInsertConnectorStatusAfterTransactionMsg())
368364
.set(CHARGE_BOX.REGISTRATION_STATUS, form.getRegistrationStatus())
369365
.set(CHARGE_BOX.NOTE, form.getNote())

src/main/java/de/rwth/idsg/steve/utils/mapper/AddressMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public static Address recordToDto(AddressRecord record) {
4040
address.setZipCode(record.getZipCode());
4141
address.setCity(record.getCity());
4242
address.setCountry(CountryCode.getByCode(record.getCountry()));
43+
address.setLatitude(record.getLatitude());
44+
address.setLongitude(record.getLongitude());
4345
}
4446
return address;
4547
}

src/main/java/de/rwth/idsg/steve/utils/mapper/ChargePointDetailsMapper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public static ChargePointForm mapToForm(ChargePoint.Details cp) {
3939
form.setChargeBoxId(chargeBox.getChargeBoxId());
4040
form.setNote(chargeBox.getNote());
4141
form.setDescription(chargeBox.getDescription());
42-
form.setLocationLatitude(chargeBox.getLocationLatitude());
43-
form.setLocationLongitude(chargeBox.getLocationLongitude());
4442
form.setInsertConnectorStatusAfterTransactionMsg(chargeBox.getInsertConnectorStatusAfterTransactionMsg());
4543
form.setAdminAddress(chargeBox.getAdminAddress());
4644
form.setRegistrationStatus(chargeBox.getRegistrationStatus());

src/main/java/de/rwth/idsg/steve/web/dto/Address.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import lombok.Getter;
2323
import lombok.Setter;
2424
import lombok.ToString;
25+
import org.hibernate.validator.constraints.Range;
26+
27+
import java.math.BigDecimal;
2528

2629
/**
2730
* @author Sevket Goekay <[email protected]>
@@ -41,13 +44,21 @@ public class Address {
4144
private String city;
4245
private CountryCode country;
4346

47+
@Range(min = -90, max = 90, message = "Latitude must be between {min} and {max}")
48+
private BigDecimal latitude;
49+
50+
@Range(min = -180, max = 180, message = "Longitude must be between {min} and {max}")
51+
private BigDecimal longitude;
52+
4453
public boolean isEmpty() {
4554
return addressPk == null
4655
&& street == null
4756
&& houseNumber == null
4857
&& zipCode == null
4958
&& city == null
50-
&& country == null;
59+
&& country == null
60+
&& latitude == null
61+
&& longitude == null;
5162
}
5263

5364
/**

src/main/java/de/rwth/idsg/steve/web/dto/ChargePointForm.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package de.rwth.idsg.steve.web.dto;
2020

2121
import de.rwth.idsg.steve.web.validation.ChargeBoxId;
22+
import jakarta.validation.Valid;
2223
import lombok.Getter;
2324
import lombok.Setter;
2425
import lombok.ToString;
@@ -51,14 +52,9 @@ public class ChargePointForm {
5152
@NotNull
5253
private Boolean insertConnectorStatusAfterTransactionMsg;
5354

55+
@Valid
5456
private Address address;
5557

56-
@Range(min = -90, max = 90, message = "Latitude must be between {min} and {max}")
57-
private BigDecimal locationLatitude;
58-
59-
@Range(min = -180, max = 180, message = "Longitude must be between {min} and {max}")
60-
private BigDecimal locationLongitude;
61-
6258
private String description;
6359
private String note;
6460

src/main/java/de/rwth/idsg/steve/web/dto/UserForm.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package de.rwth.idsg.steve.web.dto;
2020

2121
import de.rwth.idsg.steve.NotificationFeature;
22+
import jakarta.validation.Valid;
2223
import jakarta.validation.constraints.AssertTrue;
2324
import lombok.Getter;
2425
import lombok.Setter;
@@ -60,6 +61,7 @@ public class UserForm {
6061

6162
private List<NotificationFeature> notificationFeatures;
6263

64+
@Valid
6365
private Address address;
6466

6567
@AssertTrue(message = "Some of the selected notification features cannot be enabled for a user")
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
START TRANSACTION;
2+
3+
-- Step 1: Add latitude and longitude columns to address table
4+
ALTER TABLE address
5+
ADD COLUMN latitude DECIMAL(11, 8),
6+
ADD COLUMN longitude DECIMAL(11, 8);
7+
8+
-- Step 2: Migrate existing lat/long data from charge_box to address table
9+
-- For charge boxes that already have an address
10+
UPDATE address a
11+
INNER JOIN charge_box cb ON a.address_pk = cb.address_pk
12+
SET a.latitude = cb.location_latitude,
13+
a.longitude = cb.location_longitude
14+
WHERE cb.location_latitude IS NOT NULL
15+
AND cb.location_longitude IS NOT NULL;
16+
17+
-- Step 3a: Create new address rows for charge boxes without an address but with lat/long
18+
INSERT INTO address (latitude, longitude)
19+
SELECT location_latitude, location_longitude
20+
FROM charge_box
21+
WHERE address_pk IS NULL
22+
AND location_latitude IS NOT NULL
23+
AND location_longitude IS NOT NULL;
24+
25+
-- Step 3b: Update charge_box to reference the newly created addresses
26+
UPDATE charge_box cb
27+
INNER JOIN address a ON cb.location_latitude = a.latitude
28+
AND cb.location_longitude = a.longitude
29+
SET cb.address_pk = a.address_pk
30+
WHERE cb.address_pk IS NULL
31+
AND cb.location_latitude IS NOT NULL
32+
AND cb.location_longitude IS NOT NULL;
33+
34+
-- Step 4: Drop the old columns from charge_box
35+
ALTER TABLE charge_box
36+
DROP COLUMN location_latitude,
37+
DROP COLUMN location_longitude;
38+
39+
COMMIT;

0 commit comments

Comments
 (0)