From 30056e892c3cb0273a626524c311498e4d3646aa Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Mon, 5 Feb 2018 13:43:22 +0100
Subject: [PATCH] OO-3274: check if there is something to backup before doing
 it

---
 .../modules/taxonomy/manager/TaxonomyDAO.java |  3 +-
 .../taxonomy/manager/TaxonomyServiceImpl.java | 33 +++++++++++++------
 .../ui/TaxonomyLevelOverviewController.java   |  8 +++--
 3 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyDAO.java b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyDAO.java
index 8b53c14013c..52c8e88be75 100644
--- a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyDAO.java
+++ b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyDAO.java
@@ -136,8 +136,7 @@ public class TaxonomyDAO implements InitializingBean{
 		File directory = new File(storage, type);
 		directory.mkdirs();
 		Path relativePath = rootDirectory.toPath().relativize(directory.toPath());
-		String relativePathString = relativePath.toString();
-		return relativePathString;
+		return relativePath.toString();
 	}
 	
 	public VFSContainer getDocumentsLibrary(Taxonomy taxonomy) {
diff --git a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyServiceImpl.java b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyServiceImpl.java
index 624cd9be6da..3c147190e67 100644
--- a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyServiceImpl.java
+++ b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyServiceImpl.java
@@ -21,11 +21,13 @@ package org.olat.modules.taxonomy.manager;
 
 import java.util.Date;
 import java.util.List;
+import java.util.UUID;
 
 import org.olat.basesecurity.IdentityRef;
 import org.olat.core.id.Identity;
 import org.olat.core.util.StringHelper;
 import org.olat.core.util.vfs.VFSContainer;
+import org.olat.core.util.vfs.VFSItem;
 import org.olat.core.util.vfs.VFSManager;
 import org.olat.modules.taxonomy.Taxonomy;
 import org.olat.modules.taxonomy.TaxonomyCompetence;
@@ -160,16 +162,27 @@ public class TaxonomyServiceImpl implements TaxonomyService {
 			merge(reloadedTaxonomyLevel, reloadedMergeTo);
 		} else {
 			VFSContainer library = taxonomyLevelDao.getDocumentsLibrary(reloadedTaxonomyLevel);
-			Taxonomy taxonomy = reloadedTaxonomyLevel.getTaxonomy();
-			VFSContainer lostAndFound = taxonomyDao.getLostAndFoundDirectoryLibrary(taxonomy);
-			String dir = StringHelper.transformDisplayNameToFileSystemName(reloadedTaxonomyLevel.getIdentifier());
-			dir += "_" + taxonomyLevel.getKey();
-			VFSContainer lastStorage = lostAndFound.createChildContainer(dir);
-			VFSManager.copyContent(library, lastStorage);
-			//delete the competences
-			taxonomyCompetenceDao.deleteCompetences(taxonomyLevel);
-			//questions
-			taxonomyRelationsDao.removeFromQuestionItems(taxonomyLevel);
+			if(library != null) {
+				Taxonomy taxonomy = reloadedTaxonomyLevel.getTaxonomy();
+				VFSContainer lostAndFound = taxonomyDao.getLostAndFoundDirectoryLibrary(taxonomy);
+				String dir = StringHelper.transformDisplayNameToFileSystemName(reloadedTaxonomyLevel.getIdentifier());
+				dir += "_" + taxonomyLevel.getKey();
+				VFSContainer lastStorage = lostAndFound.createChildContainer(dir);
+				if(lastStorage == null) {
+					VFSItem storageItem = lostAndFound.resolve(dir);
+					if(storageItem instanceof VFSContainer) {
+						lastStorage = (VFSContainer)storageItem;
+					} else {
+						lastStorage = lostAndFound.createChildContainer(UUID.randomUUID().toString());
+					}
+				}
+				
+				VFSManager.copyContent(library, lastStorage);
+				//delete the competences
+				taxonomyCompetenceDao.deleteCompetences(taxonomyLevel);
+				//questions
+				taxonomyRelationsDao.removeFromQuestionItems(taxonomyLevel);
+			}
 		}
 
 		return taxonomyLevelDao.delete(reloadedTaxonomyLevel);
diff --git a/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyLevelOverviewController.java b/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyLevelOverviewController.java
index 2abbda811b1..65b98b210d8 100644
--- a/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyLevelOverviewController.java
+++ b/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyLevelOverviewController.java
@@ -213,13 +213,15 @@ public class TaxonomyLevelOverviewController extends BasicController implements
 			showWarning("warning.atleastone.level");
 			return;
 		}
-		
+
+		taxonomyLevel = taxonomyService.getTaxonomyLevel(taxonomyLevel);
 		Taxonomy taxonomy = taxonomyLevel.getTaxonomy();
-		confirmDeleteCtrl = new DeleteTaxonomyLevelController(ureq, getWindowControl(), null, taxonomy);
+		List<TaxonomyLevel> levelToDelete = Collections.singletonList(taxonomyLevel);
+		confirmDeleteCtrl = new DeleteTaxonomyLevelController(ureq, getWindowControl(), levelToDelete, taxonomy);
 		listenTo(confirmDeleteCtrl);
 
 		String title = translate("confirmation.delete.level.title");
-		cmc = new CloseableModalController(getWindowControl(), "close", moveLevelCtrl.getInitialComponent(), true, title);
+		cmc = new CloseableModalController(getWindowControl(), "close", confirmDeleteCtrl.getInitialComponent(), true, title);
 		listenTo(cmc);
 		cmc.activate();
 	}
-- 
GitLab