Skip to content

Commit

Permalink
Merge pull request #872 from Kapral67/issue-871-patch
Browse files Browse the repository at this point in the history
patch issue 871
  • Loading branch information
Kapral67 authored Apr 1, 2024
2 parents 7918269 + 46930af commit 8e19a59
Show file tree
Hide file tree
Showing 34 changed files with 482 additions and 362 deletions.
4 changes: 2 additions & 2 deletions AdminClient/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'org.familydirectory.sdk.adminclient'
version = '0.7'
version = '0.71'

compileJava {
sourceCompatibility = '21'
Expand All @@ -19,7 +19,7 @@ repositories {
}

dependencies {
implementation 'org.familydirectory.assets:familydirectory-service-assets:0.7'
implementation 'org.familydirectory.assets:familydirectory-service-assets:0.71'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.16.1'
implementation 'org.jetbrains:annotations:24.1.0'
implementation 'com.amazonaws:aws-lambda-java-core:1.2.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import org.familydirectory.sdk.adminclient.enums.Commands;
import org.familydirectory.sdk.adminclient.enums.cognito.CognitoManagementOptions;
import org.familydirectory.sdk.adminclient.enums.create.CreateOptions;
import org.familydirectory.sdk.adminclient.enums.flags.Flags;
import org.familydirectory.sdk.adminclient.events.cognito.CognitoManagementEvent;
import org.familydirectory.sdk.adminclient.events.create.CreateEvent;
import org.familydirectory.sdk.adminclient.events.delete.DeleteEvent;
import org.familydirectory.sdk.adminclient.events.flag.FlagEvent;
import org.familydirectory.sdk.adminclient.events.model.EventHelper;
import org.familydirectory.sdk.adminclient.events.stream.TogglePdfGeneratorEvent;
import org.familydirectory.sdk.adminclient.events.toolkitcleaner.ToolkitCleanerEvent;
Expand Down Expand Up @@ -125,6 +127,7 @@ void main (final String[] args) {
case TOGGLE_PDF_GENERATOR -> new TogglePdfGeneratorEvent(gui);
case COGNITO_MANAGEMENT -> new CognitoManagementEvent(gui, (CognitoManagementOptions) requireNonNull(option), cognitoPicker);
case TOOLKIT_CLEANER -> new ToolkitCleanerEvent(gui);
case FLAGS -> new FlagEvent(gui, (Flags) requireNonNull(option));
case EXIT -> null;
})
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.familydirectory.sdk.adminclient.enums.cognito.CognitoManagementOptions;
import org.familydirectory.sdk.adminclient.enums.create.CreateOptions;
import org.familydirectory.sdk.adminclient.enums.flags.Flags;
import org.jetbrains.annotations.Nullable;

public
Expand All @@ -12,6 +13,7 @@ enum Commands {
TOGGLE_PDF_GENERATOR(null),
COGNITO_MANAGEMENT(CognitoManagementOptions.values()),
TOOLKIT_CLEANER(null),
FLAGS(Flags.values()),
EXIT(null);

@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.familydirectory.sdk.adminclient.enums.flags;

public
enum Flags {
ISSUE_871
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.function.Predicate;
import org.familydirectory.assets.ddb.enums.DdbTable;
import org.familydirectory.assets.ddb.enums.family.FamilyTableParameter;
import org.familydirectory.assets.ddb.member.Member;
import org.familydirectory.assets.ddb.models.member.MemberRecord;
import org.familydirectory.sdk.adminclient.enums.create.CreateOptions;
import org.familydirectory.sdk.adminclient.events.model.MemberEventHelper;
Expand Down Expand Up @@ -170,7 +171,7 @@ TransactWriteItemsRequest buildCreateTransaction (final @NotNull MemberRecord me
transactionItems.add(TransactWriteItem.builder()
.put(Put.builder()
.tableName(DdbTable.MEMBER.name())
.item(MemberEventHelper.buildMember(memberRecord))
.item(Member.retrieveDdbMap(memberRecord))
.build())
.build());
return TransactWriteItemsRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.familydirectory.sdk.adminclient.events.flag;

import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import org.familydirectory.assets.ddb.enums.DdbTable;
import org.familydirectory.assets.ddb.enums.member.MemberTableParameter;
import org.familydirectory.assets.ddb.member.Member;
import org.familydirectory.assets.ddb.models.member.MemberRecord;
import org.familydirectory.sdk.adminclient.AdminClientTui;
import org.familydirectory.sdk.adminclient.enums.flags.Flags;
import org.familydirectory.sdk.adminclient.events.model.EventHelper;
import org.familydirectory.sdk.adminclient.utility.SdkClientProvider;
import org.familydirectory.sdk.adminclient.utility.lanterna.WaitingDialog;
import org.jetbrains.annotations.NotNull;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanResponse;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static java.util.Objects.isNull;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;

public final
class FlagEvent implements EventHelper {
private final @NotNull WindowBasedTextGUI gui;
private final @NotNull Flags flag;

public
FlagEvent (final @NotNull WindowBasedTextGUI gui, final @NotNull Flags flag) {
super();
this.gui = requireNonNull(gui);
this.flag = requireNonNull(flag);
}

@Override
public
void run () {
switch (this.flag) {
case ISSUE_871 -> {
final WaitingDialog waitDialog = WaitingDialog.createDialog(this.flag.name(), "Applying Database Migration, Please Wait");
waitDialog.setHints(AdminClientTui.EXTRA_WINDOW_HINTS);
waitDialog.showDialog(this.gui, false);
final CompletableFuture<?> future = CompletableFuture.runAsync(() -> {
try {
final SdkClientProvider sdkClientProvider = SdkClientProvider.getSdkClientProvider();
Map<String, AttributeValue> lastEvaluatedKey = emptyMap();
do {
final ScanRequest.Builder scanRequestBuilder = ScanRequest.builder()
.tableName(DdbTable.MEMBER.name())
.consistentRead(true);
if (!lastEvaluatedKey.isEmpty()) {
scanRequestBuilder.exclusiveStartKey(lastEvaluatedKey);
}

final ScanResponse scanResponse = sdkClientProvider.getSdkClient(DynamoDbClient.class)
.scan(scanRequestBuilder.build());
for (final Map<String, AttributeValue> entry : scanResponse.items()) {
final UUID id = UUID.fromString(entry.get(MemberTableParameter.ID.jsonFieldName())
.s());

ofNullable(entry.get(MemberTableParameter.ADDRESS.jsonFieldName())).filter(av -> isNull(av.l()) || av.l()
.isEmpty())
.map(AttributeValue::ss)
.ifPresent(ss -> {
Map<String, AttributeValue> newEntry = new HashMap<>(entry);

newEntry.remove(MemberTableParameter.ADDRESS.jsonFieldName());
if (!ss.isEmpty()) {
newEntry.put(MemberTableParameter.ADDRESS.jsonFieldName(),
AttributeValue.fromL(Member.revertAddressDdb(ss)));
}

// lazy validation
final MemberRecord memberRecord = new MemberRecord(id, Member.convertDdbMap(newEntry),
UUID.fromString(entry.get(
MemberTableParameter.FAMILY_ID.jsonFieldName())
.s()));
newEntry = Member.retrieveDdbMap(memberRecord);

sdkClientProvider.getSdkClient(DynamoDbClient.class)
.deleteItem(DeleteItemRequest.builder()
.tableName(DdbTable.MEMBER.name())
.key(singletonMap(
MemberTableParameter.ID.jsonFieldName(),
AttributeValue.fromS(id.toString())))
.build());
sdkClientProvider.getSdkClient(DynamoDbClient.class)
.putItem(PutItemRequest.builder()
.tableName(DdbTable.MEMBER.name())
.item(newEntry)
.build());
});
}

lastEvaluatedKey = scanResponse.lastEvaluatedKey();

} while (!lastEvaluatedKey.isEmpty());
} finally {
waitDialog.close();
}
});
waitDialog.waitUntilClosed();
try {
future.get();
} catch (final Throwable e) {
throw new RuntimeException(e);
}
waitDialog.waitUntilClosed();
}
default -> throw new IllegalStateException("Unhandled Flag: %s".formatted(this.flag.name()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,42 +62,6 @@
interface MemberEventHelper extends EventHelper {
@NotNull String ROOT_ID = DdbUtils.ROOT_MEMBER_ID;

@NotNull
static
Map<String, AttributeValue> buildMember (final @NotNull MemberRecord memberRecord) {
final Map<String, AttributeValue> member = new HashMap<>();

for (final MemberTableParameter field : MemberTableParameter.values()) {
switch (field) {
case ID -> member.put(field.jsonFieldName(), AttributeValue.fromS(memberRecord.id()
.toString()));
case FIRST_NAME -> member.put(field.jsonFieldName(), AttributeValue.fromS(memberRecord.member()
.getFirstName()));
case MIDDLE_NAME -> ofNullable(memberRecord.member()
.getMiddleName()).ifPresent(s -> member.put(field.jsonFieldName(), AttributeValue.fromS(s)));
case LAST_NAME -> member.put(field.jsonFieldName(), AttributeValue.fromS(memberRecord.member()
.getLastName()));
case SUFFIX -> ofNullable(memberRecord.member()
.getSuffix()).ifPresent(s -> member.put(field.jsonFieldName(), AttributeValue.fromS(s.value())));
case BIRTHDAY -> member.put(field.jsonFieldName(), AttributeValue.fromS(memberRecord.member()
.getBirthdayString()));
case DEATHDAY -> ofNullable(memberRecord.member()
.getDeathdayString()).ifPresent(s -> member.put(field.jsonFieldName(), AttributeValue.fromS(s)));
case EMAIL -> ofNullable(memberRecord.member()
.getEmail()).ifPresent(s -> member.put(field.jsonFieldName(), AttributeValue.fromS(s)));
case PHONES -> ofNullable(memberRecord.member()
.getPhonesDdbMap()).ifPresent(m -> member.put(field.jsonFieldName(), AttributeValue.fromM(m)));
case ADDRESS -> ofNullable(memberRecord.member()
.getAddress()).ifPresent(ss -> member.put(field.jsonFieldName(), AttributeValue.fromSs(ss)));
case FAMILY_ID -> member.put(field.jsonFieldName(), AttributeValue.fromS(memberRecord.familyId()
.toString()));
default -> throw new IllegalStateException("Unhandled Member Parameter: `%s`".formatted(field.jsonFieldName()));
}
}

return member;
}

static
void deleteCognitoAccountAndNotify (final @NotNull String sub) {
final SdkClientProvider sdkClientProvider = SdkClientProvider.getSdkClientProvider();
Expand Down Expand Up @@ -393,13 +357,14 @@ _ chars are removed, useful for names like McDonald (input: mc_donald)
}
case ADDRESS -> {
final List<String> addressLines = new ArrayList<>();
for (int i = 1; i <= Member.REQ_NON_NULL_ADDRESS_SIZE && (i == 1 || !addressLines.isEmpty()); ++i) {
final String desc = "[%s] Please Enter %s Line %d:".formatted((i == 1)
for (int i = 1; i <= Member.MAX_NON_NULL_ADDRESS_SIZE && (i == 1 || !addressLines.isEmpty()); ++i) {
final boolean isOptional = (i == 1 || i > Member.MIN_NON_NULL_ADDRESS_SIZE);
final String desc = "[%s] Please Enter %s Line %d:".formatted(isOptional
? "Optional"
: "Required", param.jsonFieldName(), i);
final SkippableTextInputDialog dialog = new SkippableTextInputDialog(param.jsonFieldName(), desc, i == 1, true, null);
final SkippableTextInputDialog dialog = new SkippableTextInputDialog(param.jsonFieldName(), desc, isOptional, true, null);
final String addressLine = dialog.showDialog(gui);
if (nonNull(addressLine)) {
if (!(isNull(addressLine) || addressLine.isBlank())) {
addressLines.add(addressLine);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Objects;
import org.familydirectory.assets.ddb.enums.DdbTable;
import org.familydirectory.assets.ddb.member.Member;
import org.familydirectory.assets.ddb.models.member.MemberRecord;
import org.familydirectory.sdk.adminclient.enums.Commands;
import org.familydirectory.sdk.adminclient.events.model.MemberEventHelper;
Expand Down Expand Up @@ -54,7 +55,7 @@ void run () {
.getSdkClient(DynamoDbClient.class)
.putItem(PutItemRequest.builder()
.tableName(DdbTable.MEMBER.name())
.item(MemberEventHelper.buildMember(memberRecord))
.item(Member.retrieveDdbMap(memberRecord))
.build());
this.memberPicker.addEntry(memberRecord);
for (final PickerModel pickerModel : this.pickerModels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,22 @@ MemberRecord showDialog (final @NotNull WindowBasedTextGUI gui) {
final WaitingDialog waitDialog = WaitingDialog.createDialog(this.title, this.waitText);
waitDialog.setHints(AdminClientTui.EXTRA_WINDOW_HINTS);
waitDialog.showDialog(requireNonNull(gui), false);
CompletableFuture.runAsync(() -> {
if (needsRefresh.get()) {
this.picker.refresh();
}
contentRef.set(this.picker.getEntries());
waitDialog.close();
})
.exceptionally(e -> {
throw new RuntimeException(e);
});
final CompletableFuture<?> future = CompletableFuture.runAsync(() -> {
try {
if (needsRefresh.get()) {
this.picker.refresh();
}
contentRef.set(this.picker.getEntries());
} finally {
waitDialog.close();
}
});
waitDialog.waitUntilClosed();
try {
future.get();
} catch (final Throwable e) {
throw new RuntimeException(e);
}
final RefreshableListSelectDialog<MemberRecord> listSelectDialog = new RefreshableListSelectDialog<>(this.title, this.description, true, contentRef.get(), new TerminalSize(20, 10));
memberRecordRef.set(listSelectDialog.showDialog(gui));
if (isNull(memberRecordRef.get())) {
Expand Down
Loading

0 comments on commit 8e19a59

Please sign in to comment.