Skip to content

Commit

Permalink
Add safeguard for importing fils from incorrect files
Browse files Browse the repository at this point in the history
This is based on ibans saved in baka and filenames. Therefore, this
isn't bulletproof. We need to inspect formats and see what metadata is
available for a 90% solution. The current solution at least works for
DKB2
  • Loading branch information
Bios-Marcel committed Nov 2, 2024
1 parent cd6d6fa commit a2d73e1
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/main/java/link/biosmarcel/baka/view/PaymentsView.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

public class PaymentsView extends BakaTab {
private final TableView<PaymentFX> table;
Expand Down Expand Up @@ -197,6 +198,10 @@ private void importHandler(final Account account, final BiFunction<Account, File
return;
}

if (account.iban != null && !account.iban.isBlank() && safeguardIncorrectlyImportedFile(account, files)) {
return;
}

// Since it's assumed we can only pick files from one directory, we use the next best parent dir we can find.
final String dir = files.getFirst().getParent();
state.data.convenienceState.lastImportPath = dir;
Expand All @@ -209,12 +214,41 @@ private void importHandler(final Account account, final BiFunction<Account, File
final var newPayments = files.stream()
.flatMap(file -> importer.apply(account, file).stream())
// Since the files usually have consistent order, we sort, as adding together two files contents may
// produces an incorrect order.
// produce an incorrect order.
.sorted(Comparator.comparing(payment -> payment.bookingDate))
.toList();
createPayments(newPayments);
}

private boolean safeguardIncorrectlyImportedFile(Account account, List<File> files) {
final var potentiallyWrongImports = new ArrayList<String>();
for (final var file : files) {
for (final var otherAccount : state.data.accounts) {
if (account.equals(otherAccount) || otherAccount.iban == null || otherAccount.iban.isBlank()) {
continue;
}

if (file.getName().toLowerCase().contains(otherAccount.iban.toLowerCase())) {
potentiallyWrongImports.add(file.getName());
}
}
}

if (!potentiallyWrongImports.isEmpty()) {
final var safetyCheck = new Alert(Alert.AlertType.CONFIRMATION);
safetyCheck.setTitle("Import into '%s'".formatted(account.name));
safetyCheck.setHeaderText("Potentially importing into incorrect account");
safetyCheck.setContentText(
"Are you sure you want to import the following files into account '%s (%s)':\n\n%s"
.formatted(account.name, account.iban, String.join("\n", potentiallyWrongImports)));
safetyCheck.initOwner(getTabPane().getScene().getWindow());
safetyCheck.getDialogPane().setPrefWidth(600);
final var choice = safetyCheck.showAndWait();
return choice.isEmpty() || choice.get().getButtonData() != ButtonBar.ButtonData.YES;
}
return false;
}

private void createPayments(final Collection<Payment> newPayments) {
// We simply append, preventing to restore everything (hopefully). We also won't depend on the sorting by
// accident later on.
Expand Down

0 comments on commit a2d73e1

Please sign in to comment.