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; } }
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; } }
/** * update the relationship between {@param parent} and the resource loaded by given {@param id} * @param entity Resource entity * @return {@link PersistentResource} object */ private PersistentResource updateRelationship(Entity entity) { Set<Entity.Relationship> relationshipEntities = entity.getRelationships(); PersistentResource resource = entity.toPersistentResource(); Set<PersistentResource> toUpdate; /* loop over each relationship */ for (Entity.Relationship relationship : relationshipEntities) { toUpdate = new LinkedHashSet<>(); for (Entity relation : relationship.getValue()) { toUpdate.add(relation.toPersistentResource()); } resource.updateRelation(relationship.getName(), toUpdate); } return resource; }
/** * update the relationship between {@param parent} and the resource loaded by given {@param id} * @param entity Resource entity * @return {@link PersistentResource} object */ private PersistentResource updateRelationship(Entity entity) { Set<Entity.Relationship> relationshipEntities = entity.getRelationships(); PersistentResource resource = entity.toPersistentResource(); Set<PersistentResource> toUpdate; /* loop over each relationship */ for (Entity.Relationship relationship : relationshipEntities) { toUpdate = new LinkedHashSet<>(); for (Entity relation : relationship.getValue()) { toUpdate.add(relation.toPersistentResource()); } resource.updateRelation(relationship.getName(), toUpdate); } return resource; }
/** * Add relation link from a given parent resource to a child resource. * * @param fieldName which relation link * @param newRelation the new relation */ public void addRelation(String fieldName, PersistentResource newRelation) { checkSharePermission(Collections.singleton(newRelation)); Object relation = this.getValueUnchecked(fieldName); if (relation instanceof Collection) { if (addToCollection((Collection) relation, fieldName, newRelation)) { this.markDirty(); } //Hook for updateToManyRelation transaction.updateToManyRelation(transaction, obj, fieldName, Sets.newHashSet(newRelation.getObject()), new LinkedHashSet<>(), requestScope); addInverseRelation(fieldName, newRelation.getObject()); } else { // Not a collection, but may be trying to create a ToOne relationship. // NOTE: updateRelation marks dirty. updateRelation(fieldName, Collections.singleton(newRelation)); } }
/** * Add relation link from a given parent resource to a child resource. * * @param fieldName which relation link * @param newRelation the new relation */ public void addRelation(String fieldName, PersistentResource newRelation) { checkSharePermission(Collections.singleton(newRelation)); Object relation = this.getValueUnchecked(fieldName); if (relation instanceof Collection) { if (addToCollection((Collection) relation, fieldName, newRelation)) { this.markDirty(); } //Hook for updateToManyRelation transaction.updateToManyRelation(transaction, obj, fieldName, Sets.newHashSet(newRelation.getObject()), new LinkedHashSet<>(), requestScope); addInverseRelation(fieldName, newRelation.getObject()); } else { // Not a collection, but may be trying to create a ToOne relationship. // NOTE: updateRelation marks dirty. updateRelation(fieldName, Collections.singleton(newRelation)); } }
? null : relationship.toPersistentResources(requestScope); pResource.updateRelation(fieldName, resourceSet);
? null : relationship.toPersistentResources(requestScope); pResource.updateRelation(fieldName, resourceSet);
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(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; }