Search in sources :

Example 81 with RegistryEntry

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

the class ResourceUpsert method getRequestBody.

protected Resource getRequestBody(Document requestDocument, JsonPath path, HttpMethod method) {
    String resourceType = path.getResourceType();
    assertRequestDocument(requestDocument, method, resourceType);
    if (!requestDocument.getData().isPresent() || requestDocument.getData().get() == null) {
        throw new RequestBodyException(method, resourceType, "No data field in the body.");
    }
    if (requestDocument.getData().get() instanceof Collection) {
        throw new RequestBodyException(method, resourceType, "Multiple data in body");
    }
    Resource resourceBody = (Resource) requestDocument.getData().get();
    RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
    if (bodyRegistryEntry == null) {
        throw new RepositoryNotFoundException(resourceBody.getType());
    }
    return resourceBody;
}
Also used : RequestBodyException(io.crnk.core.exception.RequestBodyException) Resource(io.crnk.core.engine.document.Resource) RepositoryNotFoundException(io.crnk.core.exception.RepositoryNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 82 with RegistryEntry

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

the class PathBuilder method build.

/**
 * Parses path provided by the application. The path provided cannot contain neither hostname nor protocol. It
 * can start or end with slash e.g. <i>/tasks/1/</i> or <i>tasks/1</i>.
 *
 * @param path Path to be parsed
 * @return doubly-linked list which represents path given at the input
 */
public JsonPath build(String path) {
    String[] strings = splitPath(path);
    if (strings.length == 0 || (strings.length == 1 && "".equals(strings[0]))) {
        return null;
    }
    JsonPath previousJsonPath = null, currentJsonPath = null;
    PathIds pathIds;
    boolean relationshipMark;
    String elementName;
    String actionName;
    int currentElementIdx = 0;
    while (currentElementIdx < strings.length) {
        elementName = null;
        pathIds = null;
        actionName = null;
        relationshipMark = false;
        if (RELATIONSHIP_MARK.equals(strings[currentElementIdx])) {
            relationshipMark = true;
            currentElementIdx++;
        }
        RegistryEntry entry = null;
        if (currentElementIdx < strings.length && !RELATIONSHIP_MARK.equals(strings[currentElementIdx])) {
            elementName = strings[currentElementIdx];
            // support "/" in resource type to group repositories
            StringBuilder potentialResourceType = new StringBuilder();
            for (int i = 0; currentElementIdx + i < strings.length; i++) {
                if (potentialResourceType.length() > 0) {
                    potentialResourceType.append("/");
                }
                potentialResourceType.append(strings[currentElementIdx + i]);
                entry = resourceRegistry.getEntry(potentialResourceType.toString());
                if (entry != null) {
                    currentElementIdx += i;
                    elementName = potentialResourceType.toString();
                    break;
                }
            }
            currentElementIdx++;
        }
        if (currentElementIdx < strings.length && entry != null && entry.getRepositoryInformation().getActions().containsKey(strings[currentElementIdx])) {
            // repository action
            actionName = strings[currentElementIdx];
            currentElementIdx++;
        } else if (currentElementIdx < strings.length && !RELATIONSHIP_MARK.equals(strings[currentElementIdx])) {
            // ids
            pathIds = createPathIds(strings[currentElementIdx]);
            currentElementIdx++;
            if (currentElementIdx < strings.length && entry != null && entry.getRepositoryInformation().getActions().containsKey(strings[currentElementIdx])) {
                // resource action
                actionName = strings[currentElementIdx];
                currentElementIdx++;
            }
        }
        if (previousJsonPath != null) {
            currentJsonPath = getNonResourcePath(previousJsonPath, elementName, relationshipMark);
            if (pathIds != null) {
                throw new ResourceException("RelationshipsPath and FieldPath cannot contain ids");
            }
        } else if (entry != null && !relationshipMark) {
            currentJsonPath = new ResourcePath(elementName);
        } else {
            return null;
        }
        if (pathIds != null) {
            currentJsonPath.setIds(pathIds);
        }
        if (actionName != null) {
            ActionPath actionPath = new ActionPath(actionName);
            actionPath.setParentResource(currentJsonPath);
            currentJsonPath = actionPath;
        }
        if (previousJsonPath != null) {
            currentJsonPath.setParentResource(previousJsonPath);
        }
        previousJsonPath = currentJsonPath;
    }
    return currentJsonPath;
}
Also used : ResourceException(io.crnk.core.exception.ResourceException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 83 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry 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 84 with RegistryEntry

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

Example 85 with RegistryEntry

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

the class IncludeLookupSetter method lookupRelatedResourceWithRelationship.

private Set<Resource> lookupRelatedResourceWithRelationship(Collection<Resource> sourceResources, ResourceField relationshipField, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, ResourceMappingConfig resourceMappingConfig) {
    ResourceInformation resourceInformation = relationshipField.getParentResourceInformation();
    RegistryEntry registyEntry = resourceRegistry.getEntry(resourceInformation.getResourceType());
    List<Serializable> resourceIds = getIds(sourceResources, resourceInformation);
    boolean isMany = Iterable.class.isAssignableFrom(relationshipField.getType());
    Set<Resource> loadedTargets = new HashSet<>();
    @SuppressWarnings("rawtypes") RelationshipRepositoryAdapter relationshipRepository = registyEntry.getRelationshipRepository(relationshipField, parameterProvider);
    if (relationshipRepository != null) {
        Map<Object, JsonApiResponse> responseMap;
        if (isMany) {
            responseMap = relationshipRepository.findBulkManyTargets(resourceIds, relationshipField, queryAdapter);
        } else {
            responseMap = relationshipRepository.findBulkOneTargets(resourceIds, relationshipField, queryAdapter);
        }
        for (Resource sourceResource : sourceResources) {
            Serializable sourceId = resourceInformation.parseIdString(sourceResource.getId());
            JsonApiResponse targetResponse = responseMap.get(sourceId);
            if (targetResponse != null && targetResponse.getEntity() != null) {
                Object targetEntity = targetResponse.getEntity();
                List<Resource> targets = setupRelation(sourceResource, relationshipField, targetEntity, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
                loadedTargets.addAll(targets);
            } else {
                Nullable<Object> emptyData = (Nullable) Nullable.of(Iterable.class.isAssignableFrom(relationshipField.getType()) ? Collections.emptyList() : null);
                Relationship relationship = sourceResource.getRelationships().get(relationshipField.getJsonName());
                relationship.setData(emptyData);
            }
        }
    } else {
        throw new RepositoryNotFoundException("no relationship repository found for " + resourceInformation.getResourceType() + "." + relationshipField.getUnderlyingName());
    }
    return loadedTargets;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Serializable(java.io.Serializable) Resource(io.crnk.core.engine.document.Resource) RepositoryNotFoundException(io.crnk.core.exception.RepositoryNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Relationship(io.crnk.core.engine.document.Relationship) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Nullable(io.crnk.core.utils.Nullable) HashSet(java.util.HashSet)

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