diff --git a/src/main/kotlin/org/sirius/dorm/ObjectManager.kt b/src/main/kotlin/org/sirius/dorm/ObjectManager.kt index 984f39a..8d69908 100644 --- a/src/main/kotlin/org/sirius/dorm/ObjectManager.kt +++ b/src/main/kotlin/org/sirius/dorm/ObjectManager.kt @@ -126,7 +126,7 @@ class ObjectManager() { } fun delete(obj: DataObject) { - obj.state?.status = Status.DELETED + obj.delete() } // tx diff --git a/src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt b/src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt index 6cb4d26..16a26d5 100644 --- a/src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt +++ b/src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt @@ -47,11 +47,11 @@ open class RelationDescriptor(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 { diff --git a/src/main/kotlin/org/sirius/dorm/object/Attribute.kt b/src/main/kotlin/org/sirius/dorm/object/Attribute.kt index 80ea896..38d58ba 100644 --- a/src/main/kotlin/org/sirius/dorm/object/Attribute.kt +++ b/src/main/kotlin/org/sirius/dorm/object/Attribute.kt @@ -16,12 +16,12 @@ class Attribute(property: PropertyEntity?, var value: Any) : Property(property) override fun set(propertyDescriptor: PropertyDescriptor, 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 { diff --git a/src/main/kotlin/org/sirius/dorm/object/DataObject.kt b/src/main/kotlin/org/sirius/dorm/object/DataObject.kt index 7a175c2..af368f8 100644 --- a/src/main/kotlin/org/sirius/dorm/object/DataObject.kt +++ b/src/main/kotlin/org/sirius/dorm/object/DataObject.kt @@ -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 @@ -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(_) { } diff --git a/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt b/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt index 671af53..f8c7473 100644 --- a/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt +++ b/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt @@ -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) } diff --git a/src/main/kotlin/org/sirius/dorm/object/Relation.kt b/src/main/kotlin/org/sirius/dorm/object/Relation.kt index 1c699c0..47e57e2 100644 --- a/src/main/kotlin/org/sirius/dorm/object/Relation.kt +++ b/src/main/kotlin/org/sirius/dorm/object/Relation.kt @@ -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 { diff --git a/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt b/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt index 5e88f97..6f870f4 100644 --- a/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt +++ b/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt @@ -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) { @@ -33,8 +37,6 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val else target = null } - // implement Relation - override fun isLoaded() : Boolean { return target !== DataObject.NONE } @@ -81,8 +83,6 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val else return false } - // new - override fun addInverse(element: DataObject) { this.target = element }