Search in sources :

Example 1 with DirectResponseRelationshipEntry

use of io.crnk.legacy.internal.DirectResponseRelationshipEntry in project crnk-framework by crnk-project.

the class CrnkClient method allocateRepositoryRelation.

private RelationshipRepositoryAdapter allocateRepositoryRelation(Class sourceClass, Class targetClass) {
    // allocate relations as well
    ClientResourceRegistry clientResourceRegistry = (ClientResourceRegistry) resourceRegistry;
    if (!clientResourceRegistry.isInitialized(sourceClass)) {
        allocateRepository(sourceClass);
    }
    if (!clientResourceRegistry.isInitialized(targetClass)) {
        allocateRepository(targetClass);
    }
    RegistryEntry sourceEntry = resourceRegistry.getEntry(sourceClass);
    final RegistryEntry targetEntry = resourceRegistry.getEntry(targetClass);
    String targetResourceType = targetEntry.getResourceInformation().getResourceType();
    Map relationshipEntries = sourceEntry.getRelationshipEntries();
    DirectResponseRelationshipEntry relationshipEntry = (DirectResponseRelationshipEntry) relationshipEntries.get(targetResourceType);
    if (!relationshipEntries.containsKey(targetResourceType)) {
        final RelationshipRepositoryStubImpl relationshipRepositoryStub = new RelationshipRepositoryStubImpl(this, sourceClass, targetClass, sourceEntry.getResourceInformation(), urlBuilder);
        RepositoryInstanceBuilder<RelationshipRepository> relationshipRepositoryInstanceBuilder = new RepositoryInstanceBuilder<RelationshipRepository>(null, null) {

            @Override
            public RelationshipRepository buildRepository() {
                return relationshipRepositoryStub;
            }
        };
        relationshipEntry = new DirectResponseRelationshipEntry(relationshipRepositoryInstanceBuilder) {

            @Override
            public String getTargetResourceType() {
                return targetEntry.getResourceInformation().getResourceType();
            }
        };
        relationshipEntries.put(targetResourceType, relationshipEntry);
    }
    Object repoInstance = relationshipEntry.getRepositoryInstanceBuilder();
    return new RelationshipRepositoryAdapter(sourceEntry.getResourceInformation(), moduleRegistry, repoInstance);
}
Also used : DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) RelationshipRepository(io.crnk.legacy.repository.RelationshipRepository) RelationshipRepositoryStubImpl(io.crnk.client.internal.RelationshipRepositoryStubImpl) RepositoryInstanceBuilder(io.crnk.legacy.registry.RepositoryInstanceBuilder) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)

Example 2 with DirectResponseRelationshipEntry

use of io.crnk.legacy.internal.DirectResponseRelationshipEntry in project crnk-framework by crnk-project.

the class RegistryEntry method getRelationshipRepository.

@SuppressWarnings("unchecked")
public RelationshipRepositoryAdapter getRelationshipRepository(ResourceField field, RepositoryMethodParameterProvider parameterProvider) {
    ResponseRelationshipEntry relationshipEntry = relationshipEntries.get(field);
    if (relationshipEntry == null) {
        throw new RelationshipRepositoryNotFoundException(getResourceInformation().getResourceType(), field.getUnderlyingName());
    }
    Object repoInstance;
    if (relationshipEntry instanceof AnnotatedRelationshipEntryBuilder) {
        repoInstance = ((AnnotatedRelationshipEntryBuilder) relationshipEntry).build(parameterProvider);
    } else {
        repoInstance = ((DirectResponseRelationshipEntry) relationshipEntry).getRepositoryInstanceBuilder();
    }
    if (repoInstance instanceof ResourceRegistryAware) {
        ((ResourceRegistryAware) repoInstance).setResourceRegistry(moduleRegistry.getResourceRegistry());
    }
    return new RelationshipRepositoryAdapter(getResourceInformation(), moduleRegistry, repoInstance);
}
Also used : DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) RelationshipRepositoryNotFoundException(io.crnk.core.exception.RelationshipRepositoryNotFoundException) AnnotatedRelationshipEntryBuilder(io.crnk.legacy.registry.AnnotatedRelationshipEntryBuilder) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)

Example 3 with DirectResponseRelationshipEntry

use of io.crnk.legacy.internal.DirectResponseRelationshipEntry in project crnk-framework by crnk-project.

the class RegistryEntryTest method onInvalidRelationshipClassShouldThrowException.

@Test
public void onInvalidRelationshipClassShouldThrowException() throws Exception {
    // GIVEN
    ResourceRepositoryInformation repositoryInformation = newRepositoryInformation(Task.class, "tasks");
    ResourceField relationshipField = repositoryInformation.getResourceInformation().get().findFieldByUnderlyingName("tasks");
    Map relRepos = new HashMap<>();
    relRepos.put(relationshipField, new DirectResponseRelationshipEntry(new RepositoryInstanceBuilder(new SampleJsonServiceLocator(), TaskToProjectRepository.class)));
    RegistryEntry sut = new RegistryEntry(new DirectResponseResourceEntry(null, repositoryInformation), relRepos);
    // THEN
    expectedException.expect(ResourceFieldNotFoundException.class);
    // WHEN
    sut.getRelationshipRepository("users", null);
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) HashMap(java.util.HashMap) SampleJsonServiceLocator(io.crnk.legacy.locator.SampleJsonServiceLocator) HashMap(java.util.HashMap) Map(java.util.Map) RepositoryInstanceBuilder(io.crnk.legacy.registry.RepositoryInstanceBuilder) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Example 4 with DirectResponseRelationshipEntry

use of io.crnk.legacy.internal.DirectResponseRelationshipEntry in project crnk-framework by crnk-project.

the class ModuleRegistryTest method testRepositoryRegistration.

@Test
public void testRepositoryRegistration() {
    RegistryEntry entry = resourceRegistry.getEntry(TestResource2.class);
    ResourceInformation info = entry.getResourceInformation();
    Assert.assertEquals(TestResource2.class, info.getResourceClass());
    Assert.assertNotNull(entry.getResourceRepository(null));
    Map relationshipEntries = entry.getRelationshipEntries();
    Assert.assertEquals(1, relationshipEntries.size());
    DirectResponseRelationshipEntry responseRelationshipEntry = (DirectResponseRelationshipEntry) relationshipEntries.values().iterator().next();
    Assert.assertNotNull(responseRelationshipEntry);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Map(java.util.Map) Test(org.junit.Test)

Aggregations

DirectResponseRelationshipEntry (io.crnk.legacy.internal.DirectResponseRelationshipEntry)4 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)2 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)2 RepositoryInstanceBuilder (io.crnk.legacy.registry.RepositoryInstanceBuilder)2 Map (java.util.Map)2 Test (org.junit.Test)2 RelationshipRepositoryStubImpl (io.crnk.client.internal.RelationshipRepositoryStubImpl)1 ResourceRepositoryInformation (io.crnk.core.engine.information.repository.ResourceRepositoryInformation)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 RelationshipRepositoryNotFoundException (io.crnk.core.exception.RelationshipRepositoryNotFoundException)1 DirectResponseResourceEntry (io.crnk.legacy.internal.DirectResponseResourceEntry)1 SampleJsonServiceLocator (io.crnk.legacy.locator.SampleJsonServiceLocator)1 AnnotatedRelationshipEntryBuilder (io.crnk.legacy.registry.AnnotatedRelationshipEntryBuilder)1 RelationshipRepository (io.crnk.legacy.repository.RelationshipRepository)1 HashMap (java.util.HashMap)1