Search in sources :

Example 1 with RepositoryMethodAccess

use of io.crnk.core.engine.information.repository.RepositoryMethodAccess in project crnk-framework by crnk-project.

the class DefaultRelationshipRepositoryInformationProvider method buildInformation.

private RepositoryInformation buildInformation(Object repository, Class<? extends Object> repositoryClass, RepositoryInformationProviderContext context) {
    RelationshipMatcher matcher;
    if (repository instanceof RelationshipRepositoryV2) {
        matcher = ((RelationshipRepositoryV2) repository).getMatcher();
    } else {
        Class<?> sourceResourceClass = getSourceResourceClass(repository, repositoryClass);
        Class<?> targetResourceClass = getTargetResourceClass(repository, repositoryClass);
        PreconditionUtil.assertNotNull("no sourceResourceClass", sourceResourceClass);
        PreconditionUtil.assertNotNull("no targetResourceClass", targetResourceClass);
        String sourceResourceType = context.getResourceInformationBuilder().getResourceType(sourceResourceClass);
        String targetResourceType = context.getResourceInformationBuilder().getResourceType(targetResourceClass);
        matcher = new RelationshipMatcher();
        matcher.rule().source(sourceResourceType).target(targetResourceType).add();
    }
    RepositoryMethodAccess access = getAccess(repository);
    return new RelationshipRepositoryInformationImpl(matcher, access);
}
Also used : RelationshipRepositoryInformationImpl(io.crnk.core.engine.internal.information.repository.RelationshipRepositoryInformationImpl) RepositoryMethodAccess(io.crnk.core.engine.information.repository.RepositoryMethodAccess) RelationshipMatcher(io.crnk.core.repository.RelationshipMatcher) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2)

Example 2 with RepositoryMethodAccess

use of io.crnk.core.engine.information.repository.RepositoryMethodAccess 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 3 with RepositoryMethodAccess

use of io.crnk.core.engine.information.repository.RepositoryMethodAccess in project crnk-framework by crnk-project.

the class DefaultInformationBuilderTest method checkRelationshipRepository.

@Test
public void checkRelationshipRepository() {
    InformationBuilder.RelationshipRepository repositoryBuilder = builder.createRelationshipRepository("projects", "tasks");
    RepositoryMethodAccess expectedAccess = new RepositoryMethodAccess(true, false, true, false);
    repositoryBuilder.setAccess(expectedAccess);
    RelationshipRepositoryInformation repositoryInformation = repositoryBuilder.build();
    RepositoryMethodAccess actualAccess = repositoryInformation.getAccess();
    Assert.assertEquals(expectedAccess, actualAccess);
}
Also used : InformationBuilder(io.crnk.core.engine.information.InformationBuilder) RelationshipRepositoryInformation(io.crnk.core.engine.information.repository.RelationshipRepositoryInformation) RepositoryMethodAccess(io.crnk.core.engine.information.repository.RepositoryMethodAccess) Test(org.junit.Test)

Example 4 with RepositoryMethodAccess

use of io.crnk.core.engine.information.repository.RepositoryMethodAccess in project crnk-framework by crnk-project.

the class DefaultInformationBuilderTest method checkResourceRepository.

@Test
public void checkResourceRepository() {
    ResourceInformation resourceInformation = builder.createResource(Task.class, "tasks").build();
    InformationBuilder.ResourceRepository repositoryBuilder = builder.createResourceRepository();
    repositoryBuilder.setResourceInformation(resourceInformation);
    RepositoryMethodAccess expectedAccess = new RepositoryMethodAccess(true, false, true, false);
    repositoryBuilder.setAccess(expectedAccess);
    ResourceRepositoryInformation repositoryInformation = repositoryBuilder.build();
    RepositoryMethodAccess actualAccess = repositoryInformation.getAccess();
    Assert.assertEquals(expectedAccess, actualAccess);
    Assert.assertSame(resourceInformation, repositoryInformation.getResourceInformation().get());
}
Also used : InformationBuilder(io.crnk.core.engine.information.InformationBuilder) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Task(io.crnk.core.mock.models.Task) ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) RepositoryMethodAccess(io.crnk.core.engine.information.repository.RepositoryMethodAccess) Test(org.junit.Test)

Aggregations

RepositoryMethodAccess (io.crnk.core.engine.information.repository.RepositoryMethodAccess)4 InformationBuilder (io.crnk.core.engine.information.InformationBuilder)2 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)2 RelationshipRepositoryInformationImpl (io.crnk.core.engine.internal.information.repository.RelationshipRepositoryInformationImpl)2 RelationshipMatcher (io.crnk.core.repository.RelationshipMatcher)2 Test (org.junit.Test)2 RelationshipRepositoryInformation (io.crnk.core.engine.information.repository.RelationshipRepositoryInformation)1 ResourceRepositoryInformation (io.crnk.core.engine.information.repository.ResourceRepositoryInformation)1 ResourceFieldAccess (io.crnk.core.engine.information.resource.ResourceFieldAccess)1 Task (io.crnk.core.mock.models.Task)1 RelationshipRepositoryV2 (io.crnk.core.repository.RelationshipRepositoryV2)1 ForwardingRelationshipRepository (io.crnk.core.repository.foward.ForwardingRelationshipRepository)1 RelationshipRepositoryBehavior (io.crnk.core.resource.annotations.RelationshipRepositoryBehavior)1