From f563515230e70adeb871cec77034b578ffd33c32 Mon Sep 17 00:00:00 2001
From: gnaegi <none@none>
Date: Thu, 25 Aug 2016 17:36:16 +0200
Subject: [PATCH] OO-2057 only show artefactv1 import button when there is
 something to import

---
 .../modules/portfolio/ui/MediaCenterController.java | 13 ++++++++++---
 .../olat/portfolio/manager/EPArtefactManager.java   | 12 ++++++++++++
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/olat/modules/portfolio/ui/MediaCenterController.java b/src/main/java/org/olat/modules/portfolio/ui/MediaCenterController.java
index a132de0144a..5fb5877c62f 100644
--- a/src/main/java/org/olat/modules/portfolio/ui/MediaCenterController.java
+++ b/src/main/java/org/olat/modules/portfolio/ui/MediaCenterController.java
@@ -84,6 +84,7 @@ import org.olat.modules.portfolio.ui.media.CollectCitationMediaController;
 import org.olat.modules.portfolio.ui.media.CollectTextMediaController;
 import org.olat.modules.portfolio.ui.model.MediaRow;
 import org.olat.modules.portfolio.ui.renderer.MediaTypeCellRenderer;
+import org.olat.portfolio.manager.EPArtefactManager;
 import org.olat.portfolio.model.artefacts.AbstractArtefact;
 import org.olat.portfolio.ui.EPArtefactPoolRunController;
 import org.olat.portfolio.ui.artefacts.view.EPArtefactChoosenEvent;
@@ -125,6 +126,9 @@ public class MediaCenterController extends FormBasicController
 	
 	@Autowired
 	private PortfolioService portfolioService;
+	
+	@Autowired
+	private EPArtefactManager legacyArtefactManager;
 	 
 	public MediaCenterController(UserRequest ureq, WindowControl wControl) {
 		super(ureq, wControl, "medias");
@@ -158,9 +162,12 @@ public class MediaCenterController extends FormBasicController
 		addCitationLink.setIconLeftCSS("o_icon o_icon-lg o_icon_citation");
 		stackPanel.addTool(addCitationLink, Align.left);
 		
-		importArtefactV1Link = LinkFactory.createToolLink("import.artefactV1", translate("import.artefactV1"), this);
-		importArtefactV1Link.setIconLeftCSS("o_icon o_icon-lg o_icon_import");
-		stackPanel.addTool(importArtefactV1Link, Align.left);
+		// only if there are v1 artefacts available
+		if (legacyArtefactManager.countArtefacts(getIdentity()) > 0) {
+			importArtefactV1Link = LinkFactory.createToolLink("import.artefactV1", translate("import.artefactV1"), this);
+			importArtefactV1Link.setIconLeftCSS("o_icon o_icon-lg o_icon_import");
+			stackPanel.addTool(importArtefactV1Link, Align.left);			
+		}
 	}
 
 	@Override
diff --git a/src/main/java/org/olat/portfolio/manager/EPArtefactManager.java b/src/main/java/org/olat/portfolio/manager/EPArtefactManager.java
index f3cb94f368a..19f127b8f8f 100755
--- a/src/main/java/org/olat/portfolio/manager/EPArtefactManager.java
+++ b/src/main/java/org/olat/portfolio/manager/EPArtefactManager.java
@@ -488,6 +488,18 @@ public class EPArtefactManager extends BasicManager {
 		return artefacts;		
 	}
 	
+	public int countArtefacts(Identity identity) {
+		StringBuilder sb = new StringBuilder();
+		sb.append("select count(artefact) from ").append(AbstractArtefact.class.getName()).append(" artefact")
+			.append(" where artefact.author=:ident");
+
+		Number count = dbInstance.getCurrentEntityManager()
+			.createQuery(sb.toString(), Number.class)
+			.setParameter("ident", identity)
+			.getSingleResult();
+		return count == null ? 0: count.intValue();
+	}
+	
 	protected Map<String,Long> loadNumOfArtefactsByStartingBusinessPath(String startOfBusinessPath, IdentityRef author) {
 		if (!StringHelper.containsNonWhitespace(startOfBusinessPath) || author == null) {
 			return Collections.emptyMap();
-- 
GitLab