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();
}
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;
}
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);
}
}
};
}
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);
}
}
}
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);
}
}
Aggregations