Skip to content

Commit

Permalink
Merge pull request #3357 from lorthirk/feature-restApiSortRound2
Browse files Browse the repository at this point in the history
REST API Entities Sorting Round 2
  • Loading branch information
Coduz committed Jul 29, 2021
2 parents dd0d015 + 597a2ca commit c090030
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
Expand Up @@ -12,13 +12,17 @@
*******************************************************************************/
package org.eclipse.kapua.app.api.resources.v1.resources;

import java.util.Collections;

import com.google.common.base.Strings;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
import org.eclipse.kapua.app.api.core.model.CountResult;
import org.eclipse.kapua.app.api.core.model.ScopeId;
import org.eclipse.kapua.app.api.core.model.StorableEntityId;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.query.SortOrder;
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.datastore.ChannelInfoFactory;
import org.eclipse.kapua.service.datastore.ChannelInfoRegistryService;
Expand All @@ -29,6 +33,8 @@
import org.eclipse.kapua.service.datastore.model.query.ChannelInfoQuery;
import org.eclipse.kapua.service.datastore.model.query.predicate.ChannelMatchPredicate;
import org.eclipse.kapua.service.datastore.model.query.predicate.DatastorePredicateFactory;
import org.eclipse.kapua.service.storable.model.query.SortDirection;
import org.eclipse.kapua.service.storable.model.query.SortField;
import org.eclipse.kapua.service.storable.model.query.predicate.AndPredicate;
import org.eclipse.kapua.service.storable.model.query.predicate.TermPredicate;

Expand All @@ -53,20 +59,24 @@ public class DataChannels extends AbstractKapuaResource {
/**
* Gets the {@link ChannelInfo} list in the scope.
*
* @param scopeId The {@link ScopeId} in which to search results.
* @param clientId The client id to filter results.
* @param name The channel name to filter results. It allows '#' wildcard in last channel level
* @param offset The result set offset.
* @param limit The result set limit.
* @return The {@link ChannelInfoListResult} of all the channelInfos associated to the current selected scope.
* @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
* @param scopeId The {@link ScopeId} in which to search results.
* @param clientId The client id to filter results.
* @param name The channel name to filter results. It allows '#' wildcard in last channel level
* @param sortParam The name of the parameter that will be used as a sorting key
* @param sortDir The sort direction. Can be ASCENDING (default), DESCENDING. Case-insensitive.
* @param offset The result set offset.
* @param limit The result set limit.
* @return The {@link ChannelInfoListResult} of all the channelInfos associated to the current selected scope.
* @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public ChannelInfoListResult simpleQuery(@PathParam("scopeId") ScopeId scopeId,
@QueryParam("clientId") String clientId,
@QueryParam("name") String name,
@QueryParam("sortParam") String sortParam,
@QueryParam("sortDir") @DefaultValue("ASCENDING") SortOrder sortDir,
@QueryParam("offset") @DefaultValue("0") int offset,
@QueryParam("limit") @DefaultValue("50") int limit)
throws KapuaException {
Expand All @@ -85,7 +95,10 @@ public ChannelInfoListResult simpleQuery(@PathParam("scopeId") ScopeId scopeId,
query.setPredicate(andPredicate);
query.setOffset(offset);
query.setLimit(limit);

if (!Strings.isNullOrEmpty(sortParam)) {
SortDirection storableSortDirection = sortDir == SortOrder.DESCENDING ? SortDirection.DESC : SortDirection.ASC;
query.setSortFields(Collections.singletonList(SortField.of(sortParam, storableSortDirection)));
}
return query(scopeId, query);
}

Expand Down
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.kapua.app.api.resources.v1.resources;

import com.google.common.base.Strings;

import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
Expand Down Expand Up @@ -103,6 +104,9 @@ public DeviceEventListResult simpleQuery(

query.setPredicate(andPredicate);

if (!Strings.isNullOrEmpty(sortParam)) {
query.setSortCriteria(query.fieldSortCriteria(sortParam, sortDir));
}
query.setOffset(offset);
query.setLimit(limit);

Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.KapuaEntityAttributes;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.SortOrder;
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.job.Job;
import org.eclipse.kapua.service.job.execution.JobExecution;
Expand All @@ -44,6 +45,8 @@
import org.eclipse.kapua.service.job.targets.JobTargetQuery;
import org.eclipse.kapua.service.job.targets.JobTargetService;

import com.google.common.base.Strings;

@Path("{scopeId}/jobs/{jobId}/executions")
public class JobExecutions extends AbstractKapuaResource {

Expand All @@ -59,6 +62,8 @@ public class JobExecutions extends AbstractKapuaResource {
* @param scopeId The {@link ScopeId} in which to search results.
* @param jobId The {@link Job} id to filter results
* @param askTotalCount Ask for the total count of the matched entities in the result
* @param sortParam The name of the parameter that will be used as a sorting key
* @param sortDir The sort direction. Can be ASCENDING (default), DESCENDING. Case-insensitive.
* @param offset The result set offset.
* @param limit The result set limit.
* @return The {@link JobExecutionListResult} of all the jobs executions associated to the current selected job.
Expand All @@ -71,12 +76,18 @@ public JobExecutionListResult simpleQuery(
@PathParam("scopeId") ScopeId scopeId,
@PathParam("jobId") EntityId jobId,
@QueryParam("askTotalCount") boolean askTotalCount,
@QueryParam("sortParam") String sortParam,
@QueryParam("sortDir") @DefaultValue("ASCENDING") SortOrder sortDir,
@QueryParam("offset") @DefaultValue("0") int offset,
@QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
JobExecutionQuery query = jobExecutionFactory.newQuery(scopeId);

query.setPredicate(query.attributePredicate(JobExecutionAttributes.JOB_ID, jobId));

if (!Strings.isNullOrEmpty(sortParam)) {
query.setSortCriteria(query.fieldSortCriteria(sortParam, sortDir));
}

query.setAskTotalCount(askTotalCount);
query.setOffset(offset);
query.setLimit(limit);
Expand Down
Expand Up @@ -30,6 +30,20 @@ paths:
schema:
type: string
description: The channel name to filter results. It allows '#' wildcard in last channel level
- description: The sort parameter
name: sortParam
in: query
schema:
type: string
- description: The sort direction. Can be ascending (default) or descending.
name: sortDir
in: query
schema:
type: string
enum:
- ASCENDING
- DESCENDING
default: ASCENDING
- $ref: '../openapi.yaml#/components/parameters/limit'
- $ref: '../openapi.yaml#/components/parameters/offset'
responses:
Expand Down

0 comments on commit c090030

Please sign in to comment.