Search in sources :

Example 16 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class FieldResourcePost method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestDocument) {
    RegistryEntry endpointRegistryEntry = getRegistryEntry(jsonPath);
    Resource resourceBody = getRequestBody(requestDocument, jsonPath, HttpMethod.POST);
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
    Serializable castedResourceId = getResourceId(resourceIds, endpointRegistryEntry);
    ResourceField relationshipField = endpointRegistryEntry.getResourceInformation().findRelationshipFieldByName(jsonPath.getElementName());
    verifyFieldNotNull(relationshipField, jsonPath.getElementName());
    Class<?> baseRelationshipFieldClass = relationshipField.getType();
    RegistryEntry relationshipRegistryEntry = resourceRegistry.getEntry(relationshipField.getOppositeResourceType());
    String relationshipResourceType = relationshipField.getOppositeResourceType();
    verifyTypes(HttpMethod.POST, relationshipRegistryEntry, bodyRegistryEntry);
    Object newResource = buildNewResource(relationshipRegistryEntry, resourceBody, relationshipResourceType);
    setAttributes(resourceBody, newResource, relationshipRegistryEntry.getResourceInformation());
    ResourceRepositoryAdapter resourceRepository = relationshipRegistryEntry.getResourceRepository(parameterProvider);
    Document savedResourceResponse = documentMapper.toDocument(resourceRepository.create(newResource, queryAdapter), queryAdapter, parameterProvider);
    setRelations(newResource, bodyRegistryEntry, resourceBody, queryAdapter, parameterProvider, false);
    ResourceIdentifier resourceId1 = savedResourceResponse.getSingleData().get();
    RelationshipRepositoryAdapter relationshipRepositoryForClass = endpointRegistryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    @SuppressWarnings("unchecked") JsonApiResponse parent = endpointRegistryEntry.getResourceRepository(parameterProvider).findOne(castedResourceId, queryAdapter);
    if (Iterable.class.isAssignableFrom(baseRelationshipFieldClass)) {
        List<ResourceIdentifier> resourceIdList = Collections.singletonList(resourceId1);
        for (ResourceModificationFilter filter : modificationFilters) {
            resourceIdList = filter.modifyManyRelationship(parent.getEntity(), relationshipField, ResourceRelationshipModificationType.ADD, resourceIdList);
        }
        List<Serializable> parsedIds = new ArrayList<>();
        for (ResourceIdentifier resourceId : resourceIdList) {
            parsedIds.add(relationshipRegistryEntry.getResourceInformation().parseIdString(resourceId.getId()));
        }
        // noinspection unchecked
        relationshipRepositoryForClass.addRelations(parent.getEntity(), parsedIds, relationshipField, queryAdapter);
    } else {
        // noinspection unchecked
        for (ResourceModificationFilter filter : modificationFilters) {
            resourceId1 = filter.modifyOneRelationship(parent.getEntity(), relationshipField, resourceId1);
        }
        Serializable parseId = relationshipRegistryEntry.getResourceInformation().parseIdString(resourceId1.getId());
        relationshipRepositoryForClass.setRelation(parent.getEntity(), parseId, relationshipField, queryAdapter);
    }
    return new Response(savedResourceResponse, HttpStatus.CREATED_201);
}
Also used : Serializable(java.io.Serializable) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter)

Example 17 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class RelationshipRepositoryStubImpl method executeWithId.

private void executeWithId(String requestUrl, HttpMethod method, Object targetId) {
    Document document = new Document();
    ResourceIdentifier resourceIdentifier = sourceResourceInformation.toResourceIdentifier(targetId);
    document.setData(Nullable.of((Object) resourceIdentifier));
    doExecute(requestUrl, method, document);
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Document(io.crnk.core.engine.document.Document)

Example 18 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class RelationshipsResourcePatchTest method supportPolymorphicRelationshipTypes.

@Test
public void supportPolymorphicRelationshipTypes() {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = new Resource();
    data.setType(ClassUtils.getAnnotation(Task.class, JsonApiResource.class).get().type());
    newTaskBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/tasks");
    ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    Long taskIdOne = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskIdOne).isNotNull();
    taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    Long taskIdTwo = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskIdOne).isNotNull();
    taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    Long taskIdThree = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskIdOne).isNotNull();
    newTaskBody = new Document();
    // Create ProjectPolymorphic object
    Document newProjectBody = new Document();
    data = new Resource();
    String type = ClassUtils.getAnnotation(ProjectPolymorphic.class, JsonApiResource.class).get().type();
    data.setType(type);
    data.getRelationships().put("task", new Relationship(new ResourceIdentifier(taskIdOne.toString(), "tasks")));
    data.getRelationships().put("tasks", new Relationship(Arrays.asList(new ResourceIdentifier(taskIdTwo.toString(), "tasks"), new ResourceIdentifier(taskIdThree.toString(), "tasks"))));
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectPolymorphicTypePath = pathBuilder.build("/" + type);
    Response projectResponse = resourcePost.handle(projectPolymorphicTypePath, emptyProjectQuery, null, newProjectBody);
    assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects-polymorphic");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    assertThat(projectId).isNotNull();
    Resource projectPolymorphic = projectResponse.getDocument().getSingleData().get();
    assertNotNull(projectPolymorphic.getRelationships().get("task").getSingleData().get());
    assertNotNull(projectPolymorphic.getRelationships().get("tasks"));
    ProjectPolymorphicRepository resourceRepository = (ProjectPolymorphicRepository) resourceRegistry.getEntry(ProjectPolymorphic.class).getResourceRepository(null).getResourceRepository();
    ProjectPolymorphic projectPolymorphicObj = resourceRepository.findOne(projectId, null);
    assertEquals(2, projectPolymorphicObj.getTasks().size());
    projectPolymorphicTypePath = pathBuilder.build("/" + type + "/" + projectPolymorphic.getId());
    ResourcePatch resourcePatch = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    data = newProjectBody.getSingleData().get();
    data.setId(projectId.toString());
    projectPolymorphic.setId(Long.toString(projectId));
    data.getRelationships().get("tasks").setData(Nullable.of((Object) new ArrayList<ResourceIdentifier>()));
    // WHEN
    Response baseResponseContext = resourcePatch.handle(projectPolymorphicTypePath, new QuerySpecAdapter(new QuerySpec(ProjectPolymorphic.class), resourceRegistry), null, newProjectBody);
    assertThat(baseResponseContext.getDocument().getSingleData().get().getType()).isEqualTo("projects-polymorphic");
    projectId = Long.parseLong(baseResponseContext.getDocument().getSingleData().get().getId());
    assertThat(projectId).isNotNull();
    projectPolymorphic = baseResponseContext.getDocument().getSingleData().get();
    assertNotNull(projectPolymorphic.getRelationships().get("task").getSingleData().get());
    assertNotNull(projectPolymorphic.getRelationships().get("tasks"));
    projectPolymorphicObj = resourceRepository.findOne(projectId, null);
    assertEquals(0, projectPolymorphicObj.getTasks().size());
}
Also used : Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) ProjectPolymorphicRepository(io.crnk.core.mock.repository.ProjectPolymorphicRepository) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ProjectPolymorphic(io.crnk.core.mock.models.ProjectPolymorphic) Relationship(io.crnk.core.engine.document.Relationship) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) RelationshipsResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch) QuerySpec(io.crnk.core.queryspec.QuerySpec) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 19 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class ResourceIdControllerTest method prepare.

@Before
public void prepare() {
    super.prepare();
    ScheduleRepositoryImpl scheduleRepository = new ScheduleRepositoryImpl();
    schedule2 = new Schedule();
    schedule2.setId(2L);
    schedule2.setName("test");
    scheduleRepository.save(schedule2);
    schedule2Id = new ResourceIdentifier(schedule2.getId().toString(), "schedules");
    schedule3 = new Schedule();
    schedule3.setId(3L);
    schedule3.setName("test");
    scheduleRepository.save(schedule3);
    schedule3Id = new ResourceIdentifier(schedule3.getId().toString(), "schedules");
    repository = new RelationIdTestRepository();
    repository.setResourceRegistry(resourceRegistry);
}
Also used : ScheduleRepositoryImpl(io.crnk.core.mock.repository.ScheduleRepositoryImpl) RelationIdTestRepository(io.crnk.core.mock.repository.RelationIdTestRepository) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Schedule(io.crnk.core.mock.models.Schedule) Before(org.junit.Before)

Example 20 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class ResourcePostTest method onNewResourcesAndRelationshipsShouldPersistThoseData.

@Test
public void onNewResourcesAndRelationshipsShouldPersistThoseData() throws Exception {
    // GIVEN
    Document newProjectBody = new Document();
    Resource data = createProject();
    newProjectBody.setData(Nullable.of((Object) data));
    data.setType("projects");
    JsonPath projectPath = pathBuilder.build("/projects");
    ResourcePost sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response projectResponse = sut.handle(projectPath, emptyProjectQuery, null, newProjectBody);
    // THEN
    assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    assertThat(projectResponse.getDocument().getSingleData().get().getId()).isNotNull();
    assertThat(projectResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    Mockito.verify(modificationFilter, Mockito.times(1)).modifyAttribute(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq("name"), Mockito.eq("sample project"));
    /* ------- */
    // GIVEN
    Document newUserBody = new Document();
    data = new Resource();
    newUserBody.setData(Nullable.of((Object) data));
    data.setType("users");
    data.setAttribute("name", objectMapper.readTree("\"some user\""));
    List<ResourceIdentifier> projectIds = Collections.singletonList(new ResourceIdentifier(projectId.toString(), "projects"));
    data.getRelationships().put("assignedProjects", new Relationship(projectIds));
    JsonPath taskPath = pathBuilder.build("/users");
    // WHEN
    Response taskResponse = sut.handle(taskPath, emptyUserQuery, null, newUserBody);
    // THEN
    Resource task = taskResponse.getDocument().getSingleData().get();
    assertThat(task.getType()).isEqualTo("users");
    Long userId = Long.parseLong(task.getId());
    assertThat(userId).isNotNull();
    assertThat(task.getAttributes().get("name").asText()).isEqualTo("some user");
    assertThat(task.getRelationships().get("assignedProjects").getCollectionData().get()).hasSize(1);
    assertThat(task.getRelationships().get("assignedProjects").getCollectionData().get().get(0).getId()).isEqualTo(projectId.toString());
    Mockito.verify(modificationFilter, Mockito.times(1)).modifyManyRelationship(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq(ResourceRelationshipModificationType.SET), Mockito.eq(projectIds));
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Aggregations

ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)60 Resource (io.crnk.core.engine.document.Resource)42 Document (io.crnk.core.engine.document.Document)40 Test (org.junit.Test)33 Relationship (io.crnk.core.engine.document.Relationship)31 QuerySpec (io.crnk.core.queryspec.QuerySpec)25 Response (io.crnk.core.engine.dispatcher.Response)14 Task (io.crnk.core.mock.models.Task)13 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)11 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)11 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)11 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)9 Serializable (java.io.Serializable)9 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)8 Project (io.crnk.core.mock.models.Project)8 ArrayList (java.util.ArrayList)8 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)6 LazyTask (io.crnk.core.mock.models.LazyTask)6 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)4