diff --git a/app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/AttachmentsViewController.java b/app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/AttachmentsViewController.java index 8737d868c1..b0bcc7354d 100644 --- a/app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/AttachmentsViewController.java +++ b/app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/AttachmentsViewController.java @@ -265,18 +265,20 @@ private void showPreview() { * @param attachment The image {@link Attachment} selected by user. */ private void showImagePreview(Attachment attachment) { - try { - BufferedImage bufferedImage = ImageIO.read(attachment.getFile()); - // BufferedImage may be null due to lazy loading strategy. - if (bufferedImage == null) { - return; + if (attachment.getFile() != null) { + try { + BufferedImage bufferedImage = ImageIO.read(attachment.getFile()); + // BufferedImage may be null due to lazy loading strategy. + if (bufferedImage == null) { + return; + } + Image image = SwingFXUtils.toFXImage(bufferedImage, null); + imagePreview.visibleProperty().setValue(true); + imagePreview.setImage(image); + } catch (IOException ex) { + Logger.getLogger(AttachmentsEditorController.class.getName()) + .log(Level.SEVERE, "Unable to load image file " + attachment.getFile().getAbsolutePath(), ex); } - Image image = SwingFXUtils.toFXImage(bufferedImage, null); - imagePreview.visibleProperty().setValue(true); - imagePreview.setImage(image); - } catch (IOException ex) { - Logger.getLogger(AttachmentsEditorController.class.getName()) - .log(Level.SEVERE, "Unable to load image file " + attachment.getFile().getAbsolutePath(), ex); } } diff --git a/app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/write/LogEntryUpdateController.java b/app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/write/LogEntryUpdateController.java index 9d2a6e7b0a..c84b37c126 100644 --- a/app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/write/LogEntryUpdateController.java +++ b/app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/write/LogEntryUpdateController.java @@ -47,6 +47,7 @@ import javafx.scene.paint.Color; import org.phoebus.framework.jobs.JobManager; import org.phoebus.framework.selection.SelectionService; +import org.phoebus.logbook.Attachment; import org.phoebus.logbook.LogClient; import org.phoebus.logbook.LogEntry; import org.phoebus.logbook.LogFactory; @@ -55,11 +56,14 @@ import org.phoebus.logbook.LogbookException; import org.phoebus.logbook.LogbookPreferences; import org.phoebus.logbook.Tag; +import org.phoebus.logbook.olog.ui.AttachmentsViewController; import org.phoebus.logbook.olog.ui.HelpViewer; import org.phoebus.logbook.olog.ui.LogbookUIPreferences; import org.phoebus.logbook.olog.ui.PreviewViewer; +import org.phoebus.logbook.olog.ui.SingleLogEntryDisplayController; import org.phoebus.logbook.olog.ui.menu.SendToLogBookApp; import org.phoebus.olog.es.api.OlogProperties; +import org.phoebus.olog.es.api.model.OlogAttachment; import org.phoebus.olog.es.api.model.OlogLog; import org.phoebus.security.store.SecureStore; import org.phoebus.security.tokens.ScopedAuthenticationToken; @@ -68,6 +72,10 @@ import org.phoebus.ui.javafx.ImageCache; import org.phoebus.util.time.TimestampFormats; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; @@ -103,7 +111,7 @@ public class LogEntryUpdateController { @SuppressWarnings("unused") @FXML - private AttachmentsEditorController attachmentsEditorController; + private AttachmentsViewController attachmentsViewController; @SuppressWarnings("unused") @FXML private LogPropertiesEditorController logPropertiesEditorController; @@ -220,7 +228,6 @@ public void initialize() { completionMessageLabel.textProperty(), submissionInProgress)); cancelButton.disableProperty().bind(submissionInProgress); - attachmentsEditorController.setTextArea(textArea); userField.textProperty().bindBidirectional(usernameProperty); userField.textProperty().addListener((changeListener, oldVal, newVal) -> @@ -382,6 +389,7 @@ public void initialize() { // Note: logbooks and tags are retrieved asynchronously from service setupLogbooksAndTags(); + retrieveAttachments(); } /** @@ -406,7 +414,7 @@ public void showHelp() { */ @FXML public void showHtmlPreview() { - new PreviewViewer(getDescription(), attachmentsEditorController.getAttachments()).show(); + new PreviewViewer(getDescription(), attachmentsViewController.getAttachments()).show(); } @@ -423,7 +431,7 @@ public void submit() { ologLog.setLevel(selectedLevelProperty.get()); ologLog.setLogbooks(getSelectedLogbooks()); ologLog.setTags(getSelectedTags()); - ologLog.setAttachments(attachmentsEditorController.getAttachments()); +// ologLog.setAttachments(attachmentsViewController.getAttachments()); ologLog.setProperties(logPropertiesEditorController.getProperties()); LogClient logClient = @@ -449,7 +457,6 @@ public void submit() { logger.log(Level.WARNING, "Secure Store file not found.", ex); } } - attachmentsEditorController.deleteTemporaryFiles(); // This will close the editor Platform.runLater(this::cancel); } @@ -632,6 +639,36 @@ private void setupLogbooksAndTags() { }); } + private void retrieveAttachments(){ + JobManager.schedule("Fetch attachment data", monitor -> { + + LogClient logClient = + LogService.getInstance().getLogFactories().get(LogbookPreferences.logbook_factory).getLogClient(); + Collection attachments = logEntry.getAttachments().stream() + .filter((attachment) -> attachment.getName() != null && !attachment.getName().isEmpty()) + .map((attachment) -> { + OlogAttachment fileAttachment = new OlogAttachment(); + fileAttachment.setContentType(attachment.getContentType()); + fileAttachment.setThumbnail(false); + fileAttachment.setFileName(attachment.getName()); + try { + Path temp = Files.createTempFile("phoebus", attachment.getName()); + Files.copy(logClient.getAttachment(logEntry.getId(), attachment.getName()), temp, StandardCopyOption.REPLACE_EXISTING); + fileAttachment.setFile(temp.toFile()); + temp.toFile().deleteOnExit(); + } catch (LogbookException | IOException e) { + Logger.getLogger(SingleLogEntryDisplayController.class.getName()) + .log(Level.WARNING, "Failed to retrieve attachment " + fileAttachment.getFileName(), e); + } + return fileAttachment; + }).collect(Collectors.toList()); + // Update UI + Platform.runLater(()->{ + attachmentsViewController.setAttachments(attachments); + }); + }); + } + public void fetchStoredUserCredentials() { // Perform file IO on background thread. JobManager.schedule("Access Secure Store", monitor -> diff --git a/app/logbook/olog/ui/src/main/resources/org/phoebus/logbook/olog/ui/write/LogEntryUpdate.fxml b/app/logbook/olog/ui/src/main/resources/org/phoebus/logbook/olog/ui/write/LogEntryUpdate.fxml index 37c841ab20..f5d54a1ce2 100644 --- a/app/logbook/olog/ui/src/main/resources/org/phoebus/logbook/olog/ui/write/LogEntryUpdate.fxml +++ b/app/logbook/olog/ui/src/main/resources/org/phoebus/logbook/olog/ui/write/LogEntryUpdate.fxml @@ -157,7 +157,7 @@ - +