use of com.formkiq.lambda.apigateway.ApiPagination in project formkiq-core by formkiq.
the class ApiGatewayRequestEventUtil method toPaginationToken.
/**
* Convert {@link String} to {@link ApiPagination}.
*
* @param cacheService {@link CacheService}
* @param key {@link String}
*
* @return {@link ApiPagination}
*/
public static ApiPagination toPaginationToken(final CacheService cacheService, final String key) {
ApiPagination pagination = null;
if (isNotBlank(key)) {
String json = cacheService.read(key);
if (isNotBlank(json)) {
Gson gson = GsonUtil.getInstance();
pagination = gson.fromJson(json, ApiPagination.class);
}
}
return pagination;
}
use of com.formkiq.lambda.apigateway.ApiPagination in project formkiq-core by formkiq.
the class ApiGatewayRequestEventUtil method createPagination.
/**
* Create Pagination.
*
* @param cacheService {@link CacheService}
* @param event {@link ApiGatewayRequestEvent}
* @param lastPagination {@link ApiPagination}
* @param results {@link PaginationResults}
* @param limit int
*
* @return {@link ApiPagination}
*/
public static ApiPagination createPagination(final CacheService cacheService, final ApiGatewayRequestEvent event, final ApiPagination lastPagination, final PaginationResults<?> results, final int limit) {
ApiPagination current = null;
final Map<String, String> q = getQueryParameterMap(event);
Gson gson = GsonUtil.getInstance();
PaginationMapToken token = results.getToken();
if (isPaginationPrevious(q)) {
String json = cacheService.read(q.get("previous"));
current = gson.fromJson(json, ApiPagination.class);
} else {
current = new ApiPagination();
current.setLimit(limit);
current.setPrevious(lastPagination != null ? lastPagination.getNext() : null);
current.setStartkey(token);
current.setHasNext(token != null);
cacheService.write(current.getNext(), gson.toJson(current), 1);
}
return current;
}
use of com.formkiq.lambda.apigateway.ApiPagination in project formkiq-core by formkiq.
the class ApiGatewayRequestEventUtil method getPagination.
/**
* Find Query Parameter 'next' or 'prev' and convert to {@link ApiPagination}.
*
* @param cacheService {@link CacheService}
* @param event {@link ApiGatewayRequestEvent}
* @return {@link ApiPagination}
*/
public static ApiPagination getPagination(final CacheService cacheService, final ApiGatewayRequestEvent event) {
ApiPagination pagination = null;
Map<String, String> q = getQueryParameterMap(event);
if (isPaginationNext(q)) {
pagination = toPaginationToken(cacheService, q.get("next"));
} else if (isPaginationPrevious(q)) {
pagination = toPaginationToken(cacheService, q.get("previous"));
if (pagination.getPrevious() != null) {
pagination = toPaginationToken(cacheService, pagination.getPrevious());
} else {
// if @ start of list, preserve the limit
int limit = pagination.getLimit();
pagination = new ApiPagination();
pagination.setLimit(limit);
}
}
if (pagination != null && pagination.getLimit() < 1) {
pagination.setLimit(MAX_RESULTS);
}
return pagination;
}
use of com.formkiq.lambda.apigateway.ApiPagination in project formkiq-core by formkiq.
the class DocumentIdRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
String siteId = authorizer.getSiteId();
int limit = getLimit(logger, event);
ApiPagination token = getPagination(awsservice.documentCacheService(), event);
String documentId = event.getPathParameters().get("documentId");
ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
PaginationResult<DocumentItem> presult = awsservice.documentService().findDocument(siteId, documentId, true, token != null ? token.getStartkey() : null, limit);
DocumentItem result = presult.getResult();
if (result == null) {
throw new NotFoundException("Document " + documentId + " not found.");
}
ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, presult.getToken(), limit);
DynamicDocumentItem item = new DocumentItemToDynamicDocumentItem().apply(result);
item.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
item.put("previous", current.getPrevious());
item.put("next", current.hasNext() ? current.getNext() : null);
return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(item));
}
use of com.formkiq.lambda.apigateway.ApiPagination in project formkiq-core by formkiq.
the class DocumentTagsRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
CacheService cacheService = awsservice.documentCacheService();
ApiPagination pagination = getPagination(cacheService, event);
int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
String siteId = authorizer.getSiteId();
String documentId = event.getPathParameters().get("documentId");
PaginationResults<DocumentTag> results = awsservice.documentService().findDocumentTags(siteId, documentId, ptoken, limit);
results.getResults().forEach(r -> r.setDocumentId(null));
ApiPagination current = createPagination(cacheService, event, pagination, results.getToken(), limit);
List<DocumentTag> tags = subList(results.getResults(), limit);
List<ApiDocumentTagItemResponse> list = tags.stream().map(t -> {
ApiDocumentTagItemResponse r = new ApiDocumentTagItemResponse();
r.setDocumentId(t.getDocumentId());
r.setInsertedDate(t.getInsertedDate());
r.setKey(t.getKey());
r.setValue(t.getValue());
r.setValues(t.getValues());
r.setUserId(t.getUserId());
r.setType(t.getType() != null ? t.getType().name().toLowerCase() : null);
return r;
}).collect(Collectors.toList());
ApiDocumentTagsItemResponse resp = new ApiDocumentTagsItemResponse();
resp.setTags(list);
resp.setPrevious(current.getPrevious());
resp.setNext(current.hasNext() ? current.getNext() : null);
return new ApiRequestHandlerResponse(SC_OK, resp);
}
Aggregations