Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions teaql-core/src/main/java/io/teaql/core/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,43 @@ public List<String> 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;
Expand Down
14 changes: 14 additions & 0 deletions teaql-core/src/main/java/io/teaql/core/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,22 @@ default Entity updateProperty(String propertyName, Object value) {

List<String> 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);
Expand Down
84 changes: 84 additions & 0 deletions teaql-core/src/test/java/io/teaql/core/BaseEntityTest.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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.<SmartList<?>>getProperty("listRel").size());
assertSame(manyChild, manyEntity.<SmartList<?>>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();
Expand Down
Loading