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>
|
||||
<groupId>com.andrewlalis</groupId>
|
||||
<artifactId>javafx-scene-router</artifactId>
|
||||
<version>1.5.1</version>
|
||||
<version>1.6.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
|
|
@ -94,8 +94,7 @@ public class AccountViewController implements RouteSelectionListener {
|
|||
);
|
||||
if (confirmResult) {
|
||||
Profile.getCurrent().getDataSource().useAccountRepository(repo -> repo.archive(account.id));
|
||||
router.getHistory().clear();
|
||||
router.navigate("accounts");
|
||||
router.replace("accounts");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,8 +105,7 @@ public class AccountViewController implements RouteSelectionListener {
|
|||
);
|
||||
if (confirm) {
|
||||
Profile.getCurrent().getDataSource().useAccountRepository(repo -> repo.unarchive(account.id));
|
||||
router.getHistory().clear();
|
||||
router.navigate("accounts");
|
||||
router.replace("accounts");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -122,8 +120,7 @@ public class AccountViewController implements RouteSelectionListener {
|
|||
);
|
||||
if (confirm) {
|
||||
Profile.getCurrent().getDataSource().useAccountRepository(repo -> repo.delete(account));
|
||||
router.getHistory().clear();
|
||||
router.navigate("accounts");
|
||||
router.replace("accounts");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -124,8 +124,7 @@ public class EditAccountController implements RouteSelectionListener {
|
|||
|
||||
// Once we create the new account, go to the account.
|
||||
Account newAccount = accountRepo.findById(id).orElseThrow();
|
||||
router.getHistory().clear();
|
||||
router.navigate("account", newAccount);
|
||||
router.replace("account", newAccount);
|
||||
}
|
||||
} else {
|
||||
log.debug("Updating account {}", account.id);
|
||||
|
@ -135,8 +134,7 @@ public class EditAccountController implements RouteSelectionListener {
|
|||
account.setCurrency(accountCurrencyComboBox.getValue());
|
||||
accountRepo.update(account);
|
||||
Account updatedAccount = accountRepo.findById(account.id).orElseThrow();
|
||||
router.getHistory().clear();
|
||||
router.navigate("account", updatedAccount);
|
||||
router.replace("account", updatedAccount);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to save (or update) account " + account.id, e);
|
||||
|
|
|
@ -25,7 +25,7 @@ public class MainViewController {
|
|||
|
||||
@FXML public void initialize() {
|
||||
AnchorPaneRouterView routerView = (AnchorPaneRouterView) router.getView();
|
||||
mainContainer.setCenter(routerView.getAnchorPane());
|
||||
mainContainer.setCenter(routerView.getPane());
|
||||
|
||||
// Set up a simple breadcrumb display in the top bar.
|
||||
BindingUtil.mapContent(
|
||||
|
@ -76,13 +76,11 @@ public class MainViewController {
|
|||
}
|
||||
|
||||
@FXML public void goToAccounts() {
|
||||
router.getHistory().clear();
|
||||
router.navigate("accounts");
|
||||
router.replace("accounts");
|
||||
}
|
||||
|
||||
@FXML public void goToTransactions() {
|
||||
router.getHistory().clear();
|
||||
router.navigate("transactions");
|
||||
router.replace("transactions");
|
||||
}
|
||||
|
||||
@FXML public void viewProfiles() {
|
||||
|
@ -98,17 +96,14 @@ public class MainViewController {
|
|||
}
|
||||
|
||||
@FXML public void helpViewHome() {
|
||||
helpRouter.getHistory().clear();
|
||||
helpRouter.navigate("home");
|
||||
helpRouter.replace("home");
|
||||
}
|
||||
|
||||
@FXML public void helpViewAccounts() {
|
||||
helpRouter.getHistory().clear();
|
||||
helpRouter.navigate("accounts");
|
||||
helpRouter.replace("accounts");
|
||||
}
|
||||
|
||||
@FXML public void helpViewTransactions() {
|
||||
helpRouter.getHistory().clear();
|
||||
helpRouter.navigate("transactions");
|
||||
helpRouter.replace("transactions");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -106,8 +106,7 @@ public class ProfilesViewController {
|
|||
try {
|
||||
Profile.load(name);
|
||||
ProfilesStage.closeView();
|
||||
router.getHistory().clear();
|
||||
router.navigate("accounts");
|
||||
router.replace("accounts");
|
||||
if (showPopup) Popups.message("The profile \"" + name + "\" has been loaded.");
|
||||
return true;
|
||||
} catch (ProfileLoadException e) {
|
||||
|
|
|
@ -95,8 +95,7 @@ public class TransactionViewController {
|
|||
Profile.getCurrent().getDataSource().useTransactionRepository(repo -> {
|
||||
// TODO: Delete attachments first!
|
||||
repo.delete(transaction.id);
|
||||
router.getHistory().clear();
|
||||
router.navigate("transactions");
|
||||
router.replace("transactions");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,4 +10,5 @@ public interface AttachmentRepository extends AutoCloseable {
|
|||
Optional<Attachment> findById(long attachmentId);
|
||||
Optional<Attachment> findByIdentifier(String identifier);
|
||||
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.FileUtil;
|
||||
import com.andrewlalis.perfin.model.Attachment;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
|
@ -18,6 +20,8 @@ import java.util.List;
|
|||
import java.util.Optional;
|
||||
|
||||
public record JdbcAttachmentRepository(Connection conn, Path contentDir) implements AttachmentRepository {
|
||||
private static final Logger log = LoggerFactory.getLogger(JdbcAttachmentRepository.class);
|
||||
|
||||
@Override
|
||||
public Attachment insert(Path sourcePath) {
|
||||
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
|
||||
public void close() throws Exception {
|
||||
conn.close();
|
||||
|
|
|
@ -151,6 +151,7 @@ public record JdbcTransactionRepository(Connection conn, Path contentDir) implem
|
|||
@Override
|
||||
public void delete(long transactionId) {
|
||||
DbUtil.updateOne(conn, "DELETE FROM transaction WHERE id = ?", List.of(transactionId));
|
||||
new JdbcAttachmentRepository(conn, contentDir).deleteAllOrphans();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue