use of io.crnk.core.engine.document.ResourceIdentifier 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.document.ResourceIdentifier in project crnk-framework by crnk-project.
the class IncludeLookupSetter method mergeResource.
private Resource mergeResource(Object targetEntity, QueryAdapter queryAdapter, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, ResourceMappingConfig resourceMappingConfig) {
Resource targetResource = resourceMapper.toData(targetEntity, queryAdapter, resourceMappingConfig);
ResourceIdentifier targetId = targetResource.toIdentifier();
if (!resourceMap.containsKey(targetId)) {
resourceMap.put(targetId, targetResource);
} else {
// TODO consider merging
targetResource = resourceMap.get(targetId);
}
if (!(targetEntity instanceof Resource)) {
entityMap.put(targetId, targetEntity);
}
return targetResource;
}
use of io.crnk.core.engine.document.ResourceIdentifier 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.document.ResourceIdentifier 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.document.ResourceIdentifier 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);
}
Aggregations