Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .env.dev.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ MONITORENV_API_KEY=DUMMY-API-KEY
MONITORENV_IMAGE=

# Spring
ENV_DB_URL=jdbc:postgresql://localhost:5432/monitorenvdb?user=postgres&password=postgres
ENV_DB_URL=jdbc:postgresql://localhost:5432/monitorenvdb
ENV_DB_USER=postgres
ENV_DB_PASSWORD=postgres
FLYWAY_MIGRATION_PATH=classpath:/db/migration,classpath:/db/testdata
CACEM_DB_URL=jdbc:postgresql://localhost:5432/cacem
CACEM_DB_USER=cacem
CACEM_DB_PASSWORD=cacem

# Sentry
MONITORENV_SENTRY_ENABLED=false
Expand Down
5 changes: 5 additions & 0 deletions .env.infra.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ MONITORENV_API_KEY=

# Spring
ENV_DB_URL=
ENV_DB_USER=
ENV_DB_PASSWORD=
FLYWAY_MIGRATION_PATH=
CACEM_DB_URL=
CACEM_DB_USER=
CACEM_DB_PASSWORD=

# Sentry
MONITORENV_SENTRY_ENABLED=
Expand Down
7 changes: 6 additions & 1 deletion .env.test.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ MONITORENV_VERSION=0.0.0
MONITORENV_API_KEY=DUMMY-API-KEY

# Spring
ENV_DB_URL=jdbc:postgresql://localhost:5432/monitorenvdb?user=postgres&password=postgres
ENV_DB_URL=jdbc:postgresql://localhost:5432/monitorenvdb
ENV_DB_USER=postgres
ENV_DB_PASSWORD=postgres
FLYWAY_MIGRATION_PATH=classpath:/db/migration,classpath:/db/testdata
CACEM_DB_URL=jdbc:postgresql://localhost:5432/cacem
CACEM_DB_USER=cacem
CACEM_DB_PASSWORD=cacem

# Sentry
MONITORENV_SENTRY_ENABLED=false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package fr.gouv.cacem.monitorenv.config

import com.zaxxer.hikari.HikariDataSource
import jakarta.persistence.EntityManagerFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.ApplicationRunner
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.orm.jpa.JpaTransactionManager
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
import org.springframework.transaction.PlatformTransactionManager
import javax.sql.DataSource

@Configuration
@EnableJpaRepositories(
basePackages = [
"fr.gouv.cacem.monitorenv.infrastructure.cacem.repositories",
],
entityManagerFactoryRef = "cacemEntityManagerFactory",
transactionManagerRef = "cacemTransactionManager",
)
class CacemJpaConfig(
@Value("\${spring.datasource.cacem.url}") private val url: String,
@Value("\${spring.datasource.cacem.user}") private val username: String,
@Value("\${spring.datasource.cacem.password}") private val password: String,
) {
@Bean
fun cacemDataSource(): HikariDataSource {
val ds = HikariDataSource()
ds.jdbcUrl = url
ds.username = username
ds.password = password
ds.driverClassName = "org.postgresql.Driver"
ds.maximumPoolSize = 10
ds.maxLifetime = 60000
return ds
}

@Bean
fun cacemEntityManagerFactory(
builder: EntityManagerFactoryBuilder,
@Qualifier("cacemDataSource") dataSource: DataSource,
): LocalContainerEntityManagerFactoryBean =
builder
.dataSource(dataSource)
.packages("fr.gouv.cacem.monitorenv.infrastructure.cacem.models")
.persistenceUnit("cacem")
.build()

@Bean
fun debugPrimaryDs(
@Qualifier("primaryDataSource") ds: DataSource,
) = ApplicationRunner {
println(ds)
}

@Bean
fun cacemTransactionManager(
@Qualifier("cacemEntityManagerFactory") emf: EntityManagerFactory,
): PlatformTransactionManager = JpaTransactionManager(emf)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fr.gouv.cacem.monitorenv.config

import com.zaxxer.hikari.HikariDataSource
import jakarta.persistence.EntityManagerFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.orm.jpa.JpaTransactionManager
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
import org.springframework.transaction.PlatformTransactionManager
import javax.sql.DataSource

@Configuration
@EnableJpaRepositories(
basePackages = [
"fr.gouv.cacem.monitorenv.infrastructure.database.repositories",
],
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager",
)
class PrimaryJpaConfig(
@Value("\${spring.datasource.primary.url}") private val url: String,
@Value("\${spring.datasource.primary.user}") private val username: String,
@Value("\${spring.datasource.primary.password}") private val password: String,
) {
@Bean
@Primary
fun primaryDataSource(): HikariDataSource {
val ds = HikariDataSource()
ds.jdbcUrl = url
ds.username = username
ds.password = password
ds.driverClassName = "org.postgresql.Driver"
ds.maximumPoolSize = 10
ds.maxLifetime = 60000
return ds
}

@Bean
@Primary
fun primaryEntityManagerFactory(
builder: EntityManagerFactoryBuilder,
@Qualifier("primaryDataSource") dataSource: DataSource,
): LocalContainerEntityManagerFactoryBean =
builder
.dataSource(dataSource)
.packages("fr.gouv.cacem.monitorenv")
.persistenceUnit("primary")
.build()

@Bean
@Primary
fun primaryTransactionManager(
@Qualifier("primaryEntityManagerFactory") emf: EntityManagerFactory,
): PlatformTransactionManager = JpaTransactionManager(emf)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.gouv.cacem.monitorenv.domain.repositories

import fr.gouv.cacem.monitorenv.domain.entities.regulatoryArea.RegulatoryAreaEntity
import org.locationtech.jts.geom.Geometry

interface IRegulatoryAreaRepositoryNew {
fun findById(id: Int): RegulatoryAreaEntity?

fun findAll(): List<RegulatoryAreaEntity>

fun count(): Long

fun findAllIdsByGeometry(geometry: Geometry): List<Int>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.regulatoryAreas

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.regulatoryArea.RegulatoryAreaEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IRegulatoryAreaRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IRegulatoryAreaRepositoryNew
import org.slf4j.LoggerFactory
import org.springframework.transaction.annotation.Transactional

@UseCase
class GetAllRegulatoryAreas(
private val regulatoryAreaRepository: IRegulatoryAreaRepository,
class GetAllRegulatoryAreasNew(
private val regulatoryAreaRepository: IRegulatoryAreaRepositoryNew,
) {
private val logger = LoggerFactory.getLogger(GetAllRegulatoryAreas::class.java)
private val logger = LoggerFactory.getLogger(GetAllRegulatoryAreasNew::class.java)

@Transactional(
transactionManager = "cacemTransactionManager",
readOnly = true,
)
fun execute(): List<RegulatoryAreaEntity> {
logger.info("Attempt to GET all regulatory areas")
val regulatoryAreas = regulatoryAreaRepository.findAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ class RegulatoryAreas(
val regulatoryAreas = getAllRegulatoryAreas.execute()
return regulatoryAreas.map { RegulatoryAreaWithMetadataDataOutput.fromRegulatoryAreaEntity(it) }
}

@GetMapping("/new")
@Operation(summary = "Get new regulatory Areas")
fun getAllNew(): List<RegulatoryAreaWithMetadataDataOutput> {
val regulatoryAreas = getAllRegulatoryAreas.execute()
return regulatoryAreas.map { RegulatoryAreaWithMetadataDataOutput.fromRegulatoryAreaEntity(it) }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model
package fr.gouv.cacem.monitorenv.infrastructure.cacem.model

import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import fr.gouv.cacem.monitorenv.domain.entities.regulatoryArea.RegulatoryAreaEntity
import fr.gouv.cacem.monitorenv.infrastructure.database.model.TagRegulatoryAreaModel.Companion.toTagEntities
import fr.gouv.cacem.monitorenv.infrastructure.database.model.ThemeRegulatoryAreaModel.Companion.toThemeEntities
import fr.gouv.cacem.monitorenv.infrastructure.cacem.model.TagRegulatoryAreaNewModel.Companion.toTagEntities
import fr.gouv.cacem.monitorenv.infrastructure.cacem.model.ThemeRegulatoryAreaNewModel.Companion.toThemeEntities
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
Expand All @@ -16,8 +16,8 @@ import org.n52.jackson.datatype.jts.GeometryDeserializer
import org.n52.jackson.datatype.jts.GeometrySerializer

@Entity
@Table(name = "regulations_cacem")
data class RegulatoryAreaModel(
@Table(name = "reg_cacem", schema = "prod")
data class RegulatoryAreaNewModel(
@Id @Column(name = "id") val id: Int,
@Column(name = "plan") val plan: String?,
@Column(name = "date") val date: String?,
Expand All @@ -40,12 +40,12 @@ data class RegulatoryAreaModel(
mappedBy = "regulatoryArea",
fetch = FetchType.LAZY,
)
var tags: List<TagRegulatoryAreaModel>,
var tags: List<TagRegulatoryAreaNewModel>,
@OneToMany(
mappedBy = "regulatoryArea",
fetch = FetchType.LAZY,
)
var themes: List<ThemeRegulatoryAreaModel>,
var themes: List<ThemeRegulatoryAreaNewModel>,
@Column(name = "resume") val resume: String?,
@Column(name = "type") val type: String?,
@Column(name = "url") val url: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package fr.gouv.cacem.monitorenv.infrastructure.cacem.model

import fr.gouv.cacem.monitorenv.domain.entities.tags.TagEntity
import jakarta.persistence.*
import java.time.ZonedDateTime

@Entity
@Table(name = "tags")
data class TagNewModel(
@Id
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int,
val name: String,
val startedAt: ZonedDateTime?,
val endedAt: ZonedDateTime?,
@ManyToOne
@JoinColumn(name = "parent_id")
val parent: TagNewModel?,
@OneToMany(
mappedBy = "parent",
fetch = FetchType.LAZY,
cascade = [CascadeType.ALL],
)
var subTags: List<TagNewModel>,
) {
fun toTagEntity(): TagEntity =
TagEntity(
id = id,
name = name,
startedAt = startedAt,
endedAt = endedAt,
subTags = subTags.map { it.toTagEntity() },
)

companion object {
fun fromTagEntity(
tagEntity: TagEntity,
parent: TagNewModel? = null,
): TagNewModel {
val tagModel =
TagNewModel(
id = tagEntity.id,
name = tagEntity.name,
parent = parent,
startedAt = tagEntity.startedAt,
endedAt = tagEntity.endedAt,
subTags = listOf(),
)
tagModel.subTags = tagEntity.subTags.map { fromTagEntity(it, tagModel) }
return tagModel
}
}

override fun toString(): String = "TagModel(id=$id, name='$name', startedAt=$startedAt, endedAt=$endedAt)"

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as TagNewModel

if (id != other.id) return false
if (name != other.name) return false
if (startedAt != other.startedAt) return false
if (endedAt != other.endedAt) return false
if (parent != other.parent) return false

return true
}

override fun hashCode(): Int {
var result = id
result = 31 * result + name.hashCode()
result = 31 * result + (startedAt?.hashCode() ?: 0)
result = 31 * result + (endedAt?.hashCode() ?: 0)
return result
}
}
Loading
Loading