Exemple #1
0
 @Override
 public void handle(StateContext state, SubCollectionReadCollectionContext ctx) {
   String subCollection = ctx.term().getText();
   EntityDictionary dictionary = state.getRequestScope().getDictionary();
   try {
     Set<PersistentResource> collection = resource.getRelation(subCollection); // Check if exists.
     String entityName =
         dictionary.getBinding(
             dictionary.getParameterizedType(resource.getObject(), subCollection));
     Class<?> entityClass = dictionary.getBinding(entityName);
     if (entityClass == null) {
       throw new IllegalArgumentException("Unknown type " + entityName);
     }
     final BaseState nextState;
     if (collection instanceof SingleElementSet) {
       PersistentResource record = collection.iterator().next();
       nextState = new RecordTerminalState(subCollection, record);
     } else {
       nextState =
           new CollectionTerminalState(
               entityClass, Optional.of(resource), Optional.of(subCollection));
     }
     state.setState(nextState);
   } catch (InvalidAttributeException e) {
     throw new InvalidCollectionException(subCollection);
   }
 }
 @Test
 public void firstLower() {
   String upperName1 = stateContext.writeName("tester");
   assertThat(upperName1, is("TESTER"));
   String lowerName1 = stateContext.writeName("tester");
   assertThat(lowerName1, is("tester"));
 }
 private Supplier<Pair<Integer, JsonNode>> handleRequest(
     StateContext state, BiFunction<Data<Resource>, RequestScope, Boolean> handler) {
   Data<Resource> data = state.getJsonApiDocument().getData();
   if (data == null) {
     throw new InvalidEntityBodyException("Expected data but received null");
   }
   handler.apply(data, state.getRequestScope());
   return () -> Pair.of(HttpStatus.SC_NO_CONTENT, null);
 }
Exemple #4
0
 @Override
 public void handle(StateContext state, SubCollectionSubCollectionContext ctx) {
   String id = ctx.entity().id().getText();
   String subCollection = ctx.entity().term().getText();
   try {
     state.setState(new RecordState(resource.getRelation(subCollection, id)));
   } catch (InvalidAttributeException e) {
     throw new InvalidCollectionException(subCollection);
   }
 }
Exemple #5
0
 @Override
 public void handle(StateContext state, SubCollectionToOneContext ctx) {
   String subCollection = ctx.term().getText();
   Set<PersistentResource> single = resource.getRelation(subCollection);
   if (single instanceof SingleElementSet) {
     state.setState(new RecordState(single.iterator().next()));
   } else {
     throw new InvalidCollectionException(
         "Expected single element but found list for '%s'", subCollection);
   }
 }
Exemple #6
0
  @Override
  public void handle(StateContext state, SubCollectionReadEntityContext ctx) {
    String id = ctx.entity().id().getText();
    String subCollection = ctx.entity().term().getText();

    try {
      PersistentResource nextRecord = resource.getRelation(subCollection, id);
      state.setState(new RecordTerminalState(subCollection, nextRecord));
    } catch (InvalidAttributeException e) {
      throw new InvalidCollectionException(subCollection);
    }
  }
Exemple #7
0
  @Override
  public void handle(StateContext state, SubCollectionRelationshipContext ctx) {
    String id = ctx.entity().id().getText();
    String subCollection = ctx.entity().term().getText();
    User user = state.getRequestScope().getUser();

    PersistentResource childRecord;
    try {
      childRecord = resource.getRelation(subCollection, id);
    } catch (InvalidAttributeException e) {
      throw new InvalidCollectionException(subCollection);
    }

    String relationName = ctx.relationship().term().getText();
    try {
      childRecord.getRelation(relationName);
    } catch (InvalidAttributeException e) {
      throw new InvalidCollectionException(relationName);
    }

    state.setState(new RelationshipTerminalState(childRecord, relationName));
  }
  @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));
  }