use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.
the class DocumentsUploadRequestHandler method get.
@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
boolean documentExists = false;
Date date = new Date();
String documentId = UUID.randomUUID().toString();
String username = getCallingCognitoUsername(event);
DocumentItem item = new DocumentItemDynamoDb(documentId, date, username);
List<DocumentTag> tags = new ArrayList<>();
Map<String, String> query = event.getQueryStringParameters();
String siteId = authorizer.getSiteId();
DocumentService service = awsservice.documentService();
if (query != null && query.containsKey("path")) {
String path = query.get("path");
path = URLDecoder.decode(path, StandardCharsets.UTF_8.toString());
item.setPath(path);
tags.add(new DocumentTag(documentId, "path", path, date, username, DocumentTagType.SYSTEMDEFINED));
}
String urlstring = generatePresignedUrl(awsservice, logger, siteId, documentId, query);
logger.log("generated presign url: " + urlstring + " for document " + documentId);
if (!documentExists) {
tags.add(new DocumentTag(documentId, "untagged", "true", date, username, DocumentTagType.SYSTEMDEFINED));
String value = this.restrictionMaxDocuments.getValue(awsservice, siteId);
if (!this.restrictionMaxDocuments.enforced(awsservice, siteId, value)) {
logger.log("saving document: " + item.getDocumentId() + " on path " + item.getPath());
service.saveDocument(siteId, item, tags);
if (value != null) {
awsservice.documentCountService().incrementDocumentCount(siteId);
}
} else {
throw new BadException("Max Number of Documents reached");
}
}
return new ApiRequestHandlerResponse(SC_OK, new ApiUrlResponse(urlstring, documentId));
}
use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.
the class WebhooksRequestHandler method saveWebhook.
private String saveWebhook(final ApiGatewayRequestEvent event, final AwsServiceCache awsservice, final String siteId, final DynamicObject o) {
Date ttlDate = getTtlDate(awsservice, siteId, o);
String name = o.getString("name");
String userId = getCallingCognitoUsername(event);
String enabled = o.containsKey("enabled") ? o.getString("enabled") : "true";
String id = awsservice.webhookService().saveWebhook(siteId, name, userId, ttlDate, enabled);
if (o.containsKey("tags")) {
List<DynamicObject> dtags = o.getList("tags");
Date date = new Date();
Collection<DocumentTag> tags = dtags.stream().map(d -> new DocumentTag(null, d.getString("key"), d.getString("value"), date, userId)).collect(Collectors.toList());
awsservice.webhookService().addTags(siteId, id, tags, ttlDate);
}
return id;
}
use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.
the class DocumentsS3Update method getObjectTags.
/**
* Get Object Tags from S3.
*
* @param s3 {@link S3Client}
* @param item {@link DocumentItem}
* @param bucket {@link String}
* @param documentId {@link String}
* @return {@link List} {@link DynamicDocumentTag}
*/
private List<DynamicDocumentTag> getObjectTags(final S3Client s3, final DocumentItem item, final String bucket, final String documentId) {
GetObjectTaggingResponse objectTags = this.s3service.getObjectTags(s3, bucket, documentId);
List<DocumentTag> tags = objectTags.tagSet().stream().map(t -> new DocumentTag(documentId, t.key(), t.value(), item.getInsertedDate(), item.getUserId())).collect(Collectors.toList());
// Any System Defined Tags in the S3 Metadata, set them to SystemDefined.
tags.stream().filter(t -> SYSTEM_DEFINED_TAGS.contains(t.getKey())).forEach(t -> t.setType(DocumentTagType.SYSTEMDEFINED));
List<DynamicDocumentTag> dtags = tags.stream().map(t -> new DocumentTagToDynamicDocumentTag().apply(t)).collect(Collectors.toList());
return dtags;
}
use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.
the class DocumentsS3UpdateTest method testHandleRequest05.
/**
* Test Processing Document with sub documents.
*
* @throws Exception Exception
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest05() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();
Date now = new Date();
DynamicDocumentItem doc = createSubDocuments(now);
service.saveDocumentItemWithTag(siteId, doc);
String key = createDatabaseKey(siteId, doc.getDocumentId());
final Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event1.json", BUCKET_KEY, key);
addS3File(key, "pdf", false, "testdata");
// when
DocumentItem item = handleRequest(siteId, doc.getDocumentId(), map);
DocumentItem itemchild = handleRequest(siteId, doc.getDocuments().get(0).getDocumentId(), map);
// then
try (DynamoDbClient client = dbBuilder.build()) {
Map<String, AttributeValue> m = client.getItem(GetItemRequest.builder().tableName("Documents").key(keysDocument(siteId, doc.getDocumentId())).build()).item();
assertNotNull(m.get(GSI1_PK));
Map<String, AttributeValue> mchild = client.getItem(GetItemRequest.builder().tableName("Documents").key(keysDocument(siteId, itemchild.getDocumentId())).build()).item();
assertNull(mchild.get(GSI1_PK));
}
assertEquals(doc.getDocumentId(), item.getDocumentId());
PaginationResults<DocumentTag> tags = service.findDocumentTags(siteId, doc.getDocumentId(), null, MAX_RESULTS);
assertEquals(2, tags.getResults().size());
assertDocumentTagEquals(new DocumentTag().setKey("category").setValue("none").setType(DocumentTagType.USERDEFINED).setDocumentId(doc.getDocumentId()), tags.getResults().get(0));
assertDocumentTagEquals(new DocumentTag().setKey("userId").setValue(doc.getUserId()).setType(DocumentTagType.SYSTEMDEFINED).setDocumentId(doc.getDocumentId()), tags.getResults().get(1));
tags = service.findDocumentTags(siteId, itemchild.getDocumentId(), null, MAX_RESULTS);
assertEquals(1, tags.getResults().size());
assertDocumentTagEquals(new DocumentTag().setKey("category1").setValue("").setType(DocumentTagType.USERDEFINED).setDocumentId(itemchild.getDocumentId()), tags.getResults().get(0));
}
}
use of com.formkiq.stacks.dynamodb.DocumentTag in project formkiq-core by formkiq.
the class DocumentsS3UpdateTest method testHandleRequest04.
/**
* Create Document Request on child document.
*
* @throws Exception Exception
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest04() throws Exception {
String documentId = UUID.randomUUID().toString();
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();
String key = createDatabaseKey(siteId, documentId);
final Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event1.json", BUCKET_KEY, key);
DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
doc.setInsertedDate(new Date());
doc.setDocumentId(BUCKET_KEY);
doc.setPath("test.txt");
doc.setUserId("joe");
DynamicDocumentItem child = new DynamicDocumentItem(Map.of());
child.setInsertedDate(new Date());
child.setDocumentId(documentId);
child.setUserId("joe");
doc.put("documents", Arrays.asList(child));
service.saveDocumentItemWithTag(siteId, doc);
addS3File(key, "pdf", false, "testdata");
// when
final DocumentItem item = handleRequest(siteId, child.getDocumentId(), map);
// then
assertNotNull(item.getBelongsToDocumentId());
PaginationResults<DocumentTag> tags = service.findDocumentTags(siteId, BUCKET_KEY, null, MAX_RESULTS);
try (DynamoDbClient client = dbBuilder.build()) {
Map<String, AttributeValue> m = client.getItem(GetItemRequest.builder().tableName("Documents").key(keysDocument(siteId, child.getDocumentId())).build()).item();
assertNull(m.get(GSI1_PK));
}
final int count = 3;
int i = 0;
assertEquals(count, tags.getResults().size());
assertDocumentTagEquals(new DocumentTag().setKey("path").setValue("test.txt").setDocumentId(BUCKET_KEY).setType(DocumentTagType.SYSTEMDEFINED).setUserId("joe"), tags.getResults().get(i++));
assertDocumentTagEquals(new DocumentTag().setKey("untagged").setValue("true").setDocumentId(BUCKET_KEY).setType(DocumentTagType.SYSTEMDEFINED).setUserId("joe"), tags.getResults().get(i++));
assertDocumentTagEquals(new DocumentTag().setKey("userId").setValue("joe").setDocumentId(BUCKET_KEY).setType(DocumentTagType.SYSTEMDEFINED).setUserId("joe"), tags.getResults().get(i++));
assertEquals(0, service.findDocumentFormats(siteId, BUCKET_KEY, null, MAX_RESULTS).getResults().size());
verifyDocumentSaved(siteId, item, "pdf", "8");
assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "create", false, true);
tags = service.findDocumentTags(siteId, documentId, null, MAX_RESULTS);
assertEquals(0, tags.getResults().size());
}
}
Aggregations