From 844fc9ceebd01223fcf0f9eb04409660966366df Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Mon, 15 Apr 2019 16:01:32 +0200
Subject: [PATCH] OO-4020: implement deletion of evaluation form if allowed

---
 .../forms/EvaluationFormReadyToDelete.java    | 40 ++++++++++++++
 .../forms/handler/EvaluationFormHandler.java  | 26 ++++++++-
 .../portfolio/manager/AssignmentDAO.java      | 21 ++++++-
 .../manager/AssignmentFormReadyToDelete.java  | 54 ++++++++++++++++++
 .../ui/_i18n/LocalStrings_de.properties       |  1 +
 .../ui/_i18n/LocalStrings_en.properties       |  1 +
 .../ui/_i18n/LocalStrings_fr.properties       |  1 +
 .../manager/QualityGeneratorDAO.java          | 17 +++++-
 .../QualityGeneratorFormReadyToDelete.java    | 55 +++++++++++++++++++
 .../ui/_i18n/LocalStrings_de.properties       |  1 +
 .../ui/_i18n/LocalStrings_en.properties       |  1 +
 .../ui/_i18n/LocalStrings_fr.properties       |  1 +
 .../ConfirmDeletePermanentlyController.java   |  4 +-
 .../portfolio/manager/AssignmentDAOTest.java  | 35 ++++++++++++
 14 files changed, 250 insertions(+), 8 deletions(-)
 create mode 100644 src/main/java/org/olat/modules/forms/EvaluationFormReadyToDelete.java
 create mode 100644 src/main/java/org/olat/modules/portfolio/manager/AssignmentFormReadyToDelete.java
 create mode 100644 src/main/java/org/olat/modules/quality/generator/manager/QualityGeneratorFormReadyToDelete.java

diff --git a/src/main/java/org/olat/modules/forms/EvaluationFormReadyToDelete.java b/src/main/java/org/olat/modules/forms/EvaluationFormReadyToDelete.java
new file mode 100644
index 00000000000..94a3e7a14e8
--- /dev/null
+++ b/src/main/java/org/olat/modules/forms/EvaluationFormReadyToDelete.java
@@ -0,0 +1,40 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.modules.forms;
+
+import java.util.Locale;
+
+import org.olat.repository.ErrorList;
+import org.olat.repository.RepositoryEntry;
+
+/**
+ * 
+ * Ask providers if the evaluation form can be deleted. If no
+ * explain why in the error list.
+ * 
+ * Initial date: 15 avr. 2019<br>
+ * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
+ *
+ */
+public interface EvaluationFormReadyToDelete {
+	
+	public boolean readyToDelete(RepositoryEntry entry, Locale locale, ErrorList errors);
+
+}
diff --git a/src/main/java/org/olat/modules/forms/handler/EvaluationFormHandler.java b/src/main/java/org/olat/modules/forms/handler/EvaluationFormHandler.java
index 4090a33bf8a..b30ff4001b3 100644
--- a/src/main/java/org/olat/modules/forms/handler/EvaluationFormHandler.java
+++ b/src/main/java/org/olat/modules/forms/handler/EvaluationFormHandler.java
@@ -32,7 +32,9 @@ import java.nio.file.SimpleFileVisitor;
 import java.nio.file.StandardCopyOption;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.util.Locale;
+import java.util.Map;
 
+import org.olat.core.CoreSpringFactory;
 import org.olat.core.commons.persistence.DB;
 import org.olat.core.gui.UserRequest;
 import org.olat.core.gui.components.stack.TooledStackedPanel;
@@ -56,6 +58,7 @@ import org.olat.core.util.PathUtils.YesMatcher;
 import org.olat.core.util.Util;
 import org.olat.core.util.coordinate.CoordinatorManager;
 import org.olat.core.util.coordinate.LockResult;
+import org.olat.core.util.resource.OLATResourceableJustBeforeDeletedEvent;
 import org.olat.core.util.vfs.VFSContainer;
 import org.olat.core.util.xml.XStreamHelper;
 import org.olat.fileresource.FileResourceManager;
@@ -63,6 +66,7 @@ import org.olat.fileresource.types.FileResource;
 import org.olat.fileresource.types.ResourceEvaluation;
 import org.olat.modules.ceditor.DataStorage;
 import org.olat.modules.forms.EvaluationFormManager;
+import org.olat.modules.forms.EvaluationFormReadyToDelete;
 import org.olat.modules.forms.EvaluationFormsModule;
 import org.olat.modules.forms.model.xml.Form;
 import org.olat.modules.forms.model.xml.FormXStream;
@@ -79,6 +83,7 @@ import org.olat.repository.handlers.RepositoryHandler;
 import org.olat.repository.model.RepositoryEntrySecurity;
 import org.olat.resource.OLATResource;
 import org.olat.resource.OLATResourceManager;
+import org.olat.resource.references.ReferenceManager;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -96,6 +101,8 @@ public class EvaluationFormHandler implements RepositoryHandler {
 	@Autowired
 	private DB dbInstance;
 	@Autowired
+	private ReferenceManager referenceManager;
+	@Autowired
 	private EvaluationFormsModule formsModule;
 	@Autowired
 	private RepositoryManager repositoryManager;
@@ -234,12 +241,27 @@ public class EvaluationFormHandler implements RepositoryHandler {
 
 	@Override
 	public boolean readyToDelete(RepositoryEntry entry, Identity identity, Roles roles, Locale locale, ErrorList errors) {
-		return false;
+		String referencesSummary = referenceManager.getReferencesToSummary(entry.getOlatResource(), locale);
+		if (referencesSummary != null) {
+			Translator translator = Util.createPackageTranslator(RepositoryManager.class, locale);
+			errors.setError(translator.translate("details.delete.error.references",
+					new String[] { entry.getDisplayname() }));
+			return false;
+		}
+		
+		boolean delete = true;
+		Map<String,EvaluationFormReadyToDelete> deleteDelegates = CoreSpringFactory.getBeansOfType(EvaluationFormReadyToDelete.class);
+		for(EvaluationFormReadyToDelete delegate:deleteDelegates.values()) {
+			delete &= delegate.readyToDelete(entry, locale, errors);
+		}
+		return delete;
 	}
 
 	@Override
 	public boolean cleanupOnDelete(RepositoryEntry entry, OLATResourceable res) {
-		return false;
+		CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(new OLATResourceableJustBeforeDeletedEvent(res), res);
+		FileResourceManager.getInstance().deleteFileResource(res);
+		return true;
 	}
 
 	/**
diff --git a/src/main/java/org/olat/modules/portfolio/manager/AssignmentDAO.java b/src/main/java/org/olat/modules/portfolio/manager/AssignmentDAO.java
index e1715e21d08..d6703fb9198 100644
--- a/src/main/java/org/olat/modules/portfolio/manager/AssignmentDAO.java
+++ b/src/main/java/org/olat/modules/portfolio/manager/AssignmentDAO.java
@@ -49,6 +49,7 @@ import org.olat.modules.portfolio.model.BinderImpl;
 import org.olat.modules.portfolio.model.EvaluationFormPart;
 import org.olat.modules.portfolio.model.SectionImpl;
 import org.olat.repository.RepositoryEntry;
+import org.olat.repository.RepositoryEntryRef;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -370,7 +371,7 @@ public class AssignmentDAO {
 	}
 	
 	public Assignment loadAssignment(PageBody body) {
-		StringBuilder sb = new StringBuilder();
+		StringBuilder sb = new StringBuilder(256);
 		sb.append("select assignment from pfassignment as assignment")
 		  .append(" inner join fetch assignment.page as page")
 		  .append(" left join fetch assignment.formEntry as formEntry")
@@ -385,7 +386,7 @@ public class AssignmentDAO {
 	}
 	
 	public boolean isAssignmentInUse(Assignment assignment) {
-		StringBuilder sb = new StringBuilder();
+		StringBuilder sb = new StringBuilder(128);
 		sb.append("select assignment.key from pfassignment as assignment")
 		  .append(" where assignment.templateReference.key=:assignmentKey");
 
@@ -395,7 +396,21 @@ public class AssignmentDAO {
 			.setFirstResult(0)
 			.setMaxResults(1)
 			.getResultList();
-		return counts != null && counts.size() > 0 && counts.get(0) != null && counts.get(0).intValue() >= 0;
+		return counts != null && !counts.isEmpty() && counts.get(0) != null && counts.get(0).intValue() >= 0;
+	}
+	
+	public boolean isFormEntryInUse(RepositoryEntryRef formEntry) {
+		StringBuilder sb = new StringBuilder(128);
+		sb.append("select assignment.key from pfassignment as assignment")
+		  .append(" where assignment.formEntry.key=:formEntryKey");
+
+		List<Long> counts = dbInstance.getCurrentEntityManager()
+			.createQuery(sb.toString(), Long.class)
+			.setParameter("formEntryKey", formEntry.getKey())
+			.setFirstResult(0)
+			.setMaxResults(1)
+			.getResultList();
+		return counts != null && !counts.isEmpty() && counts.get(0) != null && counts.get(0).intValue() >= 0;
 	}
 	
 	public int deleteAssignmentReference(Assignment assignment) {
diff --git a/src/main/java/org/olat/modules/portfolio/manager/AssignmentFormReadyToDelete.java b/src/main/java/org/olat/modules/portfolio/manager/AssignmentFormReadyToDelete.java
new file mode 100644
index 00000000000..38080b5f6d2
--- /dev/null
+++ b/src/main/java/org/olat/modules/portfolio/manager/AssignmentFormReadyToDelete.java
@@ -0,0 +1,54 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.modules.portfolio.manager;
+
+import java.util.Locale;
+
+import org.olat.core.gui.translator.Translator;
+import org.olat.core.util.Util;
+import org.olat.modules.forms.EvaluationFormReadyToDelete;
+import org.olat.modules.portfolio.ui.BinderRuntimeController;
+import org.olat.repository.ErrorList;
+import org.olat.repository.RepositoryEntry;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * 
+ * Initial date: 15 avr. 2019<br>
+ * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
+ *
+ */
+@Service
+public class AssignmentFormReadyToDelete implements EvaluationFormReadyToDelete {
+	
+	@Autowired
+	private AssignmentDAO assignmentDao;
+
+	@Override
+	public boolean readyToDelete(RepositoryEntry entry, Locale locale, ErrorList errors) {
+		if(assignmentDao.isFormEntryInUse(entry)) {
+			Translator translator = Util.createPackageTranslator(BinderRuntimeController.class, locale);
+			errors.setError(translator.translate("details.delete.error.assignments", new String[] { entry.getDisplayname() }));
+			return false;
+		}
+		return true;
+	}
+}
diff --git a/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_de.properties b/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_de.properties
index edf891978da..1e77b2cb9e4 100644
--- a/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_de.properties
+++ b/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_de.properties
@@ -157,6 +157,7 @@ delete.section.confirm.descr=Wollen Sie wirklich diesem Bereich "{0}" l\u00f6sch
 delete.section.confirm.title=Bereich l\u00f6schen
 deleted.entries=Papierkorb
 deleted.pages.breadcrump=Papierkorb
+details.delete.error.assignments=Lernressource "{0}" kann nicht gel\u00F6scht werden. Es wird von Aufgaben in Portfolio ben\u00FCtzt.
 document.by=von {0}
 document.creationdate=Hochgeladen\: {0}
 document.download=Dokument laden\: 
diff --git a/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_en.properties b/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_en.properties
index 4d6981e6636..87922bd211c 100644
--- a/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_en.properties
+++ b/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_en.properties
@@ -157,6 +157,7 @@ delete.section.confirm.descr=Do you really want to delete this section "{0}"?
 delete.section.confirm.title=Delete section
 deleted.entries=Trash
 deleted.pages.breadcrump=Trash
+details.delete.error.assignments=This learning resource "{0}" cannot be deleted. There are assignment in portfolio which use it.
 document.by=by {0}
 document.creationdate=Uploaded\: {0}
 document.download=Download document\: 
diff --git a/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_fr.properties b/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_fr.properties
index 85e55bd4220..239a4794269 100644
--- a/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_fr.properties
+++ b/src/main/java/org/olat/modules/portfolio/ui/_i18n/LocalStrings_fr.properties
@@ -157,6 +157,7 @@ delete.section.confirm.descr=Voulez-vous r\u00E9ellement effacer la section "{0}
 delete.section.confirm.title=Effacer la section
 deleted.entries=Poubelle
 deleted.pages.breadcrump=Poubelle
+details.delete.error.assignments=La ressource didactique "{0}" ne peut pas \u00EAtre supprim\u00E9e. Des devoirs de portfolios l'utilisent actuellement.
 document.by=de {0}
 document.creationdate=T\u00E9l\u00E9vers\u00E9\: {0}
 document.download=T\u00E9l\u00E9charger le document\:
diff --git a/src/main/java/org/olat/modules/quality/generator/manager/QualityGeneratorDAO.java b/src/main/java/org/olat/modules/quality/generator/manager/QualityGeneratorDAO.java
index e49769f0cc1..0e0a06708d0 100644
--- a/src/main/java/org/olat/modules/quality/generator/manager/QualityGeneratorDAO.java
+++ b/src/main/java/org/olat/modules/quality/generator/manager/QualityGeneratorDAO.java
@@ -35,6 +35,7 @@ import org.olat.modules.quality.generator.QualityGeneratorRef;
 import org.olat.modules.quality.generator.QualityGeneratorSearchParams;
 import org.olat.modules.quality.generator.QualityGeneratorView;
 import org.olat.modules.quality.generator.model.QualityGeneratorImpl;
+import org.olat.repository.RepositoryEntryRef;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -92,7 +93,21 @@ public class QualityGeneratorDAO {
 				.createQuery(sb.toString(), QualityGenerator.class)
 				.getResultList();
 	}
-
+	
+	boolean isFormEntryInUse(RepositoryEntryRef formEntry) {
+		StringBuilder sb = new StringBuilder(256);
+		sb.append("select generator.key");
+		sb.append("  from qualitygenerator as generator");
+		sb.append(" where generator.formEntry.key=:formEntryKey");
+		
+		List<Long> keys = dbInstance.getCurrentEntityManager()
+				.createQuery(sb.toString(), Long.class)
+				.setFirstResult(0)
+				.setMaxResults(1)
+				.setParameter("formEntryKey", formEntry.getKey())
+				.getResultList();
+		return keys != null && !keys.isEmpty() && keys.get(0) != null && keys.get(0).longValue() > 0;
+	}
 
 	void delete(QualityGeneratorRef generatorRef) {
 		if (generatorRef == null || generatorRef.getKey() == null) return;
diff --git a/src/main/java/org/olat/modules/quality/generator/manager/QualityGeneratorFormReadyToDelete.java b/src/main/java/org/olat/modules/quality/generator/manager/QualityGeneratorFormReadyToDelete.java
new file mode 100644
index 00000000000..4d0d7123fc4
--- /dev/null
+++ b/src/main/java/org/olat/modules/quality/generator/manager/QualityGeneratorFormReadyToDelete.java
@@ -0,0 +1,55 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.modules.quality.generator.manager;
+
+import java.util.Locale;
+
+import org.olat.core.gui.translator.Translator;
+import org.olat.core.util.Util;
+import org.olat.modules.forms.EvaluationFormReadyToDelete;
+import org.olat.modules.quality.generator.ui.GeneratorController;
+import org.olat.repository.ErrorList;
+import org.olat.repository.RepositoryEntry;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * 
+ * Initial date: 15 avr. 2019<br>
+ * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
+ *
+ */
+@Service
+public class QualityGeneratorFormReadyToDelete implements EvaluationFormReadyToDelete {
+	
+	@Autowired
+	private QualityGeneratorDAO qualityGeneratorDao;
+
+	@Override
+	public boolean readyToDelete(RepositoryEntry entry, Locale locale, ErrorList errors) {
+		if(qualityGeneratorDao.isFormEntryInUse(entry)) {
+			Translator translator = Util.createPackageTranslator(GeneratorController.class, locale);
+			errors.setError(translator.translate("details.delete.error.quality.form.entry", new String[] { entry.getDisplayname() }));
+			return false;
+		}
+		
+		return true;
+	}
+}
diff --git a/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_de.properties b/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_de.properties
index e1cc9bf8f3a..42e2b335382 100644
--- a/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_de.properties
+++ b/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_de.properties
@@ -15,6 +15,7 @@ curriculum.element.select.curriculum=Curriculum
 curriculum.element.select.curriculum.element=Curriculumelement
 curriculum.element.select.title=Curriculumelement hinzuf\u00FCgen
 curriculum.element.type.name=Typ
+details.delete.error.quality.form.entry=Lernressource "{0}" kann nicht gel\u00F6scht werden. Es wird von einen Datenerhebungen ben\u00FCtzt.
 generator.configuration=Konfiguration
 generator.create=Generator erstellen
 generator.create.button=Erstellen
diff --git a/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_en.properties b/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_en.properties
index f273c699642..9ab67696d14 100644
--- a/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_en.properties
+++ b/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_en.properties
@@ -16,6 +16,7 @@ curriculum.element.select.curriculum.element=Curriculum element
 curriculum.element.select.curriculum=Curriculum
 curriculum.element.select.title=Add curriculum element
 curriculum.element.type.name=Type
+details.delete.error.quality.form.entry=This learning resource "{0}" cannot be deleted. There are data collections which need it.
 generator.configuration=Configuration
 generator.create.button=Create
 generator.create.create=Create
diff --git a/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_fr.properties b/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_fr.properties
index 92f85acf9bf..1af634718a5 100644
--- a/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_fr.properties
+++ b/src/main/java/org/olat/modules/quality/generator/ui/_i18n/LocalStrings_fr.properties
@@ -15,6 +15,7 @@ curriculum.element.select.curriculum=Cursus
 curriculum.element.select.curriculum.element=El\u00E9ment de cursus
 curriculum.element.select.title=Ajouter un \u00E9l\u00E9ment de cursus
 curriculum.element.type.name=Type
+details.delete.error.quality.form.entry=La ressource didactique "{0}" ne peut pas \u00EAtre supprim\u00E9e. Elle est utilis\u00E9e pour collecter des donn\u00E9es.
 generator.configuration=Configuration
 generator.create=Cr\u00E9er un g\u00E9n\u00E9rateur
 generator.create.button=Cr\u00E9er
diff --git a/src/main/java/org/olat/repository/ui/author/ConfirmDeletePermanentlyController.java b/src/main/java/org/olat/repository/ui/author/ConfirmDeletePermanentlyController.java
index 94f7a8d9ff8..f13fbfb6f9f 100644
--- a/src/main/java/org/olat/repository/ui/author/ConfirmDeletePermanentlyController.java
+++ b/src/main/java/org/olat/repository/ui/author/ConfirmDeletePermanentlyController.java
@@ -100,7 +100,7 @@ public class ConfirmDeletePermanentlyController extends FormBasicController {
 	protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) {
 		if(formLayout instanceof FormLayoutContainer) {
 			FormLayoutContainer layout = (FormLayoutContainer)formLayout;
-			layout.contextPut("notAllDeleteable", new Boolean(notAllDeleteable));
+			layout.contextPut("notAllDeleteable", Boolean.valueOf(notAllDeleteable));
 			layout.contextPut("numOfMembers", Integer.toString(numOfMembers));
 
 			FormLayoutContainer layoutCont = FormLayoutContainer.createDefaultFormLayout("confirm", getTranslator());
@@ -247,7 +247,7 @@ public class ConfirmDeletePermanentlyController extends FormBasicController {
 			Collection<String> selectedKeys = referencesEl.getSelectedKeys();
 			List<RepositoryEntry> referencesToDelete = new ArrayList<>(selectedKeys.size());
 			for(String selectedRefKey:selectedKeys) {
-				Long key = new Long(selectedRefKey);
+				Long key = Long.valueOf(selectedRefKey);
 				ReferenceInfos refInfos = referencesMap.get(key);
 				if(refInfos != null && refInfos.isOrphan() && refInfos.isOwner() && !refInfos.isManaged()) {
 					referencesToDelete.add(referencesMap.get(key).getEntry());
diff --git a/src/test/java/org/olat/modules/portfolio/manager/AssignmentDAOTest.java b/src/test/java/org/olat/modules/portfolio/manager/AssignmentDAOTest.java
index 6f2aec6d26d..eea7bb4b83e 100644
--- a/src/test/java/org/olat/modules/portfolio/manager/AssignmentDAOTest.java
+++ b/src/test/java/org/olat/modules/portfolio/manager/AssignmentDAOTest.java
@@ -28,6 +28,7 @@ import org.olat.basesecurity.OrganisationService;
 import org.olat.core.commons.persistence.DB;
 import org.olat.core.id.Identity;
 import org.olat.core.id.Organisation;
+import org.olat.modules.forms.manager.EvaluationFormTestsHelper;
 import org.olat.modules.portfolio.Assignment;
 import org.olat.modules.portfolio.AssignmentStatus;
 import org.olat.modules.portfolio.AssignmentType;
@@ -64,6 +65,8 @@ public class AssignmentDAOTest extends OlatTestCase {
 	private RepositoryService repositoryService;
 	@Autowired
 	private OrganisationService organisationService;
+	@Autowired
+	private EvaluationFormTestsHelper evaTestHelper;
 	
 	@Test
 	public void createBinderWithAssignment() {
@@ -312,6 +315,38 @@ public class AssignmentDAOTest extends OlatTestCase {
 		Assert.assertTrue(assignmentInUse);
 	}
 	
+	@Test
+	public void isFormEntryInUse() {
+		Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("port-u-10");
+		RepositoryEntry templateEntry = createTemplate(owner, "Template", "TE");
+		RepositoryEntry formEntry = evaTestHelper.createSurvey().getFormEntry();
+		dbInstance.commitAndCloseSession();
+		
+		//1 section
+		Binder templateBinder = portfolioService.getBinderByResource(templateEntry.getOlatResource());
+		SectionRef sectionRef = portfolioService.getSections(templateBinder).get(0);
+		dbInstance.commit();
+		
+		//make 1 assignment
+		Section templateSection = portfolioService.getSection(sectionRef);
+		portfolioService.addAssignment("1 Assignment", "", "", AssignmentType.essay, false, templateSection, null, false, false, false, formEntry);
+		dbInstance.commit();
+		
+		// check the method
+		boolean formEntryNotInUse = assignmentDao.isFormEntryInUse(formEntry);
+		Assert.assertTrue(formEntryNotInUse);
+	}
+	
+	@Test
+	public void isFormEntryInUse_notUsed() {
+		RepositoryEntry notUsedFormEntry = evaTestHelper.createSurvey().getFormEntry();
+		dbInstance.commitAndCloseSession();
+
+		// check the method
+		boolean formEntryNotInUse = assignmentDao.isFormEntryInUse(notUsedFormEntry);
+		Assert.assertFalse(formEntryNotInUse);
+	}
+	
 	@Test
 	public void loadAssignment_pageBody() {
 		Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("port-u-10");
-- 
GitLab