use of io.crnk.core.repository.RelationshipRepositoryV2 in project crnk-framework by crnk-project.
the class CrnkClient method getRepositoryForType.
/**
* @param sourceClass source class
* @param targetClass target class
* @return stub for the relationship between the given source and target
* class
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T, I extends Serializable, D, J extends Serializable> RelationshipRepositoryV2<T, I, D, J> getRepositoryForType(Class<T> sourceClass, Class<D> targetClass) {
init();
RelationshipRepositoryAdapter repositoryAdapter = allocateRepositoryRelation(sourceClass, targetClass);
return (RelationshipRepositoryV2<T, I, D, J>) repositoryAdapter.getRelationshipRepository();
}
use of io.crnk.core.repository.RelationshipRepositoryV2 in project crnk-framework by crnk-project.
the class DefaultRelationshipRepositoryInformationProvider method buildInformation.
private RepositoryInformation buildInformation(Object repository, Class<? extends Object> repositoryClass, RepositoryInformationProviderContext context) {
RelationshipMatcher matcher;
if (repository instanceof RelationshipRepositoryV2) {
matcher = ((RelationshipRepositoryV2) repository).getMatcher();
} else {
Class<?> sourceResourceClass = getSourceResourceClass(repository, repositoryClass);
Class<?> targetResourceClass = getTargetResourceClass(repository, repositoryClass);
PreconditionUtil.assertNotNull("no sourceResourceClass", sourceResourceClass);
PreconditionUtil.assertNotNull("no targetResourceClass", targetResourceClass);
String sourceResourceType = context.getResourceInformationBuilder().getResourceType(sourceResourceClass);
String targetResourceType = context.getResourceInformationBuilder().getResourceType(targetResourceClass);
matcher = new RelationshipMatcher();
matcher.rule().source(sourceResourceType).target(targetResourceType).add();
}
RepositoryMethodAccess access = getAccess(repository);
return new RelationshipRepositoryInformationImpl(matcher, access);
}
use of io.crnk.core.repository.RelationshipRepositoryV2 in project crnk-framework by crnk-project.
the class DefaultRegistryEntryBuilder method decorateRepository.
@SuppressWarnings({ "rawtypes", "unchecked" })
public Object decorateRepository(Object repository) {
Object decoratedRepository = repository;
List<RepositoryDecoratorFactory> repositoryDecorators = moduleRegistry.getRepositoryDecoratorFactories();
for (RepositoryDecoratorFactory repositoryDecorator : repositoryDecorators) {
Decorator decorator = null;
if (decoratedRepository instanceof RelationshipRepositoryV2) {
decorator = repositoryDecorator.decorateRepository((RelationshipRepositoryV2) decoratedRepository);
} else if (decoratedRepository instanceof ResourceRepositoryV2) {
decorator = repositoryDecorator.decorateRepository((ResourceRepositoryV2) decoratedRepository);
}
if (decorator != null) {
decorator.setDecoratedObject(decoratedRepository);
decoratedRepository = decorator;
}
}
return decoratedRepository;
}
use of io.crnk.core.repository.RelationshipRepositoryV2 in project crnk-framework by crnk-project.
the class RelationshipRepositoryAdapter method setRelation.
@SuppressWarnings("rawtypes")
public JsonApiResponse setRelation(T source, J targetId, ResourceField field, QueryAdapter queryAdapter) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
Object source = request.getEntity();
Serializable targetId = request.getId();
ResourceField field = request.getRelationshipField();
QueryAdapter queryAdapter = request.getQueryAdapter();
if (isAnnotated) {
((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName(), queryAdapter);
} else if (relationshipRepository instanceof RelationshipRepositoryV2) {
((RelationshipRepositoryV2) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
} else {
((RelationshipRepository) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
}
return new JsonApiResponse();
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.PATCH, source, queryAdapter, Arrays.asList(targetId), field);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
use of io.crnk.core.repository.RelationshipRepositoryV2 in project crnk-framework by crnk-project.
the class RelationshipRepositoryAdapter method addRelations.
@SuppressWarnings("rawtypes")
public JsonApiResponse addRelations(T source, Iterable<J> targetIds, ResourceField field, QueryAdapter queryAdapter) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
Object source = request.getEntity();
Iterable<?> targetIds = request.getIds();
ResourceField field = request.getRelationshipField();
QueryAdapter queryAdapter = request.getQueryAdapter();
if (isAnnotated) {
((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName(), queryAdapter);
} else if (relationshipRepository instanceof RelationshipRepositoryV2) {
((RelationshipRepositoryV2) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
} else {
((RelationshipRepository) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
}
return new JsonApiResponse();
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.POST, source, queryAdapter, targetIds, field);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Aggregations