Skip to content

Commit 9f1397f

Browse files
author
Philip Z
committed
feat: attach relations with invoking context metadata
1 parent b2397d0 commit 9f1397f

3 files changed

Lines changed: 103 additions & 2 deletions

File tree

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,28 @@ 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+
addRelation(descriptor, relationName, value);
249+
}
250+
251+
private void addRelation(
252+
io.teaql.core.meta.EntityDescriptor descriptor, String relationName, Entity value) {
234253
if (descriptor == null) return;
235254
io.teaql.core.meta.PropertyDescriptor pd = descriptor.findProperty(relationName);
236255
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: 68 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,73 @@ 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+
single.setProperties(java.util.List.of(singleProperty));
422+
io.teaql.core.meta.SimpleEntityMetaFactory singleMetadata =
423+
new io.teaql.core.meta.SimpleEntityMetaFactory();
424+
singleMetadata.register(single);
425+
426+
io.teaql.core.meta.EntityDescriptor many = new io.teaql.core.meta.EntityDescriptor();
427+
many.setType("TestEntity");
428+
io.teaql.core.meta.PropertyDescriptor manyProperty =
429+
new io.teaql.core.meta.PropertyDescriptor();
430+
manyProperty.setName("listRel");
431+
manyProperty.setType(new io.teaql.core.meta.SimplePropertyType(SmartList.class));
432+
many.setProperties(java.util.List.of(manyProperty));
433+
io.teaql.core.meta.SimpleEntityMetaFactory manyMetadata =
434+
new io.teaql.core.meta.SimpleEntityMetaFactory();
435+
manyMetadata.register(many);
436+
437+
UserContext singleContext = contextWithMetadata(singleMetadata);
438+
UserContext manyContext = contextWithMetadata(manyMetadata);
439+
io.teaql.core.meta.EntityMetaFactory previous =
440+
io.teaql.core.meta.EntityMetaFactory.get();
441+
try {
442+
io.teaql.core.meta.EntityMetaFactory.registerGlobal(singleMetadata);
443+
TestEntity manyEntity = new TestEntity();
444+
TestEntity manyChild = new TestEntity();
445+
((Entity) manyEntity).addRelation(manyContext, "listRel", manyChild);
446+
assertEquals(1, manyEntity.<SmartList<?>>getProperty("listRel").size());
447+
assertSame(manyChild, manyEntity.<SmartList<?>>getProperty("listRel").get(0));
448+
449+
io.teaql.core.meta.EntityMetaFactory.registerGlobal(null);
450+
TestEntity singleEntity = new TestEntity();
451+
TestEntity singleChild = new TestEntity();
452+
((Entity) singleEntity).addRelation(singleContext, "entityRel", singleChild);
453+
assertSame(singleChild, singleEntity.getProperty("entityRel"));
454+
455+
try {
456+
singleEntity.addRelation("entityRel", singleChild);
457+
fail("Legacy no-context attachment must not silently pick another model");
458+
} catch (IllegalStateException expected) {
459+
assertTrue(expected.getMessage().contains("addRelation(context"));
460+
}
461+
} finally {
462+
io.teaql.core.meta.EntityMetaFactory.registerGlobal(previous);
463+
}
464+
}
465+
466+
private static UserContext contextWithMetadata(
467+
io.teaql.core.meta.EntityMetaFactory metadata) {
468+
return (UserContext) Proxy.newProxyInstance(
469+
UserContext.class.getClassLoader(),
470+
new Class<?>[] {UserContext.class},
471+
(proxy, method, args) -> {
472+
if ("capability".equals(method.getName())
473+
&& args[0] == io.teaql.core.meta.EntityMetaFactory.class) {
474+
return metadata;
475+
}
476+
throw new UnsupportedOperationException(method.getName());
477+
});
478+
}
479+
412480
@Test
413481
public void testAddRelationAndOthers() {
414482
io.teaql.core.meta.EntityDescriptor desc = new io.teaql.core.meta.EntityDescriptor();

0 commit comments

Comments
 (0)