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);
}
}
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations