Пример #1
0
  @Override
  public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
    JsonApiDocument doc = new JsonApiDocument();
    RequestScope requestScope = state.getRequestScope();
    ObjectMapper mapper = requestScope.getMapper().getObjectMapper();
    Optional<MultivaluedMap<String, String>> queryParams = requestScope.getQueryParams();

    Map<String, Relationship> relationships = record.toResource().getRelationships();
    Relationship relationship = null;
    if (relationships != null) {
      relationship = relationships.get(relationshipName);
    }

    // Handle valid relationship
    if (relationship != null) {

      // Set data
      Data<Resource> data = relationship.getData();
      doc.setData(data);

      // Run include processor
      DocumentProcessor includedProcessor = new IncludedProcessor();
      includedProcessor.execute(doc, record, queryParams);

      return () -> Pair.of(HttpStatus.SC_OK, mapper.convertValue(doc, JsonNode.class));
    }

    // Handle no data for relationship
    if (relationshipType.isToMany()) {
      doc.setData(new Data<>(new ArrayList<>()));
    } else if (relationshipType.isToOne()) {
      doc.setData(new Data<>((Resource) null));
    } else {
      throw new IllegalStateException("Failed to PATCH a relationship");
    }
    return () -> Pair.of(HttpStatus.SC_OK, mapper.convertValue(doc, JsonNode.class));
  }
Пример #2
0
 /**
  * Special copy constructor for use by PatchRequestScope.
  *
  * @param jsonApiDocument the json api document
  * @param outerRequestScope the outer request scope
  */
 protected RequestScope(
     String path, JsonApiDocument jsonApiDocument, RequestScope outerRequestScope) {
   this.jsonApiDocument = jsonApiDocument;
   this.path = path;
   this.transaction = outerRequestScope.transaction;
   this.user = outerRequestScope.user;
   this.dictionary = outerRequestScope.dictionary;
   this.mapper = outerRequestScope.mapper;
   this.auditLogger = outerRequestScope.auditLogger;
   this.queryParams = Optional.empty();
   this.sparseFields = Collections.emptyMap();
   this.sorting = Sorting.getDefaultEmptyInstance();
   this.pagination = Pagination.getDefaultPagination();
   this.objectEntityCache = outerRequestScope.objectEntityCache;
   this.securityMode = outerRequestScope.securityMode;
   this.newPersistentResources = outerRequestScope.newPersistentResources;
   this.commitTriggers = outerRequestScope.commitTriggers;
   this.permissionExecutor = outerRequestScope.getPermissionExecutor();
   this.dirtyResources = outerRequestScope.dirtyResources;
   this.filterDialect = outerRequestScope.filterDialect;
   this.expressionsByType = outerRequestScope.expressionsByType;
   this.useFilterExpressions = outerRequestScope.useFilterExpressions;
 }