Skip to content

Commit 1ce9fff

Browse files
authored
Merge pull request #149 from teaql/fix/context-owned-add-relation-134
feat: attach relations with invoking context metadata
2 parents e2109fd + 7c602cc commit 1ce9fff

3 files changed

Lines changed: 134 additions & 2 deletions

File tree

‎teaql-core/src/main/java/io/teaql/core/BaseEntity.java‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,43 @@ public List<String> getUpdatedProperties() {
228228
}
229229

230230
@Override
231+
@Deprecated
231232
public void addRelation(String relationName, Entity value) {
232-
io.teaql.core.meta.EntityDescriptor descriptor = io.teaql.core.meta.EntityMetaFactory.get()
233-
.resolveEntityDescriptor(this.typeName());
233+
io.teaql.core.meta.EntityMetaFactory metadata =
234+
io.teaql.core.meta.EntityMetaFactory.get();
235+
if (metadata == null) {
236+
throw new IllegalStateException("Global EntityMetaFactory is not initialized; "
237+
+ "use addRelation(context, relationName, value)");
238+
}
239+
addRelation(metadata.resolveEntityDescriptor(this.typeName()), relationName, value);
240+
}
241+
242+
/** Attach a relation using metadata installed in the invoking context. */
243+
@Override
244+
public void addRelation(UserContext context, String relationName, Entity value) {
245+
io.teaql.core.meta.EntityDescriptor descriptor =
246+
io.teaql.core.meta.EntityMetaFactory.requireFrom(context)
247+
.resolveEntityDescriptor(this.typeName());
248+
if (descriptor == null) {
249+
throw new IllegalStateException("No entity descriptor registered in the invoking context for "
250+
+ this.typeName());
251+
}
252+
io.teaql.core.meta.PropertyDescriptor property = descriptor.findProperty(relationName);
253+
if (property == null || property.getType() == null) {
254+
throw new IllegalArgumentException("No relation " + this.typeName() + "." + relationName
255+
+ " registered in the invoking context");
256+
}
257+
Class<?> relationType = property.getType().javaType();
258+
if (!SmartList.class.isAssignableFrom(relationType)
259+
&& !Entity.class.isAssignableFrom(relationType)) {
260+
throw new IllegalArgumentException("Property " + this.typeName() + "." + relationName
261+
+ " is not a relation in the invoking context");
262+
}
263+
addRelation(descriptor, relationName, value);
264+
}
265+
266+
private void addRelation(
267+
io.teaql.core.meta.EntityDescriptor descriptor, String relationName, Entity value) {
234268
if (descriptor == null) return;
235269
io.teaql.core.meta.PropertyDescriptor pd = descriptor.findProperty(relationName);
236270
if (pd == null || pd.getType() == null) return;

‎teaql-core/src/main/java/io/teaql/core/Entity.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,22 @@ default Entity updateProperty(String propertyName, Object value) {
5858

5959
List<String> getUpdatedProperties();
6060

61+
/** Legacy process-global metadata path. Prefer the context-bearing overload. */
62+
@Deprecated
6163
void addRelation(String relationName, Entity value);
6264

65+
/**
66+
* Attach a relation using the invoking runtime's metadata instead of the
67+
* legacy process-global metadata registry.
68+
*/
69+
default void addRelation(UserContext context, String relationName, Entity value) {
70+
if (!(this instanceof BaseEntity baseEntity)) {
71+
throw new UnsupportedOperationException(
72+
"Context-owned relation attachment requires a BaseEntity implementation");
73+
}
74+
baseEntity.addRelation(context, relationName, value);
75+
}
76+
6377
void addDynamicProperty(String propertyName, Object value);
6478

6579
void appendDynamicProperty(String propertyName, Object value);

‎teaql-core/src/test/java/io/teaql/core/BaseEntityTest.java‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.teaql.core;
22

33
import org.junit.Test;
4+
import java.lang.reflect.Proxy;
45
import java.util.Set;
56

67
import static org.junit.Assert.*;
@@ -409,6 +410,89 @@ public void testEntityMutationLedgerIntegrationExtras() {
409410
assertEquals("test_comment", e.getComment());
410411
}
411412

413+
@Test
414+
public void testAddRelationUsesInvokingContextMetadata() {
415+
io.teaql.core.meta.EntityDescriptor single = new io.teaql.core.meta.EntityDescriptor();
416+
single.setType("TestEntity");
417+
io.teaql.core.meta.PropertyDescriptor singleProperty =
418+
new io.teaql.core.meta.PropertyDescriptor();
419+
singleProperty.setName("entityRel");
420+
singleProperty.setType(new io.teaql.core.meta.SimplePropertyType(Entity.class));
421+
io.teaql.core.meta.PropertyDescriptor scalarProperty =
422+
new io.teaql.core.meta.PropertyDescriptor();
423+
scalarProperty.setName("label");
424+
scalarProperty.setType(new io.teaql.core.meta.SimplePropertyType(String.class));
425+
single.setProperties(java.util.List.of(singleProperty, scalarProperty));
426+
io.teaql.core.meta.SimpleEntityMetaFactory singleMetadata =
427+
new io.teaql.core.meta.SimpleEntityMetaFactory();
428+
singleMetadata.register(single);
429+
430+
io.teaql.core.meta.EntityDescriptor many = new io.teaql.core.meta.EntityDescriptor();
431+
many.setType("TestEntity");
432+
io.teaql.core.meta.PropertyDescriptor manyProperty =
433+
new io.teaql.core.meta.PropertyDescriptor();
434+
manyProperty.setName("listRel");
435+
manyProperty.setType(new io.teaql.core.meta.SimplePropertyType(SmartList.class));
436+
many.setProperties(java.util.List.of(manyProperty));
437+
io.teaql.core.meta.SimpleEntityMetaFactory manyMetadata =
438+
new io.teaql.core.meta.SimpleEntityMetaFactory();
439+
manyMetadata.register(many);
440+
441+
UserContext singleContext = contextWithMetadata(singleMetadata);
442+
UserContext manyContext = contextWithMetadata(manyMetadata);
443+
io.teaql.core.meta.EntityMetaFactory previous =
444+
io.teaql.core.meta.EntityMetaFactory.get();
445+
try {
446+
io.teaql.core.meta.EntityMetaFactory.registerGlobal(singleMetadata);
447+
TestEntity manyEntity = new TestEntity();
448+
TestEntity manyChild = new TestEntity();
449+
((Entity) manyEntity).addRelation(manyContext, "listRel", manyChild);
450+
assertEquals(1, manyEntity.<SmartList<?>>getProperty("listRel").size());
451+
assertSame(manyChild, manyEntity.<SmartList<?>>getProperty("listRel").get(0));
452+
453+
io.teaql.core.meta.EntityMetaFactory.registerGlobal(null);
454+
TestEntity singleEntity = new TestEntity();
455+
TestEntity singleChild = new TestEntity();
456+
((Entity) singleEntity).addRelation(singleContext, "entityRel", singleChild);
457+
assertSame(singleChild, singleEntity.getProperty("entityRel"));
458+
459+
TeaQLRuntimeException missingEntity = assertThrows(TeaQLRuntimeException.class,
460+
() -> new TestEntity().addRelation(
461+
contextWithMetadata(new io.teaql.core.meta.SimpleEntityMetaFactory()),
462+
"entityRel", singleChild));
463+
assertTrue(missingEntity.getMessage().contains("TestEntity"));
464+
IllegalArgumentException missingRelation = assertThrows(IllegalArgumentException.class,
465+
() -> singleEntity.addRelation(singleContext, "missingRel", singleChild));
466+
assertTrue(missingRelation.getMessage().contains("TestEntity.missingRel"));
467+
IllegalArgumentException scalarRelation = assertThrows(IllegalArgumentException.class,
468+
() -> singleEntity.addRelation(singleContext, "label", singleChild));
469+
assertTrue(scalarRelation.getMessage().contains("not a relation"));
470+
471+
try {
472+
singleEntity.addRelation("entityRel", singleChild);
473+
fail("Legacy no-context attachment must not silently pick another model");
474+
} catch (IllegalStateException expected) {
475+
assertTrue(expected.getMessage().contains("addRelation(context"));
476+
}
477+
} finally {
478+
io.teaql.core.meta.EntityMetaFactory.registerGlobal(previous);
479+
}
480+
}
481+
482+
private static UserContext contextWithMetadata(
483+
io.teaql.core.meta.EntityMetaFactory metadata) {
484+
return (UserContext) Proxy.newProxyInstance(
485+
UserContext.class.getClassLoader(),
486+
new Class<?>[] {UserContext.class},
487+
(proxy, method, args) -> {
488+
if ("capability".equals(method.getName())
489+
&& args[0] == io.teaql.core.meta.EntityMetaFactory.class) {
490+
return metadata;
491+
}
492+
throw new UnsupportedOperationException(method.getName());
493+
});
494+
}
495+
412496
@Test
413497
public void testAddRelationAndOthers() {
414498
io.teaql.core.meta.EntityDescriptor desc = new io.teaql.core.meta.EntityDescriptor();

0 commit comments

Comments
 (0)