use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.
the class RelationshipRepositoryBase method addRelations.
@Override
public void addRelations(T source, Iterable<J> targetIds, String fieldName) {
RegistryEntry sourceEntry = getSourceEntry();
ResourceRepositoryAdapter<T, I> sourceAdapter = sourceEntry.getResourceRepository();
ResourceInformation sourceInformation = getSourceEntry().getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
if (field.hasIdField()) {
Collection currentIds = (Collection) field.getIdAccessor().getValue(source);
currentIds.addAll((Collection) targetIds);
} else {
RegistryEntry targetEntry = getTargetEntry(field);
Iterable<D> targets = getTargets(targetEntry, targetIds);
@SuppressWarnings("unchecked") Collection<D> currentTargets = getOrCreateCollection(source, fieldName);
for (D target : targets) {
currentTargets.add(target);
}
}
sourceAdapter.update(source, getSaveQueryAdapter(fieldName));
}
use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.
the class RelationshipRepositoryBase method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = resourceRegistry.findEntry(sourceResourceClass);
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
RegistryEntry targetEntry = getTargetEntry(field);
String oppositeName = getOppositeName(fieldName);
QuerySpec idQuerySpec = querySpec.duplicate();
idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeName, sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
idQuerySpec.includeRelation(Arrays.asList(oppositeName));
ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(new QuerySpecAdapter(idQuerySpec, resourceRegistry));
List<D> results = (List<D>) response.getEntity();
MultivaluedMap<I, D> bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
Set<I> sourceIdSet = new HashSet<>();
for (I sourceId : sourceIds) {
sourceIdSet.add(sourceId);
}
for (D result : results) {
handleTarget(bulkResult, result, sourceIdSet, oppositeName, sourceInformation);
}
return bulkResult;
}
use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.
the class ForwardingStrategyContext method createSourceQuerySpec.
private QuerySpec createSourceQuerySpec() {
RegistryEntry sourceEntry = getSourceEntry();
ResourceInformation resourceInformation = sourceEntry.getResourceInformation();
return new QuerySpec(resourceInformation.getResourceClass(), resourceInformation.getResourceType());
}
use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.
the class RegistryEntry method getParentRegistryEntry.
public RegistryEntry getParentRegistryEntry() {
ResourceInformation resourceInformation = getResourceInformation();
String superResourceType = resourceInformation.getSuperResourceType();
if (superResourceType != null) {
ResourceRegistry resourceRegistry = moduleRegistry.getResourceRegistry();
return resourceRegistry.getEntry(superResourceType);
}
return parentRegistryEntry;
}
use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.
the class ResourceFilterTest method checkFilterGetOnResourceField.
@Test
public void checkFilterGetOnResourceField() {
// setup test data
RegistryEntry entry = resourceRegistry.getEntry(Task.class);
ResourceRepositoryAdapter resourceRepository = entry.getResourceRepository();
Project project = new Project();
project.setId(13L);
project.setName("myProject");
Task task = new Task();
task.setId(12L);
task.setName("myTask");
task.setProject(project);
resourceRepository.create(task, new QuerySpecAdapter(new QuerySpec(Task.class), resourceRegistry));
// get information
ResourceInformation resourceInformation = entry.getResourceInformation();
ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
ResourceField nameField = resourceInformation.findFieldByUnderlyingName("name");
String path = "/tasks/";
String method = HttpMethod.GET.toString();
Map<String, Set<String>> parameters = Collections.emptyMap();
Document requestBody = null;
// forbid field
Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
Response response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
Resource taskResource = response.getDocument().getCollectionData().get().get(0);
Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
Assert.assertTrue(taskResource.getAttributes().containsKey("name"));
// allow resource
Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
taskResource = response.getDocument().getCollectionData().get().get(0);
Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
Assert.assertFalse(taskResource.getAttributes().containsKey("name"));
}
Aggregations