@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Data<Resource> that = ((Relationship) o).getData(); if (that == null || data == null) { return that == data; } Collection<ResourceIdentifier> resourceIdentifiers = data.toResourceIdentifiers(); Collection<ResourceIdentifier> theirIdentifiers = that.toResourceIdentifiers(); return resourceIdentifiers.stream().allMatch(theirIdentifiers::contains); }
private boolean patch(Data<Resource> data, RequestScope requestScope) { boolean isUpdated; if (relationshipType.isToMany() && data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } if (relationshipType.isToMany()) { Collection<Resource> resources = data.get(); if (resources == null) { return false; } if (!resources.isEmpty()) { isUpdated = record.updateRelation(relationshipName, new Relationship(null, new Data<>(resources)).toPersistentResources(requestScope)); } else { isUpdated = record.clearRelation(relationshipName); } } else if (relationshipType.isToOne()) { if (data != null) { Resource resource = data.getSingleValue(); Relationship relationship = new Relationship(null, new Data<>(resource)); isUpdated = record.updateRelation(relationshipName, relationship.toPersistentResources(requestScope)); } else { isUpdated = record.clearRelation(relationshipName); } } else { throw new IllegalStateException("Bad relationship type"); } return isUpdated; }
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; } }
/** * Get relationship mappings. * * @param relationshipFunction a function to load the value of a relationship. Takes a string of the relationship * name and returns the relationship's value. * @return Relationship mapping */ protected Map<String, Relationship> getRelationshipsWithRelationshipFunction( final Function<String, Set<PersistentResource>> relationshipFunction) { final Map<String, Relationship> relationshipMap = new LinkedHashMap<>(); final Set<String> relationshipFields = filterFields(dictionary.getRelationships(obj)); for (String field : relationshipFields) { TreeMap<String, Resource> orderedById = new TreeMap<>(lengthFirstComparator); for (PersistentResource relationship : relationshipFunction.apply(field)) { orderedById.put(relationship.getId(), new ResourceIdentifier(relationship.getType(), relationship.getId()).castToResource()); } Collection<Resource> resources = orderedById.values(); Data<Resource> data; RelationshipType relationshipType = getRelationshipType(field); if (relationshipType.isToOne()) { data = resources.isEmpty() ? new Data<>((Resource) null) : new Data<>(resources.iterator().next()); } else { data = new Data<>(resources); } // TODO - links relationshipMap.put(field, new Relationship(null, data)); } return relationshipMap; }
Collection<ResourceIdentifier> resourceIdentifiers = resource.getRelationships().get("parents").getResourceIdentifierData().get(); ResourceIdentifier rId1 = resourceIdentifiers.iterator().next(); assertEquals(resource.getId(), "6"); resourceIdentifiers = resource.getRelationships().get("children").getResourceIdentifierData().get(); for (ResourceIdentifier resourceIdentifier : resourceIdentifiers) { hasIdentifier |= resourceIdentifier.getId().equals("6");
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; } }
/** * Get relationship mappings. * * @param relationshipFunction a function to load the value of a relationship. Takes a string of the relationship * name and returns the relationship's value. * @return Relationship mapping */ protected Map<String, Relationship> getRelationshipsWithRelationshipFunction( final Function<String, Set<PersistentResource>> relationshipFunction) { final Map<String, Relationship> relationshipMap = new LinkedHashMap<>(); final Set<String> relationshipFields = filterFields(dictionary.getRelationships(obj)); for (String field : relationshipFields) { TreeMap<String, Resource> orderedById = new TreeMap<>(lengthFirstComparator); for (PersistentResource relationship : relationshipFunction.apply(field)) { orderedById.put(relationship.getId(), new ResourceIdentifier(relationship.getType(), relationship.getId()).castToResource()); } Collection<Resource> resources = orderedById.values(); Data<Resource> data; RelationshipType relationshipType = getRelationshipType(field); if (relationshipType.isToOne()) { data = resources.isEmpty() ? new Data<>((Resource) null) : new Data<>(resources.iterator().next()); } else { data = new Data<>(resources); } // TODO - links relationshipMap.put(field, new Relationship(null, data)); } return relationshipMap; }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Data<Resource> that = ((Relationship) o).getData(); if (that == null || data == null) { return that == data; } Collection<ResourceIdentifier> resourceIdentifiers = data.toResourceIdentifiers(); Collection<ResourceIdentifier> theirIdentifiers = that.toResourceIdentifiers(); return resourceIdentifiers.stream().allMatch(theirIdentifiers::contains); }
private boolean patch(Data<Resource> data, RequestScope requestScope) { boolean isUpdated; if (relationshipType.isToMany() && data == null) { throw new InvalidEntityBodyException("Expected data but received null"); } if (relationshipType.isToMany()) { Collection<Resource> resources = data.get(); if (resources == null) { return false; } if (!resources.isEmpty()) { isUpdated = record.updateRelation(relationshipName, new Relationship(null, new Data<>(resources)).toPersistentResources(requestScope)); } else { isUpdated = record.clearRelation(relationshipName); } } else if (relationshipType.isToOne()) { if (data != null) { Resource resource = data.getSingleValue(); Relationship relationship = new Relationship(null, new Data<>(resource)); isUpdated = record.updateRelation(relationshipName, relationship.toPersistentResources(requestScope)); } else { isUpdated = record.clearRelation(relationshipName); } } else { throw new IllegalStateException("Bad relationship type"); } return isUpdated; }
Set<PersistentResource> resourceSet = (relationship == null) ? null : relationship.toPersistentResources(requestScope); pResource.updateRelation(fieldName, resourceSet);
Data<Resource> data = relationship.getData(); doc.setData(data);
Set<PersistentResource> resourceSet = (relationship == null) ? null : relationship.toPersistentResources(requestScope); pResource.updateRelation(fieldName, resourceSet);
Data<Resource> data = relationship.getData(); doc.setData(data);
@Test(priority = 1) public void testPatchAttrSingle() throws Exception { String request = jsonParser.getJson("/ResourceIT/testPatchAttrSingle.json"); given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .body(request) .patch("/parent/2") .then() .statusCode(HttpStatus.SC_NO_CONTENT) .header(HttpHeaders.CONTENT_LENGTH, (String) null); String actual = given() .contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE) .get("/parent/2") .then() .statusCode(HttpStatus.SC_OK) .contentType(JSONAPI_CONTENT_TYPE) .extract().response().asString(); JsonApiDocument doc = jsonApiMapper.readJsonApiDocument(actual); Data<Resource> data = doc.getData(); Resource resource = data.getSingleValue(); assertEquals(resource.getAttributes().get("firstName"), "syzygy"); assertEquals(resource.getRelationships().size(), 2); assertEquals(resource.getRelationships().get("children").getData().get().size(), 2); }
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();