private boolean delete(Data<Resource> data, RequestScope requestScope) { if (data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } Collection<Resource> resources = data.get(); if (resources == null || resources.isEmpty()) { // As per: http://jsonapi.org/format/#crud-updating-relationship-responses-403 throw new ForbiddenAccessException("Unknown update"); } resources.stream().forEachOrdered(resource -> record.removeRelation(relationshipName, resource.toPersistentResource(requestScope))); return true; } }
private boolean delete(Data<Resource> data, RequestScope requestScope) { if (data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } Collection<Resource> resources = data.get(); if (resources == null || resources.isEmpty()) { // As per: http://jsonapi.org/format/#crud-updating-relationship-responses-403 throw new ForbiddenAccessException("Unknown update"); } resources.stream().forEachOrdered(resource -> record.removeRelation(relationshipName, resource.toPersistentResource(requestScope))); return true; } }
/** * Removes a relationship, or deletes a root level resource * @param context Environment encapsulating graphQL's request environment * @return set of removed {@link PersistentResource} object(s) */ private Object removeObjects(Environment context) { /* sanity check for id and data argument w REPLACE */ if (context.data.isPresent()) { throw new BadRequestException("REPLACE must not include data argument"); } if (!context.ids.isPresent()) { throw new BadRequestException("REPLACE must include ids argument"); } ConnectionContainer connection = (ConnectionContainer) fetchObjects(context); Set<PersistentResource> toRemove = connection.getPersistentResources(); if (!context.isRoot()) { /* has parent */ toRemove.forEach(item -> context.parentResource.removeRelation(context.field.getName(), item)); } else { /* is root */ toRemove.forEach(PersistentResource::deleteResource); } return new ConnectionContainer( Collections.emptySet(), Optional.empty(), connection.getTypeName() ); }
/** * Removes a relationship, or deletes a root level resource * @param context Environment encapsulating graphQL's request environment * @return set of removed {@link PersistentResource} object(s) */ private Object removeObjects(Environment context) { /* sanity check for id and data argument w REPLACE */ if (context.data.isPresent()) { throw new BadRequestException("REPLACE must not include data argument"); } if (!context.ids.isPresent()) { throw new BadRequestException("REPLACE must include ids argument"); } ConnectionContainer connection = (ConnectionContainer) fetchObjects(context); Set<PersistentResource> toRemove = connection.getPersistentResources(); if (!context.isRoot()) { /* has parent */ toRemove.forEach(item -> context.parentResource.removeRelation(context.field.getName(), item)); } else { /* is root */ toRemove.forEach(PersistentResource::deleteResource); } return new ConnectionContainer( Collections.emptySet(), Optional.empty(), connection.getTypeName() ); }
/** * Replaces a resource, updates given resource and deletes the rest * belonging to the the same type/relationship family. * @param context Environment encapsulating graphQL's request environment * @return set of replaced {@link PersistentResource} object(s) */ private ConnectionContainer replaceObjects(Environment context) { /* sanity check for id and data argument w REPLACE */ if (!context.data.isPresent()) { throw new BadRequestException("REPLACE must include data argument"); } if (context.ids.isPresent()) { throw new BadRequestException("REPLACE must not include ids argument"); } ConnectionContainer existingObjects = (ConnectionContainer) context.container.processFetch(context, this); ConnectionContainer upsertedObjects = upsertObjects(context); Set<PersistentResource> toDelete = Sets.difference(existingObjects.getPersistentResources(), upsertedObjects.getPersistentResources()); if (!context.isRoot()) { /* has parent */ toDelete.forEach(item -> context.parentResource.removeRelation(context.field.getName(), item)); } else { /* is root */ toDelete.forEach(PersistentResource::deleteResource); } return upsertedObjects; }
/** * Replaces a resource, updates given resource and deletes the rest * belonging to the the same type/relationship family. * @param context Environment encapsulating graphQL's request environment * @return set of replaced {@link PersistentResource} object(s) */ private ConnectionContainer replaceObjects(Environment context) { /* sanity check for id and data argument w REPLACE */ if (!context.data.isPresent()) { throw new BadRequestException("REPLACE must include data argument"); } if (context.ids.isPresent()) { throw new BadRequestException("REPLACE must not include ids argument"); } ConnectionContainer existingObjects = (ConnectionContainer) context.container.processFetch(context, this); ConnectionContainer upsertedObjects = upsertObjects(context); Set<PersistentResource> toDelete = Sets.difference(existingObjects.getPersistentResources(), upsertedObjects.getPersistentResources()); if (!context.isRoot()) { /* has parent */ toDelete.forEach(item -> context.parentResource.removeRelation(context.field.getName(), item)); } else { /* is root */ toDelete.forEach(PersistentResource::deleteResource); } return upsertedObjects; }