Skip to content

Commit

Permalink
delete will cascade in memory...
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Sep 24, 2024
1 parent f3a3568 commit 3e56fa7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/org/sirius/dorm/ObjectManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class ObjectManager() {
}

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

// tx
Expand Down
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 @@ -47,11 +47,11 @@ open class RelationDescriptor<T:Any>(name: String, val target: String, val multi
}

override fun defaultValue(): Any? {
TODO("Not yet implemented")
return null // actually not called at all
}

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

fun isOwner() : Boolean {
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/org/sirius/dorm/object/Attribute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class Attribute(property: PropertyEntity?, var value: Any) : Property(property)
override fun set(propertyDescriptor: PropertyDescriptor<Any>, value: Any?) : Boolean {
propertyDescriptor.validate(value)

if ( value != this.value ) {
return if ( value != this.value ) {
this.value = value!!

return true
true
}
else return false
else false
}

override fun save(): Any {
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/org/sirius/dorm/object/DataObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package org.sirius.dorm.`object`
* All rights reserved
*/
import org.sirius.dorm.ObjectManager
import org.sirius.dorm.model.Cascade
import org.sirius.dorm.model.ObjectDescriptor
import org.sirius.dorm.model.PropertyDescriptor
import org.sirius.dorm.persistence.entity.EntityEntity
Expand All @@ -20,6 +21,22 @@ class DataObject(val type: ObjectDescriptor, status: Status, var state : ObjectS

// public

fun delete() {
state?.status = Status.DELETED

var i = 0
for (property in type.properties) {
if (!property.isAttribute()) {
if (property.asRelation().cascade == Cascade.DELETE) {
if ((values[i] as Relation).isLoaded())
(values[i] as Relation).deleted()
}
}

i++
}
}

var objectManager: ObjectManager
get() = type.objectManager!!
set(_) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ class MultiValuedRelation(relation: RelationDescriptor<*>, status: Status, val o

// implement Relation

override fun deleted() {
for ( obj in this.objects!!)
obj.delete()
}

override fun isLoaded() : Boolean {
return objects !== null
}

override fun addInverse(element: DataObject) {
this.objects!!.add(element)
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/org/sirius/dorm/object/Relation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import org.sirius.dorm.persistence.entity.PropertyEntity
abstract class Relation(val relation: RelationDescriptor<*>, val targetDescriptor: ObjectDescriptor, property: PropertyEntity?) : Property(property) {
abstract fun isLoaded(): Boolean

abstract fun deleted();

abstract fun load(objectManager: ObjectManager)

override fun save(): Any {
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val

var target: DataObject? = if ( status == Status.CREATED ) null else DataObject.NONE

// private
// implement Relation

override fun deleted() {
target?.delete()
}

override fun load(objectManager: ObjectManager) {
if ( property !== null) {
Expand All @@ -33,8 +37,6 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val
else target = null
}

// implement Relation

override fun isLoaded() : Boolean {
return target !== DataObject.NONE
}
Expand Down Expand Up @@ -81,8 +83,6 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val
else return false
}

// new

override fun addInverse(element: DataObject) {
this.target = element
}
Expand Down

0 comments on commit 3e56fa7

Please sign in to comment.