Search in sources :

Example 41 with RegistryEntry

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

the class HttpRequestProcessorImpl method getRequestedResource.

private ResourceInformation getRequestedResource(JsonPath jsonPath) {
    ResourceRegistry resourceRegistry = moduleRegistry.getResourceRegistry();
    RegistryEntry registryEntry = resourceRegistry.getEntry(jsonPath.getResourceType());
    PreconditionUtil.assertNotNull("repository not found, that should have been catched earlier", registryEntry);
    String elementName = jsonPath.getElementName();
    if (elementName != null && !elementName.equals(jsonPath.getResourceType())) {
        ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(elementName);
        if (relationshipField == null) {
            throw new ResourceFieldNotFoundException(elementName);
        }
        String oppositeResourceType = relationshipField.getOppositeResourceType();
        return resourceRegistry.getEntry(oppositeResourceType).getResourceInformation();
    } else {
        return registryEntry.getResourceInformation();
    }
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceFieldNotFoundException(io.crnk.core.exception.ResourceFieldNotFoundException) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 42 with RegistryEntry

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

the class FieldResourcePost method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestDocument) {
    RegistryEntry endpointRegistryEntry = getRegistryEntry(jsonPath);
    Resource resourceBody = getRequestBody(requestDocument, jsonPath, HttpMethod.POST);
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
    Serializable castedResourceId = getResourceId(resourceIds, endpointRegistryEntry);
    ResourceField relationshipField = endpointRegistryEntry.getResourceInformation().findRelationshipFieldByName(jsonPath.getElementName());
    verifyFieldNotNull(relationshipField, jsonPath.getElementName());
    Class<?> baseRelationshipFieldClass = relationshipField.getType();
    RegistryEntry relationshipRegistryEntry = resourceRegistry.getEntry(relationshipField.getOppositeResourceType());
    String relationshipResourceType = relationshipField.getOppositeResourceType();
    verifyTypes(HttpMethod.POST, relationshipRegistryEntry, bodyRegistryEntry);
    Object newResource = buildNewResource(relationshipRegistryEntry, resourceBody, relationshipResourceType);
    setAttributes(resourceBody, newResource, relationshipRegistryEntry.getResourceInformation());
    ResourceRepositoryAdapter resourceRepository = relationshipRegistryEntry.getResourceRepository(parameterProvider);
    Document savedResourceResponse = documentMapper.toDocument(resourceRepository.create(newResource, queryAdapter), queryAdapter, parameterProvider);
    setRelations(newResource, bodyRegistryEntry, resourceBody, queryAdapter, parameterProvider, false);
    ResourceIdentifier resourceId1 = savedResourceResponse.getSingleData().get();
    RelationshipRepositoryAdapter relationshipRepositoryForClass = endpointRegistryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    @SuppressWarnings("unchecked") JsonApiResponse parent = endpointRegistryEntry.getResourceRepository(parameterProvider).findOne(castedResourceId, queryAdapter);
    if (Iterable.class.isAssignableFrom(baseRelationshipFieldClass)) {
        List<ResourceIdentifier> resourceIdList = Collections.singletonList(resourceId1);
        for (ResourceModificationFilter filter : modificationFilters) {
            resourceIdList = filter.modifyManyRelationship(parent.getEntity(), relationshipField, ResourceRelationshipModificationType.ADD, resourceIdList);
        }
        List<Serializable> parsedIds = new ArrayList<>();
        for (ResourceIdentifier resourceId : resourceIdList) {
            parsedIds.add(relationshipRegistryEntry.getResourceInformation().parseIdString(resourceId.getId()));
        }
        // noinspection unchecked
        relationshipRepositoryForClass.addRelations(parent.getEntity(), parsedIds, relationshipField, queryAdapter);
    } else {
        // noinspection unchecked
        for (ResourceModificationFilter filter : modificationFilters) {
            resourceId1 = filter.modifyOneRelationship(parent.getEntity(), relationshipField, resourceId1);
        }
        Serializable parseId = relationshipRegistryEntry.getResourceInformation().parseIdString(resourceId1.getId());
        relationshipRepositoryForClass.setRelation(parent.getEntity(), parseId, relationshipField, queryAdapter);
    }
    return new Response(savedResourceResponse, HttpStatus.CREATED_201);
}
Also used : Serializable(java.io.Serializable) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter)

Example 43 with RegistryEntry

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

the class ResourcePost method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestDocument) {
    RegistryEntry endpointRegistryEntry = getRegistryEntry(jsonPath);
    Resource resourceBody = getRequestBody(requestDocument, jsonPath, HttpMethod.POST);
    RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
    verifyTypes(HttpMethod.POST, endpointRegistryEntry, bodyRegistryEntry);
    ResourceRepositoryAdapter resourceRepository = endpointRegistryEntry.getResourceRepository(parameterProvider);
    JsonApiResponse apiResponse;
    if (Resource.class.equals(resourceRepository.getResourceClass())) {
        apiResponse = resourceRepository.create(resourceBody, queryAdapter);
    } else {
        Object newResource = newResource(bodyRegistryEntry.getResourceInformation(), resourceBody);
        setId(resourceBody, newResource, bodyRegistryEntry.getResourceInformation());
        setAttributes(resourceBody, newResource, bodyRegistryEntry.getResourceInformation());
        setRelations(newResource, bodyRegistryEntry, resourceBody, queryAdapter, parameterProvider, false);
        apiResponse = resourceRepository.create(newResource, queryAdapter);
    }
    if (apiResponse.getEntity() == null) {
        throw new IllegalStateException("repository did not return the created resource");
    }
    Set<String> loadedRelationshipNames = getLoadedRelationshipNames(resourceBody);
    Document responseDocument = documentMapper.toDocument(apiResponse, queryAdapter, parameterProvider, loadedRelationshipNames);
    return new Response(responseDocument, HttpStatus.CREATED_201);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) Resource(io.crnk.core.engine.document.Resource) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 44 with RegistryEntry

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

the class DocumentMapperUtil method toResourceId.

public ResourceIdentifier toResourceId(Object entity) {
    if (entity == null) {
        return null;
    }
    RegistryEntry entry = resourceRegistry.findEntry(entity.getClass());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    return resourceInformation.toResourceIdentifier(entity);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 45 with RegistryEntry

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

the class IncludeLookupUtil method process.

private void process(String type, Set<String> processedTypes, Set<ResourceField> fields) {
    if (!processedTypes.contains(type)) {
        processedTypes.add(type);
        RegistryEntry entry = resourceRegistry.getEntry(type);
        ResourceInformation information = entry.getResourceInformation();
        ResourceInformation superInformation = getSuperInformation(information);
        if (superInformation != null) {
            process(superInformation.getResourceType(), processedTypes, fields);
        }
        // TODO same relationship on multiple children
        for (ResourceField field : information.getRelationshipFields()) {
            boolean existsOnSuperType = superInformation != null && superInformation.findRelationshipFieldByName(field.getJsonName()) != null;
            if (!existsOnSuperType) {
                fields.add(field);
            }
        }
    }
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

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