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