use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class IncludeLookupSetterBaseTest method includePropertiesProviderNonOverwriteRelationshipLookup.
@Test
public void includePropertiesProviderNonOverwriteRelationshipLookup() throws Exception {
PropertiesProvider propertiesProvider = new PropertiesProvider() {
@Override
public String getProperty(String key) {
if (key.equalsIgnoreCase(CrnkProperties.INCLUDE_AUTOMATICALLY_OVERWRITE)) {
return "false";
}
return null;
}
};
mapper = new DocumentMapper(resourceRegistry, objectMapper, propertiesProvider, resourceFilterDirectory);
QuerySpec querySpec = new QuerySpec(Task.class);
querySpec.includeRelation(Arrays.asList("project"));
Project project = new Project();
project.setId(12L);
Task task = new Task();
task.setId(1L);
task.setProject(project);
Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
Resource taskResource = document.getSingleData().get();
Relationship relationship = taskResource.getRelationships().get("project");
assertNotNull(relationship);
assertEquals(relationship.getSingleData().get().getId(), "12");
}
use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class IncludeLookupSetterBaseTest method includeOneRelationLookup.
@Test
public void includeOneRelationLookup() throws Exception {
QuerySpec querySpec = new QuerySpec(Task.class);
querySpec.includeRelation(Arrays.asList("includedProject"));
Task task = new Task();
task.setId(1L);
Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
Resource taskResource = document.getSingleData().get();
Relationship relationship = taskResource.getRelationships().get("includedProject");
assertNotNull(relationship);
assertNotNull(relationship.getSingleData());
List<Resource> included = document.getIncluded();
assertEquals(1, included.size());
}
use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class ResourceUpsert method setRelations.
protected void setRelations(Object newResource, RegistryEntry registryEntry, Resource resource, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, boolean ignoreMissing) {
if (resource.getRelationships() != null) {
for (Map.Entry<String, Relationship> entry : resource.getRelationships().entrySet()) {
String relationshipName = entry.getKey();
Relationship relationship = entry.getValue();
if (relationship != null) {
ResourceInformation resourceInformation = registryEntry.getResourceInformation();
ResourceField field = resourceInformation.findRelationshipFieldByName(relationshipName);
if (field == null && ignoreMissing) {
continue;
}
if (field == null) {
throw new ResourceException(String.format("Invalid relationship name: %s for %s", entry.getKey(), resourceInformation.getResourceType()));
}
if (field.isCollection()) {
// noinspection unchecked
setRelationsField(newResource, registryEntry, entry, queryAdapter, parameterProvider);
} else {
// noinspection unchecked
setRelationField(newResource, registryEntry, relationshipName, relationship, queryAdapter, parameterProvider);
}
}
}
}
}
use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class IncludeLookupSetter method setupRelation.
private List<Resource> setupRelation(Resource sourceResource, ResourceField relationshipField, Object targetEntity, QueryAdapter queryAdapter, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, ResourceMappingConfig resourceMappingConfig) {
// set the relation
String relationshipName = relationshipField.getJsonName();
Map<String, Relationship> relationships = sourceResource.getRelationships();
Relationship relationship = relationships.get(relationshipName);
if (targetEntity instanceof Iterable) {
List<Resource> targets = new ArrayList<>();
for (Object targetElement : (Iterable<?>) targetEntity) {
Resource targetResource = mergeResource(targetElement, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
targets.add(targetResource);
}
relationship.setData(Nullable.of((Object) util.toIds(targets)));
return targets;
} else {
Resource targetResource = mergeResource(targetEntity, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
relationship.setData(Nullable.of((Object) targetResource.toIdentifier()));
return Collections.singletonList(targetResource);
}
}
use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class IncludeLookupSetter method setupRelationId.
private void setupRelationId(Resource sourceResource, ResourceField relationshipField, Object targetEntityId) {
// set the relation
String relationshipName = relationshipField.getJsonName();
Map<String, Relationship> relationships = sourceResource.getRelationships();
Relationship relationship = relationships.get(relationshipName);
String oppositeType = relationshipField.getOppositeResourceType();
RegistryEntry entry = Objects.requireNonNull(resourceRegistry.getEntry(oppositeType));
ResourceInformation targetResourceInformation = entry.getResourceInformation();
if (targetEntityId instanceof Iterable) {
List<ResourceIdentifier> targetIds = new ArrayList<>();
for (Object targetElementId : (Iterable<?>) targetEntityId) {
targetIds.add(util.idToResourceId(targetResourceInformation, targetElementId));
}
relationship.setData(Nullable.of((Object) targetIds));
} else {
ResourceIdentifier targetResourceId = util.idToResourceId(targetResourceInformation, targetEntityId);
relationship.setData(Nullable.of((Object) targetResourceId));
}
}
Aggregations