class StateB implements State { private int count=0; public void writeName(StateContext stateContext, String name) { System.out.println(name.toUpperCase()); if(++count>1) { stateContext.setState(new StateA()); } } }
private Supplier<Pair<Integer, JsonNode>> handleRequest(StateContext state, BiFunction<Data<Resource>, RequestScope, Boolean> handler) { Data<Resource> data = state.getJsonApiDocument().getData(); handler.apply(data, state.getRequestScope()); // TODO: figure out if we've made modifications that differ from those requested by client return () -> Pair.of(HttpStatus.SC_NO_CONTENT, null); }
@Override public Supplier<Pair<Integer, JsonNode>> visitSubCollectionReadEntity(SubCollectionReadEntityContext ctx) { state.handle(ctx); return super.visitSubCollectionReadEntity(ctx); }
@Override public void handle(StateContext state, RootCollectionLoadEntitiesContext ctx) { String entityName = ctx.term().getText(); EntityDictionary dictionary = state.getRequestScope().getDictionary(); Class<?> entityClass = dictionary.getEntityClass(entityName); if (entityClass == null || !dictionary.isRoot(entityClass)) { throw new InvalidCollectionException(entityName); } state.setState(new CollectionTerminalState(entityClass, Optional.empty(), Optional.empty())); }
@Override public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) { ObjectMapper mapper = state.getRequestScope().getMapper().getObjectMapper(); return () -> Pair.of(HttpStatus.SC_OK, getResponseBody(record, state.getRequestScope())); }
@Override public Supplier<Pair<Integer, JsonNode>> visitQuery(QueryContext ctx) { return state.handleDelete(); } }
@Override public Supplier<Pair<Integer, JsonNode>> visitQuery(QueryContext ctx) { return state.handlePost(); } }
@Override public Supplier<Pair<Integer, JsonNode>> visitQuery(QueryContext ctx) { return state.handlePatch(); } }
@Override public Supplier<Pair<Integer, JsonNode>> visitQuery(QueryContext ctx) { return state.handleGet(); } }
public BaseVisitor(RequestScope requestScope) { state = new StateContext(new StartState(), requestScope); }
@Override public void handle(StateContext state, RootCollectionLoadEntitiesContext ctx) { String entityName = ctx.term().getText(); EntityDictionary dictionary = state.getRequestScope().getDictionary(); Class<?> entityClass = dictionary.getEntityClass(entityName); if (entityClass == null || !dictionary.isRoot(entityClass)) { throw new InvalidCollectionException(entityName); } state.setState(new CollectionTerminalState(entityClass, Optional.empty(), Optional.empty())); }
@Override public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) { ObjectMapper mapper = state.getRequestScope().getMapper().getObjectMapper(); return () -> Pair.of(HttpStatus.SC_OK, getResponseBody(record, state.getRequestScope())); }
@Override public Supplier<Pair<Integer, JsonNode>> visitQuery(QueryContext ctx) { return state.handleDelete(); } }
@Override public Supplier<Pair<Integer, JsonNode>> visitQuery(QueryContext ctx) { return state.handlePost(); } }
@Override public Supplier<Pair<Integer, JsonNode>> visitQuery(QueryContext ctx) { return state.handlePatch(); } }
@Override public Supplier<Pair<Integer, JsonNode>> visitQuery(QueryContext ctx) { return state.handleGet(); } }
public BaseVisitor(RequestScope requestScope) { state = new StateContext(new StartState(), requestScope); }
@Override public void handle(StateContext state, RootCollectionRelationshipContext ctx) { PersistentResource record = entityRecord(state, ctx.entity()); String relationName = ctx.relationship().term().getText(); try { Optional<FilterExpression> filterExpression = state.getRequestScope().getExpressionForRelation(record, relationName); record.getRelationCheckedFiltered(relationName, filterExpression, Optional.empty(), Optional.empty()); } catch (InvalidAttributeException e) { throw new InvalidCollectionException(relationName); } state.setState(new RelationshipTerminalState(record, relationName)); }
@Override public void handle(StateContext state, RootCollectionLoadEntityContext ctx) { PersistentResource record = entityRecord(state, ctx.entity()); state.setState(new RecordTerminalState(record)); }
private Supplier<Pair<Integer, JsonNode>> handleRequest(StateContext state, BiFunction<Data<Resource>, RequestScope, Boolean> handler) { Data<Resource> data = state.getJsonApiDocument().getData(); handler.apply(data, state.getRequestScope()); // TODO: figure out if we've made modifications that differ from those requested by client return () -> Pair.of(HttpStatus.SC_NO_CONTENT, null); }