use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.
the class PersistentResource method toResource.
/**
* Convert a persistent resource to a resource.
*
* @param relationships The relationships
* @param attributes The attributes
* @return The Resource
*/
public Resource toResource(final Map<String, Relationship> relationships, final Map<String, Object> attributes) {
final Resource resource = new Resource(typeName, (obj == null) ? uuid.orElseThrow(() -> new InvalidEntityBodyException("No id found on object")) : dictionary.getId(obj));
resource.setRelationships(relationships);
resource.setAttributes(attributes);
if (requestScope.getElideSettings().isEnableJsonLinks()) {
resource.setLinks(requestScope.getElideSettings().getJsonApiLinks().getResourceLevelLinks(this));
}
return resource;
}
use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.
the class PersistentResource method createObject.
/**
* Create a resource in the database.
*
* @param parent - The immediate ancestor in the lineage or null if this is a root.
* @param parentRelationship - The name of the parent relationship traversed to create this object.
* @param entityClass the entity class
* @param requestScope the request scope
* @param uuid the (optional) uuid
* @param <T> object type
* @return persistent resource
*/
public static <T> PersistentResource<T> createObject(PersistentResource<?> parent, String parentRelationship, Type<T> entityClass, RequestScope requestScope, Optional<String> uuid) {
T obj = requestScope.getTransaction().createNewObject(entityClass, requestScope);
String id = uuid.orElse(null);
PersistentResource<T> newResource = new PersistentResource<>(obj, parent, parentRelationship, id, requestScope);
// The ID must be assigned before we add it to the new resources set. Persistent resource
// hashcode and equals are only based on the ID/UUID & type.
assignId(newResource, id);
// Keep track of new resources for non-transferable resources
requestScope.getNewPersistentResources().add(newResource);
checkPermission(CreatePermission.class, newResource);
newResource.auditClass(Audit.Action.CREATE, new ChangeSpec(newResource, null, null, newResource.getObject()));
requestScope.publishLifecycleEvent(newResource, CREATE);
requestScope.setUUIDForObject(newResource.type, id, newResource.getObject());
// Initialize null ToMany collections
requestScope.getDictionary().getRelationships(entityClass).stream().filter(relationName -> newResource.getRelationshipType(relationName).isToMany() && newResource.getValueUnchecked(relationName) == null).forEach(relationName -> newResource.setValue(relationName, new LinkedHashSet<>()));
newResource.markDirty();
return newResource;
}
use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.
the class JSONExportFormatter method resourceToJSON.
public static String resourceToJSON(ObjectMapper mapper, PersistentResource resource) {
if (resource == null || resource.getObject() == null) {
return null;
}
StringBuilder str = new StringBuilder();
try {
Resource jsonResource = resource.toResource(getRelationships(resource), getAttributes(resource));
str.append(mapper.writeValueAsString(jsonResource.getAttributes()));
} catch (JsonProcessingException e) {
log.error("Exception when converting to JSON {}", e.getMessage());
throw new IllegalStateException(e);
}
return str.toString();
}
use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.
the class BaseState method getResponseBody.
protected static JsonNode getResponseBody(PersistentResource resource, RequestScope requestScope) {
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);
}
use of com.yahoo.elide.jsonapi.models.Resource 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;
}
Aggregations