Search in sources :

Example 1 with DynamicDocumentItem

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

the class DocumentsS3Update method processS3Delete.

/**
 * Process S3 Delete Request.
 *
 * @param logger {@link LambdaLogger}
 * @param bucket {@link String}
 * @param key {@link String}
 */
private void processS3Delete(final LambdaLogger logger, final String bucket, final String key) {
    String siteId = getSiteId(key.toString());
    String documentId = resetDatabaseKey(siteId, key.toString());
    DynamicDocumentItem doc = new DynamicDocumentItem(Map.of("documentId", documentId));
    String msg = String.format("Removing %s from bucket %s.", key, bucket);
    logger.log(msg);
    this.service.deleteDocument(siteId, documentId);
    sendSnsMessage(logger, "delete", siteId, doc, bucket, key, null);
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)

Example 2 with DynamicDocumentItem

use of com.formkiq.stacks.dynamodb.DynamicDocumentItem 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 3 with DynamicDocumentItem

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

the class StagingS3Create method processEvent.

/**
 * Process S3 Event.
 *
 * @param logger {@link LambdaLogger}
 * @param date {@link Date}
 * @param event {@link Map}
 */
private void processEvent(final LambdaLogger logger, final Date date, final Map<String, Object> event) {
    String eventName = event.get("eventName").toString();
    boolean objectCreated = eventName.contains("ObjectCreated");
    String bucket = getBucketName(event);
    String key = getObjectKey(event);
    String documentId = urlDecode(key);
    String siteId = getSiteId(documentId);
    if (objectCreated) {
        try (S3Client s = this.s3.buildClient()) {
            DynamicDocumentItem doc = configfile(s, logger, bucket, documentId);
            if (doc != null) {
                write(s, logger, doc, date, siteId);
            } else {
                copyFile(s, logger, bucket, documentId, date);
            }
            deleteObject(s, logger, bucket, documentId);
        }
    }
    if (!objectCreated) {
        logger.log("skipping event " + eventName);
    }
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 4 with DynamicDocumentItem

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

the class DocumentsS3UpdateTest method testHandleRequest02.

/**
 * Update Document Request with existing Tags.
 *
 * @throws Exception Exception
 */
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest02() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        this.logger.reset();
        String key = createDatabaseKey(siteId, BUCKET_KEY);
        final Map<String, Object> map = loadFileAsMap(this, "/objectupdate-event1.json", BUCKET_KEY, key);
        DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
        doc.setInsertedDate(new Date());
        doc.setDocumentId(BUCKET_KEY);
        doc.setUserId("asd");
        doc.setPath("test.txt");
        DynamicDocumentTag tag = new DynamicDocumentTag(Map.of("documentId", BUCKET_KEY, "key", "person", "value", "category", "insertedDate", new Date(), "userId", "asd"));
        doc.put("tags", Arrays.asList(tag));
        service.saveDocumentItemWithTag(siteId, doc);
        DocumentFormat format = new DocumentFormat();
        format.setContentType("application/pdf");
        format.setDocumentId(BUCKET_KEY);
        format.setInsertedDate(new Date());
        format.setUserId("asd");
        service.saveDocumentFormat(siteId, format);
        addS3File(key, "pdf", true, "testdata");
        // when
        final DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);
        // then
        PaginationResults<DocumentTag> tags = service.findDocumentTags(siteId, BUCKET_KEY, null, MAX_RESULTS);
        final int size = 5;
        int i = 0;
        assertEquals(size, tags.getResults().size());
        assertEquals("CLAMAV_SCAN_STATUS", tags.getResults().get(i).getKey());
        assertEquals("GOOD", tags.getResults().get(i).getValue());
        assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i++).getType());
        assertEquals("path", tags.getResults().get(i).getKey());
        assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i++).getType());
        assertEquals("person", tags.getResults().get(i).getKey());
        assertEquals(DocumentTagType.USERDEFINED, tags.getResults().get(i++).getType());
        assertEquals("12345", tags.getResults().get(i).getValue());
        assertEquals(DocumentTagType.USERDEFINED, tags.getResults().get(i).getType());
        assertEquals("12345", tags.getResults().get(i++).getValue());
        assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i).getType());
        assertEquals("asd", tags.getResults().get(i++).getValue());
        assertEquals(0, service.findDocumentFormats(siteId, BUCKET_KEY, null, MAX_RESULTS).getResults().size());
        verifyDocumentSaved(siteId, item, "pdf", "8");
        assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "update", false, false);
    }
}
Also used : DocumentFormat(com.formkiq.stacks.dynamodb.DocumentFormat) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) Date(java.util.Date) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 5 with DynamicDocumentItem

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

the class DocumentsS3UpdateTest method createSubDocuments.

/**
 * Create {@link DynamicDocumentItem} with Child Documents.
 * @param now {@link Date}
 * @return {@link DynamicDocumentItem}
 */
private DynamicDocumentItem createSubDocuments(final Date now) {
    String username = UUID.randomUUID() + "@formkiq.com";
    DynamicDocumentItem doc = new DynamicDocumentItem(Map.of("documentId", UUID.randomUUID().toString(), "userId", username, "insertedDate", now));
    doc.setContentType("text/plain");
    doc.put("tags", Arrays.asList(Map.of("documentId", doc.getDocumentId(), "key", "category", "value", "none", "insertedDate", now, "userId", username, "type", DocumentTagType.USERDEFINED.name())));
    DynamicDocumentItem doc1 = new DynamicDocumentItem(Map.of("documentId", UUID.randomUUID().toString(), "userId", username, "insertedDate", now));
    doc1.setContentType("text/html");
    doc1.put("tags", Arrays.asList(Map.of("documentId", doc1.getDocumentId(), "key", "category1", "insertedDate", now, "userId", username, "type", DocumentTagType.USERDEFINED.name())));
    DynamicDocumentItem doc2 = new DynamicDocumentItem(Map.of("documentId", UUID.randomUUID().toString(), "userId", username, "insertedDate", now));
    doc2.setContentType("application/json");
    doc2.put("tags", Arrays.asList(Map.of("documentId", doc2.getDocumentId(), "key", "category2", "insertedDate", now, "userId", username, "type", DocumentTagType.USERDEFINED.name())));
    doc.put("documents", Arrays.asList(doc1, doc2));
    return doc;
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem)

Aggregations

DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)27 Date (java.util.Date)18 Test (org.junit.jupiter.api.Test)15 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)14 Timeout (org.junit.jupiter.api.Timeout)9 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)7 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)6 S3Client (software.amazon.awssdk.services.s3.S3Client)6 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)5 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)4 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)4 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)4 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)3 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)3 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)3 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)3 SimpleDateFormat (java.text.SimpleDateFormat)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3