Search in sources :

Example 1 with AnnotatedRelationshipRepositoryAdapter

use of io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter 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 2 with AnnotatedRelationshipRepositoryAdapter

use of io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter 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)

Example 3 with AnnotatedRelationshipRepositoryAdapter

use of io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter in project crnk-framework by crnk-project.

the class AnnotatedRelationshipRepositoryAdapterTest method onClassWithAddRelationsShouldAddValue.

@Test
public void onClassWithAddRelationsShouldAddValue() throws Exception {
    // GIVEN
    RelationshipRepositoryWithAddRelations repo = spy(RelationshipRepositoryWithAddRelations.class);
    AnnotatedRelationshipRepositoryAdapter<Task, Long, Project, Long> sut = new AnnotatedRelationshipRepositoryAdapter<>(repo, parameterProvider);
    Task task = new Task();
    // WHEN
    sut.addRelations(task, Collections.singleton(1L), "project", queryAdapter);
    // THEN
    verify(repo).addRelations(eq(task), eq(Collections.singleton(1L)), eq("project"));
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) Test(org.junit.Test)

Example 4 with AnnotatedRelationshipRepositoryAdapter

use of io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter in project crnk-framework by crnk-project.

the class AnnotatedRelationshipRepositoryAdapterTest method onClassWithEmptySetRelationShouldThrowException.

@Test(expected = RepositoryMethodException.class)
public void onClassWithEmptySetRelationShouldThrowException() throws Exception {
    // GIVEN
    RelationshipRepositoryWithEmptySetRelation repo = new RelationshipRepositoryWithEmptySetRelation();
    AnnotatedRelationshipRepositoryAdapter<Task, Long, Project, Long> sut = new AnnotatedRelationshipRepositoryAdapter<>(repo, parameterProvider);
    // WHEN
    sut.setRelation(new Task(), 1L, "project", queryAdapter);
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) Test(org.junit.Test)

Example 5 with AnnotatedRelationshipRepositoryAdapter

use of io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter in project crnk-framework by crnk-project.

the class AnnotatedRelationshipRepositoryAdapterTest method onClassWithoutSetRelationsShouldThrowException.

@Test(expected = RepositoryAnnotationNotFoundException.class)
public void onClassWithoutSetRelationsShouldThrowException() throws Exception {
    // GIVEN
    RelationshipRepositoryWithoutAnyMethods repo = new RelationshipRepositoryWithoutAnyMethods();
    AnnotatedRelationshipRepositoryAdapter<Task, Long, Project, Long> sut = new AnnotatedRelationshipRepositoryAdapter<>(repo, parameterProvider);
    // WHEN
    sut.setRelations(new Task(), Collections.singleton(1L), "project", queryAdapter);
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) Test(org.junit.Test)

Aggregations

AnnotatedRelationshipRepositoryAdapter (io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter)16 Project (io.crnk.core.mock.models.Project)12 Task (io.crnk.core.mock.models.Task)12 Test (org.junit.Test)12 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)4 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)4 ResourceField (io.crnk.core.engine.information.resource.ResourceField)4 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)4 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)4 RelationshipRepositoryV2 (io.crnk.core.repository.RelationshipRepositoryV2)4 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)4 Serializable (java.io.Serializable)1