Skip to content

Commit

Permalink
rework coordinate data handling
Browse files Browse the repository at this point in the history
  • Loading branch information
svencc committed Nov 12, 2023
1 parent 6dc461f commit b25a794
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 45 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

* tidy up unfinished MapTransactions @Scheduled

* normalize map entities
* map name



Expand Down
63 changes: 63 additions & 0 deletions src/main/java/com/recom/api/map/MapTopographyController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.recom.api.map;

import com.recom.api.commons.HttpCommons;
import com.recom.dto.map.topography.MapTopographyRequestDto;
import com.recom.security.account.RECOMAccount;
import com.recom.security.account.RECOMAuthorities;
import com.recom.service.map.MapMetaDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@Tag(name = "Maps")
@RequiredArgsConstructor
@RequestMapping("/api/v1/map/topography")
public class MapTopographyController {

@NonNull
private final MapMetaDataService mapMetaDataService;


@Operation(
summary = "Gets map topography",
description = "Return a list of measured height points and some meta.",
security = @SecurityRequirement(name = HttpCommons.BEARER_AUTHENTICATION_REQUIREMENT)
)
@ApiResponses(value = {
@ApiResponse(responseCode = HttpCommons.OK_CODE, description = HttpCommons.OK),
@ApiResponse(responseCode = HttpCommons.UNAUTHORIZED_CODE, description = HttpCommons.UNAUTHORIZED, content = @Content())
})
@GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE)
@Secured({RECOMAuthorities.EVERYBODY})
public ResponseEntity<List<String>> listMapNames(
@AuthenticationPrincipal RECOMAccount recomAccount,
@RequestBody final MapTopographyRequestDto mapTopographyRequestDto
) {
log.debug("Requested GET /api/v1/maps");

return ResponseEntity.status(HttpStatus.OK)
.cacheControl(CacheControl.noCache())
.body(mapMetaDataService.provideAllMapNames());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MapTopographyEntityDto implements MapLocatedDto {

@Schema
@JsonProperty()
private Float surfaceHeight;

@Schema
@JsonProperty()
private Float oceanHeight;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.recom.dto.map.topography;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

@Data
@Schema
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MapTopographyDataPointDto implements Serializable {

@NotBlank
@Schema
@JsonProperty()
private String mapName;

@Schema
@JsonProperty()
private float oceanHeight;

@Schema
@JsonProperty()
private float coordinates;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.recom.dto.map.topography;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@Schema
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MapTopographyRequestDto implements Serializable {

@NotBlank
@Schema
@JsonProperty()
private String mapName;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.recom.dto.map.topography;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

@Data
@Schema
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MapTopographyResponseDto implements Serializable {

@NotBlank
@Schema
@JsonProperty()
private String mapName;

@Schema
@JsonProperty()
private Float oceanBaseHeight;

@Schema
@JsonProperty()
private List<MapTopographyDataPointDto> coordinates;

}
10 changes: 3 additions & 7 deletions src/main/java/com/recom/entity/MapEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,13 @@ public class MapEntity implements Persistable<Long>, Serializable, MapLocatedEnt
@Column(insertable = true, updatable = false, nullable = true)
private String rotationZ;

@Lob
@Column(insertable = true, updatable = false, nullable = true)
private String coordinates;

@Column(insertable = true, updatable = false, nullable = true)
@Column(columnDefinition="Decimal(15,10)",insertable = true, updatable = false, nullable = true)
private BigDecimal coordinateX;

@Column(insertable = true, updatable = false, nullable = true)
@Column(columnDefinition="Decimal(15,10)",insertable = true, updatable = false, nullable = true)
private BigDecimal coordinateY;

@Column(insertable = true, updatable = false, nullable = true)
@Column(columnDefinition="Decimal(15,10)",insertable = true, updatable = false, nullable = true)
private BigDecimal coordinateZ;

@Override
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/com/recom/entity/MapTopographyEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,19 @@ public class MapTopographyEntity implements Persistable<Long>, Serializable, Map
@Column(insertable = true, updatable = false, nullable = false, length = 255)
private String mapName;

@Column(insertable = true, updatable = false, nullable = false)
private Float surfaceHeight;

@Column(insertable = true, updatable = false, nullable = false)
private Float oceanHeight;

@Column(insertable = true, updatable = false, nullable = false)
private Float oceanBaseHeight;

@Lob
@Column(insertable = true, updatable = false, nullable = true)
private String coordinates;

@Column(insertable = true, updatable = false, nullable = true)
@Column(columnDefinition = "Decimal(15,10)", insertable = true, updatable = false, nullable = true)
private BigDecimal coordinateX;

@Column(insertable = true, updatable = false, nullable = true)
@Column(columnDefinition = "Decimal(15,10)", insertable = true, updatable = false, nullable = true)
private BigDecimal coordinateY;

@Column(insertable = true, updatable = false, nullable = true)
@Column(columnDefinition = "Decimal(15,10)", insertable = true, updatable = false, nullable = true)
private BigDecimal coordinateZ;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ public interface MapLocatedEntity {

String getMapName();

String getCoordinates();

void setCoordinates(@NonNull final String coordinates);

BigDecimal getCoordinateX();

void setCoordinateX(@NonNull final BigDecimal coordinateX);
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/recom/mapper/MapEntityMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.recom.dto.map.scanner.map.MapEntityDto;
import com.recom.entity.MapEntity;
import com.recom.entity.MapTopographyEntity;
import com.recom.event.listener.generic.MapLocatedEntity;
import com.recom.event.listener.generic.TransactionalMapEntityMappable;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
Expand Down Expand Up @@ -55,6 +57,12 @@ static BigDecimal extractCoordinateZ(@Nullable final List<BigDecimal> coordinate
return MapperUtil.extractCoordinateZ(coordinates);
}

@Nullable
@Named("joinEntityCoordinatesToDtoCoordinates")
static List<BigDecimal> joinEntityCoordinatesToDtoCoordinates(@Nullable final MapLocatedEntity entity) {
return MapperUtil.joinEntityCoordinatesToDtoCoordinates(entity);
}

@BeanMapping(ignoreByDefault = true)
@Mapping(source = "entityId", target = "entityId")
@Mapping(source = "name", target = "name", qualifiedByName = "blankStringToNull")
Expand All @@ -65,7 +73,6 @@ static BigDecimal extractCoordinateZ(@Nullable final List<BigDecimal> coordinate
@Mapping(source = "rotationX", target = "rotationX", qualifiedByName = "encodeVectorToJsonString")
@Mapping(source = "rotationY", target = "rotationY", qualifiedByName = "encodeVectorToJsonString")
@Mapping(source = "rotationZ", target = "rotationZ", qualifiedByName = "encodeVectorToJsonString")
@Mapping(source = "coordinates", target = "coordinates", qualifiedByName = "encodeVectorToJsonString")
@Mapping(source = "coordinates", target = "coordinateX", qualifiedByName = "extractCoordinateX")
@Mapping(source = "coordinates", target = "coordinateY", qualifiedByName = "extractCoordinateY")
@Mapping(source = "coordinates", target = "coordinateZ", qualifiedByName = "extractCoordinateZ")
Expand All @@ -81,7 +88,7 @@ static BigDecimal extractCoordinateZ(@Nullable final List<BigDecimal> coordinate
@Mapping(source = "rotationX", target = "rotationX", qualifiedByName = "decodeJsonStringToVector")
@Mapping(source = "rotationY", target = "rotationY", qualifiedByName = "decodeJsonStringToVector")
@Mapping(source = "rotationZ", target = "rotationZ", qualifiedByName = "decodeJsonStringToVector")
@Mapping(source = "coordinates", target = "coordinates", qualifiedByName = "decodeJsonStringToVector")
@Mapping(source = "entity", target = "coordinates", qualifiedByName = "joinEntityCoordinatesToDtoCoordinates")
MapEntityDto toDto(final MapEntity entity);

}
13 changes: 9 additions & 4 deletions src/main/java/com/recom/mapper/MapTopographyEntityMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.recom.dto.map.scanner.topography.MapTopographyEntityDto;
import com.recom.entity.MapEntity;
import com.recom.entity.MapTopographyEntity;
import com.recom.event.listener.generic.MapLocatedEntity;
import com.recom.event.listener.generic.TransactionalMapEntityMappable;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
Expand Down Expand Up @@ -49,21 +51,24 @@ static BigDecimal extractCoordinateZ(@Nullable final List<BigDecimal> coordinate
return MapperUtil.extractCoordinateZ(coordinates);
}

@Nullable
@Named("joinEntityCoordinatesToDtoCoordinates")
static List<BigDecimal> joinEntityCoordinatesToDtoCoordinates(@Nullable final MapLocatedEntity entity) {
return MapperUtil.joinEntityCoordinatesToDtoCoordinates(entity);
}

@BeanMapping(ignoreByDefault = true)
@Mapping(source = "surfaceHeight", target = "surfaceHeight")
@Mapping(source = "oceanHeight", target = "oceanHeight")
@Mapping(source = "oceanBaseHeight", target = "oceanBaseHeight")
@Mapping(source = "coordinates", target = "coordinates", qualifiedByName = "encodeVectorToJsonString")
@Mapping(source = "coordinates", target = "coordinateX", qualifiedByName = "extractCoordinateX")
@Mapping(source = "coordinates", target = "coordinateY", qualifiedByName = "extractCoordinateY")
@Mapping(source = "coordinates", target = "coordinateZ", qualifiedByName = "extractCoordinateZ")
MapTopographyEntity toEntity(final MapTopographyEntityDto mapTopographyEntityDto);

@BeanMapping(ignoreByDefault = true)
@Mapping(source = "surfaceHeight", target = "surfaceHeight")
@Mapping(source = "oceanHeight", target = "oceanHeight")
@Mapping(source = "oceanBaseHeight", target = "oceanBaseHeight")
@Mapping(source = "coordinates", target = "coordinates", qualifiedByName = "decodeJsonStringToVector")
@Mapping(source = "entity", target = "coordinates", qualifiedByName = "joinEntityCoordinatesToDtoCoordinates")
MapTopographyEntityDto toDto(final MapTopographyEntity entity);

}
7 changes: 7 additions & 0 deletions src/main/java/com/recom/mapper/MapperUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.recom.entity.MapEntity;
import com.recom.entity.MapTopographyEntity;
import com.recom.event.listener.generic.MapLocatedEntity;
import com.recom.service.provider.StaticObjectMapperProvider;
import lombok.experimental.UtilityClass;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -71,4 +74,8 @@ static BigDecimal extractCoordinateZ(@Nullable final List<BigDecimal> coordinate
}
}

static List<BigDecimal> joinEntityCoordinatesToDtoCoordinates(@Nullable final MapLocatedEntity entity) {
return List.of(entity.getCoordinateX(), entity.getCoordinateY(), entity.getCoordinateZ());
}

}
5 changes: 3 additions & 2 deletions src/test/java/com/recom/mapper/MapEntityMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public void testToEntity() throws JsonProcessingException {
assertEquals(objectMapper.writeValueAsString(rotationX), entityToTest.getRotationX());
assertEquals(objectMapper.writeValueAsString(rotationY), entityToTest.getRotationY());
assertEquals(objectMapper.writeValueAsString(rotationZ), entityToTest.getRotationZ());
assertEquals(objectMapper.writeValueAsString(coordinates), entityToTest.getCoordinates());
assertEquals(BigDecimal.valueOf(1.0), entityToTest.getCoordinateX());
assertEquals(BigDecimal.valueOf(2.0), entityToTest.getCoordinateY());
assertEquals(BigDecimal.valueOf(3.0), entityToTest.getCoordinateZ());
Expand Down Expand Up @@ -91,7 +90,9 @@ public void testToDto() throws JsonProcessingException {
.rotationX(objectMapper.writeValueAsString(rotationX))
.rotationY(objectMapper.writeValueAsString(rotationY))
.rotationZ(objectMapper.writeValueAsString(rotationZ))
.coordinates(objectMapper.writeValueAsString(coordinates))
.coordinateX(BigDecimal.valueOf(1.0))
.coordinateY(BigDecimal.valueOf(2.0))
.coordinateZ(BigDecimal.valueOf(3.0))
.build();

// Act
Expand Down
Loading

0 comments on commit b25a794

Please sign in to comment.