use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class ClientStubBase method toResourceResponse.
private static Object toResourceResponse(Document document, ObjectMapper objectMapper) {
Object data = document.getData().get();
if (data instanceof List) {
DefaultResourceList<Resource> list = new DefaultResourceList<>();
list.addAll((List<Resource>) data);
if (document.getMeta() != null) {
list.setMeta(new JsonMetaInformation(document.getMeta(), objectMapper));
}
if (document.getLinks() != null) {
list.setLinks(new JsonLinksInformation(document.getMeta(), objectMapper));
}
return list;
} else {
return data;
}
}
use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class GetFromOwnerStrategy method toResult.
private MultivaluedMap<I, D> toResult(ResourceField field, ResourceInformation targetInformation, List sources, List<D> targets) {
MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
Map targetMap = new HashMap();
for (D target : targets) {
Object targetId = targetInformation.getId(target);
targetMap.put(targetId, target);
}
for (Object source : sources) {
Object sourceId = field.getParentResourceInformation().getId(source);
Object targetId = field.getIdAccessor().getValue(source);
if (field.isCollection()) {
for (Object targetElementId : (Collection) targetId) {
addResult(bulkResult, field, sourceId, targetElementId, targetMap);
}
} else if (targetId != null) {
addResult(bulkResult, field, sourceId, targetId, targetMap);
} else {
bulkResult.add(sourceId, null);
}
}
return bulkResult;
}
use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class ImplicitOwnerBasedRelationshipRepository method toResult.
private MultivaluedMap<I, D> toResult(String fieldName, ResourceInformation targetInformation, List sources, List<D> targets) {
RegistryEntry sourceEntry = getSourceEntry();
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
Map targetMap = new HashMap();
for (D target : targets) {
Object targetId = targetInformation.getId(target);
targetMap.put(targetId, target);
}
for (Object source : sources) {
Object sourceId = sourceInformation.getId(source);
Object targetId = field.getIdAccessor().getValue(source);
if (field.isCollection()) {
for (Object targetElementId : (Collection) targetId) {
addResult(bulkResult, field, sourceId, targetElementId, targetMap);
}
} else if (targetId != null) {
addResult(bulkResult, field, sourceId, targetId, targetMap);
} else {
bulkResult.add(sourceId, null);
}
}
return bulkResult;
}
use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class ImplicitOwnerBasedRelationshipRepository method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = getSourceEntry();
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
List sources = (List) sourceEntry.getResourceRepository().findAll(sourceIds, getSaveQueryAdapter(fieldName)).getEntity();
ResourceInformation targetInformation = getTargetInformation(field);
List<D> targets;
if (field.hasIdField()) {
Set targetIds = new HashSet();
for (Object source : sources) {
Object targetId = field.getIdAccessor().getValue(source);
if (field.isCollection()) {
targetIds.addAll((Collection) targetId);
} else {
targetIds.add(targetId);
}
}
QuerySpec idQuerySpec = new QuerySpec(targetInformation);
ResourceRepositoryAdapter<D, J> targetAdapter = getTargetEntry(field).getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(targetIds, new QuerySpecAdapter(idQuerySpec, resourceRegistry));
targets = (List<D>) response.getEntity();
return toResult(fieldName, targetInformation, sources, targets);
} else {
MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
for (Object source : sources) {
Object sourceId = sourceInformation.getId(source);
Object target = field.getAccessor().getValue(source);
if (target != null) {
if (field.isCollection()) {
bulkResult.addAll(sourceId, (Collection) target);
} else {
bulkResult.add(sourceId, target);
}
}
}
return bulkResult;
}
}
use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class GetFromOppositeStrategy method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = context.getSourceEntry();
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
RegistryEntry targetEntry = context.getTargetEntry(field);
ResourceInformation targetInformation = targetEntry.getResourceInformation();
ResourceField oppositeField = Objects.requireNonNull(targetInformation.findFieldByUnderlyingName(field.getOppositeName()));
QuerySpec idQuerySpec = querySpec.duplicate();
idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeField.getUnderlyingName(), sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
idQuerySpec.includeRelation(Arrays.asList(oppositeField.getUnderlyingName()));
ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(context.createQueryAdapter(idQuerySpec));
Collection<D> results = (Collection<D>) response.getEntity();
MultivaluedMap<I, D> bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
Set<I> sourceIdSet = new HashSet<>();
for (I sourceId : sourceIds) {
sourceIdSet.add(sourceId);
}
for (D result : results) {
handleTarget(bulkResult, result, sourceIdSet, oppositeField, sourceInformation);
}
return bulkResult;
}
Aggregations