Search in sources :

Example 6 with Relationship

use of io.crnk.core.engine.document.Relationship 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 Relationship

use of io.crnk.core.engine.document.Relationship 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 Relationship

use of io.crnk.core.engine.document.Relationship 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 Relationship

use of io.crnk.core.engine.document.Relationship 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)

Example 10 with Relationship

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

the class RawResourceFieldAccessor method getValue.

@Override
public Object getValue(Object objResource) {
    Resource resource = (Resource) objResource;
    switch(fieldType) {
        case ID:
            return resource.getId();
        case ATTRIBUTE:
            JsonNode jsonNode = resource.getAttributes().get(fieldName);
            return toObject(jsonNode);
        case RELATIONSHIP:
            Map<String, Relationship> relationships = resource.getRelationships();
            Relationship relationship = relationships.get(fieldName);
            if (relationship != null && relationship.getData().isPresent()) {
                return relationship.getData().get();
            }
            return null;
        case META_INFORMATION:
            return toObject(resource.getMeta());
        default:
            PreconditionUtil.assertEquals("invalid type", fieldType, ResourceFieldType.LINKS_INFORMATION);
            return toObject(resource.getLinks());
    }
}
Also used : Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

Relationship (io.crnk.core.engine.document.Relationship)54 Resource (io.crnk.core.engine.document.Resource)48 Document (io.crnk.core.engine.document.Document)39 Test (org.junit.Test)39 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)31 QuerySpec (io.crnk.core.queryspec.QuerySpec)26 Task (io.crnk.core.mock.models.Task)22 Response (io.crnk.core.engine.dispatcher.Response)14 Project (io.crnk.core.mock.models.Project)14 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)13 LazyTask (io.crnk.core.mock.models.LazyTask)12 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)10 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)10 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)9 HierarchicalTask (io.crnk.core.mock.models.HierarchicalTask)8 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)7 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)4