Search in sources :

Example 51 with Relationship

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

the class ClientDocumentMapper method newResourceMapper.

@Override
protected ResourceMapper newResourceMapper(final DocumentMapperUtil util, boolean client, ObjectMapper objectMapper) {
    return new ResourceMapper(util, client, objectMapper, null) {

        @Override
        protected void setRelationship(Resource resource, ResourceField field, Object entity, ResourceInformation resourceInformation, QueryAdapter queryAdapter, ResourceMappingConfig mappingConfig) {
            // we also include relationship data if it is not null and not a
            // unloaded proxy
            boolean includeRelation = true;
            Object relationshipId = null;
            if (field.hasIdField()) {
                Object relationshipValue = field.getIdAccessor().getValue(entity);
                ResourceInformation oppositeInformation = resourceRegistry.getEntry(field.getOppositeResourceType()).getResourceInformation();
                if (relationshipValue instanceof Collection) {
                    List ids = new ArrayList();
                    for (Object elem : (Collection<?>) relationshipValue) {
                        ids.add(oppositeInformation.toResourceIdentifier(elem));
                    }
                    relationshipId = ids;
                } else if (relationshipValue != null) {
                    relationshipId = oppositeInformation.toResourceIdentifier(relationshipValue);
                }
                includeRelation = relationshipId != null || field.getSerializeType() != SerializeType.LAZY;
            } else {
                Object relationshipValue = field.getAccessor().getValue(entity);
                if (relationshipValue instanceof ObjectProxy) {
                    includeRelation = ((ObjectProxy) relationshipValue).isLoaded();
                } else {
                    // TODO for fieldSets handling in the future the lazy
                    // handling must be different
                    includeRelation = relationshipValue != null || field.getSerializeType() != SerializeType.LAZY && !field.isCollection();
                }
                if (relationshipValue != null && includeRelation) {
                    if (relationshipValue instanceof Collection) {
                        relationshipId = util.toResourceIds((Collection<?>) relationshipValue);
                    } else {
                        relationshipId = util.toResourceId(relationshipValue);
                    }
                }
            }
            if (includeRelation) {
                Relationship relationship = new Relationship();
                relationship.setData(Nullable.of((Object) relationshipId));
                resource.getRelationships().put(field.getJsonName(), relationship);
            }
        }
    };
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceMappingConfig(io.crnk.core.engine.internal.document.mapper.ResourceMappingConfig) Relationship(io.crnk.core.engine.document.Relationship) ResourceMapper(io.crnk.core.engine.internal.document.mapper.ResourceMapper) Collection(java.util.Collection) ArrayList(java.util.ArrayList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy)

Example 52 with Relationship

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

the class IncludeLookupSetterInheritanceTest method testPolymorhRelationship.

@Test
public void testPolymorhRelationship() throws Exception {
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("includedProjects"));
    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("includedProjects");
    assertNotNull(relationship);
    List<ResourceIdentifier> projects = relationship.getCollectionData().get();
    assertEquals(2, projects.size());
    List<Resource> included = document.getIncluded();
    assertEquals(2, included.size());
    Assert.assertEquals("projects", included.get(1).getType());
    Assert.assertEquals("fancy-projects", included.get(0).getType());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) 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 53 with Relationship

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

the class DependencyOrderStrategyTest method addOneDependency.

private void addOneDependency(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();
        resource1.getRelationships().put(relationshipName, relationship);
    }
    ResourceIdentifier resourceId = new ResourceIdentifier(resource2.getId(), resource2.getType());
    relationship.setData(Nullable.of((Object) 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 54 with Relationship

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

the class DependencyOrderStrategyTest method addUnitializedDependency.

private void addUnitializedDependency(Operation op, String relationshipName) {
    Resource resource1 = op.getValue();
    Relationship relationship = resource1.getRelationships().get(relationshipName);
    if (relationship == null) {
        relationship = new Relationship();
        resource1.getRelationships().put(relationshipName, relationship);
    }
    relationship.setData(Nullable.empty());
}
Also used : Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource)

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