Search in sources :

Example 1 with DocumentItemToDynamicDocumentItem

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

the class DocumentsS3Update method processS3File.

/**
 * Process S3 File.
 *
 * @param logger {@link LambdaLogger}
 * @param create boolean
 * @param s3bucket {@link String}
 * @param s3key {@link String}
 * @param debug boolean
 * @throws FileNotFoundException FileNotFoundException
 */
private void processS3File(final LambdaLogger logger, final boolean create, final String s3bucket, final String s3key, final boolean debug) throws FileNotFoundException {
    String key = urlDecode(s3key);
    String siteId = getSiteId(key);
    String documentId = resetDatabaseKey(siteId, key);
    try (S3Client s3 = this.s3service.buildClient()) {
        S3ObjectMetadata resp = this.s3service.getObjectMetadata(s3, s3bucket, key);
        if (!resp.isObjectExists()) {
            throw new FileNotFoundException("Object " + documentId + " not found in bucket " + s3bucket);
        }
        String contentType = resp.getContentType();
        Long contentLength = resp.getContentLength();
        DocumentItem item = this.service.findDocument(siteId, documentId);
        if (item != null) {
            DynamicDocumentItem doc = new DocumentItemToDynamicDocumentItem().apply(item);
            if (contentType != null && contentType.length() > 0) {
                doc.setContentType(contentType);
            }
            doc.setChecksum(resp.getEtag());
            if (contentLength != null) {
                doc.setContentLength(contentLength);
            }
            logger.log("saving document " + createDatabaseKey(siteId, item.getDocumentId()));
            List<DynamicDocumentTag> tags = getObjectTags(s3, item, s3bucket, key);
            doc.put("tags", tags);
            if (debug) {
                logger.log("original " + this.gson.toJson(item));
                logger.log("new " + this.gson.toJson(doc));
            }
            this.service.saveDocumentItemWithTag(siteId, doc);
            this.service.deleteDocumentFormats(siteId, item.getDocumentId());
            String content = getContent(s3bucket, key, s3, resp, doc);
            sendSnsMessage(logger, create ? "create" : "update", siteId, doc, s3bucket, key, content);
        } else {
            logger.log("Cannot find document " + documentId + " in site " + siteId);
        }
    }
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentTagToDynamicDocumentTag(com.formkiq.stacks.dynamodb.DocumentTagToDynamicDocumentTag) FileNotFoundException(java.io.FileNotFoundException) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) S3ObjectMetadata(com.formkiq.aws.s3.S3ObjectMetadata) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 2 with DocumentItemToDynamicDocumentItem

use of com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem 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));
}
Also used : ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 3 with DocumentItemToDynamicDocumentItem

use of com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem 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 DocumentItemToDynamicDocumentItem

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

the class ApiRequestHandlerTest method testHandleGetRequest06.

/**
 * Get Document Request, Document with sub docs found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest06() throws Exception {
    // given
    Date date = new Date();
    String documentId0 = "1a1d1938-451e-4e20-bf95-e0e7a749505a";
    String documentId1 = UUID.randomUUID().toString();
    String userId = "jsmith";
    DocumentItem item = new DocumentItemDynamoDb(documentId0, date, userId);
    DocumentItem citem = new DocumentItemDynamoDb(documentId1, date, userId);
    DynamicDocumentItem doc = new DocumentItemToDynamicDocumentItem().apply(item);
    doc.put("documents", Arrays.asList(new DocumentItemToDynamicDocumentItem().apply(citem)));
    getDocumentService().saveDocumentItemWithTag(null, doc);
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid02.json");
    // when
    String response = handleRequest(event);
    // then
    Map<String, String> m = fromJson(response, Map.class);
    final int mapsize = 3;
    assertEquals(mapsize, m.size());
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    assertEquals(documentId0, resp.get("documentId"));
    assertEquals(userId, resp.get("userId"));
    assertNotNull(resp.get("insertedDate"));
    assertEquals(DEFAULT_SITE_ID, resp.get("siteId"));
    assertNull(resp.get("next"));
    assertNull(resp.get("previous"));
    List<Map<String, Object>> children = (List<Map<String, Object>>) resp.get("documents");
    assertEquals(1, children.size());
    assertEquals(documentId1, children.get(0).get("documentId"));
    assertNotNull(userId, children.get(0).get("userId"));
    assertNotNull(children.get(0).get("belongsToDocumentId"));
    assertNotNull(children.get(0).get("insertedDate"));
    assertNull(children.get(0).get("siteId"));
}
Also used : DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) Date(java.util.Date) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ArrayList(java.util.ArrayList) List(java.util.List) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)4 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)4 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)4 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)2 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)2 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)2 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)1 S3ObjectMetadata (com.formkiq.aws.s3.S3ObjectMetadata)1 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)1 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)1 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)1 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)1 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)1 BadException (com.formkiq.lambda.apigateway.exception.BadException)1 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)1 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)1