use of io.crnk.core.engine.information.resource.ResourceField 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.engine.information.resource.ResourceField 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.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class IncludeLookupSetter method setIncludedElements.
public void setIncludedElements(Document document, Object entity, QueryAdapter queryAdapter, DocumentMappingConfig mappingConfig) {
QueryAdapter inclusionQueryAdapter = queryAdapter;
if (!allowPagination && !(queryAdapter instanceof QueryParamsAdapter) && queryAdapter != null) {
// offset/limit cannot properly work for nested inclusions if becomes cyclic
inclusionQueryAdapter = queryAdapter.duplicate();
if (queryAdapter.getResourceInformation().getPagingBehavior() != null) {
inclusionQueryAdapter.setPagingSpec(queryAdapter.getResourceInformation().getPagingBehavior().createEmptyPagingSpec());
}
}
List<Object> entityList = DocumentMapperUtil.toList(entity);
List<Resource> dataList = DocumentMapperUtil.toList(document.getData().get());
Map<ResourceIdentifier, Resource> dataMap = new HashMap<>();
Map<ResourceIdentifier, Object> entityMap = new HashMap<>();
for (int i = 0; i < dataList.size(); i++) {
Resource dataElement = dataList.get(i);
ResourceIdentifier id = dataElement.toIdentifier();
entityMap.put(id, entityList.get(i));
dataMap.put(id, dataElement);
}
Map<ResourceIdentifier, Resource> resourceMap = new HashMap<>();
resourceMap.putAll(dataMap);
Set<ResourceIdentifier> inclusions = new HashSet<>();
PopulatedCache populatedCache = new PopulatedCache();
RepositoryMethodParameterProvider parameterProvider = mappingConfig.getParameterProvider();
Set<String> fieldsWithEnforcedIdSerialization = mappingConfig.getFieldsWithEnforcedIdSerialization();
ResourceMappingConfig resourceMappingConfig = mappingConfig.getResourceMapping();
ArrayList<ResourceField> stack = new ArrayList<>();
populate(dataList, inclusions, resourceMap, entityMap, stack, inclusionQueryAdapter, parameterProvider, fieldsWithEnforcedIdSerialization, populatedCache, resourceMappingConfig);
// no need to include resources included in the data section
inclusions.removeAll(dataMap.keySet());
// setup included section
ArrayList<Resource> included = new ArrayList<>();
for (ResourceIdentifier inclusionId : inclusions) {
Resource includedResource = resourceMap.get(inclusionId);
PreconditionUtil.assertNotNull("resource not found", includedResource);
included.add(includedResource);
}
Collections.sort(included);
LOGGER.debug("Extracted included resources {}", included.toString());
document.setIncluded(included);
}
use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class IncludeLookupSetter method populate.
private void populate(Collection<Resource> resourceList, Set<ResourceIdentifier> inclusions, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, List<ResourceField> fieldPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Set<String> fieldsWithEnforceIdSerialization, PopulatedCache populatedCache, ResourceMappingConfig resourceMappingConfig) {
if (resourceList.isEmpty()) {
// nothing to do
return;
}
checkNoRecursion(fieldPath);
Set<ResourceField> relationshipFields = util.getRelationshipFields(resourceList);
for (ResourceField resourceField : relationshipFields) {
fieldPath.add(resourceField);
populateField(resourceField, queryAdapter, fieldPath, populatedCache, resourceList, fieldsWithEnforceIdSerialization, parameterProvider, resourceMap, entityMap, inclusions, resourceMappingConfig);
fieldPath.remove(fieldPath.size() - 1);
}
}
use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.
the class DefaultResourceInformationProvider method build.
public ResourceInformation build(Class<?> resourceClass, boolean allowNonResourceBaseClass) {
List<ResourceField> resourceFields = getResourceFields(resourceClass);
String resourceType = getResourceType(resourceClass, allowNonResourceBaseClass);
Optional<JsonPropertyOrder> propertyOrder = ClassUtils.getAnnotation(resourceClass, JsonPropertyOrder.class);
if (propertyOrder.isPresent()) {
JsonPropertyOrder propertyOrderAnnotation = propertyOrder.get();
Collections.sort(resourceFields, new FieldOrderedComparator(propertyOrderAnnotation.value(), propertyOrderAnnotation.alphabetic()));
}
DefaultResourceInstanceBuilder<?> instanceBuilder = new DefaultResourceInstanceBuilder(resourceClass);
Class<?> superclass = resourceClass.getSuperclass();
String superResourceType = superclass != Object.class && context.accept(superclass) ? context.getResourceType(superclass) : null;
ResourceInformation information = new ResourceInformation(context.getTypeParser(), resourceClass, resourceType, superResourceType, instanceBuilder, resourceFields, pagingBehaviors.get(ClassUtils.getAnnotation(resourceClass, JsonApiResource.class).get().pagingBehavior()));
if (!allowNonResourceBaseClass && information.getIdField() == null) {
throw new ResourceIdNotFoundException(resourceClass.getCanonicalName());
}
return information;
}
Aggregations