use of io.crnk.core.queryspec.QuerySpec 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;
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class ForwardingStrategyContext method createSourceQuerySpec.
private QuerySpec createSourceQuerySpec() {
RegistryEntry sourceEntry = getSourceEntry();
ResourceInformation resourceInformation = sourceEntry.getResourceInformation();
return new QuerySpec(resourceInformation.getResourceClass(), resourceInformation.getResourceType());
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class ForwardingStrategyContext method findAll.
public <Q> Iterable<Q> findAll(RegistryEntry entry, Iterable<?> targetIds) {
ResourceRepositoryAdapter targetAdapter = entry.getResourceRepository();
QueryAdapter queryAdapter = new QuerySpecAdapter(new QuerySpec(entry.getResourceInformation()), resourceRegistry);
return (Iterable) targetAdapter.findAll(targetIds, queryAdapter).getEntity();
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class ForwardingStrategyContext method findOne.
public <Q> Q findOne(RegistryEntry entry, Serializable id) {
ResourceRepositoryAdapter targetAdapter = entry.getResourceRepository();
QueryAdapter queryAdapter = new QuerySpecAdapter(new QuerySpec(entry.getResourceInformation()), resourceRegistry);
return (Q) targetAdapter.findOne(id, queryAdapter).getEntity();
}
use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.
the class JsonApiUrlBuilder method buildUrlInternal.
private String buildUrlInternal(ResourceInformation resourceInformation, Object id, Object query, String relationshipName) {
String url = resourceRegistry.getResourceUrl(resourceInformation);
if (id instanceof Collection) {
Collection<?> ids = (Collection<?>) id;
Collection<String> strIds = new ArrayList<>();
for (Object idElem : ids) {
String strIdElem = resourceInformation.toIdString(idElem);
strIds.add(strIdElem);
}
url += "/";
url += StringUtils.join(",", strIds);
} else if (id != null) {
String strId = resourceInformation.toIdString(id);
url += "/" + strId;
}
if (relationshipName != null) {
url += "/relationships/" + relationshipName;
}
UrlParameterBuilder urlBuilder = new UrlParameterBuilder(url);
if (query instanceof QuerySpec) {
QuerySpec querySpec = (QuerySpec) query;
urlBuilder.addQueryParameters(querySpecSerializer.serialize(querySpec));
} else if (query instanceof QueryParams) {
QueryParams queryParams = (QueryParams) query;
urlBuilder.addQueryParameters(queryParamsSerializer.serializeFilters(queryParams));
urlBuilder.addQueryParameters(queryParamsSerializer.serializeSorting(queryParams));
urlBuilder.addQueryParameters(queryParamsSerializer.serializeGrouping(queryParams));
urlBuilder.addQueryParameters(queryParamsSerializer.serializePagination(queryParams));
urlBuilder.addQueryParameters(queryParamsSerializer.serializeIncludedFields(queryParams));
urlBuilder.addQueryParameters(queryParamsSerializer.serializeIncludedRelations(queryParams));
}
return urlBuilder.toString();
}
Aggregations