use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class CollectionTerminalState method getData.
private Data getData(Set<PersistentResource> collection, EntityDictionary dictionary) {
Preconditions.checkNotNull(collection);
List<Resource> resources = collection.stream().map(PersistentResource::toResource).collect(Collectors.toList());
if (parent.isPresent()) {
Type<?> parentClass = parent.get().getResourceType();
String relationshipName = relationName.orElseThrow(IllegalStateException::new);
RelationshipType type = dictionary.getRelationshipType(parentClass, relationshipName);
return new Data<>(resources, type);
}
return new Data<>(resources);
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class CollectionTerminalState method handleGet.
@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
JsonApiDocument jsonApiDocument = new JsonApiDocument();
RequestScope requestScope = state.getRequestScope();
MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
Set<PersistentResource> collection = getResourceCollection(requestScope).toList(LinkedHashSet::new).blockingGet();
// Set data
jsonApiDocument.setData(getData(collection, requestScope.getDictionary()));
// Run include processor
DocumentProcessor includedProcessor = new IncludedProcessor();
includedProcessor.execute(jsonApiDocument, collection, queryParams);
Pagination pagination = parentProjection.getPagination();
if (parent.isPresent()) {
pagination = parentProjection.getRelationship(relationName.orElseThrow(IllegalStateException::new)).get().getProjection().getPagination();
}
// Add pagination meta data
if (!pagination.isDefaultInstance()) {
Map<String, Number> pageMetaData = new HashMap<>();
pageMetaData.put("number", (pagination.getOffset() / pagination.getLimit()) + 1);
pageMetaData.put("limit", pagination.getLimit());
// Get total records if it has been requested and add to the page meta data
if (pagination.returnPageTotals()) {
Long totalRecords = pagination.getPageTotals();
pageMetaData.put("totalPages", totalRecords / pagination.getLimit() + ((totalRecords % pagination.getLimit()) > 0 ? 1 : 0));
pageMetaData.put("totalRecords", totalRecords);
}
Map<String, Object> allMetaData = new HashMap<>();
allMetaData.put("page", pageMetaData);
Meta meta = new Meta(allMetaData);
jsonApiDocument.setMeta(meta);
}
JsonNode responseBody = requestScope.getMapper().toJsonObject(jsonApiDocument);
return () -> Pair.of(HttpStatus.SC_OK, responseBody);
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class RecordState method handle.
@Override
public void handle(StateContext state, SubCollectionRelationshipContext ctx) {
String id = ctx.entity().id().getText();
String subCollection = ctx.entity().term().getText();
String relationName = ctx.relationship().term().getText();
PersistentResource childRecord;
Relationship childRelationship = projection.getRelationship(subCollection).orElseThrow(IllegalStateException::new);
childRecord = resource.getRelation(childRelationship, id);
state.setState(new RelationshipTerminalState(childRecord, relationName, childRelationship.getProjection()));
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class RecordState method handle.
@Override
public void handle(StateContext state, SubCollectionReadCollectionContext ctx) {
String subCollection = ctx.term().getText();
EntityDictionary dictionary = state.getRequestScope().getDictionary();
Type<?> entityClass;
String entityName;
RelationshipType type = dictionary.getRelationshipType(resource.getObject(), subCollection);
Type<?> paramType = dictionary.getParameterizedType(resource.getObject(), subCollection);
entityName = dictionary.getJsonAliasFor(paramType);
entityClass = dictionary.getEntityClass(entityName, state.getRequestScope().getApiVersion());
if (entityClass == null) {
throw new IllegalArgumentException("Unknown type " + entityName);
}
final BaseState nextState;
final CollectionTerminalState collectionTerminalState = new CollectionTerminalState(entityClass, Optional.of(resource), Optional.of(subCollection), projection);
Observable<PersistentResource> collection = null;
if (type.isToOne()) {
collection = resource.getRelationCheckedFiltered(projection.getRelationship(subCollection).orElseThrow(IllegalStateException::new));
PersistentResource record = PersistentResource.firstOrNullIfEmpty(collection);
nextState = new RecordTerminalState(record, collectionTerminalState);
} else {
nextState = collectionTerminalState;
}
state.setState(nextState);
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class RecordTerminalState method patch.
private boolean patch(Resource resource, RequestScope requestScope) {
boolean isUpdated = false;
// Update attributes first
Map<String, Object> attributes = resource.getAttributes();
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String fieldName = entry.getKey();
Object newVal = entry.getValue();
isUpdated |= record.updateAttribute(fieldName, newVal);
}
}
// Relations next
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> resources = (relationship == null) ? null : relationship.toPersistentResources(requestScope);
isUpdated |= record.updateRelation(fieldName, resources);
}
}
return isUpdated;
}
Aggregations