Search in sources :

Example 1 with RelationshipRepositoryInformation

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

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

the class DefaultRegistryEntryBuilder method buildRelationships.

private Map<ResourceField, ResponseRelationshipEntry> buildRelationships(ResourceInformation resourceInformation) {
    for (String relationshipName : relationshipRepositoryMap.keySet()) {
        if (resourceInformation.findFieldByUnderlyingName(relationshipName) == null) {
            throw new ResourceFieldNotFoundException("failed to find relationship field '" + relationshipName + "' to setup " + "registered relationship repository");
        }
    }
    Map<ResourceField, ResponseRelationshipEntry> map = new HashMap<>();
    for (ResourceField relationshipField : resourceInformation.getRelationshipFields()) {
        ResponseRelationshipEntry relationshipEntry = null;
        // check for local definition
        DefaultRelationshipRepository repository = relationshipRepositoryMap.get(relationshipField.getUnderlyingName());
        if (repository != null) {
            RelationshipRepositoryInformation relationshipInformation = repository.information.build();
            relationshipEntry = setupRelationship(relationshipField, relationshipInformation, repository.instance);
        }
        // check for match
        if (relationshipEntry == null) {
            relationshipEntry = findRelationshipMatch(relationshipField);
        }
        // check for implicit
        if (relationshipEntry == null) {
            relationshipEntry = setupImplicitRelationshipRepository(relationshipField);
        }
        if (relationshipEntry != null) {
            map.put(relationshipField, relationshipEntry);
        } else {
            LOGGER.warn("no relationship repository found for " + resourceInformation.getResourceType() + "." + relationshipField.getUnderlyingName());
        }
    }
    return map;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) RelationshipRepositoryInformation(io.crnk.core.engine.information.repository.RelationshipRepositoryInformation) ResourceFieldNotFoundException(io.crnk.core.exception.ResourceFieldNotFoundException) ResponseRelationshipEntry(io.crnk.core.engine.registry.ResponseRelationshipEntry) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) HashMap(java.util.HashMap)

Example 3 with RelationshipRepositoryInformation

use of io.crnk.core.engine.information.repository.RelationshipRepositoryInformation 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)

Aggregations

RelationshipRepositoryInformation (io.crnk.core.engine.information.repository.RelationshipRepositoryInformation)3 ResponseRelationshipEntry (io.crnk.core.engine.registry.ResponseRelationshipEntry)2 DirectResponseRelationshipEntry (io.crnk.legacy.internal.DirectResponseRelationshipEntry)2 InformationBuilder (io.crnk.core.engine.information.InformationBuilder)1 RepositoryInformation (io.crnk.core.engine.information.repository.RepositoryInformation)1 RepositoryInformationProvider (io.crnk.core.engine.information.repository.RepositoryInformationProvider)1 RepositoryMethodAccess (io.crnk.core.engine.information.repository.RepositoryMethodAccess)1 ResourceRepositoryInformation (io.crnk.core.engine.information.repository.ResourceRepositoryInformation)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceFieldNotFoundException (io.crnk.core.exception.ResourceFieldNotFoundException)1 DefaultRepositoryInformationProviderContext (io.crnk.core.module.internal.DefaultRepositoryInformationProviderContext)1 RelationshipMatcher (io.crnk.core.repository.RelationshipMatcher)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1