Search in sources :

Example 1 with LogClient

use of org.phoebus.logbook.LogClient in project phoebus by ControlSystemStudio.

the class FieldsViewController method setupLogbooksAndTags.

/**
 * Retrieves logbooks and tags from service and populates all the data structures that depend
 * on the result. The call to the remote service is asynchronous.
 */
private void setupLogbooksAndTags() {
    JobManager.schedule("Fetch Logbooks and Tags", monitor -> {
        LogClient logClient = LogService.getInstance().getLogFactories().get(LogbookPreferences.logbook_factory).getLogClient();
        availableLogbooks = logClient.listLogbooks();
        availableLogbooksAsStringList = FXCollections.observableArrayList(availableLogbooks.stream().map(logbook -> logbook.getName()).collect(Collectors.toList()));
        Collections.sort(availableLogbooksAsStringList);
        List<String> preSelectedLogbooks = logEntry.getLogbooks().stream().map(l -> l.getName()).collect(Collectors.toList());
        List<String> defaultLogbooks = Arrays.asList(LogbookUIPreferences.default_logbooks);
        availableLogbooksAsStringList.forEach(logbook -> {
            CheckBox checkBox = new CheckBox(logbook);
            CustomMenuItem newLogbook = new CustomMenuItem(checkBox);
            newLogbook.setHideOnClick(false);
            checkBox.setOnAction(e -> {
                CheckBox source = (CheckBox) e.getSource();
                String text = source.getText();
                if (source.isSelected()) {
                    selectedLogbooks.add(text);
                } else {
                    selectedLogbooks.remove(text);
                }
            });
            if (!preSelectedLogbooks.isEmpty() && preSelectedLogbooks.contains(logbook)) {
                checkBox.setSelected(preSelectedLogbooks.contains(logbook));
                selectedLogbooks.add(logbook);
            } else if (defaultLogbooks.contains(logbook)) {
                checkBox.setSelected(defaultLogbooks.contains(logbook));
                selectedLogbooks.add(logbook);
            }
            logbookDropDown.getItems().add(newLogbook);
        });
        availableTags = logClient.listTags();
        availableTagsAsStringList = FXCollections.observableArrayList(availableTags.stream().map(tag -> tag.getName()).collect(Collectors.toList()));
        Collections.sort(availableLogbooksAsStringList);
        List<String> preSelectedTags = logEntry.getTags().stream().map(t -> t.getName()).collect(Collectors.toList());
        availableTagsAsStringList.forEach(tag -> {
            CheckBox checkBox = new CheckBox(tag);
            CustomMenuItem newTag = new CustomMenuItem(checkBox);
            newTag.setHideOnClick(false);
            checkBox.setOnAction(e -> {
                CheckBox source = (CheckBox) e.getSource();
                String text = source.getText();
                if (source.isSelected()) {
                    selectedTags.add(text);
                } else {
                    selectedTags.remove(text);
                }
            });
            checkBox.setSelected(preSelectedTags.contains(tag));
            if (preSelectedTags.contains(tag)) {
                selectedTags.add(tag);
            }
            tagDropDown.getItems().add(newTag);
        });
    });
}
Also used : Button(javafx.scene.control.Button) LogEntry(org.phoebus.logbook.LogEntry) Initializable(javafx.fxml.Initializable) JobManager(org.phoebus.framework.jobs.JobManager) Arrays(java.util.Arrays) URL(java.net.URL) ReadOnlyBooleanProperty(javafx.beans.property.ReadOnlyBooleanProperty) VBox(javafx.scene.layout.VBox) Side(javafx.geometry.Side) CustomMenuItem(javafx.scene.control.CustomMenuItem) ComboBox(javafx.scene.control.ComboBox) ContextMenu(javafx.scene.control.ContextMenu) PhoebusApplication.logger(org.phoebus.ui.application.PhoebusApplication.logger) TextField(javafx.scene.control.TextField) Logbook(org.phoebus.logbook.Logbook) ImageCache(org.phoebus.ui.javafx.ImageCache) MenuItem(javafx.scene.control.MenuItem) SecureStore(org.phoebus.security.store.SecureStore) LogService(org.phoebus.logbook.LogService) Collection(java.util.Collection) Tag(org.phoebus.logbook.Tag) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Messages(org.phoebus.logbook.olog.ui.Messages) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) List(java.util.List) OlogProperties(org.phoebus.olog.es.api.OlogProperties) ToggleButton(javafx.scene.control.ToggleButton) ObservableList(javafx.collections.ObservableList) ScopedAuthenticationToken(org.phoebus.security.tokens.ScopedAuthenticationToken) TextArea(javafx.scene.control.TextArea) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) FXCollections(javafx.collections.FXCollections) LogbookUIPreferences(org.phoebus.logbook.olog.ui.LogbookUIPreferences) TimestampFormats(org.phoebus.util.time.TimestampFormats) Bindings(javafx.beans.binding.Bindings) Level(java.util.logging.Level) LogbookPreferences(org.phoebus.logbook.LogbookPreferences) ResourceBundle(java.util.ResourceBundle) PasswordField(javafx.scene.control.PasswordField) Color(javafx.scene.paint.Color) Label(javafx.scene.control.Label) PhoebusApplication(org.phoebus.ui.application.PhoebusApplication) CheckBox(javafx.scene.control.CheckBox) ListSelectionDialog(org.phoebus.ui.dialog.ListSelectionDialog) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) ImageView(javafx.scene.image.ImageView) Image(javafx.scene.image.Image) LogClient(org.phoebus.logbook.LogClient) Collections(java.util.Collections) LogClient(org.phoebus.logbook.LogClient) CheckBox(javafx.scene.control.CheckBox) CustomMenuItem(javafx.scene.control.CustomMenuItem)

Example 2 with LogClient

use of org.phoebus.logbook.LogClient in project phoebus by ControlSystemStudio.

the class LogEntryEditorController method submit.

@FXML
public void submit() {
    submissionInProgress.set(true);
    JobManager.schedule("Submit Log Entry", monitor -> {
        OlogLog ologLog = new OlogLog();
        ologLog.setTitle(fieldsViewController.getTitle());
        ologLog.setDescription(fieldsViewController.getDescription());
        ologLog.setLevel(fieldsViewController.getSelectedLevel());
        ologLog.setLogbooks(fieldsViewController.getSelectedLogbooks());
        ologLog.setTags(fieldsViewController.getSelectedTags());
        ologLog.setAttachments(attachmentsViewController.getAttachments());
        ologLog.setProperties(logPropertiesEditorController.getProperties());
        LogClient logClient = logFactory.getLogClient(new SimpleAuthenticationToken(fieldsViewController.getUsernameProperty(), fieldsViewController.getPasswordProperty()));
        LogEntry result;
        try {
            if (replyTo == null) {
                result = logClient.set(ologLog);
            } else {
                result = logClient.reply(ologLog, replyTo);
            }
            if (result != null) {
                if (completionHandler != null) {
                    completionHandler.handleResult(result);
                }
                // Set username and password in secure store if submission of log entry completes successfully
                if (LogbookUIPreferences.save_credentials) {
                    // Get the SecureStore. Store username and password.
                    try {
                        SecureStore store = new SecureStore();
                        ScopedAuthenticationToken scopedAuthenticationToken = new ScopedAuthenticationToken(LogService.AUTHENTICATION_SCOPE, fieldsViewController.getUsernameProperty(), fieldsViewController.getPasswordProperty());
                        store.setScopedAuthentication(scopedAuthenticationToken);
                    } catch (Exception ex) {
                        logger.log(Level.WARNING, "Secure Store file not found.", ex);
                    }
                }
                attachmentsViewController.deleteTemporaryFiles();
                // This will close the editor
                Platform.runLater(() -> cancel());
            }
        } catch (LogbookException e) {
            logger.log(Level.WARNING, "Unable to submit log entry", e);
            Platform.runLater(() -> {
                if (e.getCause() != null && e.getCause().getMessage() != null) {
                    completionMessageLabel.textProperty().setValue(e.getCause().getMessage());
                } else if (e.getMessage() != null) {
                    completionMessageLabel.textProperty().setValue(e.getMessage());
                } else {
                    completionMessageLabel.textProperty().setValue(org.phoebus.logbook.Messages.SubmissionFailed);
                }
            });
        }
        submissionInProgress.set(false);
    });
}
Also used : LogClient(org.phoebus.logbook.LogClient) ScopedAuthenticationToken(org.phoebus.security.tokens.ScopedAuthenticationToken) LogbookException(org.phoebus.logbook.LogbookException) OlogLog(org.phoebus.olog.es.api.model.OlogLog) SecureStore(org.phoebus.security.store.SecureStore) LogEntry(org.phoebus.logbook.LogEntry) LogbookException(org.phoebus.logbook.LogbookException) ExecutionException(java.util.concurrent.ExecutionException) SimpleAuthenticationToken(org.phoebus.security.tokens.SimpleAuthenticationToken) FXML(javafx.fxml.FXML)

Example 3 with LogClient

use of org.phoebus.logbook.LogClient in project phoebus by ControlSystemStudio.

the class LogPropertiesEditorController method setupProperties.

/**
 * Refreshes the list of available properties. Properties provided by {@link LogPropertyProvider} implementations
 * are considered first, and then properties available from service. However, adding items to the list of properties
 * always consider equality, i.e. properties with same name are added only once. SPI implementations should therefore
 * not support properties with same name, and should not implement properties available from service.
 * <p>
 * Further, if the user is editing a copy (reply) based on another log entry, a set of properties may
 * already be present in the new log entry. The list of available properties will not contain such properties
 * as this would be confusing.
 * <p>
 * When user chooses to remove a property from the list of properties, the list of available properties must
 * also be refreshed, so this method should handle such a use case.
 * <p>
 * Also, properties to be excluded as listed in the preferences (properties_excluded_from_view) are not
 * added to the properties tree.
 */
private void setupProperties() {
    JobManager.schedule("Fetch Properties from service", monitor -> {
        // First add properties from SPI implementations
        List<LogPropertyProvider> factories = new ArrayList<>();
        ServiceLoader<LogPropertyProvider> loader = ServiceLoader.load(LogPropertyProvider.class);
        loader.stream().forEach(p -> {
            if (p.get().getProperty() != null) {
                factories.add(p.get());
            }
        });
        // List of property names added if user is replying to a log entry.
        List<String> selectedPropertyNames = selectedProperties.stream().map(Property::getName).collect(Collectors.toList());
        factories.stream().map(LogPropertyProvider::getProperty).forEach(property -> {
            // Do not add a property that is already selected
            if (!selectedPropertyNames.contains(property.getName())) {
                selectedProperties.add(property);
                selectedPropertyNames.add(property.getName());
            }
        });
        LogClient logClient = LogService.getInstance().getLogFactories().get(LogbookPreferences.logbook_factory).getLogClient();
        List<Property> propertyList = logClient.listProperties().stream().collect(Collectors.toList());
        List<Property> list = new ArrayList<>();
        Platform.runLater(() -> {
            propertyList.forEach(property -> {
                // Do not add a property that is already selected or already added from provider
                if (!selectedPropertyNames.contains(property.getName())) {
                    list.add(property);
                }
            });
            list.sort(Comparator.comparing(Property::getName));
            availableProperties.setAll(list);
        });
    });
}
Also used : LogClient(org.phoebus.logbook.LogClient) ArrayList(java.util.ArrayList) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) Property(org.phoebus.logbook.Property)

Example 4 with LogClient

use of org.phoebus.logbook.LogClient in project phoebus by ControlSystemStudio.

the class LogPropertiesEditorDemo method getDummyLogClient.

private LogClient getDummyLogClient() {
    return new LogClient() {

        @Override
        public LogEntry set(LogEntry log) throws LogbookException {
            return null;
        }

        @Override
        public LogEntry getLog(Long logId) {
            return null;
        }

        @Override
        public Collection<Attachment> listAttachments(Long logId) {
            return null;
        }

        @Override
        public List<LogEntry> findLogs(Map<String, String> map) {
            return null;
        }

        @Override
        public Collection<LogEntry> listLogs() {
            return null;
        }

        @Override
        public Collection<Property> listProperties() {
            Map<String, String> experimentAttributes = new HashMap<>();
            experimentAttributes.put("id", "1234");
            experimentAttributes.put("type", "XPD xray diffraction");
            experimentAttributes.put("scan-id", "6789");
            Property experimentProperty = PropertyImpl.of("Experiment", experimentAttributes);
            return Arrays.asList(experimentProperty);
        }
    };
}
Also used : LogClient(org.phoebus.logbook.LogClient) Attachment(org.phoebus.logbook.Attachment) Property(org.phoebus.logbook.Property) LogEntry(org.phoebus.logbook.LogEntry)

Aggregations

LogClient (org.phoebus.logbook.LogClient)4 LogEntry (org.phoebus.logbook.LogEntry)3 SimpleStringProperty (javafx.beans.property.SimpleStringProperty)2 FXML (javafx.fxml.FXML)2 SecureStore (org.phoebus.security.store.SecureStore)2 ScopedAuthenticationToken (org.phoebus.security.tokens.ScopedAuthenticationToken)2 URL (java.net.URL)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 ResourceBundle (java.util.ResourceBundle)1 ExecutionException (java.util.concurrent.ExecutionException)1 Level (java.util.logging.Level)1 Collectors (java.util.stream.Collectors)1 Platform (javafx.application.Platform)1 Bindings (javafx.beans.binding.Bindings)1 ReadOnlyBooleanProperty (javafx.beans.property.ReadOnlyBooleanProperty)1