Search in sources :

Example 1 with RelationshipRepositoryAdapter

use of io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter 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);
}
Also used : DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) RelationshipRepository(io.crnk.legacy.repository.RelationshipRepository) RelationshipRepositoryStubImpl(io.crnk.client.internal.RelationshipRepositoryStubImpl) RepositoryInstanceBuilder(io.crnk.legacy.registry.RepositoryInstanceBuilder) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)

Example 2 with RelationshipRepositoryAdapter

use of io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter in project crnk-framework by crnk-project.

the class CrnkClient method getQueryParamsRepository.

/**
 * @param sourceClass source class
 * @param targetClass target class
 * @return stub for the relationship between the given source and target
 * class
 * @deprecated make use of QuerySpec
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T, I extends Serializable, D, J extends Serializable> RelationshipRepositoryStub<T, I, D, J> getQueryParamsRepository(Class<T> sourceClass, Class<D> targetClass) {
    init();
    RelationshipRepositoryAdapter repositoryAdapter = allocateRepositoryRelation(sourceClass, targetClass);
    return (RelationshipRepositoryStub<T, I, D, J>) repositoryAdapter.getRelationshipRepository();
}
Also used : RelationshipRepositoryStub(io.crnk.client.legacy.RelationshipRepositoryStub) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)

Example 3 with RelationshipRepositoryAdapter

use of io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter 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 4 with RelationshipRepositoryAdapter

use of io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter 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);
}
Also used : DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) RelationshipRepositoryNotFoundException(io.crnk.core.exception.RelationshipRepositoryNotFoundException) AnnotatedRelationshipEntryBuilder(io.crnk.legacy.registry.AnnotatedRelationshipEntryBuilder) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)

Example 5 with RelationshipRepositoryAdapter

use of io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter in project crnk-framework by crnk-project.

the class FieldResourceGetTest method onGivenIncludeRequestFieldResourcesGetShouldHandleIt.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void onGivenIncludeRequestFieldResourcesGetShouldHandleIt() throws Exception {
    // get repositories
    ResourceRepositoryAdapter userRepo = resourceRegistry.getEntry(User.class).getResourceRepository(null);
    ResourceRepositoryAdapter projectRepo = resourceRegistry.getEntry(Project.class).getResourceRepository(null);
    ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    RelationshipRepositoryAdapter relRepositoryUserToProject = resourceRegistry.getEntry(User.class).getRelationshipRepository("assignedProjects", null);
    RelationshipRepositoryAdapter relRepositoryProjectToTask = resourceRegistry.getEntry(Project.class).getRelationshipRepository("tasks", null);
    ResourceInformation userInfo = resourceRegistry.getEntry(User.class).getResourceInformation();
    ResourceInformation projectInfo = resourceRegistry.getEntry(Project.class).getResourceInformation();
    ResourceField includedTaskField = projectInfo.findRelationshipFieldByName("includedTask");
    ResourceField assignedProjectsField = userInfo.findRelationshipFieldByName("assignedProjects");
    // setup test data
    User user = new User();
    user.setId(1L);
    userRepo.create(user, emptyUserQuery);
    Project project = new Project();
    project.setId(2L);
    projectRepo.create(project, emptyProjectQuery);
    Task task = new Task();
    task.setId(3L);
    taskRepo.create(task, emptyTaskQuery);
    relRepositoryUserToProject.setRelations(user, Collections.singletonList(project.getId()), assignedProjectsField, emptyProjectQuery);
    relRepositoryProjectToTask.setRelation(project, task.getId(), includedTaskField, emptyTaskQuery);
    Map<String, Set<String>> params = new HashMap<String, Set<String>>();
    addParams(params, "include[projects]", "includedTask");
    QueryParams queryParams = queryParamsBuilder.buildQueryParams(params);
    QueryAdapter queryAdapter = new QueryParamsAdapter(projectInfo, queryParams, moduleRegistry);
    JsonPath jsonPath = pathBuilder.build("/users/1/assignedProjects");
    FieldResourceGet sut = new FieldResourceGet(resourceRegistry, typeParser, documentMapper);
    Response response = sut.handle(jsonPath, queryAdapter, null, null);
    // THEN
    Assert.assertNotNull(response);
    Assert.assertNotNull(response.getDocument().getData());
    List<Resource> entityList = ((List<Resource>) response.getDocument().getData().get());
    Assert.assertEquals(true, entityList.size() > 0);
    Assert.assertEquals("projects", entityList.get(0).getType());
    Resource returnedProject = entityList.get(0);
    Assert.assertEquals(project.getId().toString(), returnedProject.getId());
    Relationship relationship = returnedProject.getRelationships().get("includedTask");
    Assert.assertNotNull(relationship);
    Assert.assertEquals(task.getId().toString(), relationship.getSingleData().get().getId());
}
Also used : Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) User(io.crnk.core.mock.models.User) FieldResourceGet(io.crnk.core.engine.internal.dispatcher.controller.FieldResourceGet) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) Relationship(io.crnk.core.engine.document.Relationship) QueryParams(io.crnk.legacy.queryParams.QueryParams) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Aggregations

RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)12 ResourceField (io.crnk.core.engine.information.resource.ResourceField)7 Response (io.crnk.core.engine.dispatcher.Response)5 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)5 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)5 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)5 Serializable (java.io.Serializable)5 Document (io.crnk.core.engine.document.Document)4 PathIds (io.crnk.core.engine.internal.dispatcher.path.PathIds)4 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)4 Resource (io.crnk.core.engine.document.Resource)3 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)3 Project (io.crnk.core.mock.models.Project)3 Task (io.crnk.core.mock.models.Task)3 Relationship (io.crnk.core.engine.document.Relationship)2 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)2 DirectResponseRelationshipEntry (io.crnk.legacy.internal.DirectResponseRelationshipEntry)2 Before (org.junit.Before)2 RelationshipRepositoryStubImpl (io.crnk.client.internal.RelationshipRepositoryStubImpl)1 RelationshipRepositoryStub (io.crnk.client.legacy.RelationshipRepositoryStub)1