Skip to content

Commit

Permalink
added cascading attribute ( delete missing )
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Sep 24, 2024
1 parent 92787f2 commit a22596d
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 96 deletions.
14 changes: 7 additions & 7 deletions src/main/kotlin/org/sirius/dorm/ObjectManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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.dorm.query.parser.OQLParser
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.transaction.PlatformTransactionManager
Expand All @@ -41,14 +42,14 @@ class ObjectDescriptorBuilder(val manager: ObjectManager, val name: String) {
return this
}

fun relation(name: String, target: String, multiplicity: Multiplicity) : ObjectDescriptorBuilder {
properties.add(RelationDescriptor(name, target, multiplicity, null))
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) : ObjectDescriptorBuilder {
properties.add(RelationDescriptor(name, target, multiplicity, inverse))
fun relation(name: String, target: String, multiplicity: Multiplicity, inverse: String, cascade: Cascade? = null) : ObjectDescriptorBuilder {
properties.add(RelationDescriptor(name, target, multiplicity, cascade, inverse))

return this
}
Expand Down Expand Up @@ -125,8 +126,7 @@ class ObjectManager() {
}

fun delete(obj: DataObject) {
if ( obj.state != null)
obj.state!!.status = Status.DELETED
obj.state?.status = Status.DELETED
}

// tx
Expand Down Expand Up @@ -162,7 +162,7 @@ class ObjectManager() {
fun <T: Any> query(query: String, resultType: Class<T> = DataObject::class.java as Class<T>) : Query<T> {
val tokenStream = CommonTokenStream(org.sirius.dorm.query.parser.OQLLexer(CharStreams.fromString(query)))

val parser = org.sirius.dorm.query.parser.OQLParser(tokenStream)
val parser = OQLParser(tokenStream)

parser.setup(queryManager())
try {
Expand Down
43 changes: 43 additions & 0 deletions src/main/kotlin/org/sirius/dorm/model/AttributeDescriptor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.sirius.dorm.model
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
* All rights reserved
*/

import org.sirius.common.type.Type
import org.sirius.dorm.`object`.Attribute
import org.sirius.dorm.`object`.DataObject
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) {
// public

fun baseType() : Class<*> {
return type.baseType
}

// override

override fun createProperty(obj: DataObject, status: Status, entity: PropertyEntity?) : Property {
return Attribute(entity, defaultValue()!!)
}

override fun asAttribute() : AttributeDescriptor<T> {
return this
}

override fun defaultValue() : Any? {
return type.defaultValue()
}

override fun validate(value: Any?) {
type.validate(value!!)
}

override fun isAttribute() : Boolean {
return true
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/org/sirius/dorm/model/Cascade.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.sirius.dorm.model
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
* All rights reserved
*/

enum class Cascade {
DELETE
}
12 changes: 12 additions & 0 deletions src/main/kotlin/org/sirius/dorm/model/Multiplicity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.sirius.dorm.model
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
* All rights reserved
*/
enum class Multiplicity(val optional: Boolean, val mutliValued: Boolean) {
ZERO_OR_ONE(true, false),
ONE(false, false),
MANY(false, true),
ZERO_OR_MANY(true, true)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package org.sirius.dorm.model

/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
Expand Down
75 changes: 0 additions & 75 deletions src/main/kotlin/org/sirius/dorm/model/PropertyDescriptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package org.sirius.dorm.model

import org.sirius.dorm.*
import org.sirius.dorm.persistence.entity.PropertyEntity
import org.sirius.common.type.Type
import org.sirius.dorm.`object`.*
import org.sirius.dorm.transaction.Status

Expand Down Expand Up @@ -36,78 +35,4 @@ abstract class PropertyDescriptor<T:Any>(val name: String) {
}
}

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

fun baseType() : Class<*> {
return type.baseType
}

// override

override fun createProperty(obj: DataObject, status: Status, entity: PropertyEntity?) : Property {
return Attribute(entity, defaultValue()!!)
}

override fun asAttribute() :AttributeDescriptor<T> {
return this
}

override fun defaultValue() : Any? {
return type.defaultValue()
}

override fun validate(value: Any?) {
type.validate(value!!)
}

override fun isAttribute() : Boolean {
return true
}
}


enum class Multiplicity(val optional: Boolean, val mutliValued: Boolean) {
ZERO_OR_ONE(true, false),
ONE(false, false),
MANY(false, true),
ZERO_OR_MANY(true, true)
}
open class RelationDescriptor<T:Any>(name: String, val target: String, val multiplicity: Multiplicity, val inverse: String?) : PropertyDescriptor<T>(name) {
// instance data

var targetDescriptor: ObjectDescriptor? = null
var inverseRelation : RelationDescriptor<*>? = null

// override

override fun createProperty(obj: DataObject, status: Status, entity: PropertyEntity?) : Property {
val relation = if ( multiplicity.mutliValued ) MultiValuedRelation(this, status, obj, entity, targetDescriptor!!) else SingleValuedRelation(this, status, obj, entity, targetDescriptor!!)

return relation
}

override fun resolve(objectManager: ObjectManager, descriptor: ObjectDescriptor) {
targetDescriptor = objectManager.getDescriptor(target)
if ( inverse !== null) {
inverseRelation = targetDescriptor!!.property(inverse) as RelationDescriptor<*>
inverseRelation!!.inverseRelation = this // both directions
}
}

override fun asRelation() :RelationDescriptor<T> {
return this
}

override fun defaultValue(): Any? {
TODO("Not yet implemented")
}

override fun validate(value: Any?) {
TODO("Not yet implemented")
}

fun isOwner() : Boolean {
return inverse !== null
}
}
60 changes: 60 additions & 0 deletions src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.sirius.dorm.model
/*
* @COPYRIGHT (C) 2023 Andreas Ernst
*
* All rights reserved
*/

import org.sirius.dorm.ObjectManager
import org.sirius.dorm.`object`.DataObject
import org.sirius.dorm.`object`.MultiValuedRelation
import org.sirius.dorm.`object`.Property
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) {
// instance data

var targetDescriptor: ObjectDescriptor? = null
var inverseRelation : RelationDescriptor<*>? = null

// override

override fun createProperty(obj: DataObject, status: Status, entity: PropertyEntity?) : Property {
val relation = if ( multiplicity.mutliValued ) MultiValuedRelation(
this,
status,
obj,
entity,
targetDescriptor!!
)
else SingleValuedRelation(this, status, obj, entity, targetDescriptor!!)

return relation
}

override fun resolve(objectManager: ObjectManager, descriptor: ObjectDescriptor) {
targetDescriptor = objectManager.getDescriptor(target)
if ( inverse !== null) {
inverseRelation = targetDescriptor!!.property(inverse) as RelationDescriptor<*>
inverseRelation!!.inverseRelation = this // both directions
}
}

override fun asRelation() : RelationDescriptor<T> {
return this
}

override fun defaultValue(): Any? {
TODO("Not yet implemented")
}

override fun validate(value: Any?) {
TODO("Not yet implemented")
}

fun isOwner() : Boolean {
return inverse !== null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ open class ObjectDescriptorDeserializer() : StdDeserializer<ObjectDescriptor>(Ob
}
else {
val multiplicity = Multiplicity.valueOf( property["target"].asText())
properties.add(RelationDescriptor(propertyName, property["target"].asText(), multiplicity, property["inverse"].asText()))
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()))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ open class ObjectDescriptorSerializer : StdSerializer<ObjectDescriptor>(ObjectDe
else {
jsonGenerator.writeStringField("target", property.asRelation().target)
jsonGenerator.writeStringField("inverse", property.asRelation().inverse)
if (property.asRelation().cascade !== null)
jsonGenerator.writeStringField("cascade", property.asRelation().cascade!!.name)
jsonGenerator.writeObjectField("multiplicity", property.asRelation().multiplicity.name)

property.asRelation()
Expand Down
16 changes: 4 additions & 12 deletions src/main/kotlin/org/sirius/dorm/persistence/DataObjectMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,16 @@ class DataObjectMapper() {
if ( Tracer.ENABLED)
Tracer.trace("com.sirius.dorm", TraceLevel.HIGH, "delete %s[%d]", obj.type.name, obj.id)

entityManager.remove(obj.entity!!)
entityManager.remove(obj.entity!!) // will delete properties

// what about aggregated objects
}

fun create(state: TransactionState, obj: DataObject) {
val descriptor = obj.type

//obj.entity = EntityEntity(0, descriptor.name, mapper.writeValueAsString(obj))

//entityManager.persist(obj.entity) // we need the id...is that required, think of a lifecycle hook?

if ( Tracer.ENABLED)
Tracer.trace("com.sirius.dorm", TraceLevel.HIGH, "create %s[%d]", obj.type.name, obj.entity!!.id)

writer4(descriptor).write(state, obj, entityManager) // will create the attribute entities

// set as value as well

obj.id = obj.entity!!.id
writer4(obj.type).write(state, obj, entityManager) // will create the attribute entities
}


Expand Down

0 comments on commit a22596d

Please sign in to comment.