Search in sources :

Example 1 with RelationshipRepositoryV2

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();
}
Also used : RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2)

Example 2 with RelationshipRepositoryV2

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);
}
Also used : RelationshipRepositoryInformationImpl(io.crnk.core.engine.internal.information.repository.RelationshipRepositoryInformationImpl) RepositoryMethodAccess(io.crnk.core.engine.information.repository.RepositoryMethodAccess) RelationshipMatcher(io.crnk.core.repository.RelationshipMatcher) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2)

Example 3 with RelationshipRepositoryV2

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;
}
Also used : Decorator(io.crnk.core.engine.internal.utils.Decorator) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) RepositoryDecoratorFactory(io.crnk.core.repository.decorate.RepositoryDecoratorFactory) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2)

Example 4 with RelationshipRepositoryV2

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));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) Serializable(java.io.Serializable) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Example 5 with RelationshipRepositoryV2

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));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Aggregations

RelationshipRepositoryV2 (io.crnk.core.repository.RelationshipRepositoryV2)9 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)6 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)6 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)6 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)6 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)4 AnnotatedRelationshipRepositoryAdapter (io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter)4 Serializable (java.io.Serializable)3 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)2 RelationshipRepository (io.crnk.legacy.repository.RelationshipRepository)2 RepositoryMethodAccess (io.crnk.core.engine.information.repository.RepositoryMethodAccess)1 RelationshipRepositoryInformationImpl (io.crnk.core.engine.internal.information.repository.RelationshipRepositoryInformationImpl)1 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)1 Decorator (io.crnk.core.engine.internal.utils.Decorator)1 RelationshipMatcher (io.crnk.core.repository.RelationshipMatcher)1 ResourceRepositoryV2 (io.crnk.core.repository.ResourceRepositoryV2)1 RepositoryDecoratorFactory (io.crnk.core.repository.decorate.RepositoryDecoratorFactory)1