diff --git a/src/main/kotlin/fr/postgresjson/entity/Entity.kt b/src/main/kotlin/fr/postgresjson/entity/Entity.kt index 446b98a..0ca917a 100644 --- a/src/main/kotlin/fr/postgresjson/entity/Entity.kt +++ b/src/main/kotlin/fr/postgresjson/entity/Entity.kt @@ -1,126 +1,5 @@ package fr.postgresjson.entity -import org.joda.time.DateTime -import java.util.* - interface Serializable interface EntityI : Serializable interface Parameter : Serializable - -abstract class Entity(open var id: T? = null) : EntityI -open class UuidEntity(override var id: UUID? = UUID.randomUUID()) : Entity(id) -open class IdEntity(override var id: Int? = null) : Entity(id) - -/* Version */ -interface EntityVersioning { - var versionId: ID - var versionNumber: NUMBER? -} - -class UuidEntityVersioning : EntityVersioning { - override var versionId: UUID = UUID.randomUUID() - override var versionNumber: Int? = null -} - -/* Dates */ -interface EntityCreatedAt { - var createdAt: DateTime? -} - -interface EntityUpdatedAt { - var updatedAt: DateTime? -} - -interface EntityDeletedAt { - var deletedAt: DateTime? - fun isDeleted(): Boolean { - val deletedAt = deletedAt - return deletedAt != null && deletedAt < DateTime.now() - } -} - -class EntityCreatedAtImp : EntityCreatedAt { - override var createdAt: DateTime? = null -} - -class EntityUpdatedAtImp : EntityUpdatedAt { - override var updatedAt: DateTime? = null -} - -class EntityDeletedAtImp : EntityDeletedAt { - override var deletedAt: DateTime? = null -} - -/* Author */ -interface EntityCreatedBy { - var createdBy: T? -} - -interface EntityUpdatedBy { - var updatedBy: T? -} - -interface EntityDeletedBy { - var deletedBy: T? -} - -class EntityCreatedByImp( - override var createdBy: UserT? -) : EntityCreatedBy - -class EntityUpdatedByImp( - override var updatedBy: UserT? -) : EntityUpdatedBy - -class EntityDeletedByImp( - override var deletedBy: UserT? -) : EntityDeletedBy - -/* Mixed */ -class EntityDeletedImp( - override var deletedBy: UserT? = null -) : EntityDeletedBy, - EntityDeletedAt by EntityDeletedAtImp() - -class EntityUpdatedImp( - override var updatedAt: DateTime? = null, - override var updatedBy: UserT? = null -) : EntityUpdatedBy, - EntityUpdatedAt by EntityUpdatedAtImp() - -class EntityCreatedImp( - override var createdAt: DateTime? = null, - override var createdBy: UserT? = null -) : EntityCreatedBy, - EntityCreatedAt by EntityCreatedAtImp() - -/* Published */ -interface Published { - var publishedAt: DateTime? - var publishedBy: UserT? -} - -class EntityPublishedImp( - override var publishedBy: UserT? -) : Published { - override var publishedAt: DateTime? = null -} - -/* Implementation */ -abstract class EntityImp( - updatedBy: UserT? -) : Entity(), - EntityCreatedAt by EntityCreatedAtImp(), - EntityUpdatedAt by EntityUpdatedAtImp(), - EntityDeletedAt by EntityDeletedAtImp(), - EntityCreatedBy by EntityCreatedByImp(updatedBy), - EntityUpdatedBy by EntityUpdatedByImp(updatedBy), - EntityDeletedBy by EntityDeletedByImp(updatedBy) - -abstract class UuidEntityExtended( - updatedBy: UserT?, - publishedBy: UserT? -) : - EntityImp(updatedBy), - EntityVersioning by UuidEntityVersioning(), - Published by EntityPublishedImp(publishedBy) diff --git a/src/main/kotlin/fr/postgresjson/entity/immutable/ImmutableEntity.kt b/src/main/kotlin/fr/postgresjson/entity/immutable/ImmutableEntity.kt new file mode 100644 index 0000000..6ed1324 --- /dev/null +++ b/src/main/kotlin/fr/postgresjson/entity/immutable/ImmutableEntity.kt @@ -0,0 +1,88 @@ +package fr.postgresjson.entity.immutable + +import fr.postgresjson.entity.EntityI +import fr.postgresjson.entity.mutable.EntityDeletedAt +import fr.postgresjson.entity.mutable.EntityDeletedAtImp +import fr.postgresjson.entity.mutable.EntityDeletedBy +import fr.postgresjson.entity.mutable.EntityDeletedByImp +import org.joda.time.DateTime +import java.util.* + +interface EntityRefI : EntityI { + val id: T +} + +interface UuidEntityI : EntityRefI { + override val id: UUID +} + +abstract class Entity(override val id: T) : EntityRefI +open class UuidEntity(override val id: UUID = UUID.randomUUID()) : UuidEntityI, Entity(id) + +/* Version */ +interface EntityVersioning { + val versionId: ID + val versionNumber: NUMBER +} + +class UuidEntityVersioning( + override val versionNumber: Int, + override val versionId: UUID = UUID.randomUUID() +) : EntityVersioning + +/* Dates */ +interface EntityCreatedAt { + val createdAt: DateTime +} +interface EntityUpdatedAt { + var updatedAt: DateTime +} + +class EntityCreatedAtImp( + override val createdAt: DateTime = DateTime.now() +) : EntityCreatedAt + +class EntityUpdatedAtImp( + override var updatedAt: DateTime = DateTime.now() +) : EntityUpdatedAt + +/* Author */ +interface EntityCreatedBy { + val createdBy: T +} +interface EntityUpdatedBy { + var updatedBy: T +} + +class EntityCreatedByImp( + override val createdBy: UserT +) : EntityCreatedBy + +class EntityUpdatedByImp( + override var updatedBy: UserT +) : EntityUpdatedBy + +/* Mixed */ +class EntityCreatedImp( + override val createdAt: DateTime = DateTime.now(), + createdBy: UserT +) : EntityCreatedBy by EntityCreatedByImp(createdBy), + EntityCreatedAt by EntityCreatedAtImp() + +class EntityUpdatedImp( + updatedAt: DateTime = DateTime.now(), + override var updatedBy: UserT +) : EntityUpdatedBy, + EntityUpdatedAt by EntityUpdatedAtImp(updatedAt) + +/* Implementation */ +abstract class EntityImp( + updatedBy: UserT, + updatedAt: DateTime = DateTime.now() +) : UuidEntity(), + EntityCreatedAt by EntityCreatedAtImp(updatedAt), + EntityUpdatedAt by EntityUpdatedAtImp(updatedAt), + EntityDeletedAt by EntityDeletedAtImp(), + EntityCreatedBy by EntityCreatedByImp(updatedBy), + EntityUpdatedBy by EntityUpdatedByImp(updatedBy), + EntityDeletedBy by EntityDeletedByImp(updatedBy) \ No newline at end of file diff --git a/src/main/kotlin/fr/postgresjson/entity/mutable/MutableEntity.kt b/src/main/kotlin/fr/postgresjson/entity/mutable/MutableEntity.kt new file mode 100644 index 0000000..96f5145 --- /dev/null +++ b/src/main/kotlin/fr/postgresjson/entity/mutable/MutableEntity.kt @@ -0,0 +1,135 @@ +package fr.postgresjson.entity.mutable + +import fr.postgresjson.entity.EntityI +import org.joda.time.DateTime +import java.util.* + +interface EntityRefI : EntityI { + var id: T? +} + +interface UuidEntityI : EntityRefI { + override var id: UUID? +} + +interface IdEntityI : EntityRefI { + override var id: Int? +} + +abstract class Entity(override var id: T? = null) : EntityRefI +open class UuidEntity(override var id: UUID? = UUID.randomUUID()) : UuidEntityI, Entity(id) +open class IdEntity(override var id: Int? = null) : IdEntityI, Entity(id) + +/* Version */ +interface EntityVersioning { + var versionId: ID + var versionNumber: NUMBER? +} + +class UuidEntityVersioning : EntityVersioning { + override var versionId: UUID = UUID.randomUUID() + override var versionNumber: Int? = null +} + +/* Dates */ +interface EntityCreatedAt { + var createdAt: DateTime? +} + +interface EntityUpdatedAt { + var updatedAt: DateTime? +} + +interface EntityDeletedAt { + var deletedAt: DateTime? + fun isDeleted(): Boolean { + val deletedAt = deletedAt + return deletedAt != null && deletedAt < DateTime.now() + } +} + +class EntityCreatedAtImp : EntityCreatedAt { + override var createdAt: DateTime? = null +} + +class EntityUpdatedAtImp : EntityUpdatedAt { + override var updatedAt: DateTime? = null +} + +class EntityDeletedAtImp : EntityDeletedAt { + override var deletedAt: DateTime? = null +} + +/* Author */ +interface EntityCreatedBy { + var createdBy: T? +} + +interface EntityUpdatedBy { + var updatedBy: T? +} + +interface EntityDeletedBy { + var deletedBy: T? +} + +class EntityCreatedByImp( + override var createdBy: UserT? +) : EntityCreatedBy + +class EntityUpdatedByImp( + override var updatedBy: UserT? +) : EntityUpdatedBy + +class EntityDeletedByImp( + override var deletedBy: UserT? +) : EntityDeletedBy + +/* Mixed */ +class EntityDeletedImp( + override var deletedBy: UserT? = null +) : EntityDeletedBy, + EntityDeletedAt by EntityDeletedAtImp() + +class EntityUpdatedImp( + override var updatedAt: DateTime? = null, + override var updatedBy: UserT? = null +) : EntityUpdatedBy, + EntityUpdatedAt by EntityUpdatedAtImp() + +class EntityCreatedImp( + override var createdAt: DateTime? = null, + override var createdBy: UserT? = null +) : EntityCreatedBy, + EntityCreatedAt by EntityCreatedAtImp() + +/* Published */ +interface Published { + var publishedAt: DateTime? + var publishedBy: UserT? +} + +class EntityPublishedImp( + override var publishedBy: UserT? +) : Published { + override var publishedAt: DateTime? = null +} + +/* Implementation */ +abstract class EntityImp( + updatedBy: UserT? +) : Entity(), + EntityCreatedAt by EntityCreatedAtImp(), + EntityUpdatedAt by EntityUpdatedAtImp(), + EntityDeletedAt by EntityDeletedAtImp(), + EntityCreatedBy by EntityCreatedByImp(updatedBy), + EntityUpdatedBy by EntityUpdatedByImp(updatedBy), + EntityDeletedBy by EntityDeletedByImp(updatedBy) + +abstract class UuidEntityExtended( + updatedBy: UserT?, + publishedBy: UserT? +) : + EntityImp(updatedBy), + EntityVersioning by UuidEntityVersioning(), + Published by EntityPublishedImp(publishedBy) diff --git a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt index 1d13d62..54c3c5e 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt @@ -3,7 +3,7 @@ package fr.postgresjson.migration import com.fasterxml.jackson.core.type.TypeReference import fr.postgresjson.connexion.Connection import fr.postgresjson.definition.Function.FunctionNotFound -import fr.postgresjson.entity.Entity +import fr.postgresjson.entity.mutable.Entity import fr.postgresjson.migration.Migration.Action import fr.postgresjson.migration.Migration.Status import fr.postgresjson.utils.LoggerDelegate diff --git a/src/main/kotlin/fr/postgresjson/migration/Query.kt b/src/main/kotlin/fr/postgresjson/migration/Query.kt index 700a86a..ef43c88 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Query.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Query.kt @@ -1,7 +1,7 @@ package fr.postgresjson.migration import fr.postgresjson.connexion.Connection -import fr.postgresjson.entity.Entity +import fr.postgresjson.entity.mutable.Entity import fr.postgresjson.migration.Migration.Action import java.util.* diff --git a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt index e9cc617..d9be2fa 100644 --- a/src/test/kotlin/fr/postgresjson/ConnectionTest.kt +++ b/src/test/kotlin/fr/postgresjson/ConnectionTest.kt @@ -1,7 +1,7 @@ package fr.postgresjson import fr.postgresjson.connexion.Paginated -import fr.postgresjson.entity.IdEntity +import fr.postgresjson.entity.mutable.IdEntity import fr.postgresjson.entity.Parameter import org.junit.Assert.* import org.junit.jupiter.api.Assertions @@ -38,8 +38,7 @@ class ConnectionTest() : TestAbstract() { """.trimIndent() ) assertNotNull(objs) - assertTrue(objs is List) - assertEquals(objs!!.size, 2) + assertEquals(objs.size, 2) assertEquals(objs[0].id, 1) assertEquals(objs[0].test!!.id, 1) } diff --git a/src/test/kotlin/fr/postgresjson/EntityTest.kt b/src/test/kotlin/fr/postgresjson/EntityTest.kt index 03cfc95..4452c4c 100644 --- a/src/test/kotlin/fr/postgresjson/EntityTest.kt +++ b/src/test/kotlin/fr/postgresjson/EntityTest.kt @@ -1,6 +1,7 @@ package fr.postgresjson -import fr.postgresjson.entity.* +import fr.postgresjson.entity.EntityI +import fr.postgresjson.entity.mutable.* import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance diff --git a/src/test/kotlin/fr/postgresjson/RequesterTest.kt b/src/test/kotlin/fr/postgresjson/RequesterTest.kt index de72950..4ac074b 100644 --- a/src/test/kotlin/fr/postgresjson/RequesterTest.kt +++ b/src/test/kotlin/fr/postgresjson/RequesterTest.kt @@ -2,7 +2,7 @@ package fr.postgresjson import fr.postgresjson.connexion.Paginated import fr.postgresjson.connexion.Requester -import fr.postgresjson.entity.IdEntity +import fr.postgresjson.entity.mutable.IdEntity import org.junit.Assert import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test diff --git a/src/test/kotlin/fr/postgresjson/SerializerTest.kt b/src/test/kotlin/fr/postgresjson/SerializerTest.kt index 9d9bd10..f222fb7 100644 --- a/src/test/kotlin/fr/postgresjson/SerializerTest.kt +++ b/src/test/kotlin/fr/postgresjson/SerializerTest.kt @@ -1,6 +1,6 @@ package fr.postgresjson -import fr.postgresjson.entity.IdEntity +import fr.postgresjson.entity.mutable.IdEntity import fr.postgresjson.serializer.Serializer import fr.postgresjson.serializer.deserialize import fr.postgresjson.serializer.serialize