Search in sources :

Example 1 with RepositoryInstanceBuilder

use of io.crnk.legacy.registry.RepositoryInstanceBuilder 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 RepositoryInstanceBuilder

use of io.crnk.legacy.registry.RepositoryInstanceBuilder in project crnk-framework by crnk-project.

the class CrnkClient method allocateRepository.

@SuppressWarnings({ "rawtypes", "unchecked" })
private <T, I extends Serializable> RegistryEntry allocateRepository(Class<T> resourceClass) {
    ResourceInformationProvider resourceInformationProvider = moduleRegistry.getResourceInformationBuilder();
    ResourceInformation resourceInformation = resourceInformationProvider.build(resourceClass);
    final ResourceRepositoryStub<T, I> repositoryStub = new ResourceRepositoryStubImpl<>(this, resourceClass, resourceInformation, urlBuilder);
    // create interface for it!
    RepositoryInstanceBuilder repositoryInstanceBuilder = new RepositoryInstanceBuilder(null, null) {

        @Override
        public Object buildRepository() {
            return repositoryStub;
        }
    };
    ResourceRepositoryInformation repositoryInformation = new ResourceRepositoryInformationImpl(resourceInformation.getResourceType(), resourceInformation, RepositoryMethodAccess.ALL);
    ResourceEntry resourceEntry = new DirectResponseResourceEntry(repositoryInstanceBuilder, repositoryInformation);
    Map<ResourceField, ResponseRelationshipEntry> relationshipEntries = new HashMap<>();
    RegistryEntry registryEntry = new RegistryEntry(resourceEntry, relationshipEntries);
    registryEntry.initialize(moduleRegistry);
    resourceRegistry.addEntry(resourceClass, registryEntry);
    allocateRepositoryRelations(registryEntry);
    return registryEntry;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceRepositoryStubImpl(io.crnk.client.internal.ResourceRepositoryStubImpl) ResourceRepositoryInformationImpl(io.crnk.core.engine.internal.information.repository.ResourceRepositoryInformationImpl) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) ResourceInformationProvider(io.crnk.core.engine.information.resource.ResourceInformationProvider) RepositoryInstanceBuilder(io.crnk.legacy.registry.RepositoryInstanceBuilder)

Example 3 with RepositoryInstanceBuilder

use of io.crnk.legacy.registry.RepositoryInstanceBuilder in project crnk-framework by crnk-project.

the class AnnotatedRelationshipEntryBuilderTest method onInstanceOfAnnotatedRelationshipRepositoryShouldReturnTargetClass.

@Test
public void onInstanceOfAnnotatedRelationshipRepositoryShouldReturnTargetClass() {
    // GIVEN
    final AnnotatedRelationshipEntryBuilder builder = new AnnotatedRelationshipEntryBuilder(moduleRegistry, new RepositoryInstanceBuilder(new SampleJsonServiceLocator(), SimpleRelationshipRepository.class));
    // WHEN
    final String targetResourceType = builder.getTargetResourceType();
    // THEN
    assertThat(targetResourceType).isEqualTo("projects");
}
Also used : SampleJsonServiceLocator(io.crnk.legacy.locator.SampleJsonServiceLocator) AnnotatedRelationshipEntryBuilder(io.crnk.legacy.registry.AnnotatedRelationshipEntryBuilder) RepositoryInstanceBuilder(io.crnk.legacy.registry.RepositoryInstanceBuilder) Test(org.junit.Test)

Example 4 with RepositoryInstanceBuilder

use of io.crnk.legacy.registry.RepositoryInstanceBuilder in project crnk-framework by crnk-project.

the class DefaultRegistryEntryBuilder method buildResourceRepository.

@SuppressWarnings({ "rawtypes", "unchecked" })
private ResourceEntry buildResourceRepository(ResourceInformation resourceInformation) {
    resourceRepository.information().setResourceInformation(resourceInformation);
    ResourceRepositoryInformation repositoryInformation = resourceRepository.information().build();
    Object instance = resourceRepository.instance;
    final Object decoratedRepository = decorateRepository(instance);
    RepositoryInstanceBuilder repositoryInstanceBuilder = new RepositoryInstanceBuilder(null, instance.getClass()) {

        @Override
        public Object buildRepository() {
            return decoratedRepository;
        }
    };
    if (ClassUtils.getAnnotation(decoratedRepository.getClass(), JsonApiResourceRepository.class).isPresent()) {
        return new AnnotatedResourceEntry(repositoryInstanceBuilder, repositoryInformation);
    } else {
        return new DirectResponseResourceEntry(repositoryInstanceBuilder, repositoryInformation);
    }
}
Also used : AnnotatedResourceEntry(io.crnk.legacy.registry.AnnotatedResourceEntry) ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) JsonApiResourceRepository(io.crnk.legacy.repository.annotations.JsonApiResourceRepository) RepositoryInstanceBuilder(io.crnk.legacy.registry.RepositoryInstanceBuilder)

Example 5 with RepositoryInstanceBuilder

use of io.crnk.legacy.registry.RepositoryInstanceBuilder 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)

Aggregations

RepositoryInstanceBuilder (io.crnk.legacy.registry.RepositoryInstanceBuilder)8 SampleJsonServiceLocator (io.crnk.legacy.locator.SampleJsonServiceLocator)5 Test (org.junit.Test)5 ResourceRepositoryInformation (io.crnk.core.engine.information.repository.ResourceRepositoryInformation)3 DirectResponseRelationshipEntry (io.crnk.legacy.internal.DirectResponseRelationshipEntry)3 DirectResponseResourceEntry (io.crnk.legacy.internal.DirectResponseResourceEntry)3 ResourceField (io.crnk.core.engine.information.resource.ResourceField)2 TaskRepository (io.crnk.core.mock.repository.TaskRepository)2 JsonServiceLocator (io.crnk.legacy.locator.JsonServiceLocator)2 AnnotatedRelationshipEntryBuilder (io.crnk.legacy.registry.AnnotatedRelationshipEntryBuilder)2 RelationshipRepositoryStubImpl (io.crnk.client.internal.RelationshipRepositoryStubImpl)1 ResourceRepositoryStubImpl (io.crnk.client.internal.ResourceRepositoryStubImpl)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 ResourceInformationProvider (io.crnk.core.engine.information.resource.ResourceInformationProvider)1 ResourceRepositoryInformationImpl (io.crnk.core.engine.internal.information.repository.ResourceRepositoryInformationImpl)1 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1 AnnotatedResourceEntry (io.crnk.legacy.registry.AnnotatedResourceEntry)1 RelationshipRepository (io.crnk.legacy.repository.RelationshipRepository)1 JsonApiResourceRepository (io.crnk.legacy.repository.annotations.JsonApiResourceRepository)1