Search in sources :

Example 26 with Relationship

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

the class IncludeLookupSetterBaseTest method includePropertiesProviderNonOverwriteRelationshipLookup.

@Test
public void includePropertiesProviderNonOverwriteRelationshipLookup() throws Exception {
    PropertiesProvider propertiesProvider = new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (key.equalsIgnoreCase(CrnkProperties.INCLUDE_AUTOMATICALLY_OVERWRITE)) {
                return "false";
            }
            return null;
        }
    };
    mapper = new DocumentMapper(resourceRegistry, objectMapper, propertiesProvider, resourceFilterDirectory);
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("project"));
    Project project = new Project();
    project.setId(12L);
    Task task = new Task();
    task.setId(1L);
    task.setProject(project);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship relationship = taskResource.getRelationships().get("project");
    assertNotNull(relationship);
    assertEquals(relationship.getSingleData().get().getId(), "12");
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) EmptyPropertiesProvider(io.crnk.core.engine.properties.EmptyPropertiesProvider) Project(io.crnk.core.mock.models.Project) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Task(io.crnk.core.mock.models.Task) DocumentMapper(io.crnk.core.engine.internal.document.mapper.DocumentMapper) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Example 27 with Relationship

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

the class IncludeLookupSetterBaseTest method includeOneRelationLookup.

@Test
public void includeOneRelationLookup() throws Exception {
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("includedProject"));
    Task task = new Task();
    task.setId(1L);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship relationship = taskResource.getRelationships().get("includedProject");
    assertNotNull(relationship);
    assertNotNull(relationship.getSingleData());
    List<Resource> included = document.getIncluded();
    assertEquals(1, included.size());
}
Also used : HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Task(io.crnk.core.mock.models.Task) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Example 28 with Relationship

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

the class ResourceUpsert method setRelations.

protected void setRelations(Object newResource, RegistryEntry registryEntry, Resource resource, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, boolean ignoreMissing) {
    if (resource.getRelationships() != null) {
        for (Map.Entry<String, Relationship> entry : resource.getRelationships().entrySet()) {
            String relationshipName = entry.getKey();
            Relationship relationship = entry.getValue();
            if (relationship != null) {
                ResourceInformation resourceInformation = registryEntry.getResourceInformation();
                ResourceField field = resourceInformation.findRelationshipFieldByName(relationshipName);
                if (field == null && ignoreMissing) {
                    continue;
                }
                if (field == null) {
                    throw new ResourceException(String.format("Invalid relationship name: %s for %s", entry.getKey(), resourceInformation.getResourceType()));
                }
                if (field.isCollection()) {
                    // noinspection unchecked
                    setRelationsField(newResource, registryEntry, entry, queryAdapter, parameterProvider);
                } else {
                    // noinspection unchecked
                    setRelationField(newResource, registryEntry, relationshipName, relationship, queryAdapter, parameterProvider);
                }
            }
        }
    }
}
Also used : Relationship(io.crnk.core.engine.document.Relationship) ResourceException(io.crnk.core.exception.ResourceException)

Example 29 with Relationship

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

the class IncludeLookupSetter method setupRelation.

private List<Resource> setupRelation(Resource sourceResource, ResourceField relationshipField, Object targetEntity, QueryAdapter queryAdapter, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, ResourceMappingConfig resourceMappingConfig) {
    // set the relation
    String relationshipName = relationshipField.getJsonName();
    Map<String, Relationship> relationships = sourceResource.getRelationships();
    Relationship relationship = relationships.get(relationshipName);
    if (targetEntity instanceof Iterable) {
        List<Resource> targets = new ArrayList<>();
        for (Object targetElement : (Iterable<?>) targetEntity) {
            Resource targetResource = mergeResource(targetElement, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
            targets.add(targetResource);
        }
        relationship.setData(Nullable.of((Object) util.toIds(targets)));
        return targets;
    } else {
        Resource targetResource = mergeResource(targetEntity, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
        relationship.setData(Nullable.of((Object) targetResource.toIdentifier()));
        return Collections.singletonList(targetResource);
    }
}
Also used : Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList)

Example 30 with Relationship

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

the class IncludeLookupSetter method setupRelationId.

private void setupRelationId(Resource sourceResource, ResourceField relationshipField, Object targetEntityId) {
    // set the relation
    String relationshipName = relationshipField.getJsonName();
    Map<String, Relationship> relationships = sourceResource.getRelationships();
    Relationship relationship = relationships.get(relationshipName);
    String oppositeType = relationshipField.getOppositeResourceType();
    RegistryEntry entry = Objects.requireNonNull(resourceRegistry.getEntry(oppositeType));
    ResourceInformation targetResourceInformation = entry.getResourceInformation();
    if (targetEntityId instanceof Iterable) {
        List<ResourceIdentifier> targetIds = new ArrayList<>();
        for (Object targetElementId : (Iterable<?>) targetEntityId) {
            targetIds.add(util.idToResourceId(targetResourceInformation, targetElementId));
        }
        relationship.setData(Nullable.of((Object) targetIds));
    } else {
        ResourceIdentifier targetResourceId = util.idToResourceId(targetResourceInformation, targetEntityId);
        relationship.setData(Nullable.of((Object) targetResourceId));
    }
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Relationship(io.crnk.core.engine.document.Relationship) ArrayList(java.util.ArrayList) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

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