use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class CollectionGet method handle.
@Override
@SuppressWarnings("unchecked")
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
String resourceName = jsonPath.getElementName();
RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
if (registryEntry == null) {
throw new ResourceNotFoundException(resourceName);
}
Document responseDocument;
ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
JsonApiResponse entities;
if (jsonPath.getIds() == null || jsonPath.getIds().getIds().isEmpty()) {
entities = resourceRepository.findAll(queryAdapter);
} else {
Class<? extends Serializable> idType = (Class<? extends Serializable>) registryEntry.getResourceInformation().getIdField().getType();
Iterable<? extends Serializable> parsedIds = typeParser.parse((Iterable<String>) jsonPath.getIds().getIds(), idType);
entities = resourceRepository.findAll(parsedIds, queryAdapter);
}
responseDocument = documentMapper.toDocument(entities, queryAdapter, parameterProvider);
return new Response(responseDocument, 200);
}
use of io.crnk.core.engine.registry.RegistryEntry 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.registry.RegistryEntry 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);
}
use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.
the class ResourceGet method handle.
/**
* {@inheritDoc}
* <p>
* Passes the request to controller method.
*/
@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
String resourceType = jsonPath.getElementName();
PathIds resourceIds = jsonPath.getIds();
RegistryEntry registryEntry = getRegistryEntry(resourceType);
String id = resourceIds.getIds().get(0);
@SuppressWarnings("unchecked") Class<? extends Serializable> idClass = (Class<? extends Serializable>) registryEntry.getResourceInformation().getIdField().getType();
Serializable castedId = typeParser.parse(id, idClass);
ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
JsonApiResponse entities = resourceRepository.findOne(castedId, queryAdapter);
Document responseDocument = documentMapper.toDocument(entities, queryAdapter);
// 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.registry.RegistryEntry in project crnk-framework by crnk-project.
the class ResourcePostTest method toQueryAdapter.
private QueryAdapter toQueryAdapter(QueryParams requestParams, Class resourceType) {
ResourceRegistry resourceRegistry = moduleRegistry.getResourceRegistry();
RegistryEntry entry = resourceRegistry.getEntry(resourceType);
ResourceInformation resourceInformation = entry.getResourceInformation();
return new QueryParamsAdapter(resourceInformation, requestParams, moduleRegistry);
}
Aggregations