Search in sources :

Example 6 with ResourceModificationFilter

use of io.crnk.core.engine.filter.ResourceModificationFilter in project crnk-framework by crnk-project.

the class ResourceUpsert method setRelationField.

protected void setRelationField(Object newResource, RegistryEntry registryEntry, String relationshipName, Relationship relationship, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider) {
    if (relationship.getData().isPresent()) {
        ResourceIdentifier relationshipId = (ResourceIdentifier) relationship.getData().get();
        ResourceField field = registryEntry.getResourceInformation().findRelationshipFieldByName(relationshipName);
        if (field == null) {
            throw new ResourceException(String.format("Invalid relationship name: %s", relationshipName));
        }
        for (ResourceModificationFilter filter : modificationFilters) {
            relationshipId = filter.modifyOneRelationship(newResource, field, relationshipId);
        }
        Object relationObject;
        if (relationshipId == null) {
            relationObject = null;
            field.getAccessor().setValue(newResource, relationObject);
        } else {
            RegistryEntry entry = resourceRegistry.getEntry(relationshipId.getType());
            Class idFieldType = entry.getResourceInformation().getIdField().getType();
            Serializable typedRelationshipId = parseId(relationshipId, idFieldType);
            if (field.hasIdField()) {
                field.getIdAccessor().setValue(newResource, typedRelationshipId);
            }
            if (decideSetRelationObjectField(entry, typedRelationshipId, field)) {
                relationObject = fetchRelatedObject(entry, typedRelationshipId, parameterProvider, queryAdapter);
                field.getAccessor().setValue(newResource, relationObject);
            }
        }
    }
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Serializable(java.io.Serializable) ResourceException(io.crnk.core.exception.ResourceException) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 7 with ResourceModificationFilter

use of io.crnk.core.engine.filter.ResourceModificationFilter in project crnk-framework by crnk-project.

the class RelationshipsResourceDelete method processToManyRelationship.

@Override
public void processToManyRelationship(Object resource, ResourceInformation targetResourceInformation, ResourceField relationshipField, Iterable<ResourceIdentifier> dataBodies, QueryAdapter queryAdapter, RelationshipRepositoryAdapter relationshipRepositoryForClass) {
    List<ResourceIdentifier> resourceIds = new ArrayList<>();
    for (ResourceIdentifier dataBody : dataBodies) {
        resourceIds.add(dataBody);
    }
    for (ResourceModificationFilter filter : modificationFilters) {
        resourceIds = filter.modifyManyRelationship(resource, relationshipField, ResourceRelationshipModificationType.REMOVE, resourceIds);
    }
    List<Serializable> parsedIds = new LinkedList<>();
    for (ResourceIdentifier resourceId : resourceIds) {
        Serializable parsedId = targetResourceInformation.parseIdString(resourceId.getId());
        parsedIds.add(parsedId);
    }
    // noinspection unchecked
    relationshipRepositoryForClass.removeRelations(resource, parsedIds, relationshipField, queryAdapter);
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) LinkedList(java.util.LinkedList)

Example 8 with ResourceModificationFilter

use of io.crnk.core.engine.filter.ResourceModificationFilter in project crnk-framework by crnk-project.

the class RelationshipsResourceDelete method processToOneRelationship.

@Override
protected void processToOneRelationship(Object resource, ResourceInformation targetResourceInformation, ResourceField relationshipField, ResourceIdentifier dataBody, QueryAdapter queryAdapter, RelationshipRepositoryAdapter relationshipRepositoryForClass) {
    ResourceIdentifier resourceId = null;
    for (ResourceModificationFilter filter : modificationFilters) {
        resourceId = filter.modifyOneRelationship(resource, relationshipField, resourceId);
    }
    Serializable parsedId = resourceId != null ? targetResourceInformation.parseIdString(resourceId.getId()) : null;
    // noinspection unchecked
    relationshipRepositoryForClass.setRelation(resource, parsedId, relationshipField, queryAdapter);
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Serializable(java.io.Serializable) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter)

Example 9 with ResourceModificationFilter

use of io.crnk.core.engine.filter.ResourceModificationFilter in project crnk-framework by crnk-project.

the class RelationshipsResourcePatch method processToOneRelationship.

@Override
protected void processToOneRelationship(Object resource, ResourceInformation targetResourceInformation, ResourceField relationshipField, ResourceIdentifier resourceId, QueryAdapter queryAdapter, RelationshipRepositoryAdapter relationshipRepositoryForClass) {
    for (ResourceModificationFilter filter : modificationFilters) {
        resourceId = filter.modifyOneRelationship(resource, relationshipField, resourceId);
    }
    Serializable parsedId = null;
    if (resourceId != null) {
        parsedId = targetResourceInformation.parseIdString(resourceId.getId());
    }
    // noinspection unchecked
    relationshipRepositoryForClass.setRelation(resource, parsedId, relationshipField, queryAdapter);
}
Also used : Serializable(java.io.Serializable) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter)

Example 10 with ResourceModificationFilter

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

Aggregations

ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)19 Serializable (java.io.Serializable)9 Test (org.junit.Test)9 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)8 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)8 Response (io.crnk.core.engine.dispatcher.Response)7 Document (io.crnk.core.engine.document.Document)6 Resource (io.crnk.core.engine.document.Resource)6 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)5 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)4 ArrayList (java.util.ArrayList)4 ResourceDelete (io.crnk.core.engine.internal.dispatcher.controller.ResourceDelete)3 Project (io.crnk.core.mock.models.Project)3 Task (io.crnk.core.mock.models.Task)3 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)3 QueryParamsAdapter (io.crnk.legacy.internal.QueryParamsAdapter)3 DefaultQueryParamsParser (io.crnk.legacy.queryParams.DefaultQueryParamsParser)3 QueryParams (io.crnk.legacy.queryParams.QueryParams)3 QueryParamsBuilder (io.crnk.legacy.queryParams.QueryParamsBuilder)3 LinkedList (java.util.LinkedList)3