Search in sources :

Example 1 with ForwardingRelationshipRepository

use of io.crnk.core.repository.foward.ForwardingRelationshipRepository in project crnk-framework by crnk-project.

the class DefaultRegistryEntryBuilder method setupImplicitRelationshipRepository.

private ResponseRelationshipEntry setupImplicitRelationshipRepository(ResourceField relationshipField) {
    RelationshipRepositoryBehavior behavior = relationshipField.getRelationshipRepositoryBehavior();
    if (behavior == RelationshipRepositoryBehavior.DEFAULT) {
        if (relationshipField.hasIdField() || relationshipField.getLookupIncludeAutomatically() == LookupIncludeBehavior.NONE) {
            behavior = RelationshipRepositoryBehavior.FORWARD_OWNER;
        } else {
            behavior = RelationshipRepositoryBehavior.CUSTOM;
        }
    }
    if (behavior == RelationshipRepositoryBehavior.IMPLICIT_FROM_OWNER) {
        behavior = RelationshipRepositoryBehavior.FORWARD_OWNER;
    }
    if (behavior == RelationshipRepositoryBehavior.IMPLICIT_GET_OPPOSITE_MODIFY_OWNER) {
        behavior = RelationshipRepositoryBehavior.FORWARD_GET_OPPOSITE_SET_OWNER;
    }
    if (behavior != RelationshipRepositoryBehavior.CUSTOM) {
        ResourceInformation sourceInformation = relationshipField.getParentResourceInformation();
        ResourceFieldAccess fieldAccess = relationshipField.getAccess();
        RepositoryMethodAccess access = new RepositoryMethodAccess(fieldAccess.isPostable(), fieldAccess.isPatchable(), fieldAccess.isReadable(), fieldAccess.isPatchable());
        RelationshipMatcher matcher = new RelationshipMatcher().rule().field(relationshipField).add();
        RelationshipRepositoryInformationImpl implicitRepoInformation = new RelationshipRepositoryInformationImpl(matcher, access);
        ForwardingRelationshipRepository repository;
        if (behavior == RelationshipRepositoryBehavior.FORWARD_OWNER) {
            repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OWNER, ForwardingDirection.OWNER);
        } else if (behavior == RelationshipRepositoryBehavior.FORWARD_GET_OPPOSITE_SET_OWNER) {
            repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OPPOSITE, ForwardingDirection.OWNER);
        } else {
            PreconditionUtil.assertEquals("unknown behavior", RelationshipRepositoryBehavior.FORWARD_OPPOSITE, behavior);
            repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
        }
        repository.setResourceRegistry(moduleRegistry.getResourceRegistry());
        return setupRelationship(relationshipField, implicitRepoInformation, repository);
    } else {
        return null;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RelationshipRepositoryInformationImpl(io.crnk.core.engine.internal.information.repository.RelationshipRepositoryInformationImpl) RepositoryMethodAccess(io.crnk.core.engine.information.repository.RepositoryMethodAccess) ForwardingRelationshipRepository(io.crnk.core.repository.foward.ForwardingRelationshipRepository) RelationshipRepositoryBehavior(io.crnk.core.resource.annotations.RelationshipRepositoryBehavior) RelationshipMatcher(io.crnk.core.repository.RelationshipMatcher) ResourceFieldAccess(io.crnk.core.engine.information.resource.ResourceFieldAccess)

Example 2 with ForwardingRelationshipRepository

use of io.crnk.core.repository.foward.ForwardingRelationshipRepository in project crnk-framework by crnk-project.

the class OppositeFowardingRelationshipRepositoryTest method checkFindOneTargetFromCollection.

@Test
public void checkFindOneTargetFromCollection() {
    TaskRepository taskRepository = new TaskRepository();
    Task task = new Task();
    task.setId(13L);
    task.setName("task");
    taskRepository.save(task);
    ProjectRepository projectRepository = new ProjectRepository();
    Project project = new Project();
    project.setId(42L);
    project.setName("project");
    project.setTasks(Arrays.asList(task));
    projectRepository.save(project);
    relRepository = new ForwardingRelationshipRepository(Task.class, null, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
    relRepository.setResourceRegistry(resourceRegistry);
    QuerySpec querySpec = new QuerySpec(Task.class);
    Project foundProject = (Project) relRepository.findOneTarget(13L, "project", querySpec);
    Assert.assertEquals(42L, foundProject.getId().longValue());
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) ProjectRepository(io.crnk.core.mock.repository.ProjectRepository) ForwardingRelationshipRepository(io.crnk.core.repository.foward.ForwardingRelationshipRepository) TaskRepository(io.crnk.core.mock.repository.TaskRepository) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 3 with ForwardingRelationshipRepository

use of io.crnk.core.repository.foward.ForwardingRelationshipRepository in project crnk-framework by crnk-project.

the class OppositeFowardingRelationshipRepositoryTest method checkFindTargetWithNotLoadedRelationship.

@Test
public void checkFindTargetWithNotLoadedRelationship() {
    TaskRepository taskRepository = new TaskRepository();
    Task task = new Task();
    task.setId(13L);
    task.setName("task");
    taskRepository.save(task);
    ProjectRepository projectRepository = new ProjectRepository();
    Project project = new Project();
    project.setId(42L);
    project.setName("project");
    project.setTasks(null);
    projectRepository.save(project);
    relRepository = new ForwardingRelationshipRepository(Task.class, null, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
    relRepository.setResourceRegistry(resourceRegistry);
    QuerySpec querySpec = new QuerySpec(Task.class);
    try {
        relRepository.findOneTarget(13L, "project", querySpec);
        Assert.fail();
    } catch (IllegalStateException e) {
        Assert.assertTrue(e.getMessage().contains("field tasks is null for"));
    }
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) ProjectRepository(io.crnk.core.mock.repository.ProjectRepository) ForwardingRelationshipRepository(io.crnk.core.repository.foward.ForwardingRelationshipRepository) TaskRepository(io.crnk.core.mock.repository.TaskRepository) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 4 with ForwardingRelationshipRepository

use of io.crnk.core.repository.foward.ForwardingRelationshipRepository in project crnk-framework by crnk-project.

the class OppositeFowardingRelationshipRepositoryTest method checkFindTargetWithInvalidNullReturnId.

@Test
public void checkFindTargetWithInvalidNullReturnId() {
    TaskRepository taskRepository = new TaskRepository();
    Task task = new Task();
    task.setId(13L);
    task.setName("task");
    taskRepository.save(task);
    ProjectRepository projectRepository = new ProjectRepository();
    Project project = new Project();
    project.setId(42L);
    project.setName("project");
    project.setTasks(Arrays.asList(task));
    projectRepository.save(project);
    // manipulate
    task.setId(null);
    relRepository = new ForwardingRelationshipRepository(Task.class, null, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
    relRepository.setResourceRegistry(resourceRegistry);
    QuerySpec querySpec = new QuerySpec(Task.class);
    try {
        relRepository.findOneTarget(13L, "project", querySpec);
        Assert.fail();
    } catch (IllegalStateException e) {
        Assert.assertTrue(e.getMessage().contains("id is null"));
    }
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) ProjectRepository(io.crnk.core.mock.repository.ProjectRepository) ForwardingRelationshipRepository(io.crnk.core.repository.foward.ForwardingRelationshipRepository) TaskRepository(io.crnk.core.mock.repository.TaskRepository) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 5 with ForwardingRelationshipRepository

use of io.crnk.core.repository.foward.ForwardingRelationshipRepository in project crnk-framework by crnk-project.

the class OppositeFowardingRelationshipRepositoryTest method checkFindTargetWithNullRelationshipValue.

@Test
public void checkFindTargetWithNullRelationshipValue() {
    TaskRepository taskRepository = new TaskRepository();
    Task task = new Task();
    task.setId(13L);
    task.setName("task");
    taskRepository.save(task);
    Task nullIdTask = new Task();
    ProjectRepository projectRepository = new ProjectRepository();
    Project project = new Project();
    project.setId(42L);
    project.setName("project");
    project.setTasks(Arrays.asList(nullIdTask));
    projectRepository.save(project);
    relRepository = new ForwardingRelationshipRepository(Task.class, null, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
    relRepository.setResourceRegistry(resourceRegistry);
    QuerySpec querySpec = new QuerySpec(Task.class);
    try {
        relRepository.findOneTarget(13L, "project", querySpec);
        Assert.fail();
    } catch (IllegalStateException e) {
        Assert.assertTrue(e.getMessage().contains("id is null for"));
    }
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) ProjectRepository(io.crnk.core.mock.repository.ProjectRepository) ForwardingRelationshipRepository(io.crnk.core.repository.foward.ForwardingRelationshipRepository) TaskRepository(io.crnk.core.mock.repository.TaskRepository) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Aggregations

ForwardingRelationshipRepository (io.crnk.core.repository.foward.ForwardingRelationshipRepository)8 Project (io.crnk.core.mock.models.Project)6 Task (io.crnk.core.mock.models.Task)6 ProjectRepository (io.crnk.core.mock.repository.ProjectRepository)6 TaskRepository (io.crnk.core.mock.repository.TaskRepository)6 QuerySpec (io.crnk.core.queryspec.QuerySpec)5 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)5 Test (org.junit.Test)5 RelationshipMatcher (io.crnk.core.repository.RelationshipMatcher)3 CrnkBoot (io.crnk.core.boot.CrnkBoot)2 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)2 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)2 ReflectionsServiceDiscovery (io.crnk.core.module.discovery.ReflectionsServiceDiscovery)2 Before (org.junit.Before)2 RepositoryMethodAccess (io.crnk.core.engine.information.repository.RepositoryMethodAccess)1 ResourceFieldAccess (io.crnk.core.engine.information.resource.ResourceFieldAccess)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 RelationshipRepositoryInformationImpl (io.crnk.core.engine.internal.information.repository.RelationshipRepositoryInformationImpl)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1 Schedule (io.crnk.core.mock.models.Schedule)1