From e46f1ffb9d60029ed64b72198e2191c99f372814 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Mon, 15 Oct 2012 12:24:24 +0200
Subject: [PATCH] O-291: remove all relation between a course and its groups
 before deleting it

---
 .../PersistingCourseGroupManager.java         |  4 +-
 .../member/AbstractMemberListController.java  |  4 +-
 .../org/olat/group/BusinessGroupService.java  |  2 +
 .../group/manager/BusinessGroupArchiver.java  |  8 +--
 .../manager/BusinessGroupServiceImpl.java     | 71 +++++++++++--------
 .../AbstractBusinessGroupListController.java  |  2 +-
 .../run/BusinessGroupSendToChooserForm.java   | 10 +--
 7 files changed, 59 insertions(+), 42 deletions(-)

diff --git a/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java b/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java
index d5df2d1a9af..0eaf64aa2bd 100644
--- a/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java
+++ b/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java
@@ -257,8 +257,8 @@ public class PersistingCourseGroupManager extends BasicManager implements Course
 	 * @see org.olat.course.groupsandrights.CourseGroupManager#deleteCourseGroupmanagement()
 	 */
 	public void deleteCourseGroupmanagement() {
-		//TODO gm
-		//delete something???
+		//delete permission group to course
+		businessGroupService.removeResource(courseResource);
 		logAudit("Deleting course groupmanagement for " + courseResource.toString());
 	}
 
diff --git a/src/main/java/org/olat/course/member/AbstractMemberListController.java b/src/main/java/org/olat/course/member/AbstractMemberListController.java
index 6bea5dfd529..697e3134e07 100644
--- a/src/main/java/org/olat/course/member/AbstractMemberListController.java
+++ b/src/main/java/org/olat/course/member/AbstractMemberListController.java
@@ -202,7 +202,7 @@ public abstract class AbstractMemberListController extends BasicController imple
 			if (event != Event.CANCELLED_EVENT && DialogBoxUIFactory.isYesEvent(event)) {
 				@SuppressWarnings("unchecked")
 				List<Identity> members = (List<Identity>)leaveDialogBox.getUserObject();
-				doLeave(ureq, members);
+				doLeave(members);
 				reloadModel();
 			}
 		} else if(source == editMemberCtrl) {
@@ -347,7 +347,7 @@ public abstract class AbstractMemberListController extends BasicController imple
 		}
 	}
 	
-	protected void doLeave(UserRequest ureq, List<Identity> members) {
+	protected void doLeave(List<Identity> members) {
 		repositoryManager.removeMembers(members, repoEntry);
 		businessGroupService.removeMembers(getIdentity(), members, repoEntry.getOlatResource());
 		reloadModel();
diff --git a/src/main/java/org/olat/group/BusinessGroupService.java b/src/main/java/org/olat/group/BusinessGroupService.java
index 281a35a3c16..55829e071db 100644
--- a/src/main/java/org/olat/group/BusinessGroupService.java
+++ b/src/main/java/org/olat/group/BusinessGroupService.java
@@ -282,6 +282,8 @@ public interface BusinessGroupService {
 	
 	public void removeResourceFrom(BusinessGroup group, RepositoryEntry re);
 	
+	public void removeResource(OLATResource resource);
+	
 	public List<RepositoryEntry> findRepositoryEntries(Collection<BusinessGroup> groups, int firstResult, int maxResults);
 	
 	/**
diff --git a/src/main/java/org/olat/group/manager/BusinessGroupArchiver.java b/src/main/java/org/olat/group/manager/BusinessGroupArchiver.java
index 4551b145855..ab8d38b6ca9 100644
--- a/src/main/java/org/olat/group/manager/BusinessGroupArchiver.java
+++ b/src/main/java/org/olat/group/manager/BusinessGroupArchiver.java
@@ -334,7 +334,7 @@ public class BusinessGroupArchiver {
 					organisationalEntityList, orgEntityTitle, userLocale, charset);
 		} else if (FILE_PER_GROUP_OR_AREA.equals(archiveType)) {
 			outFile = archiveFilePerGroup(resource, owners, participants, waitings, columnList, organisationalEntityList,
-					orgEntityTitle, userLocale, charset);
+					userLocale, charset);
 		}
 		} catch (IOException e) {
 			throw new OLATRuntimeException(BusinessGroupArchiver.class, "could not create temp file", e);
@@ -482,7 +482,7 @@ public class BusinessGroupArchiver {
 	 * @return the output zip file located into the temp dir.
 	 */
 	private File archiveFilePerGroup(OLATResource resource, List<Member> owners, List<Member> participants,
-			List<Member> waitings, List<String> columnList, List<OrganisationalEntity> groupList, String orgEntityTitle, Locale userLocale,
+			List<Member> waitings, List<String> columnList, List<OrganisationalEntity> groupList, Locale userLocale,
 			String charset) {
 		Set<String> outFiles = new HashSet<String>();
 		File root = null;
@@ -496,7 +496,7 @@ public class BusinessGroupArchiver {
 				List<Member> groupParticipants = getFilteredList(participants, group, PARTICIPANT);
 				List<Member> groupWaiting = getFilteredList(waitings, group, WAITING);
 
-				File filePerGroup = archiveFileSingleGroup(resource, groupOwners, groupParticipants, groupWaiting, columnList, groupList, orgEntityTitle,
+				File filePerGroup = archiveFileSingleGroup(resource, groupOwners, groupParticipants, groupWaiting, columnList,
 						userLocale, group.getName(), tempDir, charset);
 				if (root == null && filePerGroup != null) {
 					root = filePerGroup.getParentFile();
@@ -535,7 +535,7 @@ public class BusinessGroupArchiver {
 	 * @throws IOException
 	 */
 	private File archiveFileSingleGroup(OLATResource resource, List<Member> groupOwners, List<Member> groupParticipants, List<Member> groupWaiting,
-			List<String> columnList, List<OrganisationalEntity> organisationalEntityList, String orgEntityTitle, Locale userLocale, String fileNamePrefix,
+			List<String> columnList, Locale userLocale, String fileNamePrefix,
 			File tempDir, String charset) throws IOException {
 		File outFile = null;
 		StringBuffer stringBuffer = new StringBuffer();
diff --git a/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java b/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java
index 11ad43c46bf..24e9fd76ff7 100644
--- a/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java
+++ b/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java
@@ -488,13 +488,13 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 		}
 		
 		for(Identity newOwner:newOwners) {
-			addOwner(merger, newOwner, targetGroup, syncIM);
+			addOwner(newOwner, targetGroup, syncIM);
 		}
 		for(Identity newParticipant:newParticipants) {
-			addParticipant(merger, newParticipant, targetGroup, syncIM);
+			addParticipant(newParticipant, targetGroup, syncIM);
 		}
 		for(Identity newWaiter:newWaiters) {
-			addToWaitingList(merger, newWaiter, targetGroup);
+			addToWaitingList(newWaiter, targetGroup);
 		}
 			
 		syncIM(syncIM, targetGroup);
@@ -522,17 +522,17 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 
 		for(Identity owner:membersMod.getAddOwners()) {
 			if(!currentOwners.contains(owner)) {
-				addOwner(identity, owner, group, syncIM);
+				addOwner(owner, group, syncIM);
 			}
 		}
 		for(Identity participant:membersMod.getAddParticipants()) {
 			if(!currentParticipants.contains(participant)) {
-				addParticipant(identity, participant, group, syncIM);
+				addParticipant(participant, group, syncIM);
 			}
 		}
 		for(Identity waitingIdentity:membersMod.getAddToWaitingList()) {
 			if(!currentWaitingList.contains(waitingIdentity)) {
-				addToWaitingList(identity, waitingIdentity, group);
+				addToWaitingList(waitingIdentity, group);
 			}
 		}
 		
@@ -546,7 +546,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 				removeParticipant(identity, removed, group, syncIM);
 			}
 			if(currentWaitingList.contains(removed)) {
-				removeFromWaitingList(identity, removed, group);
+				removeFromWaitingList(removed, group);
 			}
 		}
 		removeOwners(identity, ownerToRemove, group);
@@ -603,19 +603,19 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 			group = businessGroupDAO.loadForUpdate(group.getKey());
 					
 			for(Identity id:changesWrapper.addToWaitingList) {
-				addToWaitingList(ureqIdentity, id, group);
+				addToWaitingList(id, group);
 			}
 			for(Identity id:changesWrapper.removeFromWaitingList) {
-				removeFromWaitingList(ureqIdentity, id, group);
+				removeFromWaitingList(id, group);
 			}
 			for(Identity id:changesWrapper.addTutors) {
-				addOwner(ureqIdentity, id, group, syncIM);
+				addOwner(id, group, syncIM);
 			}
 			for(Identity id:changesWrapper.removeTutors) {
 				removeOwner(ureqIdentity, id, group, syncIM);
 			}
 			for(Identity id:changesWrapper.addParticipants) {
-				addParticipant(ureqIdentity, id, group, syncIM);
+				addParticipant(id, group, syncIM);
 			}
 			for(Identity id:changesWrapper.removeParticipants) {
 				removeParticipant(ureqIdentity, id, group, syncIM);
@@ -843,7 +843,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 			group = loadBusinessGroup(group); // reload business group
 			if (securityManager.isIdentityPermittedOnResourceable(identity, Constants.PERMISSION_HASROLE, Constants.ORESOURCE_GUESTONLY)) {
 				response.getIdentitiesWithoutPermission().add(identity);
-			} else if(addOwner(ureqIdentity, identity, group, syncIM)) {
+			} else if(addOwner(identity, group, syncIM)) {
 				response.getAddedIdentities().add(identity);
 				log.audit("added identity '" + identity.getName() + "' to securitygroup with key " + group.getOwnerGroup().getKey());
 			} else {
@@ -854,7 +854,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 		return response;
 	}
 	
-	private boolean addOwner(Identity ureqIdentity, Identity identity, BusinessGroup group, SyncUserListTask syncIM) {
+	private boolean addOwner(Identity identity, BusinessGroup group, SyncUserListTask syncIM) {
 		if (!securityManager.isIdentityInSecurityGroup(identity, group.getOwnerGroup())) {
 			securityManager.addIdentityToSecurityGroup(identity, group.getOwnerGroup());
 			// add user to buddies rosters
@@ -870,7 +870,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 		return false;
 	}
 	
-	private boolean addParticipant(Identity ureqIdentity, Identity identityToAdd, BusinessGroup group, SyncUserListTask syncIM) {
+	private boolean addParticipant(Identity identityToAdd, BusinessGroup group, SyncUserListTask syncIM) {
 		if(!securityManager.isIdentityInSecurityGroup(identityToAdd, group.getPartipiciantGroup())) {
 			securityManager.addIdentityToSecurityGroup(identityToAdd, group.getPartipiciantGroup());
 			// add user to buddies rosters
@@ -898,7 +898,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 		for (final Identity identity : addIdentities) {
 			if (securityManager.isIdentityPermittedOnResourceable(identity, Constants.PERMISSION_HASROLE, Constants.ORESOURCE_GUESTONLY)) {
 				response.getIdentitiesWithoutPermission().add(identity);
-			} else if(addParticipant(ureqIdentity, identity, currBusinessGroup, syncIM)) {
+			} else if(addParticipant(identity, currBusinessGroup, syncIM)) {
 				response.getAddedIdentities().add(identity);
 				log.audit("added identity '" + identity.getName() + "' to securitygroup with key " + currBusinessGroup.getPartipiciantGroup().getKey());
 			} else {
@@ -1009,7 +1009,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 					removeParticipant(ureqIdentity, id, currentGroup, syncIM);
 				}
 				if(membership.getWaitingGroupKey() != null) {
-					removeFromWaitingList(ureqIdentity, id, currentGroup);
+					removeFromWaitingList(id, currentGroup);
 				}
 			} else {
 				return membership;
@@ -1019,7 +1019,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 		return null;
 	}
 
-	private void addToWaitingList(Identity ureqIdentity, Identity identity, BusinessGroup group) {
+	private void addToWaitingList(Identity identity, BusinessGroup group) {
 		securityManager.addIdentityToSecurityGroup(identity, group.getWaitingGroup());
 
 		// notify currently active users of this business group
@@ -1046,7 +1046,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 				response.getIdentitiesAlreadyInGroup().add(identity);
 			} else {
 				// identity has permission and is not already in group => add it
-				addToWaitingList(ureqIdentity, identity, currBusinessGroup);
+				addToWaitingList(identity, currBusinessGroup);
 				response.getAddedIdentities().add(identity);
 				log.audit("added identity '" + identity.getName() + "' to securitygroup with key " + currBusinessGroup.getPartipiciantGroup().getKey());
 			}
@@ -1054,7 +1054,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 		return response;
 	}
 
-	private final void removeFromWaitingList(Identity ureqIdentity, Identity identity, BusinessGroup group) {
+	private final void removeFromWaitingList(Identity identity, BusinessGroup group) {
 		securityManager.removeIdentityFromSecurityGroup(identity, group.getWaitingGroup());
 		// notify currently active users of this business group
 		BusinessGroupModifiedEvent.fireModifiedGroupEvents(BusinessGroupModifiedEvent.IDENTITY_REMOVED_EVENT, group, identity);
@@ -1068,7 +1068,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 		currBusinessGroup = businessGroupDAO.loadForUpdate(currBusinessGroup.getKey());
 		
 		for (Identity identity : identities) {
-		  removeFromWaitingList(ureqIdentity, identity, currBusinessGroup);
+		  removeFromWaitingList(identity, currBusinessGroup);
 		  log.audit("removed identiy '" + identity.getName() + "' from securitygroup with key " + currBusinessGroup.getOwnerGroup().getKey());
 		}
 	}
@@ -1099,8 +1099,8 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 			// check if idenity is allready in participant
 			if (!securityManager.isIdentityInSecurityGroup(identity,currBusinessGroup.getPartipiciantGroup()) ) {
 				// Idenity is not in participant-list => move idenity from waiting-list to participant-list
-				addParticipant(ureqIdentity, identity, currBusinessGroup, syncIM);
-				removeFromWaitingList(ureqIdentity, identity, currBusinessGroup);
+				addParticipant(identity, currBusinessGroup, syncIM);
+				removeFromWaitingList(identity, currBusinessGroup);
 				response.getAddedIdentities().add(identity);
 				// notification mail is handled in controller
 			} else {
@@ -1156,7 +1156,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 		
 		//reservation has the highest priority over max participant or other settings
 		if(reservation != null) {
-			addParticipant(null, identity, reloadedGroup, syncIM);
+			addParticipant(identity, reloadedGroup, syncIM);
 			enrollStatus.setEnrolled(BGMembership.participant);
 			log.info("doEnroll (reservation) - setIsEnrolled ", identity.getName());
 			if(reservation != null) {
@@ -1170,7 +1170,7 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 			if (reservation == null && (participantsCounter + reservations) >= reloadedGroup.getMaxParticipants().intValue()) {
 				// already full, show error and updated choose page again
 				if (reloadedGroup.getWaitingListEnabled().booleanValue()) {
-					addToWaitingList(null, identity, reloadedGroup);
+					addToWaitingList(identity, reloadedGroup);
 					enrollStatus.setEnrolled(BGMembership.waiting);
 				} else {
 					// No Waiting List => List is full
@@ -1179,13 +1179,13 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 				}
 			} else {
 				//enough place
-				addParticipant(null, identity, reloadedGroup, syncIM);
+				addParticipant(identity, reloadedGroup, syncIM);
 				enrollStatus.setEnrolled(BGMembership.participant);
 				log.info("doEnroll - setIsEnrolled ", identity.getName());
 			}
 		} else {
 			if (log.isDebug()) log.debug("doEnroll as participant beginTransaction");
-			addParticipant(null, identity, reloadedGroup, syncIM);
+			addParticipant(identity, reloadedGroup, syncIM);
 			enrollStatus.setEnrolled(BGMembership.participant);						
 			if (log.isDebug()) log.debug("doEnroll as participant committed");
 		}
@@ -1227,8 +1227,8 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 							//            that get triggered in the next two methods to be of ActionType admin
 							//            This is needed to make sure the targetIdentity ends up in the o_loggingtable
 							ThreadLocalUserActivityLogger.setStickyActionType(ActionType.admin);
-							addParticipant(ureqIdentity, firstWaitingListIdentity, group, syncIM);
-							removeFromWaitingList(ureqIdentity, firstWaitingListIdentity, group);
+							addParticipant(firstWaitingListIdentity, group, syncIM);
+							removeFromWaitingList(firstWaitingListIdentity, group);
 						} finally {
 							ThreadLocalUserActivityLogger.setStickyActionType(formerStickyActionType);
 						}
@@ -1378,6 +1378,21 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 		securityManager.deletePolicy(group.getOwnerGroup(), Constants.PERMISSION_COACH, re.getOlatResource());
 		securityManager.deletePolicy(group.getPartipiciantGroup(), Constants.PERMISSION_PARTI, re.getOlatResource());
 	}
+	
+	@Override
+	@Transactional
+	public void removeResource(OLATResource resource) {
+		SearchBusinessGroupParams params = new SearchBusinessGroupParams();
+		List<BusinessGroup> groups = findBusinessGroups(params, resource, 0, -1);
+		for(BusinessGroup group:groups) {
+			businessGroupRelationDAO.deleteRelation(group, resource);
+			//remove author permission
+			//securityManager.deletePolicy(re.getOwnerGroup(), Constants.PERMISSION_ACCESS, group.getResource());
+			//remove permission
+			securityManager.deletePolicy(group.getOwnerGroup(), Constants.PERMISSION_COACH, resource);
+			securityManager.deletePolicy(group.getPartipiciantGroup(), Constants.PERMISSION_PARTI, resource);
+		}
+	}
 
 	@Override
 	@Transactional(readOnly=true)
diff --git a/src/main/java/org/olat/group/ui/main/AbstractBusinessGroupListController.java b/src/main/java/org/olat/group/ui/main/AbstractBusinessGroupListController.java
index 9aa8ebd33f6..d870c6c8126 100644
--- a/src/main/java/org/olat/group/ui/main/AbstractBusinessGroupListController.java
+++ b/src/main/java/org/olat/group/ui/main/AbstractBusinessGroupListController.java
@@ -614,7 +614,7 @@ public abstract class AbstractBusinessGroupListController extends BasicControlle
 	private void finishUserManagement(MembershipModification mod, List<BusinessGroup> groups, MailTemplate template, boolean sendMail) {
 		businessGroupService.updateMembership(getIdentity(), mod, groups);
 		
-		if (template != null && !mod.isEmpty()) {
+		if (template != null && !mod.isEmpty() && sendMail) {
 			List<Identity> movedIdentities = mod.getAllIdentities();
 			MailerWithTemplate mailer = MailerWithTemplate.getInstance();
 			MailContext context = new MailContextImpl(null, null, getWindowControl().getBusinessControl().getAsString());
diff --git a/src/main/java/org/olat/group/ui/run/BusinessGroupSendToChooserForm.java b/src/main/java/org/olat/group/ui/run/BusinessGroupSendToChooserForm.java
index 0157dedc558..ca37f2c3e5c 100644
--- a/src/main/java/org/olat/group/ui/run/BusinessGroupSendToChooserForm.java
+++ b/src/main/java/org/olat/group/ui/run/BusinessGroupSendToChooserForm.java
@@ -130,7 +130,7 @@ public class BusinessGroupSendToChooserForm extends FormBasicController {
 
 			// Owner MultiSelection
 			SecurityGroup owners = businessGroup.getOwnerGroup();			
-			keysOwner = getMemberKeys(ureq, owners);
+			keysOwner = getMemberKeys(owners);
 			valuesOwner = getMemberValues(ureq, owners); 
 			ArrayHelper.sort(keysOwner, valuesOwner, false, true, false);
 		} else {
@@ -176,7 +176,7 @@ public class BusinessGroupSendToChooserForm extends FormBasicController {
 				
 				// Participant MultiSelection
 				SecurityGroup participants = businessGroup.getPartipiciantGroup();
-				keysPartips = getMemberKeys(ureq, participants);
+				keysPartips = getMemberKeys(participants);
 				valuesPartips = getMemberValues(ureq, participants); 
 				ArrayHelper.sort(keysPartips, valuesPartips, false, true, false);
 			}
@@ -215,8 +215,8 @@ public class BusinessGroupSendToChooserForm extends FormBasicController {
 		if (showWaitingList) {
 		  // Waitings MultiSelection
 			SecurityGroup waitingList = businessGroup.getWaitingGroup();
-			keysWaitings = this.getMemberKeys(ureq, waitingList);
-			valuesWaitings = this.getMemberValues(ureq, waitingList);			
+			keysWaitings = getMemberKeys(waitingList);
+			valuesWaitings = getMemberValues(ureq, waitingList);			
 			ArrayHelper.sort(keysWaitings, valuesWaitings, false, true, false);
 		} else {
 			keysWaitings = new String[]{};
@@ -258,7 +258,7 @@ public class BusinessGroupSendToChooserForm extends FormBasicController {
 	 * @param securityGroup
 	 * @return
 	 */
-	private String[] getMemberKeys(UserRequest ureq, SecurityGroup securityGroup) {
+	private String[] getMemberKeys(SecurityGroup securityGroup) {
 		String[] keys = new String[0];		
 		List<Identity> membersList = BaseSecurityManager.getInstance().getIdentitiesOfSecurityGroup(securityGroup);		
 		keys = new String[membersList.size()];
-- 
GitLab