Search in sources :

Example 56 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class DocumentServiceImpl method saveDocumentItemWithTag.

@Override
public DocumentItem saveDocumentItemWithTag(final String siteId, final DynamicDocumentItem doc) {
    Date date = new Date();
    String username = doc.getUserId();
    String documentId = resetDatabaseKey(siteId, doc.getDocumentId());
    if (isDocumentUserTagged(doc.getList("tags"))) {
        deleteDocumentTag(siteId, documentId, "untagged");
    }
    DocumentItem item = new DocumentItemDynamoDb(documentId, date, username);
    String path = doc.getPath();
    item.setDocumentId(doc.getDocumentId());
    item.setPath(path);
    item.setContentType(doc.getContentType());
    item.setChecksum(doc.getChecksum());
    item.setContentLength(doc.getContentLength());
    item.setUserId(doc.getUserId());
    item.setInsertedDate(doc.getInsertedDate() != null ? doc.getInsertedDate() : date);
    item.setBelongsToDocumentId(doc.getBelongsToDocumentId());
    List<DocumentTag> tags = saveDocumentItemGenerateTags(siteId, doc, date, username);
    boolean saveGsi1 = doc.getBelongsToDocumentId() == null;
    Map<String, AttributeValue> keys = keysDocument(siteId, item.getDocumentId());
    saveDocument(keys, siteId, item, tags, saveGsi1, doc.getString("TimeToLive"));
    List<DynamicObject> documents = doc.getList("documents");
    for (DynamicObject subdoc : documents) {
        if (subdoc.getDate("insertedDate") == null) {
            subdoc.put("insertedDate", date);
        }
        DocumentItem document = new DynamicDocumentItem(subdoc);
        document.setBelongsToDocumentId(item.getDocumentId());
        DocumentItem dockey = new DynamicDocumentItem(new HashMap<>());
        dockey.setDocumentId(subdoc.getString("documentId"));
        dockey.setBelongsToDocumentId(item.getDocumentId());
        // save child document
        keys = keysDocument(siteId, item.getDocumentId(), Optional.of(subdoc.getString("documentId")));
        saveDocument(keys, siteId, dockey, null, false, doc.getString("TimeToLive"));
        List<DynamicObject> doctags = subdoc.getList("tags");
        tags = doctags.stream().map(t -> new DynamicObjectToDocumentTag().apply(t)).collect(Collectors.toList());
        keys = keysDocument(siteId, subdoc.getString("documentId"));
        saveDocument(keys, siteId, document, tags, false, doc.getString("TimeToLive"));
    }
    return item;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Date(java.util.Date) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject)

Example 57 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class WebhooksServiceImpl method findTags.

@Override
public PaginationResults<DynamicObject> findTags(final String siteId, final String webhookId, final PaginationMapToken token) {
    String expression = PK + " = :pk and begins_with(" + SK + ", :sk)";
    Map<String, AttributeValue> values = queryKeys(keysGeneric(siteId, PREFIX_WEBHOOK + webhookId, PREFIX_TAGS));
    Map<String, AttributeValue> startkey = new PaginationToAttributeValue().apply(token);
    QueryRequest q = QueryRequest.builder().tableName(this.documentTableName).keyConditionExpression(expression).expressionAttributeValues(values).exclusiveStartKey(startkey).build();
    QueryResponse result = this.dynamoDB.query(q);
    AttributeValueToDynamicObject transform = new AttributeValueToDynamicObject();
    List<DynamicObject> objs = result.items().stream().map(i -> transform.apply(i)).collect(Collectors.toList());
    return new PaginationResults<>(objs, new QueryResponseToPagination().apply(result));
}
Also used : BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Date(java.util.Date) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) SiteIdKeyGenerator.createDatabaseKey(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.createDatabaseKey) Map(java.util.Map) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) Collection(java.util.Collection) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Put(software.amazon.awssdk.services.dynamodb.model.Put) List(java.util.List) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) GetItemResponse(software.amazon.awssdk.services.dynamodb.model.GetItemResponse) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) Collections(java.util.Collections) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse)

Example 58 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class WebhooksServiceImpl method updateTimeToLive.

@Override
public void updateTimeToLive(final String siteId, final String webhookId, final Date ttl) {
    Map<String, AttributeValue> key = keysGeneric(siteId, PREFIX_WEBHOOK + webhookId, "webhook");
    Map<String, AttributeValueUpdate> values = new HashMap<>();
    addTimeToLiveUpdate(values, ttl);
    this.dynamoDB.updateItem(UpdateItemRequest.builder().tableName(this.documentTableName).key(key).attributeUpdates(values).build());
    PaginationResults<DynamicObject> result = findTags(siteId, webhookId, null);
    for (DynamicObject ob : result.getResults()) {
        key = new HashMap<>();
        key.put(PK, AttributeValue.builder().s(ob.getString(PK)).build());
        key.put(SK, AttributeValue.builder().s(ob.getString(SK)).build());
        this.dynamoDB.updateItem(UpdateItemRequest.builder().tableName(this.documentTableName).key(key).attributeUpdates(values).build());
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap)

Example 59 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class WebhooksServiceImpl method findWebhook.

@Override
public DynamicObject findWebhook(final String siteId, final String id) {
    Map<String, AttributeValue> pkvalues = keysGeneric(siteId, PREFIX_WEBHOOK + id, "webhook");
    GetItemResponse response = this.dynamoDB.getItem(GetItemRequest.builder().tableName(this.documentTableName).key(pkvalues).build());
    AttributeValueToDynamicObject transform = new AttributeValueToDynamicObject();
    DynamicObject ob = null;
    if (!response.item().isEmpty()) {
        ob = transform.apply(response.item());
        updateWebhookTimeToLive(ob);
    }
    return ob;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) GetItemResponse(software.amazon.awssdk.services.dynamodb.model.GetItemResponse)

Example 60 with DynamicObject

use of com.formkiq.stacks.common.objects.DynamicObject in project formkiq-core by formkiq.

the class DocumentsUploadRequestTest method testGet03.

/**
 * Get Request Upload Document Url, Content Length Greater than allowed.
 *
 * @throws Exception Exception
 */
@Test(timeout = TEST_TIMEOUT)
public void testGet03() throws Exception {
    // given
    final int contentLength = 100;
    getConfigService().save(SITEID0, new DynamicObject(Map.of(MAX_DOCUMENT_SIZE_BYTES, "5")));
    GetDocumentUploadRequest request = new GetDocumentUploadRequest().siteId(SITEID0).contentLength(contentLength);
    for (FormKiqClientV1 client : getFormKiqClients()) {
        // when
        HttpResponse<String> response = client.getDocumentUploadAsHttpResponse(request);
        // then
        assertEquals(STATUS_BAD_REQUEST, response.statusCode());
        assertRequestCorsHeaders(response.headers());
        assertEquals("{\"message\":\"'contentLength' cannot exceed 5 bytes\"}", response.body());
    }
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) GetDocumentUploadRequest(com.formkiq.stacks.client.requests.GetDocumentUploadRequest) FormKiqClientV1(com.formkiq.stacks.client.FormKiqClientV1) Test(org.junit.Test)

Aggregations

DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)93 Test (org.junit.jupiter.api.Test)55 Map (java.util.Map)46 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)39 Date (java.util.Date)26 HashMap (java.util.HashMap)17 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)12 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 ZonedDateTime (java.time.ZonedDateTime)10 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)8 List (java.util.List)8 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)6 BadException (com.formkiq.lambda.apigateway.exception.BadException)6 Collectors (java.util.stream.Collectors)6 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 FormKiqClientV1 (com.formkiq.stacks.client.FormKiqClientV1)5 LocalDate (java.time.LocalDate)5 Test (org.junit.Test)5