Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/functional packages take two #66

Merged
merged 10 commits into from
May 26, 2024
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ sys-test/target
# misc
/**/.DS_Store

# kotlin compiler v 2.x
/.kotlin/sessions/
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package com.github.jactor.persistence

import java.text.SimpleDateFormat
import io.swagger.v3.oas.annotations.OpenAPIDefinition
import io.swagger.v3.oas.annotations.info.Info
import org.springframework.boot.CommandLineRunner
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import com.fasterxml.jackson.annotation.JsonInclude.Include
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule

@Configuration
@OpenAPIDefinition(info = Info(title = "jactor-persistence", version = "v1"))
class JactorPersistenceConfig {

@Bean
fun commandLineRunner(applicationContext: ApplicationContext): CommandLineRunner {
return CommandLineRunner {
JactorPersistence.inspect(applicationContext, it)
@Bean
fun commandLineRunner(applicationContext: ApplicationContext): CommandLineRunner {
return CommandLineRunner {
JactorPersistence.inspect(applicationContext, it)
}
}
}

@Bean
fun objectMapper(): ObjectMapper = ObjectMapper()
.registerModule(JavaTimeModule())
.registerModule(Jdk8Module())
.setDateFormat(SimpleDateFormat("yyyy-MM-dd"))
.setSerializationInclusion(Include.NON_NULL)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.jactor.persistence.address

import java.util.UUID

internal object AddressBuilder {
fun new(addressModel: AddressModel) = AddressData(
addressModel = addressModel.copy(
persistentModel = addressModel.persistentModel.copy(id = UUID.randomUUID())
)
)

fun unchanged(addressModel: AddressModel): AddressData = AddressData(
addressModel = addressModel
)

@JvmRecord
data class AddressData(val addressModel: AddressModel) {
fun build(): AddressEntity = AddressEntity(addressModel = addressModel)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.github.jactor.persistence.entity
package com.github.jactor.persistence.address

import java.time.LocalDateTime
import java.util.Objects
import java.util.UUID
import org.apache.commons.lang3.builder.ToStringBuilder
import org.apache.commons.lang3.builder.ToStringStyle
import com.github.jactor.persistence.dto.AddressInternalDto
import com.github.jactor.persistence.common.PersistentDataEmbeddable
import com.github.jactor.persistence.common.PersistentEntity
import jakarta.persistence.AttributeOverride
import jakarta.persistence.Column
import jakarta.persistence.Embedded
Expand All @@ -24,7 +25,8 @@ class AddressEntity : PersistentEntity<AddressEntity?> {
@AttributeOverride(name = "timeOfCreation", column = Column(name = "CREATION_TIME"))
@AttributeOverride(name = "modifiedBy", column = Column(name = "UPDATED_BY"))
@AttributeOverride(name = "timeOfModification", column = Column(name = "UPDATED_TIME"))
private lateinit var persistentDataEmbeddable: PersistentDataEmbeddable
lateinit var persistentDataEmbeddable: PersistentDataEmbeddable
internal set

@Column(name = "ADDRESS_LINE_1", nullable = false)
var addressLine1: String? = null
Expand All @@ -44,7 +46,8 @@ class AddressEntity : PersistentEntity<AddressEntity?> {
@Column(name = "ZIP_CODE", nullable = false)
var zipCode: String? = null

@Suppress("UNUSED") constructor() {
@Suppress("UNUSED")
constructor() {
// used by entity manager
}

Expand All @@ -62,19 +65,27 @@ class AddressEntity : PersistentEntity<AddressEntity?> {
zipCode = address.zipCode
}

internal constructor(addressInternalDto: AddressInternalDto) {
persistentDataEmbeddable = PersistentDataEmbeddable(addressInternalDto.persistentDto)
addressLine1 = addressInternalDto.addressLine1
addressLine2 = addressInternalDto.addressLine2
addressLine3 = addressInternalDto.addressLine3
city = addressInternalDto.city
country = addressInternalDto.country
id = addressInternalDto.id
zipCode = addressInternalDto.zipCode
internal constructor(addressModel: AddressModel) {
persistentDataEmbeddable = PersistentDataEmbeddable(addressModel.persistentModel)
addressLine1 = addressModel.addressLine1
addressLine2 = addressModel.addressLine2
addressLine3 = addressModel.addressLine3
city = addressModel.city
country = addressModel.country
id = addressModel.id
zipCode = addressModel.zipCode
}

fun asDto(): AddressInternalDto {
return AddressInternalDto(persistentDataEmbeddable.asPersistentDto(id), zipCode, addressLine1, addressLine2, addressLine3, city, country)
fun toModel(): AddressModel {
return AddressModel(
persistentDataEmbeddable.toModel(id),
zipCode,
addressLine1,
addressLine2,
addressLine3,
city,
country
)
}

override fun copyWithoutId(): AddressEntity {
Expand All @@ -96,11 +107,11 @@ class AddressEntity : PersistentEntity<AddressEntity?> {
val addressEntity = other as AddressEntity

return this === other || addressLine1 == addressEntity.addressLine1 &&
addressLine2 == addressEntity.addressLine2 &&
addressLine3 == addressEntity.addressLine3 &&
city == addressEntity.city &&
country == addressEntity.country &&
zipCode == addressEntity.zipCode
addressLine2 == addressEntity.addressLine2 &&
addressLine3 == addressEntity.addressLine3 &&
city == addressEntity.city &&
country == addressEntity.country &&
zipCode == addressEntity.zipCode
}

override fun hashCode(): Int {
Expand All @@ -119,12 +130,8 @@ class AddressEntity : PersistentEntity<AddressEntity?> {
.toString()
}

override val createdBy: String
get() = persistentDataEmbeddable.createdBy
override val timeOfCreation: LocalDateTime
get() = persistentDataEmbeddable.timeOfCreation
override val modifiedBy: String
get() = persistentDataEmbeddable.modifiedBy
override val timeOfModification: LocalDateTime
get() = persistentDataEmbeddable.timeOfModification
override val createdBy: String get() = persistentDataEmbeddable.createdBy
override val timeOfCreation: LocalDateTime get() = persistentDataEmbeddable.timeOfCreation
override val modifiedBy: String get() = persistentDataEmbeddable.modifiedBy
override val timeOfModification: LocalDateTime get() = persistentDataEmbeddable.timeOfModification
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.github.jactor.persistence.address

import java.util.UUID
import com.fasterxml.jackson.annotation.JsonIgnore
import com.github.jactor.persistence.common.PersistentModel
import com.github.jactor.shared.api.AddressDto

@JvmRecord
data class AddressModel(
val persistentModel: PersistentModel = PersistentModel(),
val zipCode: String? = null,
val addressLine1: String? = null,
val addressLine2: String? = null,
val addressLine3: String? = null,
val city: String? = null,
val country: String? = null
) {
val id: UUID? @JsonIgnore get() = persistentModel.id

constructor(
persistentModel: PersistentModel, addressModel: AddressModel
) : this(
persistentModel = persistentModel,
addressLine1 = addressModel.addressLine1,
addressLine2 = addressModel.addressLine2,
addressLine3 = addressModel.addressLine3,
city = addressModel.city,
country = addressModel.country,
zipCode = addressModel.zipCode
)

constructor(addressDto: AddressDto) : this(
persistentModel = PersistentModel(persistentDto = addressDto.persistentDto),
addressLine1 = addressDto.addressLine1,
addressLine2 = addressDto.addressLine2,
addressLine3 = addressDto.addressLine3,
city = addressDto.city,
country = addressDto.country,
zipCode = addressDto.zipCode
)

fun toAddressDto() = AddressDto(
persistentDto = persistentModel.toDto(),
addressLine1 = addressLine1,
addressLine2 = addressLine2,
addressLine3 = addressLine3,
city = city,
country = country,
zipCode = zipCode
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.jactor.persistence.repository
package com.github.jactor.persistence.address

import java.util.UUID
import com.github.jactor.persistence.entity.AddressEntity
import org.springframework.data.repository.CrudRepository

interface AddressRepository : CrudRepository<AddressEntity, UUID> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.jactor.persistence.aop

import com.github.jactor.persistence.entity.PersistentEntity
import com.github.jactor.persistence.common.PersistentEntity
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
Expand All @@ -9,7 +9,7 @@ import org.springframework.stereotype.Component
@Aspect
@Component
class ModifierAspect {
@Before("execution(* com.github.jactor.persistence.repository.*Repository.save(..))")
@Before("execution(* com.github.jactor.persistence.*.*Repository.save(..))")
fun modifyPersistentEntity(joinPoint: JoinPoint): Any? {
return joinPoint.args
.filterIsInstance<PersistentEntity<*>>()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.jactor.persistence.api.controller

import com.github.jactor.persistence.address.AddressModel
import com.github.jactor.persistence.common.PersistentModel
import com.github.jactor.persistence.person.PersonModel
import com.github.jactor.persistence.user.UserModel
import com.github.jactor.shared.api.PersistentData
import com.github.jactor.shared.api.PersonDto
import com.github.jactor.shared.api.UserDto

fun PersistentData.harIdentifikator(): Boolean = persistentDto.id != null
fun PersistentData.harIkkeIdentifikator(): Boolean = persistentDto.id == null
fun PersonDto.toModel() = PersonModel(
persistentModel = PersistentModel(persistentDto = persistentDto),
address = address?.let { AddressModel(addressDto = it) },
locale = locale,
firstName = firstName,
surname = surname,
description = description
)

fun UserDto.toModel()= UserModel (
persistentModel = PersistentModel(persistentDto = persistentDto),
person = person?.let { PersonModel(personDto = it) },
emailAddress = emailAddress,
username = username,
usertype = UserModel.Usertype.valueOf(userType.name)
)
Loading
Loading