Exemplo n.º 1
0
  private boolean patch(Data<Resource> data, RequestScope requestScope) {
    boolean isUpdated;

    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()) {
      Resource resource = data.get().iterator().next();
      if (resource != null) {
        isUpdated =
            record.updateRelation(
                relationshipName,
                new Relationship(null, new Data<>(resource)).toPersistentResources(requestScope));
      } else {
        isUpdated = record.clearRelation(relationshipName);
      }
    } else {
      throw new IllegalStateException("Bad relationship type");
    }

    return isUpdated;
  }
Exemplo n.º 2
0
 private boolean post(Data<Resource> data, RequestScope requestScope) {
   Collection<Resource> resources = data.get();
   if (resources == null) {
     return false;
   }
   resources
       .stream()
       .forEachOrdered(
           resource ->
               record.addRelation(relationshipName, resource.toPersistentResource(requestScope)));
   return true;
 }