Search in sources :

Example 1 with NonReturningTask

use of se.light.assembly64.model.NonReturningTask in project assembly64fx by freabemania.

the class SidifyExportPlaylistController method export.

public void export() throws Exception {
    int idx = locations.getSelectionModel().getSelectedIndex();
    List<String> exportLocations = new ArrayList<>();
    if (idx != 0) {
        exportLocations.addAll(Collections.singletonList(locations.getSelectionModel().getSelectedItem()));
    } else {
        exportLocations.addAll(locations.getItems().stream().filter(item -> !item.equals(ALL_LOCATIONS)).collect(Collectors.toList()));
    }
    int items = playlists.stream().map(item -> playlistService.getSongsForPlaylist(item)).map(item -> item.size()).mapToInt(i -> i.intValue()).sum() * exportLocations.size();
    CancelableTask cancelTask = CancelableTask.of();
    ProgressDBController controller = GuiUtils.showDialogOwnerNoWait("progressBarDbUpdate.fxml", "Progress", true, NullWindowOwner.of(), new Object[] { items, cancelTask, exportLocations.get(0) + MUSIC_SIDIFY, "Playlist exported to <location>/Music/Sidify" });
    NonReturningTask t = () -> {
        try {
            for (PlaylistInfo playlist : playlists) {
                List<PlaylistEntry> songsForPlaylist = playlistService.getSongsForPlaylist(playlist);
                int padding = Integer.valueOf(String.valueOf(songsForPlaylist.size()).length());
                for (String dir : exportLocations) {
                    File exportDir = new File(dir + MUSIC_SIDIFY + playlist.getName());
                    FileUtils.deleteQuietly(exportDir);
                    FileUtils.forceMkdir(exportDir);
                    int ctr = 1;
                    for (PlaylistEntry entry : playlistService.getSongsForPlaylist(playlist)) {
                        String pos = String.format("%0" + padding + "d", ctr);
                        try {
                            controller.increaseProgress();
                            controller.setProgressLabel("Exporting " + entry.getNameMasked());
                            FileUtils.copyFile(downloadService.getSid(entry), new File(exportDir + "/" + pos + "_" + entry.getNameMasked() + ".sid"));
                            ctr++;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (cancelTask.isCancelled()) {
                            FileUtils.deleteQuietly(exportDir);
                            break;
                        }
                    }
                }
                if (cancelTask.isCancelled()) {
                    break;
                }
            }
        } finally {
            controller.progressDone();
        }
    };
    ExecutorUtil.executeAsync(t);
}
Also used : NonReturningTask(se.light.assembly64.model.NonReturningTask) PlaylistEntry(se.light.assembly64.model.PlaylistEntry) ExecutorUtil(se.light.assembly64.util.ExecutorUtil) FileUtils(org.apache.commons.io.FileUtils) PlaylistInfo(se.light.assembly64.model.PlaylistInfo) Collectors(java.util.stream.Collectors) File(java.io.File) ChoiceBox(javafx.scene.control.ChoiceBox) UserService(se.light.assembly64.service.UserService) ArrayList(java.util.ArrayList) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GuiUtils(se.light.assembly64.util.GuiUtils) CancelableTask(se.light.assembly64.model.CancelableTask) WorkLocation(se.light.assembly64.model.WorkLocation) DownloadArtifactsService(se.light.assembly64.service.DownloadArtifactsService) PlaylistService(se.light.assembly64.service.PlaylistService) Collections(java.util.Collections) BaseGuiController(se.light.assembly64.model.BaseGuiController) ArrayList(java.util.ArrayList) NonReturningTask(se.light.assembly64.model.NonReturningTask) ArrayList(java.util.ArrayList) List(java.util.List) PlaylistInfo(se.light.assembly64.model.PlaylistInfo) PlaylistEntry(se.light.assembly64.model.PlaylistEntry) File(java.io.File) CancelableTask(se.light.assembly64.model.CancelableTask)

Aggregations

File (java.io.File)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 Platform (javafx.application.Platform)1 FXML (javafx.fxml.FXML)1 ChoiceBox (javafx.scene.control.ChoiceBox)1 FileUtils (org.apache.commons.io.FileUtils)1 BaseGuiController (se.light.assembly64.model.BaseGuiController)1 CancelableTask (se.light.assembly64.model.CancelableTask)1 NonReturningTask (se.light.assembly64.model.NonReturningTask)1 PlaylistEntry (se.light.assembly64.model.PlaylistEntry)1 PlaylistInfo (se.light.assembly64.model.PlaylistInfo)1 WorkLocation (se.light.assembly64.model.WorkLocation)1 DownloadArtifactsService (se.light.assembly64.service.DownloadArtifactsService)1 PlaylistService (se.light.assembly64.service.PlaylistService)1 UserService (se.light.assembly64.service.UserService)1 ExecutorUtil (se.light.assembly64.util.ExecutorUtil)1