Skip to content

Commit 3d05723

Browse files
authored
Merge pull request #1860 from steve-community/1859-add-latitude-longitude-to-address-table
add latitude longitude to address table
2 parents 3e6a392 + ff1522a commit 3d05723

File tree

10 files changed

+89
-27
lines changed

10 files changed

+89
-27
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;
@@ -58,6 +59,7 @@ public class UserForm {
5859

5960
private List<NotificationFeature> notificationFeatures;
6061

62+
@Valid
6163
private Address address;
6264

6365
@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;

src/main/webapp/WEB-INF/views/data-man/00-address.jsp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,30 @@
2626
<tr><td>Zip code:</td><td><form:input path="address.zipCode"/></td></tr>
2727
<tr><td>City:</td><td><form:input path="address.city"/></td></tr>
2828
<tr><td>Country:</td><td><form:select path="address.country" items="${countryCodes}"/></td></tr>
29-
</table>
29+
<tr><td>Latitude:</td><td><form:input path="address.latitude"/></td></tr>
30+
<tr><td>Longitude:</td><td><form:input path="address.longitude"/></td></tr>
31+
32+
<!--
33+
Extract the address object using spring:bind to make it accessible for JSTL conditionals.
34+
35+
Why: The form:input tag works with relative paths ("address.latitude"), but JSTL's c:if
36+
cannot resolve these relative paths - it needs direct object references. Since address
37+
can be nested in different parent objects (cp.address, user.address, etc.), we use
38+
spring:bind to resolve the path relative to the form's modelAttribute and expose the
39+
actual address object.
40+
41+
See: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/tags/BindTag.html
42+
-->
43+
<spring:bind path="address">
44+
<c:set var="boundAddress" value="${status.value}" />
45+
</spring:bind>
46+
47+
<c:if test="${(not empty boundAddress.latitude) and (not empty boundAddress.longitude)}">
48+
<tr>
49+
<td></td>
50+
<td><a target="_blank" href="https://maps.google.com/?q=${boundAddress.latitude},${boundAddress.longitude}">
51+
Show on Google Maps</a>
52+
</td>
53+
</tr>
54+
</c:if>
55+
</table>

src/main/webapp/WEB-INF/views/data-man/00-cp-misc.jsp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,11 @@
3131
</c:if>
3232
</td>
3333
</tr>
34-
<tr><td>Latitude:</td><td><form:input path="locationLatitude"/></td></tr>
35-
<tr><td>Longitude:</td><td><form:input path="locationLongitude"/></td></tr>
36-
37-
<c:if test="${(not empty cp.chargeBox.locationLongitude) and (not empty cp.chargeBox.locationLongitude)}">
38-
<tr>
39-
<td></td>
40-
<td><a target="_blank"
41-
href="https://maps.google.com/?q=${cp.chargeBox.locationLatitude},${cp.chargeBox.locationLongitude}">
42-
Show on Google Maps</a>
43-
</td>
44-
</tr>
45-
</c:if>
4634

4735
<tr><td>Additional Note:</td><td><form:textarea path="note"/></td></tr>
4836
<tr><td></td>
4937
<td id="add_space">
5038
<input type="submit" name="${submitButtonName}" value="${submitButtonValue}">
5139
<input type="submit" name="backToOverview" value="Back to Overview">
5240
</td></tr>
53-
</table>
41+
</table>

0 commit comments

Comments
 (0)