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);
}
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;
}
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");
}
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);
}
}
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);
}
Aggregations