Skip to content

Commit

Permalink
BeeRepository now contains persist method that is also generated
Browse files Browse the repository at this point in the history
  • Loading branch information
kurbaniec committed Jan 14, 2024
1 parent 5af87b4 commit 0f9653d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ class BeePersistentInstantiatorCodegen(
}

fun processViews(views: ViewInfo, entities: List<EntityInfo>) {
val file = FileSpec.builder(packageName, fileName)
val file = FileSpec
.builder(packageName, fileName)
.addAnnotation(
AnnotationSpec.builder(ClassName("", "Suppress"))
.addMember("%S", "UNCHECKED_CAST")
.build()
)

for (entity in entities) {
// Do not generate info for super classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,12 @@ class BeePersistentRepoCodegen(
}

private fun TypeSpec.Builder.buildPersist(): TypeSpec.Builder = apply {
// TODO: On inheritance generic T?
val typeVariable = TypeVariableName("E", poetMap.classMapping(CLAZZ))
val persistFun = FunSpec.builder("persist")
.addParameter("entity", poetMap.classMapping(CLAZZ))
.addModifiers(KModifier.OVERRIDE)
.addTypeVariable(typeVariable)
.addParameter("entity", typeVariable)
.returns(typeVariable)
val idLiteral = entity.defaultIdLiteral()

if (entity.subClasses == null) {
Expand All @@ -270,6 +273,7 @@ class BeePersistentRepoCodegen(
persistFun.buildPersistBlock(subEntity, idLiteral, padding = " ")
persistFun.addStatement("}")
}
persistFun.addStatement("throw Exception(\"Unknown entity [\${entity.javaClass.canonicalName}]\")")
}
persistFun.apply {

Expand All @@ -294,17 +298,18 @@ class BeePersistentRepoCodegen(
addNamedStmt("${padding}em.persist(e)")
addNamedStmt("${padding}em.flush()")
addNamedStmt("${padding}em.clear()")
addNamedStmt("${padding}@Suppress(\"UNCHECKED_CAST\")")

if (e.relations.isEmpty()) {
addStatement("${padding}return e")
addStatement("${padding}return e as E")
return@apply
}

addNamedStmt("${padding}return e.$_BEE_CLONE_FN { ")
for (relation in e.relations) {
addStatement("$padding ${relation.simpleName}=null")
}
addStatement("$padding}")
addStatement("$padding} as E")
}

private fun TypeSpec.Builder.buildSelect(): TypeSpec.Builder = apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface BeeBlazeRepository<T : Any, ID: Any> {
val cbf: CriteriaBuilderFactory
val evm: EntityViewManager

fun <E: T> persist(entity: E): E

fun select(
selection: BeeSelection = BeeSelection.empty(),
dsl: SelectQuery<T>.() -> Selection<T> = { this }
Expand Down

0 comments on commit 0f9653d

Please sign in to comment.