use of io.crnk.core.engine.internal.dispatcher.path.PathIds 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);
}
use of io.crnk.core.engine.internal.dispatcher.path.PathIds 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);
}
use of io.crnk.core.engine.internal.dispatcher.path.PathIds 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);
}
use of io.crnk.core.engine.internal.dispatcher.path.PathIds in project crnk-framework by crnk-project.
the class RelationshipsResourceGet method handle.
@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
String resourceName = jsonPath.getResourceType();
PathIds resourceIds = jsonPath.getIds();
RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
String elementName = jsonPath.getElementName();
ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(elementName);
verifyFieldNotNull(relationshipField, elementName);
boolean isCollection = Iterable.class.isAssignableFrom(relationshipField.getType());
RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
JsonApiResponse entities;
if (isCollection) {
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);
}
use of io.crnk.core.engine.internal.dispatcher.path.PathIds in project crnk-framework by crnk-project.
the class ResourceDelete method handle.
@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
String resourceName = jsonPath.getElementName();
PathIds resourceIds = jsonPath.getIds();
RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
if (registryEntry == null) {
// TODO: Add JsonPath toString and provide to exception?
throw new ResourceNotFoundException(resourceName);
}
for (String id : resourceIds.getIds()) {
Serializable castedId = registryEntry.getResourceInformation().parseIdString(id);
// noinspection unchecked
registryEntry.getResourceRepository(parameterProvider).delete(castedId, queryAdapter);
}
return new Response(null, 204);
}
Aggregations