Search in sources :

Example 1 with PaginationMapToken

use of com.formkiq.stacks.dynamodb.PaginationMapToken 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;
}
Also used : ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) Gson(com.google.gson.Gson) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken)

Example 2 with PaginationMapToken

use of com.formkiq.stacks.dynamodb.PaginationMapToken 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);
}
Also used : SC_CREATED(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_CREATED) Arrays(java.util.Arrays) DocumentTags(com.formkiq.stacks.dynamodb.DocumentTags) Date(java.util.Date) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) ApiDocumentTagsItemResponse(com.formkiq.stacks.api.ApiDocumentTagsItemResponse) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) ApiDocumentTagItemResponse(com.formkiq.stacks.api.ApiDocumentTagItemResponse) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) List(java.util.List) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) CacheService(com.formkiq.stacks.dynamodb.CacheService) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) Collections(java.util.Collections) DocumentTagType(com.formkiq.stacks.dynamodb.DocumentTagType) PaginationResults(com.formkiq.stacks.dynamodb.PaginationResults) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiDocumentTagItemResponse(com.formkiq.stacks.api.ApiDocumentTagItemResponse) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) ApiDocumentTagsItemResponse(com.formkiq.stacks.api.ApiDocumentTagsItemResponse) CacheService(com.formkiq.stacks.dynamodb.CacheService)

Example 3 with PaginationMapToken

use of com.formkiq.stacks.dynamodb.PaginationMapToken in project formkiq-core by formkiq.

the class DocumentsRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
    final int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
    final PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
    String tz = getParameter(event, "tz");
    String dateString = getParameter(event, "date");
    if (StringUtils.isBlank(dateString)) {
        if (StringUtils.isNotBlank(tz)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            ZoneOffset offset = DateUtil.getZoneOffset(tz);
            sdf.setTimeZone(TimeZone.getTimeZone(offset));
            dateString = sdf.format(new Date());
        } else {
            dateString = this.df.format(new Date());
        }
    }
    ZonedDateTime date = transformToDate(logger, awsservice, dateString, tz);
    String siteId = authorizer.getSiteId();
    final PaginationResults<DocumentItem> results = awsservice.documentService().findDocumentsByDate(siteId, date, ptoken, limit);
    ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, results.getToken(), limit);
    List<DocumentItem> documents = subList(results.getResults(), limit);
    List<DynamicDocumentItem> items = documents.stream().map(m -> new DocumentItemToDynamicDocumentItem().apply(m)).collect(Collectors.toList());
    items.forEach(i -> i.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID));
    Map<String, Object> map = new HashMap<>();
    map.put("documents", items);
    map.put("previous", current.getPrevious());
    map.put("next", current.hasNext() ? current.getNext() : null);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(map));
}
Also used : StringUtils(software.amazon.awssdk.utils.StringUtils) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) TimeZone(java.util.TimeZone) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) ZoneRulesException(java.time.zone.ZoneRulesException) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) List(java.util.List) DateUtil(com.formkiq.stacks.dynamodb.DateUtil) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) PaginationResults(com.formkiq.stacks.dynamodb.PaginationResults) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) HashMap(java.util.HashMap) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) Date(java.util.Date) ZoneOffset(java.time.ZoneOffset) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) ZonedDateTime(java.time.ZonedDateTime) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with PaginationMapToken

use of com.formkiq.stacks.dynamodb.PaginationMapToken in project formkiq-core by formkiq.

the class SearchRequestHandler method post.

@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    DynamoDbCacheService cacheService = awsservice.documentCacheService();
    ApiPagination pagination = getPagination(cacheService, event);
    int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
    PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
    QueryRequest q = fromBodyToObject(logger, event, QueryRequest.class);
    if (q == null || q.query() == null || q.query().tag() == null) {
        throw new BadException("Invalid JSON body.");
    }
    Collection<String> documentIds = q.query().documentIds();
    if (documentIds != null) {
        if (documentIds.size() > MAX_DOCUMENT_IDS) {
            throw new BadException("Maximum number of DocumentIds is " + MAX_DOCUMENT_IDS);
        }
        if (!getQueryParameterMap(event).containsKey("limit")) {
            limit = documentIds.size();
        }
    }
    String siteId = authorizer.getSiteId();
    PaginationResults<DynamicDocumentItem> results = awsservice.documentSearchService().search(siteId, q.query(), ptoken, limit);
    ApiPagination current = createPagination(cacheService, event, pagination, results.getToken(), limit);
    List<DynamicDocumentItem> documents = subList(results.getResults(), limit);
    Map<String, Object> map = new HashMap<>();
    map.put("documents", documents);
    map.put("previous", current.getPrevious());
    map.put("next", current.hasNext() ? current.getNext() : null);
    ApiMapResponse resp = new ApiMapResponse(map);
    return new ApiRequestHandlerResponse(SC_OK, resp);
}
Also used : ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) QueryRequest(com.formkiq.stacks.api.QueryRequest) HashMap(java.util.HashMap) BadException(com.formkiq.lambda.apigateway.exception.BadException) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamoDbCacheService(com.formkiq.stacks.dynamodb.DynamoDbCacheService) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken)

Aggregations

ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)4 PaginationMapToken (com.formkiq.stacks.dynamodb.PaginationMapToken)4 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)3 BadException (com.formkiq.lambda.apigateway.exception.BadException)3 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)2 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)2 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)2 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)2 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)2 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)2 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)2 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)2 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)2 PaginationResults (com.formkiq.stacks.dynamodb.PaginationResults)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)1 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)1