use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class GetFromOwnerStrategy method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = context.getSourceEntry();
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
RegistryEntry targetEntry = context.getTargetEntry(field);
List sources = (List) sourceEntry.getResourceRepository().findAll(sourceIds, context.createSaveQueryAdapter(fieldName)).getEntity();
ResourceInformation targetInformation = targetEntry.getResourceInformation();
List<D> targets;
if (field.hasIdField()) {
Set targetIds = new HashSet();
for (Object source : sources) {
Object targetId = field.getIdAccessor().getValue(source);
if (field.isCollection()) {
targetIds.addAll((Collection) targetId);
} else {
targetIds.add(targetId);
}
}
QuerySpec idQuerySpec = new QuerySpec(targetInformation);
ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(targetIds, context.createQueryAdapter(idQuerySpec));
targets = (List<D>) response.getEntity();
return toResult(field, targetInformation, sources, targets);
} else {
MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
for (Object source : sources) {
Object sourceId = sourceInformation.getId(source);
Object target = field.getAccessor().getValue(source);
if (target != null) {
if (field.isCollection()) {
bulkResult.addAll(sourceId, (Collection) target);
} else {
bulkResult.add(sourceId, target);
}
}
}
return bulkResult;
}
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class RelationshipRepositoryAdapter method setRelation.
@SuppressWarnings("rawtypes")
public JsonApiResponse setRelation(T source, J targetId, ResourceField field, QueryAdapter queryAdapter) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
Object source = request.getEntity();
Serializable targetId = request.getId();
ResourceField field = request.getRelationshipField();
QueryAdapter queryAdapter = request.getQueryAdapter();
if (isAnnotated) {
((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName(), queryAdapter);
} else if (relationshipRepository instanceof RelationshipRepositoryV2) {
((RelationshipRepositoryV2) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
} else {
((RelationshipRepository) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
}
return new JsonApiResponse();
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.PATCH, source, queryAdapter, Arrays.asList(targetId), field);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class RelationshipRepositoryAdapter method addRelations.
@SuppressWarnings("rawtypes")
public JsonApiResponse addRelations(T source, Iterable<J> targetIds, ResourceField field, QueryAdapter queryAdapter) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
Object source = request.getEntity();
Iterable<?> targetIds = request.getIds();
ResourceField field = request.getRelationshipField();
QueryAdapter queryAdapter = request.getQueryAdapter();
if (isAnnotated) {
((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName(), queryAdapter);
} else if (relationshipRepository instanceof RelationshipRepositoryV2) {
((RelationshipRepositoryV2) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
} else {
((RelationshipRepository) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
}
return new JsonApiResponse();
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.POST, source, queryAdapter, targetIds, field);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class ResourceRepositoryAdapter method delete.
public JsonApiResponse delete(I id, QueryAdapter queryAdapter) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@SuppressWarnings("rawtypes")
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
QueryAdapter queryAdapter = request.getQueryAdapter();
Serializable id = request.getId();
if (isAnnotated) {
((AnnotatedResourceRepositoryAdapter) resourceRepository).delete(id, queryAdapter);
} else if (resourceRepository instanceof ResourceRepositoryV2) {
((ResourceRepositoryV2) resourceRepository).delete(id);
} else {
((ResourceRepository) resourceRepository).delete(id);
}
return new JsonApiResponse();
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forDelete(moduleRegistry, resourceInformation, queryAdapter, id);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.
the class ResourcePatch method handle.
@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestDocument) {
RegistryEntry endpointRegistryEntry = getRegistryEntry(jsonPath);
final Resource resourceBody = getRequestBody(requestDocument, jsonPath, HttpMethod.PATCH);
RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
String idString = jsonPath.getIds().getIds().get(0);
ResourceInformation resourceInformation = bodyRegistryEntry.getResourceInformation();
Serializable resourceId = resourceInformation.parseIdString(idString);
verifyTypes(HttpMethod.PATCH, endpointRegistryEntry, bodyRegistryEntry);
ResourceRepositoryAdapter resourceRepository = endpointRegistryEntry.getResourceRepository(parameterProvider);
JsonApiResponse resourceFindResponse = resourceRepository.findOne(resourceId, queryAdapter);
Object resource = resourceFindResponse.getEntity();
if (resource == null) {
throw new ResourceNotFoundException(jsonPath.toString());
}
final Resource resourceFindData = documentMapper.toDocument(resourceFindResponse, queryAdapter, parameterProvider).getSingleData().get();
resourceInformation.verify(resource, requestDocument);
// extract current attributes from findOne without any manipulation by query params (such as sparse fieldsets)
ExceptionUtil.wrapCatchedExceptions(new Callable<Object>() {
@Override
public Object call() throws Exception {
String attributesFromFindOne = extractAttributesFromResourceAsJson(resourceFindData);
Map<String, Object> attributesToUpdate = new HashMap<>(emptyIfNull(objectMapper.readValue(attributesFromFindOne, Map.class)));
// deserialize the request JSON's attributes object into a map
String attributesAsJson = objectMapper.writeValueAsString(resourceBody.getAttributes());
Map<String, Object> attributesFromRequest = emptyIfNull(objectMapper.readValue(attributesAsJson, Map.class));
// remove attributes that were omitted in the request
Iterator<String> it = attributesToUpdate.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
if (!attributesFromRequest.containsKey(key)) {
it.remove();
}
}
// walk the source map and apply target values from request
updateValues(attributesToUpdate, attributesFromRequest);
Map<String, JsonNode> upsertedAttributes = new HashMap<>();
for (Map.Entry<String, Object> entry : attributesToUpdate.entrySet()) {
JsonNode value = objectMapper.valueToTree(entry.getValue());
upsertedAttributes.put(entry.getKey(), value);
}
resourceBody.setAttributes(upsertedAttributes);
return null;
}
}, "failed to merge patched attributes");
JsonApiResponse updatedResource;
Set<String> loadedRelationshipNames;
if (resourceInformation.getResourceClass() == Resource.class) {
loadedRelationshipNames = getLoadedRelationshipNames(resourceBody);
updatedResource = resourceRepository.update(resourceBody, queryAdapter);
} else {
setAttributes(resourceBody, resource, bodyRegistryEntry.getResourceInformation());
setRelations(resource, bodyRegistryEntry, resourceBody, queryAdapter, parameterProvider, false);
loadedRelationshipNames = getLoadedRelationshipNames(resourceBody);
updatedResource = resourceRepository.update(resource, queryAdapter);
}
Document responseDocument = documentMapper.toDocument(updatedResource, queryAdapter, parameterProvider, loadedRelationshipNames);
return new Response(responseDocument, 200);
}
Aggregations