use of com.yahoo.elide.core.exceptions.UnknownEntityException 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.exceptions.UnknownEntityException in project elide by yahoo.
the class Resource method toPersistentResource.
public PersistentResource<?> toPersistentResource(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException {
EntityDictionary dictionary = requestScope.getDictionary();
Type<?> cls = dictionary.getEntityClass(type, requestScope.getApiVersion());
if (cls == null) {
throw new UnknownEntityException(type);
}
if (id == null) {
throw new InvalidObjectIdentifierException(id, type);
}
EntityProjection projection = EntityProjection.builder().type(cls).build();
return PersistentResource.loadRecord(projection, id, requestScope);
}
Aggregations