use of com.yahoo.elide.jsonapi.JsonApiMapper in project elide by yahoo.
the class CollectionTerminalState method handlePost.
@Override
public Supplier<Pair<Integer, JsonNode>> handlePost(StateContext state) {
RequestScope requestScope = state.getRequestScope();
JsonApiMapper mapper = requestScope.getMapper();
newObject = createObject(requestScope);
parent.ifPresent(persistentResource -> persistentResource.addRelation(relationName.get(), newObject));
return () -> {
JsonApiDocument returnDoc = new JsonApiDocument();
returnDoc.setData(new Data<>(newObject.toResource()));
JsonNode responseBody = mapper.getObjectMapper().convertValue(returnDoc, JsonNode.class);
return Pair.of(HttpStatus.SC_CREATED, responseBody);
};
}
use of com.yahoo.elide.jsonapi.JsonApiMapper in project elide by yahoo.
the class NoopTransactionTest method setup.
@BeforeAll
public void setup() {
dictionary = EntityDictionary.builder().build();
dictionary.bindEntity(NoopBean.class);
requestScope = mock(RequestScope.class);
JsonApiMapper mapper = mock(JsonApiMapper.class);
when(requestScope.getDictionary()).thenReturn(dictionary);
when(requestScope.getObjectEntityCache()).thenReturn(new ObjectEntityCache());
when(requestScope.getMapper()).thenReturn(mapper);
when(mapper.getObjectMapper()).thenReturn(new ObjectMapper());
}
use of com.yahoo.elide.jsonapi.JsonApiMapper in project faf-java-api by FAForever.
the class ElideConfig method elide.
@Bean
public Elide elide(SpringHibernateDataStore springHibernateDataStore, ObjectMapper objectMapper, EntityDictionary entityDictionary, ExtendedAuditLogger extendedAuditLogger) {
RSQLFilterDialect rsqlFilterDialect = new RSQLFilterDialect(entityDictionary);
registerAdditionalConverters();
return new Elide(new ElideSettingsBuilder(springHibernateDataStore).withJsonApiMapper(new JsonApiMapper(entityDictionary, objectMapper)).withAuditLogger(extendedAuditLogger).withEntityDictionary(entityDictionary).withJoinFilterDialect(rsqlFilterDialect).withSubqueryFilterDialect(rsqlFilterDialect).build());
}
use of com.yahoo.elide.jsonapi.JsonApiMapper 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.jsonapi.JsonApiMapper in project elide by yahoo.
the class RelationshipTerminalState method handleGet.
@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
JsonApiDocument doc = new JsonApiDocument();
RequestScope requestScope = state.getRequestScope();
JsonApiMapper mapper = requestScope.getMapper();
MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
Map<String, Relationship> relationships = record.toResource(parentProjection).getRelationships();
if (relationships != null && relationships.containsKey(relationshipName)) {
Relationship relationship = relationships.get(relationshipName);
// Handle valid relationship
// Set data
Data<Resource> data = relationship.getData();
doc.setData(data);
// Run include processor
DocumentProcessor includedProcessor = new IncludedProcessor();
includedProcessor.execute(doc, record, queryParams);
return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
}
// Handle no data for relationship
if (relationshipType.isToMany()) {
doc.setData(new Data<>(new ArrayList<>()));
} else if (relationshipType.isToOne()) {
doc.setData(new Data<>((Resource) null));
} else {
throw new IllegalStateException("Failed to GET a relationship; relationship is neither toMany nor toOne");
}
return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
}
Aggregations