Search in sources :

Example 91 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class SleuthUtil method getSpanName.

public static String getSpanName(RepositoryRequestSpec request) {
    ResourceField relationshipField = request.getRelationshipField();
    StringBuilder pathBuilder = new StringBuilder();
    String method = request.getMethod().toString();
    pathBuilder.append(SleuthRepositoryFilter.COMPONENT_NAME);
    pathBuilder.append(SleuthRepositoryFilter.COMPONENT_NAME_SEPARATOR);
    pathBuilder.append(method);
    pathBuilder.append(SleuthRepositoryFilter.COMPONENT_NAME_SEPARATOR);
    pathBuilder.append("/");
    if (relationshipField == null) {
        pathBuilder.append(request.getQueryAdapter().getResourceInformation().getResourceType());
    } else {
        pathBuilder.append(relationshipField.getParentResourceInformation().getResourceType());
    }
    Iterable<Object> ids = request.getIds();
    if (ids != null) {
        pathBuilder.append("/");
        pathBuilder.append(StringUtils.join(",", ids));
    }
    if (relationshipField != null) {
        pathBuilder.append("/");
        pathBuilder.append(relationshipField.getJsonName());
    }
    return pathBuilder.toString();
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField)

Example 92 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class ConstraintViolationExceptionMapper method getResourceId.

/**
 * @param resource to get the id from
 * @return id of the given resource
 */
protected String getResourceId(Object resource) {
    ResourceRegistry resourceRegistry = context.getResourceRegistry();
    RegistryEntry entry = resourceRegistry.findEntry(resource.getClass());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField idField = resourceInformation.getIdField();
    Object id = idField.getAccessor().getValue(resource);
    if (id != null) {
        return id.toString();
    }
    return null;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 93 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class ClientDocumentMapper method newResourceMapper.

@Override
protected ResourceMapper newResourceMapper(final DocumentMapperUtil util, boolean client, ObjectMapper objectMapper) {
    return new ResourceMapper(util, client, objectMapper, null) {

        @Override
        protected void setRelationship(Resource resource, ResourceField field, Object entity, ResourceInformation resourceInformation, QueryAdapter queryAdapter, ResourceMappingConfig mappingConfig) {
            // we also include relationship data if it is not null and not a
            // unloaded proxy
            boolean includeRelation = true;
            Object relationshipId = null;
            if (field.hasIdField()) {
                Object relationshipValue = field.getIdAccessor().getValue(entity);
                ResourceInformation oppositeInformation = resourceRegistry.getEntry(field.getOppositeResourceType()).getResourceInformation();
                if (relationshipValue instanceof Collection) {
                    List ids = new ArrayList();
                    for (Object elem : (Collection<?>) relationshipValue) {
                        ids.add(oppositeInformation.toResourceIdentifier(elem));
                    }
                    relationshipId = ids;
                } else if (relationshipValue != null) {
                    relationshipId = oppositeInformation.toResourceIdentifier(relationshipValue);
                }
                includeRelation = relationshipId != null || field.getSerializeType() != SerializeType.LAZY;
            } else {
                Object relationshipValue = field.getAccessor().getValue(entity);
                if (relationshipValue instanceof ObjectProxy) {
                    includeRelation = ((ObjectProxy) relationshipValue).isLoaded();
                } else {
                    // TODO for fieldSets handling in the future the lazy
                    // handling must be different
                    includeRelation = relationshipValue != null || field.getSerializeType() != SerializeType.LAZY && !field.isCollection();
                }
                if (relationshipValue != null && includeRelation) {
                    if (relationshipValue instanceof Collection) {
                        relationshipId = util.toResourceIds((Collection<?>) relationshipValue);
                    } else {
                        relationshipId = util.toResourceId(relationshipValue);
                    }
                }
            }
            if (includeRelation) {
                Relationship relationship = new Relationship();
                relationship.setData(Nullable.of((Object) relationshipId));
                resource.getRelationships().put(field.getJsonName(), relationship);
            }
        }
    };
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceMappingConfig(io.crnk.core.engine.internal.document.mapper.ResourceMappingConfig) Relationship(io.crnk.core.engine.document.Relationship) ResourceMapper(io.crnk.core.engine.internal.document.mapper.ResourceMapper) Collection(java.util.Collection) ArrayList(java.util.ArrayList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) ObjectProxy(io.crnk.client.internal.proxy.ObjectProxy)

Example 94 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class ClientResourceUpsert method setMeta.

protected void setMeta(Resource dataBody, Object instance, ResourceInformation resourceInformation) {
    ResourceField metaField = resourceInformation.getMetaField();
    if (dataBody.getMeta() != null && metaField != null) {
        JsonNode metaNode = dataBody.getMeta();
        Class<?> metaClass = metaField.getType();
        ObjectReader metaMapper = objectMapper.readerFor(metaClass);
        try {
            Object meta = metaMapper.readValue(metaNode);
            metaField.getAccessor().setValue(instance, meta);
        } catch (IOException e) {
            throw new ResponseBodyException("failed to parse links information", e);
        }
    }
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException) ResponseBodyException(io.crnk.client.ResponseBodyException)

Example 95 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class CrnkClient method allocateRepositoryRelations.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void allocateRepositoryRelations(RegistryEntry registryEntry) {
    ResourceInformation resourceInformation = registryEntry.getResourceInformation();
    List<ResourceField> relationshipFields = resourceInformation.getRelationshipFields();
    for (ResourceField relationshipField : relationshipFields) {
        final Class<?> targetClass = relationshipField.getElementType();
        Class sourceClass = resourceInformation.getResourceClass();
        allocateRepositoryRelation(sourceClass, targetClass);
    }
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation)

Aggregations

ResourceField (io.crnk.core.engine.information.resource.ResourceField)109 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)75 Test (org.junit.Test)41 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)36 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)13 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 ArrayList (java.util.ArrayList)9 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)8 Response (io.crnk.core.engine.dispatcher.Response)8 Resource (io.crnk.core.engine.document.Resource)8 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)8 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 HashSet (java.util.HashSet)8 Document (io.crnk.core.engine.document.Document)7 Task (io.crnk.core.mock.models.Task)7 Serializable (java.io.Serializable)7 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)6 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)6 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)6