Skip to content

Commit

Permalink
Add find method to retrieve all distinct positions
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Oct 12, 2020
1 parent f04d7aa commit e3528a3
Show file tree
Hide file tree
Showing 12 changed files with 1,554 additions and 1,474 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum class NAFCode(val code: String) {
FLAG("FS"),
RADIO_CALL_SIGN("RC"),
VESSEL_NAME("NA"),
EXTERNAL_REFERENC_NUMBER("XR"),
EXTERNAL_REFERENCE_NUMBER("XR"),
LATITUDE_DECIMAL("LT"),
LONGITUDE_DECIMAL("LG"),
SPEED("SP"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class NAFMessageMapper(private val naf: String) {
NAFCode.INTERNAL_REFERENCE_NUMBER -> this.internalReferenceNumber = value
NAFCode.RADIO_CALL_SIGN -> this.IRCS = value
NAFCode.VESSEL_NAME -> this.vesselName = value
NAFCode.EXTERNAL_REFERENC_NUMBER -> this.externalReferenceNumber = value
NAFCode.EXTERNAL_REFERENCE_NUMBER -> this.externalReferenceNumber = value
NAFCode.FLAG -> this.flagState = getCountryOrThrowIfCountryNotFound(value)
NAFCode.FROM -> this.from = getCountryOrThrowIfCountryNotFound(value)
NAFCode.TO -> this.destination = getCountryOrThrowIfCountryNotFound(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import fr.gouv.cnsp.monitorfish.domain.entities.Position

interface PositionsRepository {
fun findAll(): List<Position>
fun findAllByMMSI(MMSI: String): List<Position>
fun findLastDistinctPositions(): List<Position>
fun save(position: Position)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import fr.gouv.cnsp.monitorfish.domain.repositories.PositionsRepository
class GetAllPositions(private val positionsRepository: PositionsRepository) {

fun execute(): List<Position> {
return positionsRepository.findAll()
return positionsRepository.findLastDistinctPositions()
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package fr.gouv.cnsp.monitorfish.domain.use_cases

import fr.gouv.cnsp.monitorfish.config.UseCase
import fr.gouv.cnsp.monitorfish.domain.entities.Position
import fr.gouv.cnsp.monitorfish.domain.exceptions.NAFMessageParsingException
import fr.gouv.cnsp.monitorfish.domain.mappers.NAFMessageMapper
import fr.gouv.cnsp.monitorfish.domain.repositories.PositionsRepository
import org.slf4j.Logger
import org.slf4j.LoggerFactory

@UseCase
class ParseAndSavePosition(private val positionsRepository: PositionsRepository) {
private val logger: Logger = LoggerFactory.getLogger(ParseAndSavePosition::class.java)

@Throws(NAFMessageParsingException::class)
fun execute(naf: String) {
val position = NAFMessageMapper(naf).toPosition()
if (position.internalReferenceNumber == null) {
logger.warn("No internal reference number for position $position")
}
positionsRepository.save(position)
logger.debug("Saved new position $position")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ControllersExceptionHandler {
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(NAFMessageParsingException::class)
fun handleNAFMessageParsingException(e: Exception): ApiError {
logger.error(e.message, e.stackTrace, e.cause)
logger.error(e.message, e.cause)
return ApiError(e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import javax.persistence.*

@Entity
@Table(name = "positions", indexes = [
Index(columnList = "internal_reference_number"),
Index(columnList = "mmsi"),
Index(columnList = "ircs"),
Index(columnList = "date_time")])
Index(columnList = "id", unique = true),
Index(columnList = "external_reference_number", unique = false),
Index(columnList = "internal_reference_number", unique = false),
Index(columnList = "mmsi", unique = false),
Index(columnList = "ircs", unique = false),
Index(columnList = "date_time", unique = false)])
data class PositionEntity(
@Id
@SequenceGenerator(name = "POSITION_ID_SEQ", sequenceName = "POSITION_ID_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "POSITION_ID_SEQ")
@SequenceGenerator(name = "position_id_seq", sequenceName = "position_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "position_id_seq")
@Column(name = "id")
val id: Int? = null,

Expand Down Expand Up @@ -58,6 +60,7 @@ data class PositionEntity(
val dateTime: ZonedDateTime) {

fun toPosition() = Position(
id = id,
internalReferenceNumber = internalReferenceNumber,
IRCS = IRCS,
MMSI = MMSI,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
package fr.gouv.cnsp.monitorfish.infrastructure.database.repositories

import fr.gouv.cnsp.monitorfish.infrastructure.database.entities.PositionEntity
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.CrudRepository
import javax.persistence.Tuple

interface DBPositionRepository : CrudRepository<PositionEntity, Long>
interface DBPositionRepository : CrudRepository<PositionEntity, Long> {
fun findAllByMMSI(MMSI: String): List<PositionEntity>

@Query(value = "select distinct " +
"n.internal_reference_number, " +
"n.external_reference_number, " +
"n.mmsi, " +
"n.ircs, " +
"p.id, " +
"p.date_time, " +
"p.vessel_name, " +
"p.flag_state, " +
"p.from_country, " +
"p.destination_country, " +
"p.trip_number, " +
"p.latitude, " +
"p.longitude, " +
"p.speed, " +
"p.course, " +
"p.position_type " +
"from positions n " +
"left join positions p on p.id = (select po.ID from positions po where " +
"n.internal_reference_number = po.internal_reference_number " +
"order by po.date_time desc limit 1) " +
"group by " +
"n.internal_reference_number, " +
"n.external_reference_number, " +
"n.mmsi, " +
"n.ircs, " +
"p.id, " +
"p.date_time, " +
"p.vessel_name, " +
"p.flag_state, " +
"p.from_country, " +
"p.destination_country, " +
"p.trip_number, " +
"p.latitude, " +
"p.longitude, " +
"p.speed, " +
"p.course, " +
"p.position_type ",
nativeQuery = true)
fun findLastDistinctPositions(): List<PositionEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ class JpaPositionsRepository(private val dbPositionRepository: DBPositionReposit
.map(PositionEntity::toPosition)
}

override fun findLastDistinctPositions(): List<Position> {
return dbPositionRepository.findLastDistinctPositions()
.map(PositionEntity::toPosition)
}

override fun save(position: Position) {
val positionEntity = PositionEntity.fromPosition(position)
dbPositionRepository.save(positionEntity)
}

override fun findAllByMMSI(MMSI: String): List<Position> {
return dbPositionRepository.findAllByMMSI(MMSI)
.map(PositionEntity::toPosition)
}
}
Loading

0 comments on commit e3528a3

Please sign in to comment.