Search in sources :

Example 6 with ResourceIdentifier

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

the class DependencyOrderStrategyTest method addManyDependency.

private void addManyDependency(Operation op1, Operation op2, String relationshipName) {
    Resource resource1 = op1.getValue();
    Resource resource2 = op2.getValue();
    Relationship relationship = resource1.getRelationships().get(relationshipName);
    if (relationship == null) {
        relationship = new Relationship();
        relationship.setData((Nullable) Nullable.of(new ArrayList<ResourceIdentifier>()));
        resource1.getRelationships().put(relationshipName, relationship);
    }
    ResourceIdentifier resourceId = new ResourceIdentifier(resource2.getId(), resource2.getType());
    relationship.getCollectionData().get().add(resourceId);
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource)

Example 7 with ResourceIdentifier

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

the class OperationsDeleteRelationTest method testDeleteRelation.

@Test
public void testDeleteRelation() {
    PersonEntity person1 = newPerson("1");
    PersonEntity person2 = newPerson("2");
    MovieEntity movie = newMovie("test");
    movie.setDirectors(new HashSet<>(Arrays.asList(person1, person2)));
    OperationsCall insertCall = operationsClient.createCall();
    insertCall.add(HttpMethod.POST, movie);
    insertCall.add(HttpMethod.POST, person1);
    insertCall.add(HttpMethod.POST, person2);
    insertCall.execute();
    QuerySpec querySpec = new QuerySpec(MovieEntity.class);
    querySpec.includeRelation(Arrays.asList("directors"));
    MovieEntity updatedMovie = movieRepo.findOne(movie.getId(), querySpec);
    Set<PersonEntity> directors = updatedMovie.getDirectors();
    PersonEntity deletedDirector = directors.iterator().next();
    directors.remove(deletedDirector);
    OperationsCall call = operationsClient.createCall();
    call.add(HttpMethod.PATCH, updatedMovie);
    call.add(HttpMethod.DELETE, deletedDirector);
    call.execute();
    // check whether updated relationship is included in the response
    Resource movieResource = call.getResponse(0).getSingleData().get();
    Relationship directorsRelationship = movieResource.getRelationships().get("directors");
    List<ResourceIdentifier> directorIds = directorsRelationship.getCollectionData().get();
    Assert.assertEquals(1, directorIds.size());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) MovieEntity(io.crnk.operations.model.MovieEntity) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) OperationsCall(io.crnk.operations.client.OperationsCall) PersonEntity(io.crnk.operations.model.PersonEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test)

Example 8 with ResourceIdentifier

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

the class ResourceUpsert method setRelationsField.

protected void setRelationsField(Object newResource, RegistryEntry registryEntry, Map.Entry<String, Relationship> property, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider) {
    Relationship relationship = property.getValue();
    if (relationship.getData().isPresent()) {
        String propertyName = property.getKey();
        ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(propertyName);
        List<ResourceIdentifier> relationshipIds = relationship.getCollectionData().get();
        for (ResourceModificationFilter filter : modificationFilters) {
            relationshipIds = filter.modifyManyRelationship(newResource, relationshipField, ResourceRelationshipModificationType.SET, relationshipIds);
        }
        List relationshipTypedIds = new LinkedList<>();
        for (ResourceIdentifier resourceId : relationshipIds) {
            RegistryEntry entry = resourceRegistry.getEntry(resourceId.getType());
            Class idFieldType = entry.getResourceInformation().getIdField().getType();
            Serializable typedRelationshipId = parseId(resourceId, idFieldType);
            relationshipTypedIds.add(typedRelationshipId);
        }
        if (relationshipField.hasIdField()) {
            relationshipField.getIdAccessor().setValue(newResource, relationshipTypedIds);
        }
        // TODO batch fetchRelatedObject
        if (decideSetRelationObjectsField(relationshipField)) {
            List relationshipEntities = new LinkedList<>();
            for (int i = 0; i < relationshipIds.size(); i++) {
                ResourceIdentifier resourceId = relationshipIds.get(i);
                Serializable typedRelationshipId = (Serializable) relationshipTypedIds.get(i);
                RegistryEntry entry = resourceRegistry.getEntry(resourceId.getType());
                Object relationObject = fetchRelatedObject(entry, typedRelationshipId, parameterProvider, queryAdapter);
                relationshipEntities.add(relationObject);
            }
            relationshipField.getAccessor().setValue(newResource, relationshipEntities);
        }
    }
}
Also used : Serializable(java.io.Serializable) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter)

Example 9 with ResourceIdentifier

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

the class ResourceUpsert method setRelationField.

protected void setRelationField(Object newResource, RegistryEntry registryEntry, String relationshipName, Relationship relationship, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider) {
    if (relationship.getData().isPresent()) {
        ResourceIdentifier relationshipId = (ResourceIdentifier) relationship.getData().get();
        ResourceField field = registryEntry.getResourceInformation().findRelationshipFieldByName(relationshipName);
        if (field == null) {
            throw new ResourceException(String.format("Invalid relationship name: %s", relationshipName));
        }
        for (ResourceModificationFilter filter : modificationFilters) {
            relationshipId = filter.modifyOneRelationship(newResource, field, relationshipId);
        }
        Object relationObject;
        if (relationshipId == null) {
            relationObject = null;
            field.getAccessor().setValue(newResource, relationObject);
        } else {
            RegistryEntry entry = resourceRegistry.getEntry(relationshipId.getType());
            Class idFieldType = entry.getResourceInformation().getIdField().getType();
            Serializable typedRelationshipId = parseId(relationshipId, idFieldType);
            if (field.hasIdField()) {
                field.getIdAccessor().setValue(newResource, typedRelationshipId);
            }
            if (decideSetRelationObjectField(entry, typedRelationshipId, field)) {
                relationObject = fetchRelatedObject(entry, typedRelationshipId, parameterProvider, queryAdapter);
                field.getAccessor().setValue(newResource, relationObject);
            }
        }
    }
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Serializable(java.io.Serializable) ResourceException(io.crnk.core.exception.ResourceException) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 10 with ResourceIdentifier

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

the class IncludeLookupSetter method lookupRelatedResourcesWithId.

private Set<Resource> lookupRelatedResourcesWithId(Collection<Resource> sourceResources, ResourceField relationshipField, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, ResourceMappingConfig resourceMappingConfig) {
    String oppositeResourceType = relationshipField.getOppositeResourceType();
    RegistryEntry oppositeEntry = resourceRegistry.getEntry(oppositeResourceType);
    if (oppositeEntry == null) {
        throw new RepositoryNotFoundException("no resource with type " + oppositeResourceType + " found");
    }
    ResourceInformation oppositeResourceInformation = oppositeEntry.getResourceInformation();
    ResourceRepositoryAdapter oppositeResourceRepository = oppositeEntry.getResourceRepository(parameterProvider);
    if (oppositeResourceRepository == null) {
        throw new RepositoryNotFoundException("no relationship repository found for " + oppositeResourceInformation.getResourceType());
    }
    Set<Resource> related = new HashSet<>();
    Set<Object> relatedIdsToLoad = new HashSet<>();
    for (Resource sourceResource : sourceResources) {
        Relationship relationship = sourceResource.getRelationships().get(relationshipField.getJsonName());
        PreconditionUtil.assertTrue("expected relationship data to be loaded for @JsonApiResourceId annotated field", relationship.getData().isPresent());
        if (relationship.getData().get() != null) {
            for (ResourceIdentifier id : relationship.getCollectionData().get()) {
                if (resourceMap.containsKey(id)) {
                    // load from cache
                    related.add(resourceMap.get(id));
                } else {
                    relatedIdsToLoad.add(oppositeResourceInformation.parseIdString(id.getId()));
                }
            }
        }
    }
    if (!relatedIdsToLoad.isEmpty()) {
        JsonApiResponse response = oppositeResourceRepository.findAll(relatedIdsToLoad, queryAdapter);
        Collection responseList = (Collection) response.getEntity();
        for (Object responseEntity : responseList) {
            Resource relatedResource = mergeResource(responseEntity, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
            related.add(relatedResource);
            Object responseEntityId = oppositeResourceInformation.getId(responseEntity);
            relatedIdsToLoad.remove(responseEntityId);
        }
        if (!relatedIdsToLoad.isEmpty()) {
            throw new ResourceNotFoundException("type=" + relationshipField.getOppositeResourceType() + ", " + "ids=" + relatedIdsToLoad);
        }
    }
    return related;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Resource(io.crnk.core.engine.document.Resource) RepositoryNotFoundException(io.crnk.core.exception.RepositoryNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) Relationship(io.crnk.core.engine.document.Relationship) Collection(java.util.Collection) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) HashSet(java.util.HashSet)

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