diff --git a/teaql-core/src/main/java/io/teaql/core/BaseEntity.java b/teaql-core/src/main/java/io/teaql/core/BaseEntity.java index 80463b2e8..b96e229eb 100644 --- a/teaql-core/src/main/java/io/teaql/core/BaseEntity.java +++ b/teaql-core/src/main/java/io/teaql/core/BaseEntity.java @@ -228,9 +228,43 @@ public List getUpdatedProperties() { } @Override + @Deprecated public void addRelation(String relationName, Entity value) { - io.teaql.core.meta.EntityDescriptor descriptor = io.teaql.core.meta.EntityMetaFactory.get() - .resolveEntityDescriptor(this.typeName()); + io.teaql.core.meta.EntityMetaFactory metadata = + io.teaql.core.meta.EntityMetaFactory.get(); + if (metadata == null) { + throw new IllegalStateException("Global EntityMetaFactory is not initialized; " + + "use addRelation(context, relationName, value)"); + } + addRelation(metadata.resolveEntityDescriptor(this.typeName()), relationName, value); + } + + /** Attach a relation using metadata installed in the invoking context. */ + @Override + public void addRelation(UserContext context, String relationName, Entity value) { + io.teaql.core.meta.EntityDescriptor descriptor = + io.teaql.core.meta.EntityMetaFactory.requireFrom(context) + .resolveEntityDescriptor(this.typeName()); + if (descriptor == null) { + throw new IllegalStateException("No entity descriptor registered in the invoking context for " + + this.typeName()); + } + io.teaql.core.meta.PropertyDescriptor property = descriptor.findProperty(relationName); + if (property == null || property.getType() == null) { + throw new IllegalArgumentException("No relation " + this.typeName() + "." + relationName + + " registered in the invoking context"); + } + Class relationType = property.getType().javaType(); + if (!SmartList.class.isAssignableFrom(relationType) + && !Entity.class.isAssignableFrom(relationType)) { + throw new IllegalArgumentException("Property " + this.typeName() + "." + relationName + + " is not a relation in the invoking context"); + } + addRelation(descriptor, relationName, value); + } + + private void addRelation( + io.teaql.core.meta.EntityDescriptor descriptor, String relationName, Entity value) { if (descriptor == null) return; io.teaql.core.meta.PropertyDescriptor pd = descriptor.findProperty(relationName); if (pd == null || pd.getType() == null) return; diff --git a/teaql-core/src/main/java/io/teaql/core/Entity.java b/teaql-core/src/main/java/io/teaql/core/Entity.java index 66ea0dece..8976b3ec3 100644 --- a/teaql-core/src/main/java/io/teaql/core/Entity.java +++ b/teaql-core/src/main/java/io/teaql/core/Entity.java @@ -58,8 +58,22 @@ default Entity updateProperty(String propertyName, Object value) { List getUpdatedProperties(); + /** Legacy process-global metadata path. Prefer the context-bearing overload. */ + @Deprecated void addRelation(String relationName, Entity value); + /** + * Attach a relation using the invoking runtime's metadata instead of the + * legacy process-global metadata registry. + */ + default void addRelation(UserContext context, String relationName, Entity value) { + if (!(this instanceof BaseEntity baseEntity)) { + throw new UnsupportedOperationException( + "Context-owned relation attachment requires a BaseEntity implementation"); + } + baseEntity.addRelation(context, relationName, value); + } + void addDynamicProperty(String propertyName, Object value); void appendDynamicProperty(String propertyName, Object value); diff --git a/teaql-core/src/test/java/io/teaql/core/BaseEntityTest.java b/teaql-core/src/test/java/io/teaql/core/BaseEntityTest.java index 931607441..591acacc9 100644 --- a/teaql-core/src/test/java/io/teaql/core/BaseEntityTest.java +++ b/teaql-core/src/test/java/io/teaql/core/BaseEntityTest.java @@ -1,6 +1,7 @@ package io.teaql.core; import org.junit.Test; +import java.lang.reflect.Proxy; import java.util.Set; import static org.junit.Assert.*; @@ -409,6 +410,89 @@ public void testEntityMutationLedgerIntegrationExtras() { assertEquals("test_comment", e.getComment()); } + @Test + public void testAddRelationUsesInvokingContextMetadata() { + io.teaql.core.meta.EntityDescriptor single = new io.teaql.core.meta.EntityDescriptor(); + single.setType("TestEntity"); + io.teaql.core.meta.PropertyDescriptor singleProperty = + new io.teaql.core.meta.PropertyDescriptor(); + singleProperty.setName("entityRel"); + singleProperty.setType(new io.teaql.core.meta.SimplePropertyType(Entity.class)); + io.teaql.core.meta.PropertyDescriptor scalarProperty = + new io.teaql.core.meta.PropertyDescriptor(); + scalarProperty.setName("label"); + scalarProperty.setType(new io.teaql.core.meta.SimplePropertyType(String.class)); + single.setProperties(java.util.List.of(singleProperty, scalarProperty)); + io.teaql.core.meta.SimpleEntityMetaFactory singleMetadata = + new io.teaql.core.meta.SimpleEntityMetaFactory(); + singleMetadata.register(single); + + io.teaql.core.meta.EntityDescriptor many = new io.teaql.core.meta.EntityDescriptor(); + many.setType("TestEntity"); + io.teaql.core.meta.PropertyDescriptor manyProperty = + new io.teaql.core.meta.PropertyDescriptor(); + manyProperty.setName("listRel"); + manyProperty.setType(new io.teaql.core.meta.SimplePropertyType(SmartList.class)); + many.setProperties(java.util.List.of(manyProperty)); + io.teaql.core.meta.SimpleEntityMetaFactory manyMetadata = + new io.teaql.core.meta.SimpleEntityMetaFactory(); + manyMetadata.register(many); + + UserContext singleContext = contextWithMetadata(singleMetadata); + UserContext manyContext = contextWithMetadata(manyMetadata); + io.teaql.core.meta.EntityMetaFactory previous = + io.teaql.core.meta.EntityMetaFactory.get(); + try { + io.teaql.core.meta.EntityMetaFactory.registerGlobal(singleMetadata); + TestEntity manyEntity = new TestEntity(); + TestEntity manyChild = new TestEntity(); + ((Entity) manyEntity).addRelation(manyContext, "listRel", manyChild); + assertEquals(1, manyEntity.>getProperty("listRel").size()); + assertSame(manyChild, manyEntity.>getProperty("listRel").get(0)); + + io.teaql.core.meta.EntityMetaFactory.registerGlobal(null); + TestEntity singleEntity = new TestEntity(); + TestEntity singleChild = new TestEntity(); + ((Entity) singleEntity).addRelation(singleContext, "entityRel", singleChild); + assertSame(singleChild, singleEntity.getProperty("entityRel")); + + TeaQLRuntimeException missingEntity = assertThrows(TeaQLRuntimeException.class, + () -> new TestEntity().addRelation( + contextWithMetadata(new io.teaql.core.meta.SimpleEntityMetaFactory()), + "entityRel", singleChild)); + assertTrue(missingEntity.getMessage().contains("TestEntity")); + IllegalArgumentException missingRelation = assertThrows(IllegalArgumentException.class, + () -> singleEntity.addRelation(singleContext, "missingRel", singleChild)); + assertTrue(missingRelation.getMessage().contains("TestEntity.missingRel")); + IllegalArgumentException scalarRelation = assertThrows(IllegalArgumentException.class, + () -> singleEntity.addRelation(singleContext, "label", singleChild)); + assertTrue(scalarRelation.getMessage().contains("not a relation")); + + try { + singleEntity.addRelation("entityRel", singleChild); + fail("Legacy no-context attachment must not silently pick another model"); + } catch (IllegalStateException expected) { + assertTrue(expected.getMessage().contains("addRelation(context")); + } + } finally { + io.teaql.core.meta.EntityMetaFactory.registerGlobal(previous); + } + } + + private static UserContext contextWithMetadata( + io.teaql.core.meta.EntityMetaFactory metadata) { + return (UserContext) Proxy.newProxyInstance( + UserContext.class.getClassLoader(), + new Class[] {UserContext.class}, + (proxy, method, args) -> { + if ("capability".equals(method.getName()) + && args[0] == io.teaql.core.meta.EntityMetaFactory.class) { + return metadata; + } + throw new UnsupportedOperationException(method.getName()); + }); + } + @Test public void testAddRelationAndOthers() { io.teaql.core.meta.EntityDescriptor desc = new io.teaql.core.meta.EntityDescriptor();