use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class ProjectToTaskRepository method findManyTargets.
@Override
public ResourceList<Task> findManyTargets(Long sourceId, String fieldName, QuerySpec queryParams) {
DefaultResourceList<Task> tasks = new DefaultResourceList<>();
for (Relation<Project> relation : THREAD_LOCAL_REPOSITORY.keySet()) {
if (relation.getSource().getId().equals(sourceId) && relation.getFieldName().equals(fieldName)) {
Task task = taskRepo.findOne((long) relation.getTargetId(), null);
Assert.assertNotNull(task);
tasks.add(task);
}
}
return tasks;
}
use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class CrnkClient method setProxyFactory.
public void setProxyFactory(ClientProxyFactory proxyFactory) {
proxyFactory.init(new ClientProxyFactoryContext() {
@Override
public ModuleRegistry getModuleRegistry() {
return moduleRegistry;
}
@Override
public <T> DefaultResourceList<T> getCollection(Class<T> resourceClass, String url) {
RegistryEntry entry = resourceRegistry.findEntry(resourceClass);
ResourceInformation resourceInformation = entry.getResourceInformation();
final ResourceRepositoryStubImpl<T, ?> repositoryStub = new ResourceRepositoryStubImpl<>(CrnkClient.this, resourceClass, resourceInformation, urlBuilder);
return repositoryStub.findAll(url);
}
});
documentMapper.setProxyFactory(proxyFactory);
}
use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class ClientDocumentMapper method fromDocument.
public Object fromDocument(Document document, boolean getList) {
ClientResourceUpsert upsert = new ClientResourceUpsert(resourceRegistry, propertiesProvider, typeParser, objectMapper, null, proxyFactory);
PreconditionUtil.assertFalse("document contains json api errors and cannot be processed", document.getErrors() != null && !document.getErrors().isEmpty());
if (!document.getData().isPresent()) {
return null;
}
List<Resource> included = document.getIncluded();
List<Resource> data = document.getCollectionData().get();
List<Object> dataObjects = upsert.allocateResources(data);
if (included != null) {
upsert.allocateResources(included);
}
upsert.setRelations(data);
if (included != null) {
upsert.setRelations(included);
}
if (getList) {
DefaultResourceList<Object> resourceList = new DefaultResourceList();
resourceList.addAll(dataObjects);
if (document.getLinks() != null) {
resourceList.setLinks(new JsonLinksInformation(document.getLinks(), objectMapper));
}
if (document.getMeta() != null) {
resourceList.setMeta(new JsonMetaInformation(document.getMeta(), objectMapper));
}
return resourceList;
} else {
if (dataObjects.isEmpty()) {
return null;
}
PreconditionUtil.assertFalse("expected unique result", dataObjects.size() > 1);
return dataObjects.get(0);
}
}
use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class ClientStubInvocationHandler method createTypesafeList.
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object createTypesafeList(Object result, Class<?> returnType) {
DefaultResourceList defaultList = (DefaultResourceList) result;
Class<?>[] typeArguments = TypeResolver.resolveRawArguments(ResourceListBase.class, returnType);
Class<?> metaType = typeArguments[1];
Class<?> linksType = typeArguments[2];
ResourceListBase typedList = (ResourceListBase) ClassUtils.newInstance(returnType);
typedList.addAll(defaultList);
typedList.setMeta(defaultList.getMeta(metaType));
typedList.setLinks(defaultList.getLinks(linksType));
return typedList;
}
use of io.crnk.core.resource.list.DefaultResourceList in project crnk-framework by crnk-project.
the class RelationshipRepositoryBase method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = resourceRegistry.findEntry(sourceResourceClass);
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
RegistryEntry targetEntry = getTargetEntry(field);
String oppositeName = getOppositeName(fieldName);
QuerySpec idQuerySpec = querySpec.duplicate();
idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeName, sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
idQuerySpec.includeRelation(Arrays.asList(oppositeName));
ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(new QuerySpecAdapter(idQuerySpec, resourceRegistry));
List<D> results = (List<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, oppositeName, sourceInformation);
}
return bulkResult;
}
Aggregations