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);
}
}
}
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));
}
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));
}
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"));
}
Aggregations