use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class DependencyOrderStrategyTest method addManyDependency.
private void addManyDependency(Operation op1, Operation op2, String relationshipName) {
Resource resource1 = op1.getValue();
Resource resource2 = op2.getValue();
Relationship relationship = resource1.getRelationships().get(relationshipName);
if (relationship == null) {
relationship = new Relationship();
relationship.setData((Nullable) Nullable.of(new ArrayList<ResourceIdentifier>()));
resource1.getRelationships().put(relationshipName, relationship);
}
ResourceIdentifier resourceId = new ResourceIdentifier(resource2.getId(), resource2.getType());
relationship.getCollectionData().get().add(resourceId);
}
use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class OperationsDeleteRelationTest method testDeleteRelation.
@Test
public void testDeleteRelation() {
PersonEntity person1 = newPerson("1");
PersonEntity person2 = newPerson("2");
MovieEntity movie = newMovie("test");
movie.setDirectors(new HashSet<>(Arrays.asList(person1, person2)));
OperationsCall insertCall = operationsClient.createCall();
insertCall.add(HttpMethod.POST, movie);
insertCall.add(HttpMethod.POST, person1);
insertCall.add(HttpMethod.POST, person2);
insertCall.execute();
QuerySpec querySpec = new QuerySpec(MovieEntity.class);
querySpec.includeRelation(Arrays.asList("directors"));
MovieEntity updatedMovie = movieRepo.findOne(movie.getId(), querySpec);
Set<PersonEntity> directors = updatedMovie.getDirectors();
PersonEntity deletedDirector = directors.iterator().next();
directors.remove(deletedDirector);
OperationsCall call = operationsClient.createCall();
call.add(HttpMethod.PATCH, updatedMovie);
call.add(HttpMethod.DELETE, deletedDirector);
call.execute();
// check whether updated relationship is included in the response
Resource movieResource = call.getResponse(0).getSingleData().get();
Relationship directorsRelationship = movieResource.getRelationships().get("directors");
List<ResourceIdentifier> directorIds = directorsRelationship.getCollectionData().get();
Assert.assertEquals(1, directorIds.size());
}
use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class ResourceUpsert method setRelationsField.
protected void setRelationsField(Object newResource, RegistryEntry registryEntry, Map.Entry<String, Relationship> property, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider) {
Relationship relationship = property.getValue();
if (relationship.getData().isPresent()) {
String propertyName = property.getKey();
ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(propertyName);
List<ResourceIdentifier> relationshipIds = relationship.getCollectionData().get();
for (ResourceModificationFilter filter : modificationFilters) {
relationshipIds = filter.modifyManyRelationship(newResource, relationshipField, ResourceRelationshipModificationType.SET, relationshipIds);
}
List relationshipTypedIds = new LinkedList<>();
for (ResourceIdentifier resourceId : relationshipIds) {
RegistryEntry entry = resourceRegistry.getEntry(resourceId.getType());
Class idFieldType = entry.getResourceInformation().getIdField().getType();
Serializable typedRelationshipId = parseId(resourceId, idFieldType);
relationshipTypedIds.add(typedRelationshipId);
}
if (relationshipField.hasIdField()) {
relationshipField.getIdAccessor().setValue(newResource, relationshipTypedIds);
}
// TODO batch fetchRelatedObject
if (decideSetRelationObjectsField(relationshipField)) {
List relationshipEntities = new LinkedList<>();
for (int i = 0; i < relationshipIds.size(); i++) {
ResourceIdentifier resourceId = relationshipIds.get(i);
Serializable typedRelationshipId = (Serializable) relationshipTypedIds.get(i);
RegistryEntry entry = resourceRegistry.getEntry(resourceId.getType());
Object relationObject = fetchRelatedObject(entry, typedRelationshipId, parameterProvider, queryAdapter);
relationshipEntities.add(relationObject);
}
relationshipField.getAccessor().setValue(newResource, relationshipEntities);
}
}
}
use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class IncludeLookupSetter method lookupRelatedResourcesWithId.
private Set<Resource> lookupRelatedResourcesWithId(Collection<Resource> sourceResources, ResourceField relationshipField, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, ResourceMappingConfig resourceMappingConfig) {
String oppositeResourceType = relationshipField.getOppositeResourceType();
RegistryEntry oppositeEntry = resourceRegistry.getEntry(oppositeResourceType);
if (oppositeEntry == null) {
throw new RepositoryNotFoundException("no resource with type " + oppositeResourceType + " found");
}
ResourceInformation oppositeResourceInformation = oppositeEntry.getResourceInformation();
ResourceRepositoryAdapter oppositeResourceRepository = oppositeEntry.getResourceRepository(parameterProvider);
if (oppositeResourceRepository == null) {
throw new RepositoryNotFoundException("no relationship repository found for " + oppositeResourceInformation.getResourceType());
}
Set<Resource> related = new HashSet<>();
Set<Object> relatedIdsToLoad = new HashSet<>();
for (Resource sourceResource : sourceResources) {
Relationship relationship = sourceResource.getRelationships().get(relationshipField.getJsonName());
PreconditionUtil.assertTrue("expected relationship data to be loaded for @JsonApiResourceId annotated field", relationship.getData().isPresent());
if (relationship.getData().get() != null) {
for (ResourceIdentifier id : relationship.getCollectionData().get()) {
if (resourceMap.containsKey(id)) {
// load from cache
related.add(resourceMap.get(id));
} else {
relatedIdsToLoad.add(oppositeResourceInformation.parseIdString(id.getId()));
}
}
}
}
if (!relatedIdsToLoad.isEmpty()) {
JsonApiResponse response = oppositeResourceRepository.findAll(relatedIdsToLoad, queryAdapter);
Collection responseList = (Collection) response.getEntity();
for (Object responseEntity : responseList) {
Resource relatedResource = mergeResource(responseEntity, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
related.add(relatedResource);
Object responseEntityId = oppositeResourceInformation.getId(responseEntity);
relatedIdsToLoad.remove(responseEntityId);
}
if (!relatedIdsToLoad.isEmpty()) {
throw new ResourceNotFoundException("type=" + relationshipField.getOppositeResourceType() + ", " + "ids=" + relatedIdsToLoad);
}
}
return related;
}
use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class RawResourceFieldAccessor method getValue.
@Override
public Object getValue(Object objResource) {
Resource resource = (Resource) objResource;
switch(fieldType) {
case ID:
return resource.getId();
case ATTRIBUTE:
JsonNode jsonNode = resource.getAttributes().get(fieldName);
return toObject(jsonNode);
case RELATIONSHIP:
Map<String, Relationship> relationships = resource.getRelationships();
Relationship relationship = relationships.get(fieldName);
if (relationship != null && relationship.getData().isPresent()) {
return relationship.getData().get();
}
return null;
case META_INFORMATION:
return toObject(resource.getMeta());
default:
PreconditionUtil.assertEquals("invalid type", fieldType, ResourceFieldType.LINKS_INFORMATION);
return toObject(resource.getLinks());
}
}
Aggregations