From e3e9763b248d78dad3cc1f4f84ec18197b583a94 Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Mon, 30 Sep 2019 09:12:05 +0200
Subject: [PATCH] OO-4281: optimize query to search lectures especially for
 managers

---
 .../lecture/manager/LectureBlockDAO.java      | 15 ++---
 .../lecture/manager/LectureBlockDAOTest.java  | 62 ++++++++++++++++++-
 2 files changed, 67 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/olat/modules/lecture/manager/LectureBlockDAO.java b/src/main/java/org/olat/modules/lecture/manager/LectureBlockDAO.java
index aa4cfaf7d68..2494b77fb23 100644
--- a/src/main/java/org/olat/modules/lecture/manager/LectureBlockDAO.java
+++ b/src/main/java/org/olat/modules/lecture/manager/LectureBlockDAO.java
@@ -180,9 +180,8 @@ public class LectureBlockDAO {
 	public List<LectureBlock> searchLectureBlocks(LecturesBlockSearchParameters searchParams) {
 		QueryBuilder sb = new QueryBuilder(2048);
 		sb.append("select distinct block from lectureblock block")
-		  .append(" inner join block.teacherGroup tGroup")
-		  .append(" inner join tGroup.members membership")
-		  .append(" inner join fetch block.entry entry");
+		  .append(" inner join fetch block.entry entry")
+		  .append(" inner join fetch entry.olatResource oRes");
 		addSearchParametersToQuery(sb, searchParams);
 		sb.and()
 		  .append(" exists (select config.key from lectureentryconfig config")
@@ -198,8 +197,6 @@ public class LectureBlockDAO {
 	public List<LectureBlockRef> searchAssessedLectureBlocks(LecturesBlockSearchParameters searchParams) {
 		QueryBuilder sb = new QueryBuilder(512);
 		sb.append("select distinct block.key from lectureblock block")
-		  .append(" inner join block.teacherGroup tGroup")
-		  .append(" inner join tGroup.members membership")
 		  .append(" inner join courseassessmentmode mode on (mode.lectureBlock.key=block.key)")
 		  .append(" inner join block.entry entry");
 		addSearchParametersToQuery(sb, searchParams);
@@ -588,9 +585,9 @@ public class LectureBlockDAO {
 		}
 		if(searchParams.getManager() != null) {
 			sb.and()
-			  .append(" exists (select membership.key from repoentrytogroup as rel, bgroupmember as membership")
-	          .append("   where rel.entry.key=entry.key and rel.group.key=membership.group.key and membership.identity.key=:managerKey")
-	          .append("   and membership.role ").in(OrganisationRoles.administrator, OrganisationRoles.learnresourcemanager, OrganisationRoles.lecturemanager, GroupRoles.owner.name())
+			  .append(" exists (select managerMembership.key from repoentrytogroup as rel, bgroupmember as managerMembership")
+	          .append("   where rel.entry.key=entry.key and rel.group.key=managerMembership.group.key and managerMembership.identity.key=:managerKey")
+	          .append("   and managerMembership.role ").in(OrganisationRoles.administrator, OrganisationRoles.learnresourcemanager, OrganisationRoles.lecturemanager, GroupRoles.owner.name())
 	          .append(" )");
 		}
 		if(searchParams.getMasterCoach() != null) {
@@ -610,7 +607,7 @@ public class LectureBlockDAO {
 		if(searchParams.getTeacher() != null) {
 			sb.and()
 			  .append(" exists (select teachership.key from bgroupmember teachership where")
-			  .append("  teachership.group.key=tGroup.key and teachership.identity.key=:teacherKey")
+			  .append("  teachership.group.key=block.teacherGroup.key and teachership.identity.key=:teacherKey")
 			  .append(" )");
 		}
 	}
diff --git a/src/test/java/org/olat/modules/lecture/manager/LectureBlockDAOTest.java b/src/test/java/org/olat/modules/lecture/manager/LectureBlockDAOTest.java
index 15e974bd7fe..ec6a3cf8de0 100644
--- a/src/test/java/org/olat/modules/lecture/manager/LectureBlockDAOTest.java
+++ b/src/test/java/org/olat/modules/lecture/manager/LectureBlockDAOTest.java
@@ -177,7 +177,7 @@ public class LectureBlockDAOTest extends OlatTestCase {
 	}
 	
 	@Test
-	public void searchLectureBlocks() {
+	public void searchLectureBlocks_lectureManager() {
 		Identity teacher = JunitTestHelper.createAndPersistIdentityAsRndUser("lec-teacher-1");
 		Identity lectureManager = JunitTestHelper.createAndPersistIdentityAsRndUser("lec-manager-1");
 		RepositoryEntry entry = createResourceWithLecturesEnabled();
@@ -241,6 +241,66 @@ public class LectureBlockDAOTest extends OlatTestCase {
 		Assert.assertTrue(blocks.isEmpty());
 	}
 	
+	@Test
+	public void searchLectureBlocks_lectureManager_negative() {
+		Identity teacher = JunitTestHelper.createAndPersistIdentityAsRndUser("lec-teacher-1");
+		Identity lectureManager = JunitTestHelper.createAndPersistIdentityAsRndUser("lec-manager-1");
+		RepositoryEntry entry = createResourceWithLecturesEnabled();
+		repositoryEntryRelationDao.addRole(lectureManager, entry, OrganisationRoles.lecturemanager.name());
+		
+		LectureBlock lectureBlock = lectureBlockDao.createLectureBlock(entry);
+		lectureBlock.setStartDate(new Date());
+		lectureBlock.setEndDate(new Date());
+		lectureBlock.setTitle("Hello lecture manager");
+		lectureBlock = lectureBlockDao.update(lectureBlock);
+		lectureService.addTeacher(lectureBlock, teacher);
+		dbInstance.commitAndCloseSession();
+		
+		// other course, other lecture manager
+		Identity otherLectureManager = JunitTestHelper.createAndPersistIdentityAsRndUser("lec-manager-alien");
+		RepositoryEntry otherEntry = createResourceWithLecturesEnabled();
+		repositoryEntryRelationDao.addRole(otherLectureManager, otherEntry, OrganisationRoles.lecturemanager.name());
+		dbInstance.commitAndCloseSession();
+
+		// first see something
+		LecturesBlockSearchParameters searchParams = new LecturesBlockSearchParameters();
+		searchParams.setManager(lectureManager);
+		List<LectureBlock> blocks = lectureBlockDao.searchLectureBlocks(searchParams);
+		Assert.assertNotNull(blocks);
+		Assert.assertEquals(1, blocks.size());
+		
+		// second not
+		LecturesBlockSearchParameters otherSearchParams = new LecturesBlockSearchParameters();
+		otherSearchParams.setManager(otherLectureManager);
+		List<LectureBlock> otherBlocks = lectureBlockDao.searchLectureBlocks(otherSearchParams);
+		Assert.assertNotNull(otherBlocks);
+		Assert.assertTrue(otherBlocks.isEmpty());
+	}
+	
+	@Test
+	public void searchLectureBlocks_lectureManager_organisation() {
+		Identity teacher = JunitTestHelper.createAndPersistIdentityAsRndUser("lec-teacher-1");
+		Identity lectureManager = JunitTestHelper.createAndPersistIdentityAsRndUser("lec-manager-org");
+		RepositoryEntry entry = createResourceWithLecturesEnabled();
+		organisationService.addMember(lectureManager, OrganisationRoles.lecturemanager);
+		
+		LectureBlock lectureBlock = lectureBlockDao.createLectureBlock(entry);
+		lectureBlock.setStartDate(new Date());
+		lectureBlock.setEndDate(new Date());
+		lectureBlock.setTitle("Hello lecture manager");
+		lectureBlock = lectureBlockDao.update(lectureBlock);
+		lectureService.addTeacher(lectureBlock, teacher);
+		dbInstance.commitAndCloseSession();
+
+		// first see something
+		LecturesBlockSearchParameters searchParams = new LecturesBlockSearchParameters();
+		searchParams.setManager(lectureManager);
+		List<LectureBlock> blocks = lectureBlockDao.searchLectureBlocks(searchParams);
+		Assert.assertNotNull(blocks);
+		Assert.assertFalse(blocks.isEmpty());
+		Assert.assertTrue(blocks.contains(lectureBlock));
+	}
+	
 	@Test
 	public void getLectureBlocks_all() {
 		RepositoryEntry entry = JunitTestHelper.createAndPersistRepositoryEntry();
-- 
GitLab