/** * Fetch the item if the data is toOne. * * @return T if toOne * @throws IllegalAccessError when the data is not toOne */ public T getSingleValue() { if (isToOne()) { return ((SingleElementSet<T>) values).getValue(); } throw new IllegalAccessError("Data is not toOne"); }
/** * Fetch the item if the data is toOne. * * @return T if toOne * @throws IllegalAccessError when the data is not toOne */ public T getSingleValue() { if (isToOne()) { return ((SingleElementSet<T>) values).getValue(); } throw new IllegalAccessError("Data is not toOne"); }
@Override public void serialize(Data<Resource> data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { Collection<Resource> list = data.get(); if (data.isToOne()) { if (list == null || list.isEmpty()) { jsonGenerator.writeObject(null); return; } jsonGenerator.writeObject(list.iterator().next()); return; } jsonGenerator.writeObject((list == null) ? Collections.emptyList() : list); } }
@Override public void serialize(Data<Resource> data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { Collection<Resource> list = data.get(); if (data.isToOne()) { if (list == null || list.isEmpty()) { jsonGenerator.writeObject(null); return; } jsonGenerator.writeObject(list.iterator().next()); return; } jsonGenerator.writeObject((list == null) ? Collections.emptyList() : list); } }
public Set<PersistentResource> toPersistentResources(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException { Set<PersistentResource> res = new LinkedHashSet<>(); if (data == null) { return null; } Collection<Resource> resources = data.get(); if (resources != null) { for (Resource resource : resources) { try { if (data.isToOne() && resource == null) { continue; } res.add(resource.toPersistentResource(requestScope)); } catch (ForbiddenAccessException e) { //skip resource } } } return res.isEmpty() ? (data.isToOne() ? null : res) : res; }
public Set<PersistentResource> toPersistentResources(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException { Set<PersistentResource> res = new LinkedHashSet<>(); if (data == null) { return null; } Collection<Resource> resources = data.get(); if (resources != null) { for (Resource resource : resources) { try { if (data.isToOne() && resource == null) { continue; } res.add(resource.toPersistentResource(requestScope)); } catch (ForbiddenAccessException e) { //skip resource } } } return res.isEmpty() ? (data.isToOne() ? null : res) : res; }
public Relationship(@JsonProperty("links") Map<String, String> links, @JsonProperty("data") Data<Resource> data) { this.links = links; this.data = data; if (data != null) { if (data.isToOne()) { Resource resource = data.getSingleValue(); this.idData = new Data<>(resource != null ? resource.toResourceIdentifier() : null); } else { this.idData = new Data<>( data.get().stream() .map(Resource::toResourceIdentifier) .collect(Collectors.toList()) ); } } else { this.idData = null; } }
public Relationship(@JsonProperty("links") Map<String, String> links, @JsonProperty("data") Data<Resource> data) { this.links = links; this.data = data; if (data != null) { if (data.isToOne()) { Resource resource = data.getSingleValue(); this.idData = new Data<>(resource != null ? resource.toResourceIdentifier() : null); } else { this.idData = new Data<>( data.get().stream() .map(Resource::toResourceIdentifier) .collect(Collectors.toList()) ); } } else { this.idData = null; } }
@Override public Supplier<Pair<Integer, JsonNode>> handlePatch(StateContext state) { JsonApiDocument jsonApiDocument = state.getJsonApiDocument(); Data<Resource> data = jsonApiDocument.getData(); if (data == null) { throw new InvalidEntityBodyException("Expected data but found null"); } if (!data.isToOne()) { throw new InvalidEntityBodyException("Expected single element but found list"); } Resource resource = data.getSingleValue(); if (!record.matchesId(resource.getId())) { throw new InvalidEntityBodyException("Id in response body does not match requested id to update from path"); } patch(resource, state.getRequestScope()); return constructPatchResponse(record, state); }
@Override public Supplier<Pair<Integer, JsonNode>> handlePatch(StateContext state) { JsonApiDocument jsonApiDocument = state.getJsonApiDocument(); Data<Resource> data = jsonApiDocument.getData(); if (data == null) { throw new InvalidEntityBodyException("Expected data but found null"); } if (!data.isToOne()) { throw new InvalidEntityBodyException("Expected single element but found list"); } Resource resource = data.getSingleValue(); if (!record.matchesId(resource.getId())) { throw new InvalidEntityBodyException("Id in response body does not match requested id to update from path"); } patch(resource, state.getRequestScope()); return constructPatchResponse(record, state); }