Skip to content

Commit

Permalink
Fix exceptions on project import with media
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobisaninfo committed Sep 20, 2019
1 parent 976ff43 commit f489f32
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
6 changes: 0 additions & 6 deletions .idea/ant.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import de.tobias.playpad.viewcontroller.dialog.PathMatchDialog;
import javafx.scene.control.TableCell;

import java.nio.file.Path;

public class PathMatchPathCell extends TableCell<PathMatchDialog.TempMediaPath, PathMatchDialog.TempMediaPath> {

@Override
Expand All @@ -14,7 +16,10 @@ protected void updateItem(PathMatchDialog.TempMediaPath item, boolean empty) {
if (item.isMatched()) {
setText(item.getLocalPath().toString());
} else {
setText(item.getMediaPath().getPath().toString());
Path path = item.getMediaPath().getPath();
if (path != null) {
setText(path.toString());
}
}
} else {
setText(null);
Expand Down
3 changes: 3 additions & 0 deletions PlayWallCore/src/main/java/de/tobias/playpad/pad/Pad.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,10 @@ public void addPath(MediaPath mediaPath) {

public void removePath(MediaPath path) {
mediaPaths.remove(path);
}


public void removePathListener(MediaPath path) {
if (project.getProjectReference().isSync()) {
CommandManager.execute(Commands.PATH_REMOVE, project.getProjectReference(), path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

/**
* Created by tobias on 11.03.17.
Expand Down Expand Up @@ -69,10 +66,11 @@ public void execute() throws IOException, ProjectNotFoundException, DocumentExce
}

importedProjectReference = importProjectFile();
replaceMediaPathIds(loadMediaPaths());

if (includeMedia && delegate.shouldImportMedia()) {
importMedia();
importMedia(loadMediaPaths());
} else {
replaceMediaPathIds(loadMediaPaths());
}
}

Expand Down Expand Up @@ -146,10 +144,20 @@ private void replaceMediaPathIds(List<MediaPath> mediaPaths) throws ProjectNotFo
Project project = loader.load();

for (Pad pad : project.getPads()) {
pad.getPaths().forEach(oldMediaPath -> find(mediaPaths, oldMediaPath.getId()).ifPresent(result -> {
pad.removePath(oldMediaPath);
pad.setPath(result.getPath());
}));
Iterator<MediaPath> iterator = pad.getPaths().iterator();
while (iterator.hasNext()) {
MediaPath oldMediaPath = iterator.next();
find(mediaPaths, oldMediaPath.getId()).ifPresent(result -> {
try {
pad.removePathListener(oldMediaPath);
iterator.remove();

pad.setPath(result.getPath());
} catch (NullPointerException e) {
Logger.error("Import Error on Pad: " + pad.getUuid());
}
});
}
}
}

Expand Down Expand Up @@ -202,7 +210,7 @@ private void importProfile() throws IOException {
profileUUID = localProfileUUID; // Update Profile UUID with new local profile uuid
}

private void importMedia() throws ProjectNotFoundException, ProfileNotFoundException, DocumentException,
private void importMedia(List<MediaPath> mediaPaths) throws ProjectNotFoundException, ProfileNotFoundException, DocumentException,
IOException, ProfileAbortException {
Path folder = delegate.getMediaPath();

Expand All @@ -212,12 +220,16 @@ private void importMedia() throws ProjectNotFoundException, ProfileNotFoundExcep

for (Pad pad : project.getPads()) {
for (MediaPath path : pad.getPaths()) {
Path fileName = path.getPath().getFileName();
Path zipMediaFile = Paths.get("/media").resolve(fileName);
Path newMediaPath = folder.resolve(fileName);
Optional<MediaPath> result = find(mediaPaths, path.getId());

if (result.isPresent()) {
String fileName = result.get().getFileName();
Path zipMediaFile = Paths.get("/media").resolve(fileName);
Path newMediaPath = folder.resolve(fileName);

zip.getFile(zipMediaFile, newMediaPath);
path.setPath(newMediaPath, false);
zip.getFile(zipMediaFile, newMediaPath);
path.setPath(newMediaPath, false);
}
}
}
ProjectReferenceManager.saveSingleProject(project);
Expand Down

0 comments on commit f489f32

Please sign in to comment.