Search in sources :

Example 1 with RelationshipMatcher

use of io.crnk.core.repository.RelationshipMatcher 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 RelationshipMatcher

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

the class DefaultRegistryEntryBuilder method findRelationshipMatch.

private ResponseRelationshipEntry findRelationshipMatch(ResourceField relationshipField) {
    Collection<Object> repositories = moduleRegistry.getRepositories();
    RepositoryInformationProvider repositoryInformationBuilder = moduleRegistry.getRepositoryInformationBuilder();
    ResponseRelationshipEntry matchedEntry = null;
    for (Object repository : repositories) {
        RepositoryInformation repositoryInformation = repositoryInformationBuilder.build(repository, new DefaultRepositoryInformationProviderContext(moduleRegistry));
        if (repositoryInformation instanceof RelationshipRepositoryInformation) {
            RelationshipRepositoryInformation relationshipRepositoryInformation = (RelationshipRepositoryInformation) repositoryInformation;
            RelationshipMatcher matcher = relationshipRepositoryInformation.getMatcher();
            if (matcher.matches(relationshipField)) {
                if (matchedEntry != null) {
                    throw new IllegalStateException("multiple repositories for " + relationshipField + ": " + repository + ", " + matchedEntry);
                }
                matcher.matches(relationshipField);
                matchedEntry = setupRelationship(relationshipField, relationshipRepositoryInformation, repository);
            }
        }
    }
    return matchedEntry;
}
Also used : RelationshipRepositoryInformation(io.crnk.core.engine.information.repository.RelationshipRepositoryInformation) ResponseRelationshipEntry(io.crnk.core.engine.registry.ResponseRelationshipEntry) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) DefaultRepositoryInformationProviderContext(io.crnk.core.module.internal.DefaultRepositoryInformationProviderContext) RepositoryInformationProvider(io.crnk.core.engine.information.repository.RepositoryInformationProvider) RelationshipMatcher(io.crnk.core.repository.RelationshipMatcher) ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) RepositoryInformation(io.crnk.core.engine.information.repository.RepositoryInformation) RelationshipRepositoryInformation(io.crnk.core.engine.information.repository.RelationshipRepositoryInformation)

Example 3 with RelationshipMatcher

use of io.crnk.core.repository.RelationshipMatcher 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 4 with RelationshipMatcher

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

the class DefaultInformationBuilder method createRelationshipRepository.

@Override
public RelationshipRepository createRelationshipRepository(String sourceResourceType, String targetResourceType) {
    RelationshipMatcher matcher = new RelationshipMatcher();
    matcher.rule().target(targetResourceType).source(sourceResourceType).add();
    return createRelationshipRepository(matcher);
}
Also used : RelationshipMatcher(io.crnk.core.repository.RelationshipMatcher)

Example 5 with RelationshipMatcher

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

the class OwnerFowardingRelationshipRepositoryTest method setup.

@Before
public void setup() {
    MockRepositoryUtil.clear();
    CrnkBoot boot = new CrnkBoot();
    boot.setServiceDiscovery(new ReflectionsServiceDiscovery(MockConstants.TEST_MODELS_PACKAGE));
    boot.setServiceUrlProvider(new ConstantServiceUrlProvider(ResourceRegistryTest.TEST_MODELS_URL));
    boot.boot();
    resourceRegistry = boot.getResourceRegistry();
    RegistryEntry entry = resourceRegistry.getEntry(RelationIdTestResource.class);
    relRepository = (ForwardingRelationshipRepository) entry.getRelationshipRepository("testSerializeEager", null).getRelationshipRepository();
    RelationshipMatcher taskProjectMatcher = new RelationshipMatcher().rule().source(Task.class).target(Project.class).add();
    taskProjectRepository = new ForwardingRelationshipRepository(Task.class, taskProjectMatcher, ForwardingDirection.OWNER, ForwardingDirection.OWNER);
    taskProjectRepository.setResourceRegistry(resourceRegistry);
    projectTaskRepository = new ForwardingRelationshipRepository(Project.class, taskProjectMatcher, ForwardingDirection.OWNER, ForwardingDirection.OWNER);
    projectTaskRepository.setResourceRegistry(resourceRegistry);
    testRepository = (RelationIdTestRepository) entry.getResourceRepository().getResourceRepository();
    testRepository.setResourceRegistry(resourceRegistry);
    resource = new RelationIdTestResource();
    resource.setId(2L);
    resource.setName("relationId");
    testRepository.create(resource);
    scheduleRepository = new ScheduleRepositoryImpl();
    schedule3 = new Schedule();
    schedule3.setId(3L);
    schedule3.setName("schedule");
    scheduleRepository.create(schedule3);
    for (int i = 0; i < 10; i++) {
        schedule = new Schedule();
        schedule.setId(4L + i);
        schedule.setName("schedule");
        scheduleRepository.create(schedule);
        projectRepository = new ProjectRepository();
        project = new Project();
        project.setId(42L + i);
        project.setName("project");
        projectRepository.save(project);
        taskRepository = new TaskRepository();
        task = new Task();
        task.setId(13L + i);
        task.setName("task");
        taskRepository.save(task);
    }
}
Also used : 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) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipMatcher(io.crnk.core.repository.RelationshipMatcher) ScheduleRepositoryImpl(io.crnk.core.mock.repository.ScheduleRepositoryImpl) Project(io.crnk.core.mock.models.Project) CrnkBoot(io.crnk.core.boot.CrnkBoot) Schedule(io.crnk.core.mock.models.Schedule) ReflectionsServiceDiscovery(io.crnk.core.module.discovery.ReflectionsServiceDiscovery) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource) Before(org.junit.Before)

Aggregations

RelationshipMatcher (io.crnk.core.repository.RelationshipMatcher)6 ForwardingRelationshipRepository (io.crnk.core.repository.foward.ForwardingRelationshipRepository)3 CrnkBoot (io.crnk.core.boot.CrnkBoot)2 RepositoryMethodAccess (io.crnk.core.engine.information.repository.RepositoryMethodAccess)2 RelationshipRepositoryInformationImpl (io.crnk.core.engine.internal.information.repository.RelationshipRepositoryInformationImpl)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 RelationshipRepositoryInformation (io.crnk.core.engine.information.repository.RelationshipRepositoryInformation)1 RepositoryInformation (io.crnk.core.engine.information.repository.RepositoryInformation)1 RepositoryInformationProvider (io.crnk.core.engine.information.repository.RepositoryInformationProvider)1 ResourceRepositoryInformation (io.crnk.core.engine.information.repository.ResourceRepositoryInformation)1 ResourceFieldAccess (io.crnk.core.engine.information.resource.ResourceFieldAccess)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1 ResponseRelationshipEntry (io.crnk.core.engine.registry.ResponseRelationshipEntry)1 Project (io.crnk.core.mock.models.Project)1 Schedule (io.crnk.core.mock.models.Schedule)1 Task (io.crnk.core.mock.models.Task)1