use of io.crnk.core.engine.internal.document.mapper.ResourceMappingConfig 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);
}
}
};
}
Aggregations