From b9e387c3236705cc2fb7fa8a01eb328c68f0be68 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Mon, 6 Aug 2012 09:50:52 +0200
Subject: [PATCH] OO-291: avoid groups and areas with the same name if a course
 is exported with the backward compatibility flag

---
 .../org/olat/course/PersistingCourseImpl.java | 20 ++++++-----
 .../export/CourseEnvironmentMapper.java       | 36 +++++++++++++++++++
 .../groupsandrights/CourseGroupManager.java   |  3 +-
 .../PersistingCourseGroupManager.java         |  9 +++--
 .../preview/PreviewCourseGroupManager.java    |  2 +-
 .../org/olat/group/BusinessGroupService.java  |  2 +-
 .../manager/BusinessGroupImportExport.java    | 23 ++++++++----
 .../manager/BusinessGroupServiceImpl.java     |  5 +--
 .../group/model/BusinessGroupEnvironment.java | 18 ++++++++++
 .../group/model/BusinessGroupReference.java   |  4 +++
 10 files changed, 100 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/olat/course/PersistingCourseImpl.java b/src/main/java/org/olat/course/PersistingCourseImpl.java
index 11fc170adc8..1e3c09bd902 100644
--- a/src/main/java/org/olat/course/PersistingCourseImpl.java
+++ b/src/main/java/org/olat/course/PersistingCourseImpl.java
@@ -295,13 +295,23 @@ public class PersistingCourseImpl implements ICourse, OLATResourceable, Serializ
 		long s = System.currentTimeMillis();
 		log.info("exportToFilesystem: exporting course "+this+" to "+exportDirectory+"...");
 		File fCourseBase = getCourseBaseContainer().getBasefile();
+		//make the folder structure
+		File fExportedDataDir = new File(exportDirectory, EXPORTED_DATA_FOLDERNAME);
+		fExportedDataDir.mkdirs();
 
+		//export course config
 		FileUtils.copyFileToDir(new File(fCourseBase, CourseConfigManager.COURSECONFIG_XML), exportDirectory, "course export courseconfig");
 		
+		//export business groups
+		CourseEnvironmentMapper envMapper = PersistingCourseGroupManager.getInstance(this).getBusinessGroupEnvironment();
+		if(backwardsCompatible) {
+			//prevents duplicate names
+			envMapper.avoidDuplicateNames();
+		}
+		PersistingCourseGroupManager.getInstance(this).exportCourseBusinessGroups(fExportedDataDir, envMapper, backwardsCompatible);
 		if(backwardsCompatible) {
 			XStream xstream = CourseXStreamAliases.getReadCourseXStream();
-			CourseEnvironmentMapper envMapper = PersistingCourseGroupManager.getInstance(this).getBusinessGroupEnvironment();
-			
+
 			Structure exportedStructure = (Structure)XStreamHelper.readObject(xstream, new File(fCourseBase, RUNSTRUCTURE_XML));
 			visit(new NodePostExportVisitor(envMapper, backwardsCompatible), exportedStructure.getRootNode());
 			XStreamHelper.writeObject(xstream, new File(exportDirectory, RUNSTRUCTURE_XML), exportedStructure);
@@ -321,8 +331,6 @@ public class PersistingCourseImpl implements ICourse, OLATResourceable, Serializ
 		// export course folder
 		FileUtils.copyDirToDir(getIsolatedCourseFolder().getBasefile(), exportDirectory, "course export folder");
 		// export any node data
-		File fExportedDataDir = new File(exportDirectory, EXPORTED_DATA_FOLDERNAME);
-		fExportedDataDir.mkdirs();
 		log.info("exportToFilesystem: exporting course "+this+": exporting all nodes...");
 		Visitor visitor = new NodeExportVisitor(fExportedDataDir, this);
 		TreeVisitor tv = new TreeVisitor(visitor, getEditorTreeModel().getRootNode(), true);
@@ -380,11 +388,7 @@ public class PersistingCourseImpl implements ICourse, OLATResourceable, Serializ
 		log.info("exportToFilesystem: exporting course "+this+": configuration and repo data...");
 		// export configuration file
 		FileUtils.copyFileToDir(new File(fCourseBase, CourseConfigManager.COURSECONFIG_XML), exportDirectory, "course export configuration and repo info");
-		// export learning groups
 		
-		PersistingCourseGroupManager.getInstance(this).exportCourseBusinessGroups(fExportedDataDir, backwardsCompatible);
-		// export right groups
-		//PersistingCourseGroupManager.getInstance(this).exportCourseRightGroups(fExportedDataDir);
 		// export repo metadata
 		RepositoryManager rm = RepositoryManager.getInstance();
 		RepositoryEntry myRE = rm.lookupRepositoryEntry(this, true);
diff --git a/src/main/java/org/olat/course/export/CourseEnvironmentMapper.java b/src/main/java/org/olat/course/export/CourseEnvironmentMapper.java
index a5edd8e576a..e43cdbfadef 100644
--- a/src/main/java/org/olat/course/export/CourseEnvironmentMapper.java
+++ b/src/main/java/org/olat/course/export/CourseEnvironmentMapper.java
@@ -20,7 +20,11 @@
 package org.olat.course.export;
 
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
+import java.util.UUID;
 
 import org.olat.core.util.StringHelper;
 import org.olat.group.model.BGAreaReference;
@@ -49,6 +53,38 @@ public class CourseEnvironmentMapper {
 		groups.addAll(env.getGroups());
 	}
 	
+	public void avoidDuplicateNames() {
+		Set<String> names = new HashSet<String>();
+		for(BusinessGroupReference ref:getGroups()) {
+			if(names.contains(ref.getName())) {
+				String newName = generateName(ref.getName(), names);
+				names.add(newName);
+				ref.setName(newName);
+			} else {
+				names.add(ref.getName());
+			}
+		}
+		names.clear();
+		for(BGAreaReference ref:getAreas()) {
+			if(names.contains(ref.getName())) {
+				String newName = generateName(ref.getName(), names);
+				names.add(newName);
+				ref.setName(newName);
+			} else {
+				names.add(ref.getName());
+			}
+		}
+	}
+	
+	private String generateName(String name, Collection<String> names) {
+		for(int i=1; i<100; i++) {
+			String newName = name + "_" + i;
+			if(!names.contains(newName)) {
+				return newName;
+			}
+		}
+		return name + "_" + UUID.randomUUID().toString();
+	}
 	
 	public List<Long> toGroupKeyFromOriginalNames(String groupNames) {
 		if(!StringHelper.containsNonWhitespace(groupNames)) return null;
diff --git a/src/main/java/org/olat/course/groupsandrights/CourseGroupManager.java b/src/main/java/org/olat/course/groupsandrights/CourseGroupManager.java
index 420ea2317bc..3251aaf5979 100644
--- a/src/main/java/org/olat/course/groupsandrights/CourseGroupManager.java
+++ b/src/main/java/org/olat/course/groupsandrights/CourseGroupManager.java
@@ -32,7 +32,6 @@ import org.olat.core.id.Identity;
 import org.olat.course.export.CourseEnvironmentMapper;
 import org.olat.group.BusinessGroup;
 import org.olat.group.area.BGArea;
-import org.olat.group.model.BusinessGroupEnvironment;
 import org.olat.resource.OLATResource;
 
 /**
@@ -184,7 +183,7 @@ public interface CourseGroupManager {
 	 * 
 	 * @param fExportDirectory
 	 */
-	public void exportCourseBusinessGroups(File fExportDirectory, boolean backwardsCompatible);
+	public void exportCourseBusinessGroups(File fExportDirectory, CourseEnvironmentMapper env, boolean backwardsCompatible);
 
 	/**
 	 * Import course internal groups fa previous export.
diff --git a/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java b/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java
index 04ca05dbb83..b1d67183e9f 100644
--- a/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java
+++ b/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java
@@ -309,11 +309,16 @@ public class PersistingCourseGroupManager extends BasicManager implements Course
 	/**
 	 * @see org.olat.course.groupsandrights.CourseGroupManager#exportCourseBusinessGroups(java.io.File)
 	 */
-	public void exportCourseBusinessGroups(File fExportDirectory, boolean backwardsCompatible) {
+	@Override
+	public void exportCourseBusinessGroups(File fExportDirectory, CourseEnvironmentMapper courseEnv, boolean backwardsCompatible) {
 		File fExportFile = new File(fExportDirectory, LEARNINGGROUPEXPORT_XML);
 		List<BGArea> areas = getAllAreas();
 		List<BusinessGroup> groups = getAllBusinessGroups();
-		businessGroupService.exportGroups(groups, areas, fExportFile, backwardsCompatible);
+
+		BusinessGroupEnvironment bgEnv = new BusinessGroupEnvironment();
+		bgEnv.getGroups().addAll(courseEnv.getGroups());
+		bgEnv.getAreas().addAll(courseEnv.getAreas());
+		businessGroupService.exportGroups(groups, areas, fExportFile, bgEnv, backwardsCompatible);
 	}
 	
 	/**
diff --git a/src/main/java/org/olat/course/run/preview/PreviewCourseGroupManager.java b/src/main/java/org/olat/course/run/preview/PreviewCourseGroupManager.java
index aa5bb3f3168..8e653e6b08f 100644
--- a/src/main/java/org/olat/course/run/preview/PreviewCourseGroupManager.java
+++ b/src/main/java/org/olat/course/run/preview/PreviewCourseGroupManager.java
@@ -267,7 +267,7 @@ final class PreviewCourseGroupManager extends BasicManager implements CourseGrou
 	}
 
 	@Override
-	public void exportCourseBusinessGroups(File fExportDirectory, boolean backwardsCompatible) {
+	public void exportCourseBusinessGroups(File fExportDirectory, CourseEnvironmentMapper env, boolean backwardsCompatible) {
 		throw new AssertException("unsupported");
 	}
 
diff --git a/src/main/java/org/olat/group/BusinessGroupService.java b/src/main/java/org/olat/group/BusinessGroupService.java
index 77b95d5096e..39e117ed149 100644
--- a/src/main/java/org/olat/group/BusinessGroupService.java
+++ b/src/main/java/org/olat/group/BusinessGroupService.java
@@ -464,7 +464,7 @@ public interface BusinessGroupService {
 	 * @param groups
 	 * @param fExportFile
 	 */
-	public void exportGroups(List<BusinessGroup> groups, List<BGArea> areas, File fExportFile, boolean backwardsCompatible);
+	public void exportGroups(List<BusinessGroup> groups, List<BGArea> areas, File fExportFile, BusinessGroupEnvironment env, boolean backwardsCompatible);
 
 	/**
 	 * Import previously exported group definitions.
diff --git a/src/main/java/org/olat/group/manager/BusinessGroupImportExport.java b/src/main/java/org/olat/group/manager/BusinessGroupImportExport.java
index 092ffe8d01c..887f4bf42e1 100644
--- a/src/main/java/org/olat/group/manager/BusinessGroupImportExport.java
+++ b/src/main/java/org/olat/group/manager/BusinessGroupImportExport.java
@@ -69,9 +69,10 @@ public class BusinessGroupImportExport {
 	private BusinessGroupPropertyDAO businessGroupPropertyManager;
 	
 	
-	public void exportGroups(List<BusinessGroup> groups, List<BGArea> areas, File fExportFile, boolean backwardsCompatible) {
-		if (groups == null || groups.isEmpty())
+	public void exportGroups(List<BusinessGroup> groups, List<BGArea> areas, File fExportFile, BusinessGroupEnvironment env, boolean backwardsCompatible) {
+		if (groups == null || groups.isEmpty()) {
 			return; // nothing to do... says Florian.
+		}
 
 		OLATGroupExport root = new OLATGroupExport();
 		// export areas
@@ -81,6 +82,12 @@ public class BusinessGroupImportExport {
 			Area newArea = new Area();
 			newArea.key = backwardsCompatible ? null : area.getKey();
 			newArea.name = area.getName();
+			if(backwardsCompatible && env != null) {
+				String newName = env.getAreaName(area.getKey());
+				if(StringHelper.containsNonWhitespace(newName)) {
+					newArea.name = newName;
+				}
+			}
 			newArea.description = Collections.singletonList(area.getDescription());
 			root.getAreas().getGroups().add(newArea);
 		}
@@ -89,7 +96,11 @@ public class BusinessGroupImportExport {
 		root.setGroups(new GroupCollection());
 		root.getGroups().setGroups(new ArrayList<Group>());
 		for (BusinessGroup group : groups) {
-			Group newGroup = exportGroup(fExportFile, group, backwardsCompatible);
+			String groupName = null;
+			if(backwardsCompatible && env != null) {
+				groupName = env.getGroupName(group.getKey());
+			}
+			Group newGroup = exportGroup(fExportFile, group, groupName, backwardsCompatible);
 			root.getGroups().getGroups().add(newGroup);
 		}
 		saveGroupConfiguration(fExportFile, root);
@@ -97,17 +108,17 @@ public class BusinessGroupImportExport {
 	
 	public void exportGroup(BusinessGroup group, File fExportFile, boolean backwardsCompatible) {
 		OLATGroupExport root = new OLATGroupExport();
-		Group newGroup = exportGroup(fExportFile, group, backwardsCompatible);
+		Group newGroup = exportGroup(fExportFile, group, null, backwardsCompatible);
 		root.setGroups(new GroupCollection());
 		root.getGroups().setGroups(new ArrayList<Group>());
 		root.getGroups().getGroups().add(newGroup);
 		saveGroupConfiguration(fExportFile, root);
 	}
 	
-	private Group exportGroup(File fExportFile, BusinessGroup group, boolean backwardsCompatible) {
+	private Group exportGroup(File fExportFile, BusinessGroup group, String groupName, boolean backwardsCompatible) {
 		Group newGroup = new Group();
 		newGroup.key = backwardsCompatible ? null : group.getKey();
-		newGroup.name = group.getName();
+		newGroup.name = StringHelper.containsNonWhitespace(groupName) ? groupName : group.getName();
 		if (group.getMinParticipants() != null) {
 			newGroup.minParticipants = group.getMinParticipants();
 		}
diff --git a/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java b/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java
index 66da4ca56c1..4ddb31ca3df 100644
--- a/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java
+++ b/src/main/java/org/olat/group/manager/BusinessGroupServiceImpl.java
@@ -1269,8 +1269,9 @@ public class BusinessGroupServiceImpl implements BusinessGroupService, UserDataD
 	}
 	
 	@Override
-	public void exportGroups(List<BusinessGroup> groups, List<BGArea> areas, File fExportFile, boolean backwardsCompatible) {
-		businessGroupImportExport.exportGroups(groups, areas, fExportFile, backwardsCompatible);
+	public void exportGroups(List<BusinessGroup> groups, List<BGArea> areas, File fExportFile,
+			BusinessGroupEnvironment env, boolean backwardsCompatible) {
+		businessGroupImportExport.exportGroups(groups, areas, fExportFile, env, backwardsCompatible);
 	}
 
 	@Override
diff --git a/src/main/java/org/olat/group/model/BusinessGroupEnvironment.java b/src/main/java/org/olat/group/model/BusinessGroupEnvironment.java
index cdd75c6a43c..1e394122393 100644
--- a/src/main/java/org/olat/group/model/BusinessGroupEnvironment.java
+++ b/src/main/java/org/olat/group/model/BusinessGroupEnvironment.java
@@ -38,4 +38,22 @@ public class BusinessGroupEnvironment {
 	public List<BusinessGroupReference> getGroups() {
 		return groups;
 	}
+	
+	public String getGroupName(Long groupKey) {
+		for(BusinessGroupReference ref:getGroups()) {
+			if(ref.getKey().equals(groupKey)) {
+				return ref.getName();
+			}
+		}
+		return null;
+	}
+	
+	public String getAreaName(Long areaKey) {
+		for(BGAreaReference ref:getAreas()) {
+			if(ref.getKey().equals(areaKey)) {
+				return ref.getName();
+			}
+		}
+		return null;
+	}
 }
diff --git a/src/main/java/org/olat/group/model/BusinessGroupReference.java b/src/main/java/org/olat/group/model/BusinessGroupReference.java
index 767c24691a3..aac83b2f0f0 100644
--- a/src/main/java/org/olat/group/model/BusinessGroupReference.java
+++ b/src/main/java/org/olat/group/model/BusinessGroupReference.java
@@ -58,6 +58,10 @@ public class BusinessGroupReference {
 	public String getName() {
 		return name;
 	}
+	
+	public void setName(String name) {
+		this.name = name;
+	}
 
 	public Long getOriginalKey() {
 		return originalKey;
-- 
GitLab