From f489f32ea08033428b882631b842e39b84e82d7f Mon Sep 17 00:00:00 2001
From: tobias <tobias@thecodedev.de>
Date: Fri, 20 Sep 2019 22:20:43 +0200
Subject: [PATCH] Fix exceptions on project import with media

---
 .idea/ant.xml                                 |  6 ---
 .../cell/path/PathMatchPathCell.java          |  7 ++-
 .../main/java/de/tobias/playpad/pad/Pad.java  |  3 ++
 .../project/importer/ProjectImporter.java     | 44 ++++++++++++-------
 4 files changed, 37 insertions(+), 23 deletions(-)
 delete mode 100644 .idea/ant.xml

diff --git a/.idea/ant.xml b/.idea/ant.xml
deleted file mode 100644
index 9ec298f2..00000000
--- a/.idea/ant.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="AntConfiguration">
-    <buildFile url="file://$PROJECT_DIR$/PlayWall/build.xml" />
-  </component>
-</project>
\ No newline at end of file
diff --git a/PlayWall/src/main/java/de/tobias/playpad/viewcontroller/cell/path/PathMatchPathCell.java b/PlayWall/src/main/java/de/tobias/playpad/viewcontroller/cell/path/PathMatchPathCell.java
index 1b8eeec9..b7617a56 100644
--- a/PlayWall/src/main/java/de/tobias/playpad/viewcontroller/cell/path/PathMatchPathCell.java
+++ b/PlayWall/src/main/java/de/tobias/playpad/viewcontroller/cell/path/PathMatchPathCell.java
@@ -4,6 +4,8 @@ import de.tobias.playpad.view.PseudoClasses;
 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
@@ -14,7 +16,10 @@ public class PathMatchPathCell extends TableCell<PathMatchDialog.TempMediaPath,
 			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);
diff --git a/PlayWallCore/src/main/java/de/tobias/playpad/pad/Pad.java b/PlayWallCore/src/main/java/de/tobias/playpad/pad/Pad.java
index 813e6792..2a0fd4c2 100644
--- a/PlayWallCore/src/main/java/de/tobias/playpad/pad/Pad.java
+++ b/PlayWallCore/src/main/java/de/tobias/playpad/pad/Pad.java
@@ -365,7 +365,10 @@ public class Pad {
 
 	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);
 		}
diff --git a/PlayWallCore/src/main/java/de/tobias/playpad/project/importer/ProjectImporter.java b/PlayWallCore/src/main/java/de/tobias/playpad/project/importer/ProjectImporter.java
index 9efe6faa..92943932 100644
--- a/PlayWallCore/src/main/java/de/tobias/playpad/project/importer/ProjectImporter.java
+++ b/PlayWallCore/src/main/java/de/tobias/playpad/project/importer/ProjectImporter.java
@@ -26,10 +26,7 @@ import java.io.InputStream;
 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.
@@ -69,10 +66,11 @@ public class ProjectImporter {
 		}
 
 		importedProjectReference = importProjectFile();
-		replaceMediaPathIds(loadMediaPaths());
 
 		if (includeMedia && delegate.shouldImportMedia()) {
-			importMedia();
+			importMedia(loadMediaPaths());
+		} else {
+			replaceMediaPathIds(loadMediaPaths());
 		}
 	}
 
@@ -146,10 +144,20 @@ public class ProjectImporter {
 		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());
+					}
+				});
+			}
 		}
 	}
 
@@ -202,7 +210,7 @@ public class ProjectImporter {
 		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();
 
@@ -212,12 +220,16 @@ public class ProjectImporter {
 
 		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);
-- 
GitLab