Skip to content

Commit

Permalink
started relation validation
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Sep 24, 2024
1 parent 01334ee commit b1aec21
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 34 deletions.
23 changes: 6 additions & 17 deletions src/main/kotlin/org/sirius/dorm/ObjectManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,8 @@ class ObjectDescriptorBuilder(val manager: ObjectManager, val name: String) {

// public

fun register() : ObjectDescriptor {
val descriptor = ObjectDescriptor(name, properties.toTypedArray(), manager)

manager.register(descriptor)

descriptor.resolve(manager)

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

Expand Down Expand Up @@ -108,13 +102,12 @@ class ObjectManager() {

if ( descriptor == null) {
descriptor = objectDescriptorStorage.findByName(name)
if ( descriptor != null) {
descriptor.resolve(this)

if ( descriptor != null)
descriptors[name] = descriptor
}
}

descriptor?.resolve(this)

return descriptor
}

Expand All @@ -128,11 +121,7 @@ class ObjectManager() {
}

fun create(objectDescriptor: ObjectDescriptor) : DataObject {
val obj = objectDescriptor.create()

TransactionState.current().create(obj)

return obj
return TransactionState.current().create(objectDescriptor.create())
}

fun delete(obj: DataObject) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/kotlin/org/sirius/dorm/model/ObjectDescriptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class ObjectDescriptor(val name: String, val properties: Array<PropertyDescripto
}

fun resolve(objectManager: ObjectManager) {
this.objectManager = objectManager
for ( property in properties)
property.resolve(objectManager, this)
if ( this.objectManager == null) {
this.objectManager = objectManager
for (property in properties)
property.resolve(objectManager, this)
}
}

fun create() : DataObject {
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/org/sirius/dorm/model/PropertyDescriptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ open class RelationDescriptor<T:Any>(name: String, val target: String, val multi

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

override fun asRelation() :RelationDescriptor<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, val obj: DataObject,
if ( target !== null) {
relations().add(target!!.values[relation.inverseRelation!!.index].property!!)
}
else {
if ( !relation.multiplicity.optional)
throw Error("missing ...") // TODO
}
}
}
override fun get(objectManager: ObjectManager) : Any? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class DataObjectMapper() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ class TransactionState(val objectManager: ObjectManager, val transactionManager:
states.put(state.obj.id, state)
}

fun create(obj: DataObject) {
fun create(obj: DataObject) : DataObject{
val state = ObjectState(obj, Status.MANAGED) // as soon as it is flushed it will be managed TODO ID

// force flush

objectManager.mapper.create(this, obj)

states.put(obj.id, state)

return obj
}

fun flush(objectDescriptor: ObjectDescriptor) {
Expand Down
5 changes: 3 additions & 2 deletions src/test/kotlin/org/sirius/dorm/AbstractTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AbstractTest {
val stringType = string().length(100)
val intType = int()

personDescriptor = objectManager.type("person")
objectManager.type("person")
.attribute("name", stringType)
.attribute("age", intType)

Expand All @@ -85,7 +85,8 @@ class AbstractTest {
.attribute("double", double())
.register()
}
else personDescriptor = objectManager.getDescriptor("person")

personDescriptor = objectManager.getDescriptor("person")
}
}

Expand Down
61 changes: 52 additions & 9 deletions src/test/kotlin/org/sirius/dorm/RelationTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,23 @@ class RelationTests: AbstractTest() {
fun testOneToMany() {
// create schema

var descriptor : ObjectDescriptor?= null

withTransaction {
descriptor = objectManager.type("p")
objectManager.type("p")
.attribute("name", string())
.relation("children", "p", Multiplicity.ZERO_OR_MANY, "father")
.relation("father", "p", Multiplicity.ZERO_OR_ONE, "children")
.register()
}

val descriptor = objectManager.getDescriptor("p")

// test

var id = 0

withTransaction {
val person = objectManager.create(descriptor!!)
val person = objectManager.create(descriptor)

person["name"] = "Andi"

Expand All @@ -82,7 +84,7 @@ class RelationTests: AbstractTest() {

// child 1

val child1 = objectManager.create(descriptor!!)
val child1 = objectManager.create(descriptor)

child1["name"] = "Nika"
child1["father"] = person
Expand All @@ -91,7 +93,7 @@ class RelationTests: AbstractTest() {

// child 2

val child2 = objectManager.create(descriptor!!)
val child2 = objectManager.create(descriptor)

child2["name"] = "Pupsi"
child2["father"] = person
Expand All @@ -115,7 +117,7 @@ class RelationTests: AbstractTest() {
// reread

withTransaction {
val person = objectManager.findById(descriptor!!, id)
val person = objectManager.findById(descriptor, id)

assert(person !== null)

Expand All @@ -135,19 +137,60 @@ class RelationTests: AbstractTest() {
}
}

@Test
fun testValidateRelation() {
// create schema


withTransaction {
objectManager.type("product")
.attribute("name", string())
.relation("part", "part", Multiplicity.ZERO_OR_MANY, "product")
.register()

objectManager.type("part")
.attribute("name", string())
.relation("product", "product", Multiplicity.ONE)
.register()
}

val productDescriptor = objectManager.getDescriptor("product")
val partDescriptor = objectManager.getDescriptor("part")

// test

try {
withTransaction {
val product = objectManager.create(productDescriptor)

product["name"] = "Car"

val part = objectManager.create(partDescriptor)

part["name"] = "Motor"

//part["product"] = product
}
}
catch(exception: Throwable) {
println() // T
}
}

@Test
fun testOneToManyIncludingInverse() {
// create schema

var descriptor : ObjectDescriptor?= null
withTransaction {
descriptor = objectManager.type("p1")
objectManager.type("p1")
.attribute("name", string())
.relation("parent", "p1", Multiplicity.ZERO_OR_ONE, "children")
.relation("children", "p1", Multiplicity.ZERO_OR_MANY, "parent")
.register()
}

val descriptor = objectManager.findDescriptor("p1")

// test

var id = 0
Expand All @@ -159,7 +202,7 @@ class RelationTests: AbstractTest() {

id = person.id

val child = objectManager.create(descriptor!!)
val child = objectManager.create(descriptor)

child["name"] = "Nika"

Expand Down

0 comments on commit b1aec21

Please sign in to comment.