Skip to content

Commit

Permalink
#37: Implement changing IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-sita committed Jul 19, 2024
1 parent 1e06b33 commit d97049f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand Down Expand Up @@ -99,6 +115,7 @@ private void disable() {
isVisibleForChildrenCheckBox.setSelected(false);
parentPersistentIdTextField.setText("");
persistentIdTextField.setText("");
saveButton.setDisable(true);

internalIdTextField.setDisable(true);
isVisibleForChildrenCheckBox.setDisable(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<FlowPane hgap="10.0" maxWidth="180.0" minWidth="140.0" prefWidth="180.0" vgap="10.0" GridPane.columnIndex="3">
<children>
<CheckBox fx:id="isVisibleForChildrenCheckBox" minWidth="15.0" mnemonicParsing="false" selected="true" text="Visible to children" />
<Button disable="true" mnemonicParsing="false" onAction="#onSavePress" text="Save" />
<Button fx:id="saveButton" disable="true" mnemonicParsing="false" onAction="#onSavePress" text="Save" />
<Button fx:id="duplicateButton" disable="true" mnemonicParsing="false" onAction="#onDuplicatePress" text="Duplicate" />
</children>
<padding>
Expand Down

0 comments on commit d97049f

Please sign in to comment.