private boolean patch(Resource resource, RequestScope requestScope) { boolean isUpdated = false; // Update attributes first Map<String, Object> attributes = resource.getAttributes(); if (attributes != null) { for (Map.Entry<String, Object> entry : attributes.entrySet()) { String fieldName = entry.getKey(); Object newVal = entry.getValue(); isUpdated |= record.updateAttribute(fieldName, newVal); } } // Relations next Map<String, Relationship> relationships = resource.getRelationships(); if (relationships != null) { for (Map.Entry<String, Relationship> entry : relationships.entrySet()) { String fieldName = entry.getKey(); Relationship relationship = entry.getValue(); Set<PersistentResource> resources = (relationship == null) ? null : relationship.toPersistentResources(requestScope); isUpdated |= record.updateRelation(fieldName, resources); } } return isUpdated; } }
/** * Fetch a resource with support for lambda function for getting relationships and attributes. * @param relationshipSupplier The relationship supplier (getRelationships()) * @param attributeSupplier The attribute supplier * @return The Resource */ public Resource toResource(final Supplier<Map<String, Relationship>> relationshipSupplier, final Supplier<Map<String, Object>> attributeSupplier) { final Resource resource = new Resource(type, (obj == null) ? uuid.orElseThrow( () -> new InvalidEntityBodyException("No id found on object")) : dictionary.getId(obj)); resource.setRelationships(relationshipSupplier.get()); resource.setAttributes(attributeSupplier.get()); return resource; }
/** * Clear all properties except the relationships. */ private static void clearAllExceptRelationships(Resource resource) { resource.setAttributes(null); resource.setLinks(null); resource.setMeta(null); }
String id = resource.getId(); Class<?> newObjectClass = requestScope.getDictionary().getEntityClass(resource.getType()); throw new UnknownEntityException("Entity " + resource.getType() + " not found"); throw new InvalidValueException("Cannot assign value of type: " + resource.getType() + " to type: " + entityClass); parent.orElse(null), newObjectClass, requestScope, Optional.ofNullable(id)); Map<String, Object> attributes = resource.getAttributes(); if (attributes != null) { for (Map.Entry<String, Object> entry : attributes.entrySet()) { Map<String, Relationship> relationships = resource.getRelationships(); if (relationships != null) { for (Map.Entry<String, Relationship> entry : relationships.entrySet()) {
Data<Resource> data = doc.getData(); Resource resource = data.getSingleValue(); Iterator<Resource> itr = resource.getRelationships().get("children").getData().get().iterator(); String rel1 = itr.next().getId(); String rel2 = itr.next().getId(); assertEquals(resource.getAttributes().get("firstName"), "Unknown"); assertEquals(id1, "4"); assertEquals(id2, "5");
Collection<ResourceIdentifier> resourceIdentifiers = resource.getRelationships().get("parents").getResourceIdentifierData().get(); ResourceIdentifier rId1 = resourceIdentifiers.iterator().next(); assertEquals(resource.getId(), "6"); assertEquals(resource.getType(), "child"); assertEquals(resource.getRelationships().size(), 2); assertEquals(resourceIdentifiers.size(), 2); assertEquals(rId1.getType(), "parent"); resourceIdentifiers = resource.getRelationships().get("children").getResourceIdentifierData().get(); for (ResourceIdentifier resourceIdentifier : resourceIdentifiers) { hasIdentifier |= resourceIdentifier.getId().equals("6");
/** * Remove data via patch extension. */ private Supplier<Pair<Integer, JsonNode>> handleRemoveOp(String path, JsonNode patchValue, PatchRequestScope requestScope) { try { JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue); String fullPath; if (path.contains("relationships")) { // Reserved keyword for relationships fullPath = path; } else { Data<Resource> data = value.getData(); if (data == null || data.get() == null) { fullPath = path; } else { Collection<Resource> resources = data.get(); String id = getSingleResource(resources).getId(); fullPath = path + "/" + id; } } DeleteVisitor visitor = new DeleteVisitor( new PatchRequestScope(path, value, requestScope)); return visitor.visit(JsonApiParser.parse(fullPath)); } catch (IOException e) { throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchValue); } }
String id = getSingleResource(resources).getId(); String fullPath = path + "/" + id; getSingleResource(resources).setRelationships(null);
Optional<MultivaluedMap<String, String>> queryParams = requestScope.getQueryParams(); Map<String, Relationship> relationships = record.toResourceWithSortingAndPagination().getRelationships(); Relationship relationship = null; if (relationships != null) {
private boolean post(Data<Resource> data, RequestScope requestScope) { if (data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } Collection<Resource> resources = data.get(); if (resources == null) { return false; } resources.stream().forEachOrdered(resource -> record.addRelation(relationshipName, resource.toPersistentResource(requestScope))); return true; }
public Resource castToResource() { return new Resource(type, id); }
String id = resource.getId(); Class<?> newObjectClass = requestScope.getDictionary().getEntityClass(resource.getType()); throw new UnknownEntityException("Entity " + resource.getType() + " not found"); throw new InvalidValueException("Cannot assign value of type: " + resource.getType() + " to type: " + entityClass); parent.orElse(null), newObjectClass, requestScope, Optional.ofNullable(id)); Map<String, Object> attributes = resource.getAttributes(); if (attributes != null) { for (Map.Entry<String, Object> entry : attributes.entrySet()) { Map<String, Relationship> relationships = resource.getRelationships(); if (relationships != null) { for (Map.Entry<String, Relationship> entry : relationships.entrySet()) {
/** * Remove data via patch extension. */ private Supplier<Pair<Integer, JsonNode>> handleRemoveOp(String path, JsonNode patchValue, PatchRequestScope requestScope) { try { JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue); String fullPath; if (path.contains("relationships")) { // Reserved keyword for relationships fullPath = path; } else { Data<Resource> data = value.getData(); if (data == null || data.get() == null) { fullPath = path; } else { Collection<Resource> resources = data.get(); String id = getSingleResource(resources).getId(); fullPath = path + "/" + id; } } DeleteVisitor visitor = new DeleteVisitor( new PatchRequestScope(path, value, requestScope)); return visitor.visit(JsonApiParser.parse(fullPath)); } catch (IOException e) { throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchValue); } }
String id = getSingleResource(resources).getId(); String fullPath = path + "/" + id; getSingleResource(resources).setRelationships(null);
Optional<MultivaluedMap<String, String>> queryParams = requestScope.getQueryParams(); Map<String, Relationship> relationships = record.toResourceWithSortingAndPagination().getRelationships(); Relationship relationship = null; if (relationships != null) {
private boolean post(Data<Resource> data, RequestScope requestScope) { if (data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } Collection<Resource> resources = data.get(); if (resources == null) { return false; } resources.stream().forEachOrdered(resource -> record.addRelation(relationshipName, resource.toPersistentResource(requestScope))); return true; }
public Resource castToResource() { return new Resource(type, id); }
private boolean patch(Resource resource, RequestScope requestScope) { boolean isUpdated = false; // Update attributes first Map<String, Object> attributes = resource.getAttributes(); if (attributes != null) { for (Map.Entry<String, Object> entry : attributes.entrySet()) { String fieldName = entry.getKey(); Object newVal = entry.getValue(); isUpdated |= record.updateAttribute(fieldName, newVal); } } // Relations next Map<String, Relationship> relationships = resource.getRelationships(); if (relationships != null) { for (Map.Entry<String, Relationship> entry : relationships.entrySet()) { String fieldName = entry.getKey(); Relationship relationship = entry.getValue(); Set<PersistentResource> resources = (relationship == null) ? null : relationship.toPersistentResources(requestScope); isUpdated |= record.updateRelation(fieldName, resources); } } return isUpdated; } }
@Override public Supplier<Pair<Integer, JsonNode>> handlePatch(StateContext state) { JsonApiDocument jsonApiDocument = state.getJsonApiDocument(); Data<Resource> data = jsonApiDocument.getData(); if (data == null) { throw new InvalidEntityBodyException("Expected data but found null"); } if (!data.isToOne()) { throw new InvalidEntityBodyException("Expected single element but found list"); } Resource resource = data.getSingleValue(); if (!record.matchesId(resource.getId())) { throw new InvalidEntityBodyException("Id in response body does not match requested id to update from path"); } patch(resource, state.getRequestScope()); return constructPatchResponse(record, state); }
/** * Clear all properties except the relationships. */ private static void clearAllExceptRelationships(Resource resource) { resource.setAttributes(null); resource.setLinks(null); resource.setMeta(null); }