Added latest scene router version, replaced history clear and navigate by router's new "replace" function, and added orphan deletion to transaction delete logic.
This commit is contained in:
parent
fdfb9d0412
commit
952d149825
2
pom.xml
2
pom.xml
|
@ -30,7 +30,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.andrewlalis</groupId>
|
<groupId>com.andrewlalis</groupId>
|
||||||
<artifactId>javafx-scene-router</artifactId>
|
<artifactId>javafx-scene-router</artifactId>
|
||||||
<version>1.5.1</version>
|
<version>1.6.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -94,8 +94,7 @@ public class AccountViewController implements RouteSelectionListener {
|
||||||
);
|
);
|
||||||
if (confirmResult) {
|
if (confirmResult) {
|
||||||
Profile.getCurrent().getDataSource().useAccountRepository(repo -> repo.archive(account.id));
|
Profile.getCurrent().getDataSource().useAccountRepository(repo -> repo.archive(account.id));
|
||||||
router.getHistory().clear();
|
router.replace("accounts");
|
||||||
router.navigate("accounts");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,8 +105,7 @@ public class AccountViewController implements RouteSelectionListener {
|
||||||
);
|
);
|
||||||
if (confirm) {
|
if (confirm) {
|
||||||
Profile.getCurrent().getDataSource().useAccountRepository(repo -> repo.unarchive(account.id));
|
Profile.getCurrent().getDataSource().useAccountRepository(repo -> repo.unarchive(account.id));
|
||||||
router.getHistory().clear();
|
router.replace("accounts");
|
||||||
router.navigate("accounts");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,8 +120,7 @@ public class AccountViewController implements RouteSelectionListener {
|
||||||
);
|
);
|
||||||
if (confirm) {
|
if (confirm) {
|
||||||
Profile.getCurrent().getDataSource().useAccountRepository(repo -> repo.delete(account));
|
Profile.getCurrent().getDataSource().useAccountRepository(repo -> repo.delete(account));
|
||||||
router.getHistory().clear();
|
router.replace("accounts");
|
||||||
router.navigate("accounts");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -124,8 +124,7 @@ public class EditAccountController implements RouteSelectionListener {
|
||||||
|
|
||||||
// Once we create the new account, go to the account.
|
// Once we create the new account, go to the account.
|
||||||
Account newAccount = accountRepo.findById(id).orElseThrow();
|
Account newAccount = accountRepo.findById(id).orElseThrow();
|
||||||
router.getHistory().clear();
|
router.replace("account", newAccount);
|
||||||
router.navigate("account", newAccount);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.debug("Updating account {}", account.id);
|
log.debug("Updating account {}", account.id);
|
||||||
|
@ -135,8 +134,7 @@ public class EditAccountController implements RouteSelectionListener {
|
||||||
account.setCurrency(accountCurrencyComboBox.getValue());
|
account.setCurrency(accountCurrencyComboBox.getValue());
|
||||||
accountRepo.update(account);
|
accountRepo.update(account);
|
||||||
Account updatedAccount = accountRepo.findById(account.id).orElseThrow();
|
Account updatedAccount = accountRepo.findById(account.id).orElseThrow();
|
||||||
router.getHistory().clear();
|
router.replace("account", updatedAccount);
|
||||||
router.navigate("account", updatedAccount);
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Failed to save (or update) account " + account.id, e);
|
log.error("Failed to save (or update) account " + account.id, e);
|
||||||
|
|
|
@ -25,7 +25,7 @@ public class MainViewController {
|
||||||
|
|
||||||
@FXML public void initialize() {
|
@FXML public void initialize() {
|
||||||
AnchorPaneRouterView routerView = (AnchorPaneRouterView) router.getView();
|
AnchorPaneRouterView routerView = (AnchorPaneRouterView) router.getView();
|
||||||
mainContainer.setCenter(routerView.getAnchorPane());
|
mainContainer.setCenter(routerView.getPane());
|
||||||
|
|
||||||
// Set up a simple breadcrumb display in the top bar.
|
// Set up a simple breadcrumb display in the top bar.
|
||||||
BindingUtil.mapContent(
|
BindingUtil.mapContent(
|
||||||
|
@ -76,13 +76,11 @@ public class MainViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML public void goToAccounts() {
|
@FXML public void goToAccounts() {
|
||||||
router.getHistory().clear();
|
router.replace("accounts");
|
||||||
router.navigate("accounts");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML public void goToTransactions() {
|
@FXML public void goToTransactions() {
|
||||||
router.getHistory().clear();
|
router.replace("transactions");
|
||||||
router.navigate("transactions");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML public void viewProfiles() {
|
@FXML public void viewProfiles() {
|
||||||
|
@ -98,17 +96,14 @@ public class MainViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML public void helpViewHome() {
|
@FXML public void helpViewHome() {
|
||||||
helpRouter.getHistory().clear();
|
helpRouter.replace("home");
|
||||||
helpRouter.navigate("home");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML public void helpViewAccounts() {
|
@FXML public void helpViewAccounts() {
|
||||||
helpRouter.getHistory().clear();
|
helpRouter.replace("accounts");
|
||||||
helpRouter.navigate("accounts");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML public void helpViewTransactions() {
|
@FXML public void helpViewTransactions() {
|
||||||
helpRouter.getHistory().clear();
|
helpRouter.replace("transactions");
|
||||||
helpRouter.navigate("transactions");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,8 +106,7 @@ public class ProfilesViewController {
|
||||||
try {
|
try {
|
||||||
Profile.load(name);
|
Profile.load(name);
|
||||||
ProfilesStage.closeView();
|
ProfilesStage.closeView();
|
||||||
router.getHistory().clear();
|
router.replace("accounts");
|
||||||
router.navigate("accounts");
|
|
||||||
if (showPopup) Popups.message("The profile \"" + name + "\" has been loaded.");
|
if (showPopup) Popups.message("The profile \"" + name + "\" has been loaded.");
|
||||||
return true;
|
return true;
|
||||||
} catch (ProfileLoadException e) {
|
} catch (ProfileLoadException e) {
|
||||||
|
|
|
@ -95,8 +95,7 @@ public class TransactionViewController {
|
||||||
Profile.getCurrent().getDataSource().useTransactionRepository(repo -> {
|
Profile.getCurrent().getDataSource().useTransactionRepository(repo -> {
|
||||||
// TODO: Delete attachments first!
|
// TODO: Delete attachments first!
|
||||||
repo.delete(transaction.id);
|
repo.delete(transaction.id);
|
||||||
router.getHistory().clear();
|
router.replace("transactions");
|
||||||
router.navigate("transactions");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,4 +10,5 @@ public interface AttachmentRepository extends AutoCloseable {
|
||||||
Optional<Attachment> findById(long attachmentId);
|
Optional<Attachment> findById(long attachmentId);
|
||||||
Optional<Attachment> findByIdentifier(String identifier);
|
Optional<Attachment> findByIdentifier(String identifier);
|
||||||
void deleteById(long attachmentId);
|
void deleteById(long attachmentId);
|
||||||
|
void deleteAllOrphans();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import com.andrewlalis.perfin.data.ulid.UlidCreator;
|
||||||
import com.andrewlalis.perfin.data.util.DbUtil;
|
import com.andrewlalis.perfin.data.util.DbUtil;
|
||||||
import com.andrewlalis.perfin.data.util.FileUtil;
|
import com.andrewlalis.perfin.data.util.FileUtil;
|
||||||
import com.andrewlalis.perfin.model.Attachment;
|
import com.andrewlalis.perfin.model.Attachment;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
|
@ -18,6 +20,8 @@ import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public record JdbcAttachmentRepository(Connection conn, Path contentDir) implements AttachmentRepository {
|
public record JdbcAttachmentRepository(Connection conn, Path contentDir) implements AttachmentRepository {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(JdbcAttachmentRepository.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Attachment insert(Path sourcePath) {
|
public Attachment insert(Path sourcePath) {
|
||||||
String filename = sourcePath.getFileName().toString();
|
String filename = sourcePath.getFileName().toString();
|
||||||
|
@ -67,6 +71,41 @@ public record JdbcAttachmentRepository(Connection conn, Path contentDir) impleme
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteAllOrphans() {
|
||||||
|
DbUtil.doTransaction(conn, () -> {
|
||||||
|
List<Attachment> orphans = DbUtil.findAll(
|
||||||
|
conn,
|
||||||
|
"""
|
||||||
|
SELECT * FROM attachment
|
||||||
|
WHERE
|
||||||
|
id NOT IN (SELECT attachment_id FROM transaction_attachment) AND
|
||||||
|
id NOT IN (SELECT attachment_id FROM balance_record_attachment)""",
|
||||||
|
JdbcAttachmentRepository::parseAttachment
|
||||||
|
);
|
||||||
|
for (Attachment orphan : orphans) {
|
||||||
|
DbUtil.updateOne(
|
||||||
|
conn,
|
||||||
|
"DELETE FROM attachment WHERE id = ?",
|
||||||
|
List.of(orphan.id)
|
||||||
|
);
|
||||||
|
Path filePath = orphan.getPath(contentDir);
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(filePath);
|
||||||
|
Path parentDir = filePath.getParent();
|
||||||
|
try (var filesRemaining = Files.list(parentDir)) {
|
||||||
|
if (filesRemaining.findAny().isEmpty()) {
|
||||||
|
Files.delete(parentDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn("Failed to delete attachment at " + filePath + ".", e);
|
||||||
|
}
|
||||||
|
log.debug("Deleted orphan attachment with id {} at {}.", orphan.id, filePath);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws Exception {
|
public void close() throws Exception {
|
||||||
conn.close();
|
conn.close();
|
||||||
|
|
|
@ -151,6 +151,7 @@ public record JdbcTransactionRepository(Connection conn, Path contentDir) implem
|
||||||
@Override
|
@Override
|
||||||
public void delete(long transactionId) {
|
public void delete(long transactionId) {
|
||||||
DbUtil.updateOne(conn, "DELETE FROM transaction WHERE id = ?", List.of(transactionId));
|
DbUtil.updateOne(conn, "DELETE FROM transaction WHERE id = ?", List.of(transactionId));
|
||||||
|
new JdbcAttachmentRepository(conn, contentDir).deleteAllOrphans();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue