Skip to content

Commit 3264f94

Browse files
committed
#269 - Add an endpoint to retrieve a folder index key based on the path
1 parent f1b4728 commit 3264f94

File tree

11 files changed

+193
-60
lines changed

11 files changed

+193
-60
lines changed

docs/openapi/openapi-iam.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,7 @@
26462646
parameters:
26472647
- $ref: '#/components/parameters/siteIdParam'
26482648
- $ref: '#/components/parameters/indexKeyQueryParam'
2649+
- $ref: '#/components/parameters/pathQueryParam'
26492650
- $ref: '#/components/parameters/limitParam'
26502651
- $ref: '#/components/parameters/shareKeyParam'
26512652
- $ref: '#/components/parameters/nextParam'
@@ -6406,6 +6407,13 @@
64066407
required: false
64076408
schema:
64086409
type: string
6410+
pathQueryParam:
6411+
name: path
6412+
in: query
6413+
description: Path query parameter (must be URL Encoded)
6414+
required: false
6415+
schema:
6416+
type: string
64096417
indexKeyParam:
64106418
name: indexKey
64116419
in: path

docs/openapi/openapi-jwt.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,7 @@
26462646
parameters:
26472647
- $ref: '#/components/parameters/siteIdParam'
26482648
- $ref: '#/components/parameters/indexKeyQueryParam'
2649+
- $ref: '#/components/parameters/pathQueryParam'
26492650
- $ref: '#/components/parameters/limitParam'
26502651
- $ref: '#/components/parameters/shareKeyParam'
26512652
- $ref: '#/components/parameters/nextParam'
@@ -6406,6 +6407,13 @@
64066407
required: false
64076408
schema:
64086409
type: string
6410+
pathQueryParam:
6411+
name: path
6412+
in: query
6413+
description: Path query parameter (must be URL Encoded)
6414+
required: false
6415+
schema:
6416+
type: string
64096417
indexKeyParam:
64106418
name: indexKey
64116419
in: path

docs/openapi/openapi-key.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,7 @@
26462646
parameters:
26472647
- $ref: '#/components/parameters/siteIdParam'
26482648
- $ref: '#/components/parameters/indexKeyQueryParam'
2649+
- $ref: '#/components/parameters/pathQueryParam'
26492650
- $ref: '#/components/parameters/limitParam'
26502651
- $ref: '#/components/parameters/shareKeyParam'
26512652
- $ref: '#/components/parameters/nextParam'
@@ -6406,6 +6407,13 @@
64066407
required: false
64076408
schema:
64086409
type: string
6410+
pathQueryParam:
6411+
name: path
6412+
in: query
6413+
description: Path query parameter (must be URL Encoded)
6414+
required: false
6415+
schema:
6416+
type: string
64096417
indexKeyParam:
64106418
name: indexKey
64116419
in: path

dynamodb-documents/src/main/java/com/formkiq/stacks/dynamodb/FolderIndexProcessorImpl.java

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.nio.charset.StandardCharsets;
3232
import java.text.SimpleDateFormat;
3333
import java.util.ArrayList;
34-
import java.util.Arrays;
3534
import java.util.Collections;
3635
import java.util.Date;
3736
import java.util.Deque;
@@ -297,7 +296,7 @@ private Map<String, Map<String, AttributeValue>> generateFileKeys(final String s
297296

298297
String sk = getSk(folder, true);
299298
String docId = documentId;
300-
FolderIndexRecord record = null;
299+
FolderIndexRecord record;
301300
if (docId == null) {
302301
record = getFolderId(siteId, pk, sk, folder);
303302
} else {
@@ -353,8 +352,8 @@ public List<Map<String, AttributeValue>> generateIndex(final String siteId,
353352
record.documentId(item.getDocumentId());
354353
}
355354

356-
return records.stream().filter(r -> r.isChanged()).map(r -> r.record().getAttributes(siteId))
357-
.collect(Collectors.toList());
355+
return records.stream().filter(FolderIndexRecordExtended::isChanged)
356+
.map(r -> r.record().getAttributes(siteId)).collect(Collectors.toList());
358357
}
359358

360359
@Override
@@ -371,7 +370,7 @@ public List<FolderIndexRecordExtended> get(final String siteId, final String pat
371370

372371
for (String token : tokens) {
373372

374-
boolean isRecordChanged = false;
373+
boolean isRecordChanged;
375374
String type = "file".equals(pathType) && (i == len - 1) ? "file" : "folder";
376375
FolderIndexRecord record =
377376
new FolderIndexRecord().parentDocumentId(parentId).documentId("").path(token).type(type);
@@ -425,25 +424,21 @@ record = record.getFromAttributes(siteId, attrs);
425424
@Override
426425
public FolderIndexRecord getFolderByDocumentId(final String siteId, final String documentId) {
427426

428-
Map<String, FolderIndexRecord> map = getFolderByDocumentIds(siteId, Arrays.asList(documentId));
429-
return map.containsKey(documentId) ? map.get(documentId) : null;
427+
Map<String, FolderIndexRecord> map = getFolderByDocumentIds(siteId, List.of(documentId));
428+
return map.getOrDefault(documentId, null);
430429
}
431430

432431
@Override
433432
public Map<String, FolderIndexRecord> getFolderByDocumentIds(final String siteId,
434433
final List<String> documentIds) {
435434

436-
List<Map<String, AttributeValue>> responses = documentIds.stream()
437-
.map(documentId -> queryForFolderByDocumentId(siteId, documentId))
438-
.filter(r -> !r.items().isEmpty()).map(r -> r.items().get(0)).collect(Collectors.toList());
439-
440-
Map<String, FolderIndexRecord> recordMap =
441-
responses.stream().map(map -> this.dynamoDb.get(map.get(PK), map.get(SK)))
442-
.map(attr -> new FolderIndexRecord().getFromAttributes(siteId, attr))
443-
.collect(Collectors.toMap(r -> r.documentId(), r -> r));
444-
445-
return recordMap;
435+
List<Map<String, AttributeValue>> responses =
436+
documentIds.stream().map(documentId -> queryForFolderByDocumentId(siteId, documentId))
437+
.filter(r -> !r.items().isEmpty()).map(r -> r.items().get(0)).toList();
446438

439+
return responses.stream().map(map -> this.dynamoDb.get(map.get(PK), map.get(SK)))
440+
.map(attr -> new FolderIndexRecord().getFromAttributes(siteId, attr))
441+
.collect(Collectors.toMap(FolderIndexRecord::documentId, r -> r));
447442
}
448443

449444
/**
@@ -466,9 +461,7 @@ private FolderIndexRecord getFolderId(final String siteId, final String pk, fina
466461
throw new IOException(String.format("index for '%s' does not exist", folder));
467462
}
468463

469-
FolderIndexRecord record = new FolderIndexRecord().getFromAttributes(siteId, map);
470-
471-
return record;
464+
return new FolderIndexRecord().getFromAttributes(siteId, map);
472465
}
473466

474467
@Override
@@ -519,7 +512,7 @@ public Map<String, String> getIndex(final String siteId, final String path) thro
519512

520513
String key = folders[folders.length - 1];
521514
map = keys.get(key).entrySet().stream()
522-
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().s()));
515+
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().s()));
523516
}
524517

525518
return map;
@@ -563,9 +556,8 @@ private boolean hasFiles(final String siteId, final String documentId) {
563556

564557
Map<String, AttributeValue> values = Map.of(":pk", AttributeValue.fromS(pk));
565558

566-
QueryRequest q =
567-
QueryRequest.builder().tableName(this.documentTableName).keyConditionExpression(expression)
568-
.expressionAttributeValues(values).limit(Integer.valueOf(1)).build();
559+
QueryRequest q = QueryRequest.builder().tableName(this.documentTableName)
560+
.keyConditionExpression(expression).expressionAttributeValues(values).limit(1).build();
569561

570562
QueryResponse response = this.dbClient.query(q);
571563

@@ -618,7 +610,7 @@ private void moveFileToFolder(final String siteId, final String sourcePath,
618610
if (targetRecords.isEmpty()) {
619611
FolderIndexRecord record =
620612
new FolderIndexRecord().parentDocumentId("").type("folder").documentId("").path("");
621-
targetRecords = Arrays.asList(new FolderIndexRecordExtended(record, false));
613+
targetRecords = List.of(new FolderIndexRecordExtended(record, false));
622614
}
623615

624616
FolderIndexRecordExtended sourceRecord = last(sourceRecords);
@@ -633,8 +625,9 @@ private void moveFileToFolder(final String siteId, final String sourcePath,
633625
source.parentDocumentId(target.documentId());
634626

635627
// save target folder if needed and source
636-
List<Map<String, AttributeValue>> toBeSaved = targetRecords.stream().filter(r -> r.isChanged())
637-
.map(r -> r.record().getAttributes(siteId)).collect(Collectors.toList());
628+
List<Map<String, AttributeValue>> toBeSaved =
629+
targetRecords.stream().filter(FolderIndexRecordExtended::isChanged)
630+
.map(r -> r.record().getAttributes(siteId)).collect(Collectors.toList());
638631
toBeSaved.add(source.getAttributes(siteId));
639632
this.dynamoDb.putItems(toBeSaved);
640633

@@ -688,16 +681,13 @@ private void moveFolderToFolder(final String siteId, final String sourcePath,
688681
this.dynamoDb.deleteItem(AttributeValue.fromS(source.pk(siteId)),
689682
AttributeValue.fromS(source.sk()));
690683

691-
// String site = siteId != null ? siteId : DEFAULT_SITE_ID;
692-
// final FolderEvent event = new FolderEvent().siteId(site).documentId(source.documentId())
693-
// .sourcePath(source.path()).destinationPath(destinationPath).type(FolderEventType.MOVE);
694-
695684
source.parentDocumentId(target.documentId());
696685
source.path(newPath);
697686

698687
// save target folder if needed and source
699-
List<Map<String, AttributeValue>> toBeSaved = targetRecords.stream().filter(r -> r.isChanged())
700-
.map(r -> r.record().getAttributes(siteId)).collect(Collectors.toList());
688+
List<Map<String, AttributeValue>> toBeSaved =
689+
targetRecords.stream().filter(FolderIndexRecordExtended::isChanged)
690+
.map(r -> r.record().getAttributes(siteId)).collect(Collectors.toList());
701691
toBeSaved.add(source.getAttributes(siteId));
702692
this.dynamoDb.putItems(toBeSaved);
703693

@@ -708,8 +698,8 @@ private void moveFolderToFolder(final String siteId, final String sourcePath,
708698
public void moveIndex(final String siteId, final String sourcePath, final String targetPath,
709699
final String userId) throws IOException {
710700

711-
String sourceType = sourcePath.endsWith("/") || "".equals(sourcePath) ? "folder" : "file";
712-
String targetType = targetPath.endsWith("/") || "".equals(targetPath) ? "folder" : "file";
701+
String sourceType = sourcePath.endsWith("/") || sourcePath.isEmpty() ? "folder" : "file";
702+
String targetType = targetPath.endsWith("/") || targetPath.isEmpty() ? "folder" : "file";
713703

714704
if ("file".equals(sourceType) && "folder".equals(targetType)) {
715705

@@ -735,8 +725,7 @@ public void moveIndex(final String siteId, final String sourcePath, final String
735725
private QueryResponse queryForFolderByDocumentId(final String siteId, final String documentId) {
736726
FolderIndexRecord record = new FolderIndexRecord().documentId(documentId);
737727
String pk = record.pkGsi1(siteId);
738-
QueryResponse response = this.dynamoDb.queryIndex(GSI1, AttributeValue.fromS(pk), null, 1);
739-
return response;
728+
return this.dynamoDb.queryIndex(GSI1, AttributeValue.fromS(pk), null, 1);
740729
}
741730

742731
/**
@@ -750,7 +739,7 @@ private void validateExists(final String path,
750739
final List<FolderIndexRecordExtended> sourceRecords) throws IOException {
751740

752741
Optional<FolderIndexRecordExtended> o =
753-
sourceRecords.stream().filter(r -> r.isChanged()).findAny();
742+
sourceRecords.stream().filter(FolderIndexRecordExtended::isChanged).findAny();
754743
if (o.isPresent()) {
755744
String msg = "folder '" + path + "' does not exist";
756745
throw new IOException(msg);

lambda-api-graalvm/src/main/resources/cloudformation/openapi-iam.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,7 @@ Resources:
26782678
parameters:
26792679
- $ref: '#/components/parameters/siteIdParam'
26802680
- $ref: '#/components/parameters/indexKeyQueryParam'
2681+
- $ref: '#/components/parameters/pathQueryParam'
26812682
- $ref: '#/components/parameters/limitParam'
26822683
- $ref: '#/components/parameters/shareKeyParam'
26832684
- $ref: '#/components/parameters/nextParam'
@@ -6446,6 +6447,13 @@ Resources:
64466447
required: false
64476448
schema:
64486449
type: string
6450+
pathQueryParam:
6451+
name: path
6452+
in: query
6453+
description: Path query parameter (must be URL Encoded)
6454+
required: false
6455+
schema:
6456+
type: string
64496457
indexKeyParam:
64506458
name: indexKey
64516459
in: path

lambda-api-graalvm/src/main/resources/cloudformation/openapi-jwt.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,7 @@ Resources:
26782678
parameters:
26792679
- $ref: '#/components/parameters/siteIdParam'
26802680
- $ref: '#/components/parameters/indexKeyQueryParam'
2681+
- $ref: '#/components/parameters/pathQueryParam'
26812682
- $ref: '#/components/parameters/limitParam'
26822683
- $ref: '#/components/parameters/shareKeyParam'
26832684
- $ref: '#/components/parameters/nextParam'
@@ -6446,6 +6447,13 @@ Resources:
64466447
required: false
64476448
schema:
64486449
type: string
6450+
pathQueryParam:
6451+
name: path
6452+
in: query
6453+
description: Path query parameter (must be URL Encoded)
6454+
required: false
6455+
schema:
6456+
type: string
64496457
indexKeyParam:
64506458
name: indexKey
64516459
in: path

lambda-api-graalvm/src/main/resources/cloudformation/openapi-key.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,7 @@ Resources:
26782678
parameters:
26792679
- $ref: '#/components/parameters/siteIdParam'
26802680
- $ref: '#/components/parameters/indexKeyQueryParam'
2681+
- $ref: '#/components/parameters/pathQueryParam'
26812682
- $ref: '#/components/parameters/limitParam'
26822683
- $ref: '#/components/parameters/shareKeyParam'
26832684
- $ref: '#/components/parameters/nextParam'
@@ -6446,6 +6447,13 @@ Resources:
64466447
required: false
64476448
schema:
64486449
type: string
6450+
pathQueryParam:
6451+
name: path
6452+
in: query
6453+
description: Path query parameter (must be URL Encoded)
6454+
required: false
6455+
schema:
6456+
type: string
64496457
indexKeyParam:
64506458
name: indexKey
64516459
in: path

lambda-api/src/integration/java/com/formkiq/stacks/api/awstest/FoldersRequestTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void testGetFolders01() throws Exception {
108108

109109
// when
110110
GetFoldersResponse folderDocuments =
111-
foldersApi.getFolderDocuments(siteId, null, null, null, null);
111+
foldersApi.getFolderDocuments(siteId, null, null, null, null, null);
112112

113113
// then
114114
assertNotNull(folderDocuments.getDocuments());
@@ -149,10 +149,10 @@ public void testPostFolders01() throws Exception {
149149
assertEquals("created folder", response.getMessage());
150150

151151
GetFoldersResponse folderDocuments =
152-
foldersApi.getFolderDocuments(siteId, null, "100", null, null);
152+
foldersApi.getFolderDocuments(siteId, null, null, "100", null, null);
153153
assertFalse(folderDocuments.getDocuments().isEmpty());
154154
folderDocuments =
155-
foldersApi.getFolderDocuments(siteId, response.getIndexKey(), "100", null, null);
155+
foldersApi.getFolderDocuments(siteId, response.getIndexKey(), null, "100", null, null);
156156
assertEquals(1, folderDocuments.getDocuments().size());
157157
assertEquals(folder + "/test.txt", folderDocuments.getDocuments().get(0).getPath());
158158
}

lambda-api/src/main/java/com/formkiq/stacks/api/handler/FoldersRequestHandler.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
package com.formkiq.stacks.api.handler;
2525

2626
import static com.formkiq.aws.services.lambda.ApiResponseStatus.SC_OK;
27+
28+
import java.util.Date;
2729
import java.util.HashMap;
2830
import java.util.List;
2931
import java.util.Map;
@@ -32,6 +34,7 @@
3234
import com.formkiq.aws.dynamodb.PaginationMapToken;
3335
import com.formkiq.aws.dynamodb.PaginationResults;
3436
import com.formkiq.aws.dynamodb.model.DynamicDocumentItem;
37+
import com.formkiq.aws.dynamodb.objects.Objects;
3538
import com.formkiq.aws.dynamodb.objects.Strings;
3639
import com.formkiq.aws.dynamodb.ApiAuthorization;
3740
import com.formkiq.aws.services.lambda.ApiGatewayRequestEvent;
@@ -45,6 +48,7 @@
4548
import com.formkiq.module.lambdaservices.AwsServiceCache;
4649
import com.formkiq.stacks.dynamodb.DocumentSearchService;
4750
import com.formkiq.stacks.dynamodb.FolderIndexProcessor;
51+
import com.formkiq.stacks.dynamodb.FolderIndexRecordExtended;
4852

4953
/** {@link ApiGatewayRequestHandler} for "/folders". */
5054
public class FoldersRequestHandler implements ApiGatewayRequestHandler, ApiGatewayRequestEventUtil {
@@ -94,10 +98,7 @@ public ApiRequestHandlerResponse get(final LambdaLogger logger,
9498
DocumentSearchService documentSearchService =
9599
awsservice.getExtension(DocumentSearchService.class);
96100

97-
String indexKey = event.getQueryStringParameter("indexKey");
98-
if (indexKey == null) {
99-
indexKey = "";
100-
}
101+
String indexKey = getIndexKey(event, awsservice, siteId);
101102

102103
PaginationResults<DynamicDocumentItem> results =
103104
documentSearchService.findInFolder(siteId, indexKey, ptoken, limit);
@@ -116,6 +117,31 @@ public ApiRequestHandlerResponse get(final LambdaLogger logger,
116117
return new ApiRequestHandlerResponse(SC_OK, resp);
117118
}
118119

120+
private String getIndexKey(final ApiGatewayRequestEvent event, final AwsServiceCache awsservice,
121+
final String siteId) {
122+
123+
String indexKey = event.getQueryStringParameter("indexKey");
124+
String path = event.getQueryStringParameter("path");
125+
126+
if (!Strings.isEmpty(path)) {
127+
FolderIndexProcessor indexProcessor = awsservice.getExtension(FolderIndexProcessor.class);
128+
129+
List<FolderIndexRecordExtended> folders =
130+
indexProcessor.get(siteId, path, "folder", "", new Date());
131+
FolderIndexRecordExtended folder = Objects.last(folders);
132+
133+
if (folder != null && folder.record() != null) {
134+
indexKey = folder.record().createIndexKey(siteId);
135+
}
136+
}
137+
138+
if (indexKey == null) {
139+
indexKey = "";
140+
}
141+
142+
return indexKey;
143+
}
144+
119145
@Override
120146
public String getRequestUrl() {
121147
return "/folders";

0 commit comments

Comments
 (0)