Search in sources :

Example 36 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class ResourceRegistryImpl method getResourceClass.

public Optional<Class<?>> getResourceClass(Class<?> resourceClass) {
    Class<?> currentClass = resourceClass;
    while (currentClass != null && currentClass != Object.class) {
        RegistryEntry entry = rootPart.getEntry(currentClass);
        if (entry != null) {
            return (Optional) Optional.of(currentClass);
        }
        currentClass = currentClass.getSuperclass();
    }
    return Optional.empty();
}
Also used : Optional(io.crnk.core.utils.Optional) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 37 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class ResourceRegistryImpl method getBaseResourceInformation.

@Override
public ResourceInformation getBaseResourceInformation(String resourceType) {
    ResourceInformation baseInformation = baseTypeCache.get(resourceType);
    if (baseInformation != null) {
        return baseInformation;
    }
    RegistryEntry entry = getEntry(resourceType);
    baseInformation = entry.getResourceInformation();
    while (baseInformation.getSuperResourceType() != null) {
        String superResourceType = baseInformation.getSuperResourceType();
        entry = getEntry(superResourceType);
        PreconditionUtil.assertNotNull(superResourceType, entry);
        baseInformation = entry.getResourceInformation();
    }
    baseTypeCache.put(resourceType, baseInformation);
    return baseInformation;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 38 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class RepositoryRequestSpecImpl method getResponseResourceInformation.

@Override
public ResourceInformation getResponseResourceInformation() {
    if (relationshipField != null) {
        String oppositeResourceType = relationshipField.getOppositeResourceType();
        ResourceRegistry resourceRegistry = moduleRegistry.getResourceRegistry();
        RegistryEntry entry = resourceRegistry.getEntry(oppositeResourceType);
        return entry.getResourceInformation();
    }
    return owningResourceInformation;
}
Also used : ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 39 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class FieldResourceGet method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    PathIds resourceIds = jsonPath.getIds();
    String resourceName = jsonPath.getResourceType();
    String elementName = jsonPath.getElementName();
    RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
    Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
    ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(elementName);
    verifyFieldNotNull(relationshipField, elementName);
    // TODO remove Class usage and replace by resourceId
    Class<?> baseRelationshipFieldClass = relationshipField.getType();
    RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    JsonApiResponse entities;
    if (Iterable.class.isAssignableFrom(baseRelationshipFieldClass)) {
        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)

Example 40 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class RelationshipsResourceUpsert method handle.

@Override
public final Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceName = jsonPath.getResourceType();
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry registryEntry = getRegistryEntry(resourceName);
    assertRequestDocument(requestBody, HttpMethod.POST, resourceName);
    Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
    ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(jsonPath.getElementName());
    verifyFieldNotNull(relationshipField, jsonPath.getElementName());
    ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
    @SuppressWarnings("unchecked") Object resource = resourceRepository.findOne(castedResourceId, queryAdapter).getEntity();
    ResourceInformation targetInformation = getRegistryEntry(relationshipField.getOppositeResourceType()).getResourceInformation();
    @SuppressWarnings("unchecked") RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    if (Iterable.class.isAssignableFrom(relationshipField.getType())) {
        Iterable<ResourceIdentifier> dataBodies = (Iterable<ResourceIdentifier>) (requestBody.isMultiple() ? requestBody.getData().get() : Collections.singletonList(requestBody.getData().get()));
        processToManyRelationship(resource, targetInformation, relationshipField, dataBodies, queryAdapter, relationshipRepositoryForClass);
    } else {
        if (requestBody.isMultiple()) {
            throw new RequestBodyException(HttpMethod.POST, resourceName, "Multiple data in body");
        }
        ResourceIdentifier dataBody = (ResourceIdentifier) requestBody.getData().get();
        processToOneRelationship(resource, targetInformation, relationshipField, dataBody, queryAdapter, relationshipRepositoryForClass);
    }
    return new Response(new Document(), HttpStatus.NO_CONTENT_204);
}
Also used : Serializable(java.io.Serializable) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) RequestBodyException(io.crnk.core.exception.RequestBodyException) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds)

Aggregations

RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)119 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)60 Test (org.junit.Test)38 ResourceField (io.crnk.core.engine.information.resource.ResourceField)36 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)19 QuerySpec (io.crnk.core.queryspec.QuerySpec)18 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 Serializable (java.io.Serializable)14 Task (io.crnk.core.mock.models.Task)13 Response (io.crnk.core.engine.dispatcher.Response)12 Document (io.crnk.core.engine.document.Document)11 Resource (io.crnk.core.engine.document.Resource)11 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)9 FilterSpec (io.crnk.core.queryspec.FilterSpec)9 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)8 Before (org.junit.Before)8 Collection (java.util.Collection)7 HashSet (java.util.HashSet)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)6