Search in sources :

Example 1 with JsonApiMapper

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);
    };
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Data(com.yahoo.elide.jsonapi.models.Data) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) RequestScope(com.yahoo.elide.core.RequestScope)

Example 2 with JsonApiMapper

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());
}
Also used : ObjectEntityCache(com.yahoo.elide.core.ObjectEntityCache) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) RequestScope(com.yahoo.elide.core.RequestScope) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 3 with JsonApiMapper

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());
}
Also used : ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) Elide(com.yahoo.elide.Elide) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) Bean(org.springframework.context.annotation.Bean)

Example 4 with JsonApiMapper

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;
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) UnknownEntityException(com.yahoo.elide.core.exceptions.UnknownEntityException) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) ToString(lombok.ToString) InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) Relationship(com.yahoo.elide.jsonapi.models.Relationship) InternalServerErrorException(com.yahoo.elide.core.exceptions.InternalServerErrorException) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 5 with JsonApiMapper

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));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) DocumentProcessor(com.yahoo.elide.jsonapi.document.processors.DocumentProcessor) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) ArrayList(java.util.ArrayList) Data(com.yahoo.elide.jsonapi.models.Data) RequestScope(com.yahoo.elide.core.RequestScope) Relationship(com.yahoo.elide.jsonapi.models.Relationship) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) IncludedProcessor(com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)

Aggregations

JsonApiMapper (com.yahoo.elide.jsonapi.JsonApiMapper)5 RequestScope (com.yahoo.elide.core.RequestScope)3 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)3 PersistentResource (com.yahoo.elide.core.PersistentResource)2 Data (com.yahoo.elide.jsonapi.models.Data)2 Relationship (com.yahoo.elide.jsonapi.models.Relationship)2 Resource (com.yahoo.elide.jsonapi.models.Resource)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Elide (com.yahoo.elide.Elide)1 ElideSettingsBuilder (com.yahoo.elide.ElideSettingsBuilder)1 ObjectEntityCache (com.yahoo.elide.core.ObjectEntityCache)1 InternalServerErrorException (com.yahoo.elide.core.exceptions.InternalServerErrorException)1 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)1 InvalidValueException (com.yahoo.elide.core.exceptions.InvalidValueException)1 UnknownEntityException (com.yahoo.elide.core.exceptions.UnknownEntityException)1 RSQLFilterDialect (com.yahoo.elide.core.filter.dialect.RSQLFilterDialect)1 DocumentProcessor (com.yahoo.elide.jsonapi.document.processors.DocumentProcessor)1 IncludedProcessor (com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)1