From d97049f4a9f4cc8264f913cddab5a08f941cb997 Mon Sep 17 00:00:00 2001 From: Sita Date: Fri, 19 Jul 2024 18:00:41 +0200 Subject: [PATCH] #37: Implement changing IDs --- .../inrepository/InRepositoryService.java | 31 ++++++++++++++++++- .../repositoryfx/PersistentIdController.java | 19 +++++++++++- .../repository/persistent_id_component.fxml | 2 +- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/place/sita/labelle/core/repository/inrepository/InRepositoryService.java b/core/src/main/java/place/sita/labelle/core/repository/inrepository/InRepositoryService.java index 5eacfbd..ebb6a45 100644 --- a/core/src/main/java/place/sita/labelle/core/repository/inrepository/InRepositoryService.java +++ b/core/src/main/java/place/sita/labelle/core/repository/inrepository/InRepositoryService.java @@ -21,7 +21,7 @@ import place.sita.labelle.jooq.Tables; import javax.annotation.Nullable; -import java.io.*; +import java.io.File; import java.util.*; import static place.sita.labelle.jooq.Tables.*; @@ -122,6 +122,35 @@ public UUID referImage(UUID newRepoId, UUID originalImageId, String persistentId return copyOrRefer(newRepoId, originalImageId, CopyOrRefer.REFER, persistentId); } + @Transactional + public UpdateIdsResult updateIds(UUID imageId, String persistentId, String parentPersistentId, boolean isVisibleToChildren) { + UUID imageRepoId = JqRepo.fetchOne(() -> + dslContext + .select(IMAGE.REPOSITORY_ID) + .from(IMAGE) + .where(IMAGE.ID.eq(imageId)) + .fetch() + ); + boolean exists = dslContext.fetchExists(IMAGE, + IMAGE.REFERENCE_ID.eq(persistentId) + .and(IMAGE.REPOSITORY_ID.eq(imageRepoId)) + .and(IMAGE.ID.ne(imageId)) + ); + if (exists) { + return new UpdateIdsResult.IdReuse(); + } + + setPersistentId(imageId, persistentId); + setVisibility(imageId, isVisibleToChildren); + setParentPersistentId(imageId, parentPersistentId); + return new UpdateIdsResult.Success(); + } + + public sealed interface UpdateIdsResult { + record Success() implements UpdateIdsResult {} + record IdReuse() implements UpdateIdsResult {} + } + @Transactional // todo make this one throw if something goes wrong - unless it already does? public void setPersistentId(UUID imageId, String persistentId) { dslContext.update(IMAGE) diff --git a/gui-local/src/main/java/place/sita/labelle/gui/local/repositoryfx/PersistentIdController.java b/gui-local/src/main/java/place/sita/labelle/gui/local/repositoryfx/PersistentIdController.java index bfe8e6a..f160c9e 100644 --- a/gui-local/src/main/java/place/sita/labelle/gui/local/repositoryfx/PersistentIdController.java +++ b/gui-local/src/main/java/place/sita/labelle/gui/local/repositoryfx/PersistentIdController.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Component; import place.sita.labelle.core.repository.inrepository.Ids; import place.sita.labelle.core.repository.inrepository.InRepositoryService; +import place.sita.labelle.gui.local.fx.Alerts; import place.sita.modulefx.annotations.FxDictatesHeight; import place.sita.modulefx.annotations.FxMessageListener; import place.sita.modulefx.annotations.FxNode; @@ -46,13 +47,27 @@ public class PersistentIdController { @ModuleFx private MessageSender messageSender; + @FXML + private Button saveButton; + public PersistentIdController(InRepositoryService inRepositoryService) { this.inRepositoryService = inRepositoryService; } @FXML public void onSavePress(ActionEvent event) { - // todo + InRepositoryService.UpdateIdsResult result = inRepositoryService.updateIds( + selectedImageId, + persistentIdTextField.getText(), + parentPersistentIdTextField.getText(), + isVisibleForChildrenCheckBox.isSelected() + ); + switch (result) { + case InRepositoryService.UpdateIdsResult.Success success -> { } // what we wanted, so let's ignore it + case InRepositoryService.UpdateIdsResult.IdReuse idReuse -> { + Alerts.error("Persistent ID is already in use"); + } + } } @FXML @@ -72,6 +87,7 @@ public void onImageSelected(ImageSelectedEvent event) { internalIdTextField.setText(event.imageId().toString()); internalIdTextField.setDisable(false); duplicateButton.setDisable(false); + saveButton.setDisable(false); Threading.onSeparateThread(keyStone, toolkit -> { Ids id = inRepositoryService.getIds(event.imageId()); @@ -99,6 +115,7 @@ private void disable() { isVisibleForChildrenCheckBox.setSelected(false); parentPersistentIdTextField.setText(""); persistentIdTextField.setText(""); + saveButton.setDisable(true); internalIdTextField.setDisable(true); isVisibleForChildrenCheckBox.setDisable(true); diff --git a/gui-local/src/main/resources/fx/repository/persistent_id_component.fxml b/gui-local/src/main/resources/fx/repository/persistent_id_component.fxml index 6e8c65b..14e179e 100644 --- a/gui-local/src/main/resources/fx/repository/persistent_id_component.fxml +++ b/gui-local/src/main/resources/fx/repository/persistent_id_component.fxml @@ -43,7 +43,7 @@ -