/** * Fetch a resource with support for lambda function for getting relationships and attributes. * @return The Resource */ public Resource toResourceWithSortingAndPagination() { return toResource(this::getRelationshipsWithSortingAndPagination, this::getAttributes); }
/** * Convert a persistent resource to a resource. * * @return a resource */ public Resource toResource() { return toResource(this::getRelationships, this::getAttributes); }
/** * Convert a persistent resource to a resource. * * @return a resource */ public Resource toResource() { return toResource(this::getRelationships, this::getAttributes); }
/** * Fetch a resource with support for lambda function for getting relationships and attributes. * @return The Resource */ public Resource toResourceWithSortingAndPagination() { return toResource(this::getRelationshipsWithSortingAndPagination, this::getAttributes); }
/** * Adds all the relation resources for a given relation path to the included block of the * JsonApiDocument. */ private void addResourcesForPath(JsonApiDocument jsonApiDocument, PersistentResource<?> rec, List<String> relationPath) { //Pop off a relation of relation path String relation = relationPath.remove(0); Optional<FilterExpression> filterExpression = rec.getRequestScope().getExpressionForRelation(rec, relation); Set<PersistentResource> collection; try { collection = rec.getRelationCheckedFiltered(relation, filterExpression, Optional.empty(), Optional.empty()); } catch (ForbiddenAccessException e) { return; } collection.forEach(resource -> { jsonApiDocument.addIncluded(resource.toResource()); //If more relations left in the path, process a level deeper if (!relationPath.isEmpty()) { //Use a copy of the relationPath to preserve the path for remaining branches of the relationship tree addResourcesForPath(jsonApiDocument, resource, new ArrayList<>(relationPath)); } }); }
/** * Adds all the relation resources for a given relation path to the included block of the * JsonApiDocument. */ private void addResourcesForPath(JsonApiDocument jsonApiDocument, PersistentResource<?> rec, List<String> relationPath) { //Pop off a relation of relation path String relation = relationPath.remove(0); Optional<FilterExpression> filterExpression = rec.getRequestScope().getExpressionForRelation(rec, relation); Set<PersistentResource> collection; try { collection = rec.getRelationCheckedFiltered(relation, filterExpression, Optional.empty(), Optional.empty()); } catch (ForbiddenAccessException e) { return; } collection.forEach(resource -> { jsonApiDocument.addIncluded(resource.toResource()); //If more relations left in the path, process a level deeper if (!relationPath.isEmpty()) { //Use a copy of the relationPath to preserve the path for remaining branches of the relationship tree addResourcesForPath(jsonApiDocument, resource, new ArrayList<>(relationPath)); } }); }
@Override public Supplier<Pair<Integer, JsonNode>> handlePost(StateContext state) { RequestScope requestScope = state.getRequestScope(); JsonApiMapper mapper = requestScope.getMapper(); newObject = createObject(requestScope); parent.ifPresent(persistentResource -> persistentResource.addRelation(relationName.get(), newObject)); return () -> { JsonApiDocument returnDoc = new JsonApiDocument(); returnDoc.setData(new Data(newObject.toResource())); JsonNode responseBody = mapper.getObjectMapper().convertValue(returnDoc, JsonNode.class); return Pair.of(HttpStatus.SC_CREATED, responseBody); }; }
@Override public Supplier<Pair<Integer, JsonNode>> handlePost(StateContext state) { RequestScope requestScope = state.getRequestScope(); JsonApiMapper mapper = requestScope.getMapper(); newObject = createObject(requestScope); parent.ifPresent(persistentResource -> persistentResource.addRelation(relationName.get(), newObject)); return () -> { JsonApiDocument returnDoc = new JsonApiDocument(); returnDoc.setData(new Data(newObject.toResource())); JsonNode responseBody = mapper.getObjectMapper().convertValue(returnDoc, JsonNode.class); return Pair.of(HttpStatus.SC_CREATED, responseBody); }; }
/** * Assign provided id if id field is not generated. * * @param persistentResource resource * @param id resource id */ private static void assignId(PersistentResource persistentResource, String id) { //If id field is not a `@GeneratedValue` persist the provided id if (!persistentResource.isIdGenerated()) { if (id != null && !id.isEmpty()) { persistentResource.setId(id); } else { //If expecting id to persist and id is not present, throw exception throw new InvalidValueException( persistentResource.toResource(), "No id provided, cannot persist " + persistentResource.getObject() ); } } } }
/** * Assign provided id if id field is not generated. * * @param persistentResource resource * @param id resource id */ private static void assignId(PersistentResource persistentResource, String id) { //If id field is not a `@GeneratedValue` persist the provided id if (!persistentResource.isIdGenerated()) { if (id != null && !id.isEmpty()) { persistentResource.setId(id); } else { //If expecting id to persist and id is not present, throw exception throw new InvalidValueException( persistentResource.toResource(), "No id provided, cannot persist " + persistentResource.getObject() ); } } } }
protected static JsonNode getResponseBody(PersistentResource resource, RequestScope requestScope) { Optional<MultivaluedMap<String, String>> queryParams = requestScope.getQueryParams(); JsonApiDocument jsonApiDocument = new JsonApiDocument(); //TODO Make this a document processor Data<Resource> data = resource == null ? null : new Data<>(resource.toResource()); jsonApiDocument.setData(data); //TODO Iterate over set of document processors DocumentProcessor includedProcessor = new IncludedProcessor(); includedProcessor.execute(jsonApiDocument, resource, queryParams); return requestScope.getMapper().toJsonObject(jsonApiDocument); } }
protected static JsonNode getResponseBody(PersistentResource resource, RequestScope requestScope) { Optional<MultivaluedMap<String, String>> queryParams = requestScope.getQueryParams(); JsonApiDocument jsonApiDocument = new JsonApiDocument(); //TODO Make this a document processor Data<Resource> data = resource == null ? null : new Data<>(resource.toResource()); jsonApiDocument.setData(data); //TODO Iterate over set of document processors DocumentProcessor includedProcessor = new IncludedProcessor(); includedProcessor.execute(jsonApiDocument, resource, queryParams); return requestScope.getMapper().toJsonObject(jsonApiDocument); } }