Search in sources :

Example 76 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class JpaResourceInformationProviderTest method testManyToManyRelation.

@Test
public void testManyToManyRelation() {
    ResourceInformation info = builder.build(ManyToManyTestEntity.class);
    ResourceField field = info.findRelationshipFieldByName("opposites");
    Assert.assertEquals(ResourceFieldType.RELATIONSHIP, field.getResourceFieldType());
    Assert.assertEquals("manyToManyOpposite", field.getOppositeResourceType());
    Assert.assertEquals(SerializeType.LAZY, field.getSerializeType());
// TODO consider bidirectiona assignment Assert.assertEquals("tests", field.getOppositeName());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Test(org.junit.Test)

Example 77 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class JpaResourceInformationProviderTest method testOneToOneRelation.

@Test
public void testOneToOneRelation() {
    ResourceInformation info = builder.build(OneToOneTestEntity.class);
    ResourceField field = info.findRelationshipFieldByName("oneRelatedValue");
    Assert.assertEquals(ResourceFieldType.RELATIONSHIP, field.getResourceFieldType());
    Assert.assertEquals("related", field.getOppositeResourceType());
    Assert.assertEquals(SerializeType.LAZY, field.getSerializeType());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Test(org.junit.Test)

Example 78 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class BraveUtil method getComponentName.

public static String getComponentName(RepositoryRequestSpec request) {
    ResourceField relationshipField = request.getRelationshipField();
    StringBuilder pathBuilder = new StringBuilder();
    String method = request.getMethod().toString();
    pathBuilder.append(BraveRepositoryFilter.COMPONENT_NAME);
    pathBuilder.append(BraveRepositoryFilter.COMPONENT_NAME_SEPARATOR);
    pathBuilder.append(method);
    pathBuilder.append(BraveRepositoryFilter.COMPONENT_NAME_SEPARATOR);
    pathBuilder.append("/");
    if (relationshipField == null) {
        pathBuilder.append(request.getQueryAdapter().getResourceInformation().getResourceType());
    } else {
        pathBuilder.append(relationshipField.getParentResourceInformation().getResourceType());
    }
    Iterable<Object> ids = request.getIds();
    if (ids != null) {
        pathBuilder.append("/");
        pathBuilder.append(StringUtils.join(",", ids));
    }
    if (relationshipField != null) {
        pathBuilder.append("/");
        pathBuilder.append(relationshipField.getJsonName());
    }
    return pathBuilder.toString();
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField)

Example 79 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class PathBuilder method getNonResourcePath.

private JsonPath getNonResourcePath(JsonPath previousJsonPath, String elementName, boolean relationshipMark) {
    String previousElementName = previousJsonPath.getElementName();
    RegistryEntry previousEntry = resourceRegistry.getEntry(previousElementName);
    ResourceInformation resourceInformation = previousEntry.getResourceInformation();
    List<ResourceField> resourceFields = resourceInformation.getRelationshipFields();
    for (ResourceField field : resourceFields) {
        if (field.getJsonName().equals(elementName)) {
            if (relationshipMark) {
                return new RelationshipsPath(elementName);
            } else {
                return new FieldPath(elementName);
            }
        }
    }
    // TODO: Throw different exception? element name can be null..
    throw new ResourceFieldNotFoundException(elementName);
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceFieldNotFoundException(io.crnk.core.exception.ResourceFieldNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 80 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class RelationshipsResourceGet method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceName = jsonPath.getResourceType();
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
    Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
    String elementName = jsonPath.getElementName();
    ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(elementName);
    verifyFieldNotNull(relationshipField, elementName);
    boolean isCollection = Iterable.class.isAssignableFrom(relationshipField.getType());
    RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    JsonApiResponse entities;
    if (isCollection) {
        entities = relationshipRepositoryForClass.findManyTargets(castedResourceId, relationshipField, queryAdapter);
    } else {
        entities = relationshipRepositoryForClass.findOneTarget(castedResourceId, relationshipField, queryAdapter);
    }
    Document responseDocument = documentMapper.toDocument(entities, queryAdapter, parameterProvider);
    // return explicit { data : null } if values found
    if (!responseDocument.getData().isPresent()) {
        responseDocument.setData(Nullable.nullValue());
    }
    return new Response(responseDocument, 200);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) Serializable(java.io.Serializable) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)

Aggregations

ResourceField (io.crnk.core.engine.information.resource.ResourceField)109 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)75 Test (org.junit.Test)41 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)36 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)13 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 ArrayList (java.util.ArrayList)9 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)8 Response (io.crnk.core.engine.dispatcher.Response)8 Resource (io.crnk.core.engine.document.Resource)8 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)8 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 HashSet (java.util.HashSet)8 Document (io.crnk.core.engine.document.Document)7 Task (io.crnk.core.mock.models.Task)7 Serializable (java.io.Serializable)7 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)6 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)6 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)6