Skip to content

Commit

Permalink
prePersist event for batchInsert
Browse files Browse the repository at this point in the history
  • Loading branch information
gonmarques committed Feb 27, 2017
1 parent 1f3d4b4 commit ac547c8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ import java.util.concurrent.ConcurrentHashMap
import scala.collection.concurrent.Map
import scala.collection.convert.decorateAsScala._

/**
* Aggregates entity lifecycle utility methods.
*/
class LifecycleHelper {

private[repository] val lifecycleHandlerCache: Map[LifecycleHandlerCacheKey, Boolean] = new ConcurrentHashMap[LifecycleHandlerCacheKey, Boolean]().asScala

/**
* Checks if a given repository defines a lifecycle listener.
*/
def isLifecycleHandlerDefined(clazz: Class[_ <: Repository[_, _]], event: LifecycleEvent): Boolean = {
val key = LifecycleHandlerCacheKey(clazz, event)
val entry = lifecycleHandlerCache.get(key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ abstract class Repository[T <: Entity[T, ID], ID](val driver: JdbcProfile) {
* Builds a batch persister
*/
protected def getBatchPersister(transformer: T => T): Seq[T] => DBIO[Option[Int]] =
(entities: Seq[T]) => tableQueryCompiled ++= entities.map(transformer)
(entities: Seq[T]) => tableQueryCompiled ++= entities.map(prePersist compose transformer)

/**
* Updates a given entity in the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ abstract class RepositoryLifecycleEventsTest(override val config: Config) extend
read.name should equal("prePersist")
}

it should "trigger a prePersist event - non versioned entity + auto PK - batchInsert" in {
import scala.concurrent.ExecutionContext.Implicits.global
executeAction(lifecycleEntityRepositoryPrePersistAutoPk.batchInsert(Seq(LifecycleEntity(None, "john"), LifecycleEntity(None, "john"))))
executeAction(lifecycleEntityRepositoryPrePersistAutoPk.findOne(1)).get.name should equal("prePersist")
executeAction(lifecycleEntityRepositoryPrePersistAutoPk.findOne(2)).get.name should equal("prePersist")
}

it should "trigger a prePersist event - non versioned entity + manual PK - batchInsert" in {
import scala.concurrent.ExecutionContext.Implicits.global
executeAction(lifecycleEntityRepositoryManualPk.batchInsert(Seq(LifecycleEntityManualPk(Some(7), "john"), LifecycleEntityManualPk(Some(8), "john"))))
executeAction(lifecycleEntityRepositoryManualPk.findOne(7)).get.name should equal("prePersist")
executeAction(lifecycleEntityRepositoryManualPk.findOne(8)).get.name should equal("prePersist")
}

it should "trigger a prePersist event - versioned entity + auto PK - save" in {
import scala.concurrent.ExecutionContext.Implicits.global
val entity = executeAction(lifecycleVersionedEntityRepositoryPrePersistAutoPk.save(LifecycleVersionedEntity(None, "john", None)))
Expand All @@ -86,6 +100,20 @@ abstract class RepositoryLifecycleEventsTest(override val config: Config) extend
val read = executeAction(lifecycleVersionedEntityRepositoryManualPk.findOne(entity.id.get)).get
read.name should equal("prePersist")
}

it should "trigger a prePersist event - versioned entity + auto PK - batchInsert" in {
import scala.concurrent.ExecutionContext.Implicits.global
executeAction(lifecycleVersionedEntityRepositoryPrePersistAutoPk.batchInsert(Seq(LifecycleVersionedEntity(None, "john", None), LifecycleVersionedEntity(None, "john", None))))
executeAction(lifecycleVersionedEntityRepositoryPrePersistAutoPk.findOne(1)).get.name should equal("prePersist")
}

it should "trigger a prePersist event - versioned entity + manual PK - batchInsert" in {
import scala.concurrent.ExecutionContext.Implicits.global
executeAction(lifecycleVersionedEntityRepositoryManualPk.batchInsert(Seq(LifecycleVersionedEntityManualPk(Some(7), "john", None), LifecycleVersionedEntityManualPk(Some(8), "john", None))))
executeAction(lifecycleVersionedEntityRepositoryManualPk.findOne(7)).get.name should equal("prePersist")
executeAction(lifecycleVersionedEntityRepositoryManualPk.findOne(8)).get.name should equal("prePersist")
}

}

case class LifecycleEntity(override val id: Option[Int] = None, name: String) extends Entity[LifecycleEntity, Int]{
Expand Down

0 comments on commit ac547c8

Please sign in to comment.