Skip to content

Commit

Permalink
refactored builder and introduced owner property
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Sep 26, 2024
1 parent 6c910bf commit a0b3564
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 97 deletions.
40 changes: 0 additions & 40 deletions src/main/kotlin/org/sirius/dorm/ObjectManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,18 @@ import org.sirius.dorm.persistence.entity.EntityEntity
import org.sirius.dorm.query.*
import org.sirius.dorm.transaction.Status
import org.sirius.dorm.transaction.TransactionState
import org.sirius.common.type.Type
import org.sirius.common.type.base.int
import jakarta.persistence.EntityManager
import jakarta.persistence.PersistenceContext
import org.antlr.v4.runtime.CharStreams
import org.antlr.v4.runtime.CommonTokenStream
import org.antlr.v4.runtime.RecognitionException
import org.sirius.common.type.base.long
import org.sirius.dorm.query.parser.OQLParser
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Component
import org.springframework.transaction.PlatformTransactionManager
import java.util.concurrent.ConcurrentHashMap


class ObjectDescriptorBuilder(val manager: ObjectManager, val name: String) {
// instance data

private val properties = ArrayList<PropertyDescriptor<Any>>()

init {
attribute("id", long(), true)
}

// fluent

fun <T: Any> attribute(name: String, type: Type<T>, isPrimaryKey: Boolean = false) : ObjectDescriptorBuilder {
properties.add(AttributeDescriptor(name, type as Type<Any>, isPrimaryKey))

return this
}

fun relation(name: String, target: String, multiplicity: Multiplicity, cascade: Cascade? = null) : ObjectDescriptorBuilder {
properties.add(RelationDescriptor(name, target, multiplicity, cascade, null))

return this
}

fun relation(name: String, target: String, multiplicity: Multiplicity, inverse: String, cascade: Cascade? = null) : ObjectDescriptorBuilder {
properties.add(RelationDescriptor(name, target, multiplicity, cascade, inverse))

return this
}

// public

fun register() {
manager.register( ObjectDescriptor(name, properties.toTypedArray()))
}
}

@Component
class ObjectManager() {
// instance data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.sirius.dorm.`object`.Property
import org.sirius.dorm.persistence.entity.PropertyEntity
import org.sirius.dorm.transaction.Status

class AttributeDescriptor<T:Any>(name: String, val type: Type<T>, val isPrimaryKey : Boolean = false) : PropertyDescriptor<T>(name) {
class AttributeDescriptor<T:Any>(name: String, val type: Type<*>, val isPrimaryKey : Boolean = false) : PropertyDescriptor<T>(name) {
// public

fun baseType() : Class<*> {
Expand Down
145 changes: 145 additions & 0 deletions src/main/kotlin/org/sirius/dorm/model/ObjectDescriptorBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package org.sirius.dorm.model
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
* All rights reserved
*/

import org.sirius.common.type.Type
import org.sirius.common.type.base.long
import org.sirius.dorm.ObjectManager


abstract class PropertyBuilder() {
// instance data

protected var name = ""

// protected

abstract fun build() : PropertyDescriptor<Any>
}
class AttributeBuilder() : PropertyBuilder() {
// instance data

private var primaryKey = false
private var type: Type<*>? = null

// fluent

fun name(name: String) : AttributeBuilder {
this.name = name

return this
}

fun type(type: Type<*>) : AttributeBuilder {
this.type = type

return this
}

fun primaryKey() : AttributeBuilder {
this.primaryKey = true

return this
}

// public

override fun build() : AttributeDescriptor<Any> {
// done

return AttributeDescriptor(name, type!!, primaryKey)
}
}

fun attribute(name: String) : AttributeBuilder {
return AttributeBuilder().name(name)
}

class RelationBuilder() : PropertyBuilder() {
// instance data

private var target = ""
private var multiplicity : Multiplicity? = null
private var cascade : Cascade? = null
private var inverse : String? = null
private var owner = false

// fluent

fun name(name: String) : RelationBuilder {
this.name = name

return this
}

fun target(target: String) : RelationBuilder {
this.target = target

return this
}

fun inverse(inverse: String) : RelationBuilder {
this.inverse = inverse

return this
}

fun multiplicity(multiplicity: Multiplicity) : RelationBuilder {
this.multiplicity = multiplicity

return this
}

fun cascade(cascade: Cascade) : RelationBuilder {
this.cascade = cascade

return this
}

fun owner() : RelationBuilder {
this.owner = true

return this
}

// build

override fun build() : RelationDescriptor<Any> {
return RelationDescriptor(name, target, multiplicity!!, cascade, inverse, owner)
}
}

fun relation(name: String) : RelationBuilder {
return RelationBuilder().name(name)
}

class ObjectDescriptorBuilder(val manager: ObjectManager, val name: String) {
// instance data

private val properties = ArrayList<PropertyDescriptor<Any>>()

init {
property(id)
}

// fluent

fun property(propertyBuilder: PropertyBuilder) : ObjectDescriptorBuilder {
properties.add(propertyBuilder.build())

return this
}

// public

fun register() {
manager.register(ObjectDescriptor(name, properties.toTypedArray()))
}

companion object {
val id = attribute("id").type(long()).primaryKey()
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.sirius.dorm.`object`.SingleValuedRelation
import org.sirius.dorm.persistence.entity.PropertyEntity
import org.sirius.dorm.transaction.Status

open class RelationDescriptor<T:Any>(name: String, val target: String, val multiplicity: Multiplicity, val cascade: Cascade?, val inverse: String?) : PropertyDescriptor<T>(name) {
open class RelationDescriptor<T:Any>(name: String, val target: String, val multiplicity: Multiplicity, val cascade: Cascade?, val inverse: String?, val owner: Boolean) : PropertyDescriptor<T>(name) {
// instance data

var targetDescriptor: ObjectDescriptor? = null
Expand Down Expand Up @@ -55,6 +55,6 @@ open class RelationDescriptor<T:Any>(name: String, val target: String, val multi
}

fun isOwner() : Boolean {
return inverse !== null
return owner
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ open class ObjectDescriptorDeserializer() : StdDeserializer<ObjectDescriptor>(Ob
}
else {
val multiplicity = Multiplicity.valueOf( property["target"].asText())
val owner = property["owner"].asBoolean()
val cascade: Cascade? = null
if ( property.has("cascade"))
Cascade.valueOf( property["cascade"].asText())
properties.add(RelationDescriptor(propertyName, property["target"].asText(), multiplicity, cascade, property["inverse"].asText()))
properties.add(RelationDescriptor(propertyName, property["target"].asText(), multiplicity, cascade, property["inverse"].asText(), owner))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ open class ObjectDescriptorSerializer : StdSerializer<ObjectDescriptor>(ObjectDe
else {
jsonGenerator.writeStringField("target", property.asRelation().target)
jsonGenerator.writeStringField("inverse", property.asRelation().inverse)
jsonGenerator.writeBooleanField("owner", property.asRelation().owner)
if (property.asRelation().cascade !== null)
jsonGenerator.writeStringField("cascade", property.asRelation().cascade!!.name)
jsonGenerator.writeObjectField("multiplicity", property.asRelation().multiplicity.name)
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class MultiValuedRelation(relation: RelationDescriptor<*>, status: Status, val o
override fun isLoaded() : Boolean {
return objects !== null
}
override fun addInverse(element: DataObject) {
override fun addedToInverse(element: DataObject) {
this.objects!!.add(element)
}
override fun removeInverse(element: DataObject) {
override fun removedFromInverse(element: DataObject) {
this.objects!!.remove(element)
}

Expand Down Expand Up @@ -112,7 +112,7 @@ class MultiValuedRelation(relation: RelationDescriptor<*>, status: Status, val o

val inverse = inverseRelation(element)
if ( inverse !== null)
inverse.addInverse(obj)
inverse.addedToInverse(obj)
else
TransactionState.current().addRedo(element.id, relation.inverseRelation!!.name, AddToRelation(this.obj))

Expand All @@ -138,7 +138,7 @@ class MultiValuedRelation(relation: RelationDescriptor<*>, status: Status, val o

val inverse = inverseRelation(element)
if ( inverse !== null)
inverse.removeInverse(element)
inverse.removedFromInverse(element)
else
TransactionState.current().addRedo(element.id, relation.inverseRelation!!.name, RemoveFromRelation(this.obj))

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/sirius/dorm/object/Relation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ abstract class Relation(val relation: RelationDescriptor<*>, val targetDescripto

// abstract

abstract fun addInverse(element: DataObject)
abstract fun removeInverse(element: DataObject)
abstract fun addedToInverse(element: DataObject)
abstract fun removedFromInverse(element: DataObject)
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val
if ( value !== this.target) {
var inverse = inverseRelation(this.target)
if ( inverse !== null)
inverse.removeInverse(this.obj)
inverse.removedFromInverse(this.obj)
else if ( this.target !== null)
TransactionState.current().addRedo(this.target!!.id, relation.inverseRelation!!.name, RemoveFromRelation(this.obj))

Expand All @@ -88,7 +88,7 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val
if ( this.target !== null) {
inverse = inverseRelation(this.target)
if ( inverse !== null)
inverse.addInverse(this.obj)
inverse.addedToInverse(this.obj)
else
TransactionState.current().addRedo(this.target!!.id, relation.inverseRelation!!.name, AddToRelation(this.obj))
}
Expand All @@ -100,10 +100,10 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val
else return false
}

override fun addInverse(element: DataObject) {
override fun addedToInverse(element: DataObject) {
this.target = element
}
override fun removeInverse(element: DataObject) {
override fun removedFromInverse(element: DataObject) {
this.target = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ObjectReader(descriptor: ObjectDescriptor, objectManager: ObjectManager) {
// companion

companion object {
fun valueReader(clazz: Class<Any>) : (attribute: PropertyEntity) -> Any {
fun valueReader(clazz: Class<out Any>) : (attribute: PropertyEntity) -> Any {
return when (clazz) {
Boolean::class.javaObjectType -> { attribute: PropertyEntity -> attribute.intValue == 1 }

Expand Down
10 changes: 4 additions & 6 deletions src/main/kotlin/org/sirius/dorm/persistence/ObjectWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ class ObjectWriter(private val descriptor: ObjectDescriptor) {
var i = 1
for ( writer in writer) {
val propertyDescriptor = descriptor.properties[i]
val attribute = PropertyEntity(obj.entity!!, propertyDescriptor.name, descriptor.name, "", 0, 0.0)
val property = PropertyEntity(obj.entity!!, propertyDescriptor.name, descriptor.name, "", 0, 0.0)

// set entity, we may need it for flushing relations
obj.values[i].property = property

obj.values[i].property = attribute
writer(state, obj, i++, property)

writer(state, obj, i++, attribute)

entityManager.persist(attribute)
entityManager.persist(property)

//obj.entity!!.properties.add(attribute)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/sirius/dorm/query/ObjectPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class ObjectPath(val parent : ObjectPath? = null) {

abstract fun <T>expression(root: Root<PropertyEntity>): Path<T>

abstract fun type() : Class<Any>
abstract fun type() : Class<out Any>

open fun attributeName() : String {
throw Error("abstract")
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/sirius/dorm/query/PropertyPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PropertyPath(parent: ObjectPath, val property: PropertyDescriptor<Any>) :
return property.name
}

override fun type() : Class<Any> {
override fun type() : Class<out Any> {
return property.asAttribute().type.baseType
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ abstract class UpdateRelation {

class AddToRelation(val obj: DataObject) : UpdateRelation() {
override fun execute(relation: Relation) {
relation.addInverse(obj)
relation.addedToInverse(obj)
}
}

class RemoveFromRelation(val obj: DataObject) : UpdateRelation() {
override fun execute(relation: Relation) {
relation.removeInverse(obj)
relation.removedFromInverse(obj)
}
}
class Redo() {
Expand Down
Loading

0 comments on commit a0b3564

Please sign in to comment.