use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class CollectionTerminalState method createObject.
private PersistentResource createObject(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException {
JsonApiDocument doc = requestScope.getJsonApiDocument();
JsonApiMapper mapper = requestScope.getMapper();
if (doc.getData() == null) {
throw new InvalidEntityBodyException("Invalid JSON-API document: " + doc);
}
Data<Resource> data = doc.getData();
Collection<Resource> resources = data.get();
Resource resource = (resources.size() == 1) ? IterableUtils.first(resources) : null;
if (resource == null) {
try {
throw new InvalidEntityBodyException(mapper.writeJsonApiDocument(doc));
} catch (JsonProcessingException e) {
throw new InternalServerErrorException(e);
}
}
String id = resource.getId();
Type<?> newObjectClass = requestScope.getDictionary().getEntityClass(resource.getType(), requestScope.getApiVersion());
if (newObjectClass == null) {
throw new UnknownEntityException("Entity " + resource.getType() + " not found");
}
if (!entityClass.isAssignableFrom(newObjectClass)) {
throw new InvalidValueException("Cannot assign value of type: " + resource.getType() + " to type: " + entityClass);
}
PersistentResource pResource = PersistentResource.createObject(parent.orElse(null), relationName.orElse(null), newObjectClass, requestScope, Optional.ofNullable(id));
Map<String, Object> attributes = resource.getAttributes();
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String fieldName = entry.getKey();
Object val = entry.getValue();
pResource.updateAttribute(fieldName, val);
}
}
Map<String, Relationship> relationships = resource.getRelationships();
if (relationships != null) {
for (Map.Entry<String, Relationship> entry : relationships.entrySet()) {
String fieldName = entry.getKey();
Relationship relationship = entry.getValue();
Set<PersistentResource> resourceSet = (relationship == null) ? null : relationship.toPersistentResources(requestScope);
pResource.updateRelation(fieldName, resourceSet);
}
}
return pResource;
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class RecordState method handle.
@Override
public void handle(StateContext state, SubCollectionReadEntityContext ctx) {
String id = ctx.entity().id().getText();
String subCollection = ctx.entity().term().getText();
PersistentResource nextRecord = resource.getRelation(projection.getRelationship(subCollection).orElseThrow(IllegalStateException::new), id);
state.setState(new RecordTerminalState(nextRecord));
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class StartState method handle.
@Override
public void handle(StateContext state, RootCollectionSubCollectionContext ctx) {
PersistentResource record = entityRecord(state, ctx.entity());
state.setState(new RecordState(record, state.getRequestScope().getEntityProjection()));
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class IncludedProcessor method addResourcesForPath.
/**
* 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, EntityProjection projection) {
// Pop off a relation of relation path
String relation = relationPath.remove(0);
Set<PersistentResource> collection;
Relationship relationship = projection.getRelationship(relation).orElseThrow(IllegalStateException::new);
try {
collection = rec.getRelationCheckedFiltered(relationship).toList(LinkedHashSet::new).blockingGet();
} 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), relationship.getProjection());
}
});
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class Relationship method toPersistentResources.
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 (resource == null) {
continue;
}
res.add(resource.toPersistentResource(requestScope));
} catch (ForbiddenAccessException e) {
// skip resource
}
}
}
return res.isEmpty() ? (data.isToOne() ? null : res) : res;
}
Aggregations