@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); } }
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); }
@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)); }