From c5e2919b98f1feb3a30bc9c97f7113c6a5521ad6 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Wed, 7 Jun 2017 17:21:07 +0200
Subject: [PATCH] OO-2636: implement deletion of courses and users

---
 .../olat/modules/lecture/LectureService.java  |  7 ++++++
 .../lecture/manager/LectureBlockDAO.java      |  7 ++++++
 .../manager/LectureBlockReminderDAO.java      |  8 +++++++
 .../manager/LectureBlockRollCallDAO.java      |  8 +++++++
 .../manager/LectureParticipantSummaryDAO.java | 18 +++++++++++++++
 .../lecture/manager/LectureServiceImpl.java   | 23 ++++++++++++++++++-
 ...epositoryEntryLectureConfigurationDAO.java | 13 +++++++----
 .../lecture/model/LectureBlockImpl.java       |  5 ++++
 ...positoryEntryLectureConfigurationImpl.java |  5 ++++
 .../lecture/ui/ConfigurationHelper.java       | 14 +++++------
 .../manager/RepositoryServiceImpl.java        |  4 ++++
 11 files changed, 99 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/olat/modules/lecture/LectureService.java b/src/main/java/org/olat/modules/lecture/LectureService.java
index 3e7e29061ea..8f58f0e8ce4 100644
--- a/src/main/java/org/olat/modules/lecture/LectureService.java
+++ b/src/main/java/org/olat/modules/lecture/LectureService.java
@@ -102,6 +102,13 @@ public interface LectureService {
 	 */
 	public void deleteLectureBlock(LectureBlock block);
 	
+	/**
+	 * Delete all the lecture blocks and configuration of the specified course.
+	 * 
+	 * @param entry
+	 */
+	public int delete(RepositoryEntry entry);
+	
 	/**
 	 * Returns all configured reasons.
 	 * 
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 6e198d066fa..8e93146f9e1 100644
--- a/src/main/java/org/olat/modules/lecture/manager/LectureBlockDAO.java
+++ b/src/main/java/org/olat/modules/lecture/manager/LectureBlockDAO.java
@@ -142,6 +142,13 @@ public class LectureBlockDAO {
 		return rows;
 	}
 	
+	public List<LectureBlock> getLectureBlocks(RepositoryEntryRef entry) {
+		return dbInstance.getCurrentEntityManager()
+				.createNamedQuery("lectureBlocksByRepositoryEntry", LectureBlock.class)
+				.setParameter("repoEntryKey", entry.getKey())
+				.getResultList();
+	}
+	
 	/**
 	 * 
 	 * @param entry The course (mandatory)
diff --git a/src/main/java/org/olat/modules/lecture/manager/LectureBlockReminderDAO.java b/src/main/java/org/olat/modules/lecture/manager/LectureBlockReminderDAO.java
index e34d33f87e1..3a020fa7f6a 100644
--- a/src/main/java/org/olat/modules/lecture/manager/LectureBlockReminderDAO.java
+++ b/src/main/java/org/olat/modules/lecture/manager/LectureBlockReminderDAO.java
@@ -78,4 +78,12 @@ public class LectureBlockReminderDAO {
 		}
 		return blockToTeachers;
 	}
+	
+	public int deleteReminders(Identity identity) {
+		String del = "delete from lecturereminder reminder where reminder.identity.key=:identityKey";
+		return dbInstance.getCurrentEntityManager()
+				.createQuery(del)
+				.setParameter("identityKey", identity.getKey())
+				.executeUpdate();
+	}
 }
diff --git a/src/main/java/org/olat/modules/lecture/manager/LectureBlockRollCallDAO.java b/src/main/java/org/olat/modules/lecture/manager/LectureBlockRollCallDAO.java
index eec5074694a..0e54c074963 100644
--- a/src/main/java/org/olat/modules/lecture/manager/LectureBlockRollCallDAO.java
+++ b/src/main/java/org/olat/modules/lecture/manager/LectureBlockRollCallDAO.java
@@ -151,6 +151,14 @@ public class LectureBlockRollCallDAO {
 		return dbInstance.getCurrentEntityManager().merge(rollCall);
 	}
 	
+	public int deleteRollCalls(Identity identity) {
+		String del = "delete from lectureblockrollcall rollcall where rollcall.identity.key=:identityKey";
+		return dbInstance.getCurrentEntityManager()
+				.createQuery(del)
+				.setParameter("identityKey", identity.getKey())
+				.executeUpdate();
+	}
+	
 	public List<LectureBlockRollCall> getRollCalls(LectureBlockRef block) {
 		StringBuilder sb = new StringBuilder();
 		sb.append("select rollcall from lectureblockrollcall rollcall")
diff --git a/src/main/java/org/olat/modules/lecture/manager/LectureParticipantSummaryDAO.java b/src/main/java/org/olat/modules/lecture/manager/LectureParticipantSummaryDAO.java
index 771b2d2ec5a..d2e9f04cdad 100644
--- a/src/main/java/org/olat/modules/lecture/manager/LectureParticipantSummaryDAO.java
+++ b/src/main/java/org/olat/modules/lecture/manager/LectureParticipantSummaryDAO.java
@@ -152,4 +152,22 @@ public class LectureParticipantSummaryDAO {
 	public LectureParticipantSummary update(LectureParticipantSummary summary) {
 		return dbInstance.getCurrentEntityManager().merge(summary);
 	}
+	
+	public int deleteSummaries(RepositoryEntryRef entry) {
+		//delete summaries
+		String deleteSummaries = "delete from lectureparticipantsummary summary where summary.entry.key=:repoEntryKey";
+		return dbInstance.getCurrentEntityManager()
+			.createQuery(deleteSummaries)
+			.setParameter("repoEntryKey", entry.getKey())
+			.executeUpdate();
+	}
+	
+	public int deleteSummaries(Identity identity) {
+		//delete summaries
+		String deleteSummaries = "delete from lectureparticipantsummary summary where summary.identity.key=:identityKey";
+		return dbInstance.getCurrentEntityManager()
+			.createQuery(deleteSummaries)
+			.setParameter("identityKey", identity.getKey())
+			.executeUpdate();
+	}
 }
diff --git a/src/main/java/org/olat/modules/lecture/manager/LectureServiceImpl.java b/src/main/java/org/olat/modules/lecture/manager/LectureServiceImpl.java
index c7cb358fb5d..fd0415062d9 100644
--- a/src/main/java/org/olat/modules/lecture/manager/LectureServiceImpl.java
+++ b/src/main/java/org/olat/modules/lecture/manager/LectureServiceImpl.java
@@ -19,6 +19,7 @@
  */
 package org.olat.modules.lecture.manager;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
@@ -75,6 +76,7 @@ import org.olat.modules.lecture.ui.LectureAdminController;
 import org.olat.repository.RepositoryEntry;
 import org.olat.repository.RepositoryEntryRef;
 import org.olat.repository.manager.RepositoryEntryDAO;
+import org.olat.user.UserDataDeletable;
 import org.olat.user.UserManager;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -86,7 +88,7 @@ import org.springframework.stereotype.Service;
  *
  */
 @Service
-public class LectureServiceImpl implements LectureService {
+public class LectureServiceImpl implements LectureService, UserDataDeletable {
 	
 	private static final CalendarManagedFlag[] CAL_MANAGED_FLAGS = new CalendarManagedFlag[] { CalendarManagedFlag.all };
 
@@ -208,6 +210,25 @@ public class LectureServiceImpl implements LectureService {
 		lectureBlockDao.delete(block);
 	}
 
+	@Override
+	public int delete(RepositoryEntry entry) {
+		int rows = 0;
+		List<LectureBlock> blocksToDelete = lectureBlockDao.getLectureBlocks(entry);
+		for(LectureBlock blockToDelete:blocksToDelete) {
+			rows += lectureBlockDao.delete(blockToDelete);
+		}
+		rows += lectureConfigurationDao.deleteConfiguration(entry);
+		rows += lectureParticipantSummaryDao.deleteSummaries(entry);
+		return rows;
+	}
+
+	@Override
+	public void deleteUserData(Identity identity, String newDeletedUserName, File archivePath) {
+		lectureParticipantSummaryDao.deleteSummaries(identity);
+		lectureBlockRollCallDao.deleteRollCalls(identity);
+		lectureBlockReminderDao.deleteReminders(identity);
+	}
+
 	@Override
 	public LectureBlock getLectureBlock(LectureBlockRef block) {
 		return lectureBlockDao.loadByKey(block.getKey());
diff --git a/src/main/java/org/olat/modules/lecture/manager/RepositoryEntryLectureConfigurationDAO.java b/src/main/java/org/olat/modules/lecture/manager/RepositoryEntryLectureConfigurationDAO.java
index d5f0b2444df..e56955f5dc4 100644
--- a/src/main/java/org/olat/modules/lecture/manager/RepositoryEntryLectureConfigurationDAO.java
+++ b/src/main/java/org/olat/modules/lecture/manager/RepositoryEntryLectureConfigurationDAO.java
@@ -70,12 +70,8 @@ public class RepositoryEntryLectureConfigurationDAO {
 	}
 	
 	public RepositoryEntryLectureConfiguration getConfiguration(RepositoryEntryRef entry) {
-		StringBuilder sb = new StringBuilder();
-		sb.append("select config from lectureentryconfig config")
-		  .append(" where config.entry.key=:entryKey");
-		
 		List<RepositoryEntryLectureConfiguration> configs = dbInstance.getCurrentEntityManager()
-				.createQuery(sb.toString(), RepositoryEntryLectureConfiguration.class)
+				.createNamedQuery("lectureconfigByRepositoryEntry", RepositoryEntryLectureConfiguration.class)
 				.setParameter("entryKey", entry.getKey())
 				.getResultList();
 		return configs == null || configs.isEmpty() ? null : configs.get(0);
@@ -98,4 +94,11 @@ public class RepositoryEntryLectureConfigurationDAO {
 	public RepositoryEntryLectureConfiguration update(RepositoryEntryLectureConfiguration config) {
 		return dbInstance.getCurrentEntityManager().merge(config);
 	}
+	
+	public int deleteConfiguration(RepositoryEntryRef entry) {
+		String sb = "delete from  lectureentryconfig config where config.entry.key=:repoEntryKey";
+		return dbInstance.getCurrentEntityManager().createQuery(sb)
+				.setParameter("repoEntryKey", entry.getKey())
+				.executeUpdate();
+	}
 }
diff --git a/src/main/java/org/olat/modules/lecture/model/LectureBlockImpl.java b/src/main/java/org/olat/modules/lecture/model/LectureBlockImpl.java
index 8fd595bfb7b..b1d87ad3ac3 100644
--- a/src/main/java/org/olat/modules/lecture/model/LectureBlockImpl.java
+++ b/src/main/java/org/olat/modules/lecture/model/LectureBlockImpl.java
@@ -31,6 +31,8 @@ import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
 import javax.persistence.OneToMany;
 import javax.persistence.Table;
 import javax.persistence.Temporal;
@@ -55,6 +57,9 @@ import org.olat.repository.RepositoryEntry;
  */
 @Entity(name="lectureblock")
 @Table(name="o_lecture_block")
+@NamedQueries(
+	@NamedQuery(name="lectureBlocksByRepositoryEntry", query="select block from lectureblock block where block.entry.key=:repoEntryKey")
+)
 public class LectureBlockImpl implements Persistable, LectureBlock {
 
 	private static final long serialVersionUID = -1010006683915268916L;
diff --git a/src/main/java/org/olat/modules/lecture/model/RepositoryEntryLectureConfigurationImpl.java b/src/main/java/org/olat/modules/lecture/model/RepositoryEntryLectureConfigurationImpl.java
index 38d00e55b43..0a79ebd57c9 100644
--- a/src/main/java/org/olat/modules/lecture/model/RepositoryEntryLectureConfigurationImpl.java
+++ b/src/main/java/org/olat/modules/lecture/model/RepositoryEntryLectureConfigurationImpl.java
@@ -29,6 +29,8 @@ import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
 import javax.persistence.Table;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
@@ -45,6 +47,9 @@ import org.olat.repository.RepositoryEntry;
  */
 @Entity(name="lectureentryconfig")
 @Table(name="o_lecture_entry_config")
+@NamedQueries(
+	@NamedQuery(name="lectureconfigByRepositoryEntry", query="select config from lectureentryconfig config where config.entry.key=:entryKey")
+)
 public class RepositoryEntryLectureConfigurationImpl implements Persistable, RepositoryEntryLectureConfiguration {
 
 	private static final long serialVersionUID = -728141275261361935L;
diff --git a/src/main/java/org/olat/modules/lecture/ui/ConfigurationHelper.java b/src/main/java/org/olat/modules/lecture/ui/ConfigurationHelper.java
index 35126550cec..af3ced287fc 100644
--- a/src/main/java/org/olat/modules/lecture/ui/ConfigurationHelper.java
+++ b/src/main/java/org/olat/modules/lecture/ui/ConfigurationHelper.java
@@ -32,22 +32,22 @@ import org.olat.modules.lecture.RepositoryEntryLectureConfiguration;
 public class ConfigurationHelper {
 	
 	public static boolean isRollCallEnabled(RepositoryEntryLectureConfiguration lectureConfig, LectureModule lectureModule) {
-		return (lectureConfig.isOverrideModuleDefault() && lectureConfig.getRollCallEnabled() != null && lectureConfig.getRollCallEnabled().booleanValue())
-				|| (!lectureConfig.isOverrideModuleDefault() && lectureModule.isRollCallDefaultEnabled());
+		return (lectureConfig != null && lectureConfig.isOverrideModuleDefault() && lectureConfig.getRollCallEnabled() != null && lectureConfig.getRollCallEnabled().booleanValue())
+				|| ((lectureConfig == null || !lectureConfig.isOverrideModuleDefault()) && lectureModule.isRollCallDefaultEnabled());
 	}
 
 	public static boolean isSyncTeacherCalendarEnabled(RepositoryEntryLectureConfiguration lectureConfig, LectureModule lectureModule) {
-		return (lectureConfig.isOverrideModuleDefault() && lectureConfig.getTeacherCalendarSyncEnabled() != null && lectureConfig.getTeacherCalendarSyncEnabled().booleanValue())
-				|| (!lectureConfig.isOverrideModuleDefault() && lectureModule.isTeacherCalendarSyncEnabledDefault());
+		return (lectureConfig != null && lectureConfig.isOverrideModuleDefault() && lectureConfig.getTeacherCalendarSyncEnabled() != null && lectureConfig.getTeacherCalendarSyncEnabled().booleanValue())
+				|| ((lectureConfig == null || !lectureConfig.isOverrideModuleDefault()) && lectureModule.isTeacherCalendarSyncEnabledDefault());
 	}
 	
 	public static boolean isSyncParticipantCalendarEnabled(RepositoryEntryLectureConfiguration lectureConfig, LectureModule lectureModule) {
-		return (lectureConfig.isOverrideModuleDefault() && lectureConfig.getParticipantCalendarSyncEnabled() != null && lectureConfig.getParticipantCalendarSyncEnabled().booleanValue())
-				|| (!lectureConfig.isOverrideModuleDefault() && lectureModule.isParticipantCalendarSyncEnabledDefault());
+		return (lectureConfig != null && lectureConfig.isOverrideModuleDefault() && lectureConfig.getParticipantCalendarSyncEnabled() != null && lectureConfig.getParticipantCalendarSyncEnabled().booleanValue())
+				|| ((lectureConfig == null || !lectureConfig.isOverrideModuleDefault()) && lectureModule.isParticipantCalendarSyncEnabledDefault());
 	}
 	
 	public static boolean isRateEnabled(RepositoryEntryLectureConfiguration lectureConfig, LectureModule lectureModule) {
-		if(lectureConfig.isOverrideModuleDefault()) {
+		if(lectureConfig != null && lectureConfig.isOverrideModuleDefault()) {
 			return lectureConfig.getCalculateAttendanceRate() == null ?
 					lectureModule.isRollCallCalculateAttendanceRateDefaultEnabled() : lectureConfig.getCalculateAttendanceRate().booleanValue();
 		}
diff --git a/src/main/java/org/olat/repository/manager/RepositoryServiceImpl.java b/src/main/java/org/olat/repository/manager/RepositoryServiceImpl.java
index d84ac4f5389..c5a3f7cb568 100644
--- a/src/main/java/org/olat/repository/manager/RepositoryServiceImpl.java
+++ b/src/main/java/org/olat/repository/manager/RepositoryServiceImpl.java
@@ -62,6 +62,7 @@ import org.olat.course.assessment.manager.UserCourseInformationsManager;
 import org.olat.course.certificate.CertificatesManager;
 import org.olat.ims.qti21.manager.AssessmentTestSessionDAO;
 import org.olat.modules.assessment.manager.AssessmentEntryDAO;
+import org.olat.modules.lecture.LectureService;
 import org.olat.modules.reminder.manager.ReminderDAO;
 import org.olat.repository.ErrorList;
 import org.olat.repository.RepositoryEntry;
@@ -403,6 +404,9 @@ public class RepositoryServiceImpl implements RepositoryService {
 		//delete all pending tasks
 		persistentTaskDao.delete(resource);
 		dbInstance.commit();
+		//delete lectures
+		CoreSpringFactory.getImpl(LectureService.class).delete(entry);
+		dbInstance.commit();
 		
 		// inform handler to do any cleanup work... handler must delete the
 		// referenced resourceable a swell.
-- 
GitLab