Search in sources :

Example 1 with LogEntryBuilder

use of org.phoebus.logbook.LogEntryImpl.LogEntryBuilder in project phoebus by ControlSystemStudio.

the class InMemoryLogClient method set.

@Override
public LogEntry set(LogEntry log) {
    long id = logIdCounter.incrementAndGet();
    LogEntryBuilder logBuilder = LogEntryImpl.LogEntryBuilder.log(log);
    logBuilder.id(id);
    logBuilder.createdDate(Instant.now());
    Set<Attachment> attachmentsBuilder = log.getAttachments().stream().map(attachment -> {
        try {
            File file = attachment.getFile();
            int i = file.getName().lastIndexOf(".");
            String ext = null;
            if (i >= 0) {
                ext = file.getName().substring(i);
            }
            File tempFile = File.createTempFile(prefix, ext);
            Files.copy(file, tempFile);
            tempFile.deleteOnExit();
            String mimeType = URLConnection.guessContentTypeFromName(tempFile.getName());
            return AttachmentImpl.of(tempFile, mimeType != null ? mimeType : ext, false);
        } catch (IOException e) {
            logger.log(Level.WARNING, "failed to get in memory attachment", e);
            return null;
        }
    }).collect(Collectors.toSet());
    logBuilder.setAttach(attachmentsBuilder);
    LogEntry logEntry = logBuilder.build();
    logEntries.put(id, logEntry);
    return logEntry;
}
Also used : java.util(java.util) Stream(java.util.stream.Stream) java.io(java.io) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Files(com.google.common.io.Files) URLConnection(java.net.URLConnection) Instant(java.time.Instant) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) LogEntryBuilder(org.phoebus.logbook.LogEntryImpl.LogEntryBuilder) org.phoebus.logbook(org.phoebus.logbook) Level(java.util.logging.Level) LogEntryBuilder(org.phoebus.logbook.LogEntryImpl.LogEntryBuilder)

Example 2 with LogEntryBuilder

use of org.phoebus.logbook.LogEntryImpl.LogEntryBuilder in project phoebus by ControlSystemStudio.

the class LogEntryTableDemo method start.

@Override
public void start(Stage primaryStage) throws Exception {
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(this.getClass().getResource("LogEntryTableView.fxml"));
    loader.setControllerFactory(clazz -> {
        try {
            if (clazz.isAssignableFrom(LogEntryTableViewController.class)) {
                return clazz.getConstructor(LogClient.class).newInstance(getLogClient());
            } else if (clazz.isAssignableFrom(AdvancedSearchViewController.class)) {
                return clazz.getConstructor(LogClient.class).newInstance(getLogClient());
            }
        } catch (Exception e) {
            Logger.getLogger(LogEntryEditorStage.class.getName()).log(Level.SEVERE, "Failed to construct controller for log calendar view", e);
        }
        return null;
    });
    loader.load();
    LogEntryTableViewController controller = loader.getController();
    Parent root = loader.getRoot();
    primaryStage.setScene(new Scene(root, 400, 400));
    primaryStage.show();
    List<LogEntry> logs = new ArrayList<LogEntry>();
    Set<Tag> tags = new HashSet<Tag>();
    tags.add(TagImpl.of("tag1", "active"));
    tags.add(TagImpl.of("tag2", "active"));
    Set<Logbook> logbooks = new HashSet<Logbook>();
    logbooks.add(LogbookImpl.of("logbook1", "active"));
    logbooks.add(LogbookImpl.of("logbook2", "active"));
    File imageFile = new File(this.getClass().getClassLoader().getResource("image_1.png").toURI());
    File textFile = new File(this.getClass().getClassLoader().getResource("file_phoebus.txt").toURI());
    List<File> listOfFiles = Arrays.asList(imageFile, textFile);
    for (int i = 0; i < 10; i++) {
        LogEntryBuilder lb = LogEntryBuilder.log().owner("Owner").title("log " + i).description("First line for log " + i).createdDate(Instant.now()).inLogbooks(logbooks).withTags(tags);
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < i; j++) {
            sb.append("Some additional log text");
        }
        lb.appendDescription(sb.toString());
        listOfFiles.forEach(file -> {
            try {
                lb.attach(AttachmentImpl.of(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        });
        logs.add(lb.build());
    }
    controller.setLogs(logs);
}
Also used : LogClient(org.phoebus.logbook.LogClient) Parent(javafx.scene.Parent) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) LogEntryEditorStage(org.phoebus.logbook.ui.write.LogEntryEditorStage) Scene(javafx.scene.Scene) FXMLLoader(javafx.fxml.FXMLLoader) LogbookException(org.phoebus.logbook.LogbookException) FileNotFoundException(java.io.FileNotFoundException) Logbook(org.phoebus.logbook.Logbook) LogEntryBuilder(org.phoebus.logbook.LogEntryImpl.LogEntryBuilder) Tag(org.phoebus.logbook.Tag) File(java.io.File) LogEntry(org.phoebus.logbook.LogEntry) HashSet(java.util.HashSet)

Example 3 with LogEntryBuilder

use of org.phoebus.logbook.LogEntryImpl.LogEntryBuilder in project phoebus by ControlSystemStudio.

the class LogEntryModel method submitEntry.

/**
 * Create and return a log entry with the current data in the log entry form.
 * NOTE: this method calls the remote service in a synchronous manner. Calling code should handle potential
 * threading issues, e.g. invoking this method on the UI thread. Using a synchronous approach facilitates
 * handling of connection or authentication issues.
 *
 * @throws IOException
 */
public LogEntry submitEntry() throws Exception {
    // Create a log entry with the form data.
    LogEntryBuilder logEntryBuilder = new LogEntryBuilder();
    logEntryBuilder.title(title).description(text).level(level);
    for (String selectedLogbook : selectedLogbooks) logEntryBuilder.appendToLogbook(LogbookImpl.of(selectedLogbook));
    for (String selectedTag : selectedTags) logEntryBuilder.appendTag(TagImpl.of(selectedTag));
    // List of temporary image files to delete.
    List<File> toDelete = new ArrayList<>();
    // Add Images
    for (Image image : images) {
        File imageFile = File.createTempFile("log_entry_image_", ".png");
        imageFile.deleteOnExit();
        toDelete.add(imageFile);
        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", imageFile);
        logEntryBuilder.attach(AttachmentImpl.of(imageFile, "image", false));
    }
    // Add Files
    for (File file : files) {
        logEntryBuilder.attach(AttachmentImpl.of(file, "file", false));
    }
    LogEntry logEntry = logEntryBuilder.build();
    if (LogbookUiPreferences.save_credentials) {
        // Get the SecureStore. Store username and password.
        try {
            SecureStore store = new SecureStore();
            store.set(SecureStore.USERNAME_TAG, username);
            store.set(SecureStore.PASSWORD_TAG, password);
        } catch (Exception ex) {
            logger.log(Level.WARNING, "Secure Store file not found.", ex);
        }
    }
    LogEntry result = null;
    if (null != logFactory)
        result = logFactory.getLogClient(new SimpleAuthenticationToken(username, password)).set(logEntry);
    // Delete the temporary files.
    for (File file : toDelete) file.delete();
    // Run the onSubmitAction runnable
    if (null != onSubmitAction)
        onSubmitAction.run();
    return result;
}
Also used : ArrayList(java.util.ArrayList) LogEntryBuilder(org.phoebus.logbook.LogEntryImpl.LogEntryBuilder) Image(javafx.scene.image.Image) File(java.io.File) SecureStore(org.phoebus.security.store.SecureStore) LogEntry(org.phoebus.logbook.LogEntry) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SimpleAuthenticationToken(org.phoebus.security.tokens.SimpleAuthenticationToken)

Example 4 with LogEntryBuilder

use of org.phoebus.logbook.LogEntryImpl.LogEntryBuilder in project phoebus by ControlSystemStudio.

the class LogEntrySearchDemo method start.

@Override
public void start(Stage primaryStage) throws Exception {
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(this.getClass().getResource("LogEntryTableView.fxml"));
    loader.setControllerFactory(clazz -> {
        try {
            if (clazz.isAssignableFrom(LogEntryTableViewController.class)) {
                return clazz.getConstructor(LogClient.class).newInstance(getLogClient());
            } else if (clazz.isAssignableFrom(AdvancedSearchViewController.class)) {
                return clazz.getConstructor(LogClient.class).newInstance(getLogClient());
            }
        } catch (Exception e) {
            Logger.getLogger(LogEntryEditorStage.class.getName()).log(Level.SEVERE, "Failed to construct controller for log calendar view", e);
        }
        return null;
    });
    loader.load();
    LogEntryTableViewController controller = loader.getController();
    Parent root = loader.getRoot();
    primaryStage.setScene(new Scene(root, 400, 400));
    primaryStage.show();
    List<LogEntry> logs = new ArrayList<LogEntry>();
    Set<Tag> tags = new HashSet<Tag>();
    tags.add(TagImpl.of("tag1", "active"));
    tags.add(TagImpl.of("tag2", "active"));
    Set<Logbook> logbooks = new HashSet<Logbook>();
    logbooks.add(LogbookImpl.of("logbook1", "active"));
    logbooks.add(LogbookImpl.of("logbook2", "active"));
    String path = "C:\\Users\\Kunal Shroff\\Pictures\\screenshot-git\\log-att";
    File folder = new File(path);
    List<File> listOfFiles = Arrays.asList(folder.listFiles());
    for (int i = 0; i < 10; i++) {
        LogEntryBuilder lb = LogEntryBuilder.log().owner("Owner").title("log " + i).description("First line for log " + i).createdDate(Instant.now()).inLogbooks(logbooks).withTags(tags);
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < i; j++) {
            sb.append("Some additional log text");
        }
        lb.appendDescription(sb.toString());
        listOfFiles.forEach(file -> {
            try {
                lb.attach(AttachmentImpl.of(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        });
        logs.add(lb.build());
    }
    controller.setLogs(logs);
}
Also used : LogClient(org.phoebus.logbook.LogClient) Parent(javafx.scene.Parent) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) LogEntryEditorStage(org.phoebus.logbook.ui.write.LogEntryEditorStage) Scene(javafx.scene.Scene) FXMLLoader(javafx.fxml.FXMLLoader) LogbookException(org.phoebus.logbook.LogbookException) FileNotFoundException(java.io.FileNotFoundException) Logbook(org.phoebus.logbook.Logbook) LogEntryBuilder(org.phoebus.logbook.LogEntryImpl.LogEntryBuilder) Tag(org.phoebus.logbook.Tag) File(java.io.File) LogEntry(org.phoebus.logbook.LogEntry) HashSet(java.util.HashSet)

Example 5 with LogEntryBuilder

use of org.phoebus.logbook.LogEntryImpl.LogEntryBuilder in project phoebus by ControlSystemStudio.

the class SendLogbookAction method submitLogEntry.

private void submitLogEntry(final Node parent, final String title, final String body, final File image_file) {
    LogEntryBuilder logEntryBuilder = new LogEntryBuilder();
    if (title != null)
        logEntryBuilder.title(title);
    if (body != null)
        logEntryBuilder.appendDescription(body);
    if (image_file != null) {
        try {
            final Attachment attachment = AttachmentImpl.of(image_file, "image", false);
            logEntryBuilder.attach(attachment);
        } catch (FileNotFoundException ex) {
            logger.log(Level.WARNING, "Cannot attach " + image_file, ex);
        }
    }
    final LogEntryModel model = new LogEntryModel(logEntryBuilder.createdDate(Instant.now()).build());
    new LogEntryEditorStage(parent, model, null).show();
}
Also used : LogEntryModel(org.phoebus.logbook.ui.write.LogEntryModel) FileNotFoundException(java.io.FileNotFoundException) LogEntryBuilder(org.phoebus.logbook.LogEntryImpl.LogEntryBuilder) Attachment(org.phoebus.logbook.Attachment) LogEntryEditorStage(org.phoebus.logbook.ui.write.LogEntryEditorStage)

Aggregations

LogEntryBuilder (org.phoebus.logbook.LogEntryImpl.LogEntryBuilder)22 FileNotFoundException (java.io.FileNotFoundException)16 File (java.io.File)13 LogEntry (org.phoebus.logbook.LogEntry)12 HashSet (java.util.HashSet)8 FXMLLoader (javafx.fxml.FXMLLoader)8 Scene (javafx.scene.Scene)8 Logbook (org.phoebus.logbook.Logbook)8 Tag (org.phoebus.logbook.Tag)8 ArrayList (java.util.ArrayList)7 LocalDateTime (java.time.LocalDateTime)6 DateTimeParseException (java.time.format.DateTimeParseException)6 Parent (javafx.scene.Parent)6 LogClient (org.phoebus.logbook.LogClient)6 LogbookException (org.phoebus.logbook.LogbookException)5 LogEntryEditorStage (org.phoebus.logbook.ui.write.LogEntryEditorStage)5 LogEntryEditorStage (org.phoebus.logbook.olog.ui.write.LogEntryEditorStage)3 IOException (java.io.IOException)2 Instant (java.time.Instant)2 HashMap (java.util.HashMap)2