From 62ad524085b0a62652a2f17c509fbe64a3a54dae Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Thu, 23 Apr 2015 14:15:28 +0200
Subject: [PATCH] OO-1524: fix too much parameters in a select not in ()

---
 .../archiver/ScoreAccountingHelper.java       |  7 ++++-
 .../PersistingCoursePropertyManager.java      | 28 +++++++++++--------
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/olat/course/archiver/ScoreAccountingHelper.java b/src/main/java/org/olat/course/archiver/ScoreAccountingHelper.java
index 61d9e503a41..c9eed9a88e0 100644
--- a/src/main/java/org/olat/course/archiver/ScoreAccountingHelper.java
+++ b/src/main/java/org/olat/course/archiver/ScoreAccountingHelper.java
@@ -28,6 +28,7 @@ package org.olat.course.archiver;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -383,6 +384,7 @@ public class ScoreAccountingHelper {
 		
 		BusinessGroupService businessGroupService = CoreSpringFactory.getImpl(BusinessGroupService.class);
 		List<Identity> userList = businessGroupService.getMembers(groups, GroupRoles.participant.name());
+		userList = new ArrayList<>(new HashSet<>(userList));
 		OLATResourceable ores = OresHelper.createOLATResourceableInstance(CourseModule.class, courseEnv.getCourseResourceableId());
 		RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(ores, false);
 		if(re != null) {
@@ -391,7 +393,10 @@ public class ScoreAccountingHelper {
 		}
 
 		List<Identity> assessedList = courseEnv.getCoursePropertyManager().getAllIdentitiesWithCourseAssessmentData(userList);
-		userList.addAll(assessedList);
+		if(assessedList.size() > 0) {
+			assessedList.removeAll(userList);//deduplicate
+			userList.addAll(assessedList);
+		}
 		return userList;
 	}
 	
diff --git a/src/main/java/org/olat/course/properties/PersistingCoursePropertyManager.java b/src/main/java/org/olat/course/properties/PersistingCoursePropertyManager.java
index c4ed659ad87..82f3b8aaf80 100644
--- a/src/main/java/org/olat/course/properties/PersistingCoursePropertyManager.java
+++ b/src/main/java/org/olat/course/properties/PersistingCoursePropertyManager.java
@@ -32,10 +32,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.Random;
 
+import javax.persistence.TypedQuery;
+
 import org.olat.basesecurity.IdentityImpl;
-import org.olat.core.commons.persistence.DB;
 import org.olat.core.commons.persistence.DBFactory;
-import org.olat.core.commons.persistence.DBQuery;
 import org.olat.core.id.Identity;
 import org.olat.core.id.OLATResourceable;
 import org.olat.core.logging.AssertException;
@@ -219,8 +219,11 @@ public class PersistingCoursePropertyManager extends BasicManager implements Cou
 	}
 
 	/**
-	 * @see org.olat.course.properties.CoursePropertyManager#getAllIdentitiesWithCourseAssessmentData()
+	 * The specified exclude identities is only a best effort used for performance. If you want
+	 * unique identities, deduplicate them afterwards.
+	 * 
 	 */
+	@Override
 	public List<Identity> getAllIdentitiesWithCourseAssessmentData(Collection<Identity> excludeIdentities) {
 		StringBuilder query = new StringBuilder();
 		query.append("select distinct i from ")
@@ -235,20 +238,21 @@ public class PersistingCoursePropertyManager extends BasicManager implements Cou
 			query.append(" and p.identity.key not in (:excludeIdentities) ");
 		}
 
-		DB db = DBFactory.getInstance();
-		DBQuery dbq = db.createQuery(query.toString());
-		dbq.setLong("resid", ores.getResourceableId());
-		dbq.setString("resname", ores.getResourceableTypeName());
+		TypedQuery<Identity> db = DBFactory.getInstance().getCurrentEntityManager()
+				.createQuery(query.toString(), Identity.class)
+				.setParameter("resid", ores.getResourceableId())
+				.setParameter("resname", ores.getResourceableTypeName());
 		if(excludeIdentities != null && !excludeIdentities.isEmpty()) {
 			List<Long> excludeKeys = new ArrayList<Long>();
 			for(Identity identity:excludeIdentities) {
 				excludeKeys.add(identity.getKey());
 			}
-			dbq.setParameterList("excludeIdentities", excludeKeys);
+			//limit because Oracle and Hibernate doesn't like more than 1000
+			if(excludeKeys.size() > 900) {
+				excludeKeys = excludeKeys.subList(0, 900);
+			}
+			db.setParameter("excludeIdentities", excludeKeys);
 		}
-
-		List<Identity> res = dbq.list();
-		return res;
+		return db.getResultList();
 	}
-
 }
\ No newline at end of file
-- 
GitLab