From b4f637f547076b22f633d4d0f67723d071e4acfa Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Wed, 26 Apr 2017 12:10:29 +0200
Subject: [PATCH] OO-2720: add the link to group name and a description if the
 list of courses is empty

---
 .../group/manager/BusinessGroupMailing.java   |  55 ++------
 .../java/org/olat/group/ui/BGMailHelper.java  | 127 +++++++++++++-----
 .../group/ui/_i18n/LocalStrings_de.properties |   1 +
 .../group/ui/_i18n/LocalStrings_en.properties |   1 +
 4 files changed, 109 insertions(+), 75 deletions(-)

diff --git a/src/main/java/org/olat/group/manager/BusinessGroupMailing.java b/src/main/java/org/olat/group/manager/BusinessGroupMailing.java
index 4d558a10663..36d9c811f9f 100644
--- a/src/main/java/org/olat/group/manager/BusinessGroupMailing.java
+++ b/src/main/java/org/olat/group/manager/BusinessGroupMailing.java
@@ -28,9 +28,7 @@ import org.olat.basesecurity.BaseSecurity;
 import org.olat.core.CoreSpringFactory;
 import org.olat.core.id.Identity;
 import org.olat.core.id.Roles;
-import org.olat.core.id.context.BusinessControlFactory;
 import org.olat.core.util.StringHelper;
-import org.olat.core.util.filter.FilterFactory;
 import org.olat.core.util.mail.MailBundle;
 import org.olat.core.util.mail.MailContext;
 import org.olat.core.util.mail.MailContextImpl;
@@ -38,13 +36,13 @@ import org.olat.core.util.mail.MailManager;
 import org.olat.core.util.mail.MailPackage;
 import org.olat.core.util.mail.MailTemplate;
 import org.olat.core.util.mail.MailerResult;
-import org.olat.group.BusinessGroup;
 import org.olat.group.BusinessGroupModule;
 import org.olat.group.BusinessGroupService;
 import org.olat.group.BusinessGroupShort;
 import org.olat.group.model.BusinessGroupMembershipChange;
 import org.olat.group.model.MembershipModification;
 import org.olat.group.ui.BGMailHelper;
+import org.olat.group.ui.BGMailHelper.BGMailTemplateInfos;
 import org.olat.group.ui.main.MemberPermissionChangeEvent;
 import org.olat.repository.RepositoryEntryShort;
 
@@ -175,45 +173,30 @@ public class BusinessGroupMailing {
 	public static class MailTemplateDelegate extends MailTemplate {
 		
 		private final MailTemplate delegate;
-		private final BusinessGroupShort group;
-		private final List<RepositoryEntryShort> entries;
+		private final BGMailTemplateInfos infos;
 		
 		public MailTemplateDelegate(MailTemplate delegate, BusinessGroupShort group, List<RepositoryEntryShort> entries) {
 			super(null, null, null);
 			this.delegate = delegate;
-			this.group = group;
-			this.entries = entries;
+			infos = BGMailHelper.getTemplateInfos(group, entries);
+			String subject = delegate.getSubjectTemplate();
+			if(subject != null) {
+				subject = subject.replace("$groupname", infos.getGroupName());
+			}
+			setSubjectTemplate(subject);
 		}
 
 		@Override
 		public void putVariablesInMailContext(VelocityContext vContext, Identity recipient) {
 			delegate.putVariablesInMailContext(vContext, recipient);
 			
-			StringBuilder learningResources = new StringBuilder();
-			if(entries != null && entries.size() > 0) {
-				for (RepositoryEntryShort entry: entries) {
-					String title = entry.getDisplayname();
-					String url = BusinessControlFactory.getInstance().getURLFromBusinessPathString("[RepositoryEntry:" + entry.getKey() + "]");
-					learningResources.append(title);
-					learningResources.append(" (");
-					learningResources.append(url);
-					learningResources.append(")\n");
-				}
-			}
-			vContext.put("courselist", learningResources.toString());
-			
-			if(group != null) {
-				vContext.put("groupname", group.getName());
-				
-				String description;
-				if(group instanceof BusinessGroup) {
-					description = ((BusinessGroup)group).getDescription(); 
-				} else {
-					description = CoreSpringFactory.getImpl(BusinessGroupDAO.class).loadDescription(group.getKey());
-				}
-				description = FilterFactory.getHtmlTagAndDescapingFilter().filter(description); 
-				vContext.put("groupdescription", description);
+			if(StringHelper.containsNonWhitespace(infos.getCourseList())) {
+				vContext.put("courselist", infos.getCourseList());
+			} else if(vContext.get("courselistempty") != null) {
+				vContext.put("courselist", vContext.get("courselistempty"));
 			}
+			vContext.put("groupname", infos.getGroupNameWithUrl());
+			vContext.put("groupdescription", infos.getGroupDescription());
 		}
 
 		@Override
@@ -225,16 +208,6 @@ public class BusinessGroupMailing {
 		public void setCpfrom(Boolean cpfrom) {
 			delegate.setCpfrom(cpfrom);
 		}
-
-		@Override
-		public String getSubjectTemplate() {
-			return delegate.getSubjectTemplate();
-		}
-
-		@Override
-		public void setSubjectTemplate(String subjectTemplate) {
-			delegate.setSubjectTemplate(subjectTemplate);
-		}
 		
 		@Override
 		public String getBodyTemplate() {
diff --git a/src/main/java/org/olat/group/ui/BGMailHelper.java b/src/main/java/org/olat/group/ui/BGMailHelper.java
index bb389b9e94e..e29acffcc06 100644
--- a/src/main/java/org/olat/group/ui/BGMailHelper.java
+++ b/src/main/java/org/olat/group/ui/BGMailHelper.java
@@ -45,6 +45,7 @@ import org.olat.core.id.Identity;
 import org.olat.core.id.User;
 import org.olat.core.id.UserConstants;
 import org.olat.core.id.context.BusinessControlFactory;
+import org.olat.core.util.StringHelper;
 import org.olat.core.util.Util;
 import org.olat.core.util.filter.FilterFactory;
 import org.olat.core.util.i18n.I18nManager;
@@ -204,43 +205,21 @@ public class BGMailHelper {
 		String body = trans.translate(bodyKey, bodyArgs);
 		
 		// build learning resources as list of url as string
-		
-		final String courselist;
-		final String groupname;
-		final String groupdescription; 
+		final BGMailTemplateInfos infos; 
 		if(group != null) {
-			StringBuilder learningResources = new StringBuilder();
 			BusinessGroupService businessGroupService = CoreSpringFactory.getImpl(BusinessGroupService.class);
 			List<RepositoryEntryShort> repoEntries = businessGroupService.findShortRepositoryEntries(Collections.singletonList(group), 0, -1);
-			for (RepositoryEntryShort entry: repoEntries) {
-				String title = entry.getDisplayname();
-				String url = BusinessControlFactory.getInstance().getURLFromBusinessPathString("[RepositoryEntry:" + entry.getKey() + "]");
-				learningResources.append(title);
-				learningResources.append(" (");
-				learningResources.append(url);
-				learningResources.append(")\n");
-			}
-	
-			courselist = learningResources.toString();
-			// get group name and description
-			groupname = group.getName();
-			
-			String description;
-			if(group instanceof BusinessGroup) {
-				description = ((BusinessGroup)group).getDescription(); 
+			infos = getTemplateInfos(group, repoEntries);
+			subject = subject.replace("$groupname", infos.getGroupName());
+			body = body.replace("$groupname", infos.getGroupNameWithUrl());
+			body = body.replace("$groupdescription", infos.getGroupDescription());
+			if(StringHelper.containsNonWhitespace(infos.getCourseList())) {
+				body = body.replace("$courselist", infos.getCourseList());
 			} else {
-				description = CoreSpringFactory.getImpl(BusinessGroupDAO.class).loadDescription(group.getKey());
+				body = body.replace("$courselist", trans.translate("notification.mail.no.ressource", null));
 			}
-			groupdescription = FilterFactory.getHtmlTagAndDescapingFilter().filter(description);
-
-			subject = subject.replace("$groupname", groupname == null ? "" : groupname);
-			body = body.replace("$groupname", groupname == null ? "" : groupname);
-			body = body.replace("$groupdescription", groupdescription == null ? "" : groupdescription);
-			body = body.replace("$courselist", courselist == null ? "" : courselist);
 		} else {
-			courselist = "";
-			groupname = "";
-			groupdescription = "";
+			infos = new BGMailTemplateInfos("", "", "", "");
 		}
 		
 		// create a mail template which all these data
@@ -254,11 +233,91 @@ public class BGMailHelper {
 				//the email of the user, needs to stay named 'login'
 				context.put("login", user.getProperty(UserConstants.EMAIL, null));
 				// Put variables from greater context
-				context.put("groupname", groupname);
-				context.put("groupdescription", groupdescription);
-				context.put("courselist", courselist);
+				context.put("groupname", infos.getGroupNameWithUrl());
+				context.put("groupdescription", infos.getGroupDescription());
+				if(StringHelper.containsNonWhitespace(infos.getCourseList())) {
+					context.put("courselist", infos.getCourseList());
+				} else {
+					context.put("courselist", trans.translate("notification.mail.no.ressource", null));
+				}
+				context.put("courselistempty", trans.translate("notification.mail.no.ressource", null));
 			}
 		};
 		return mailTempl;
 	}
+	
+	public static BGMailTemplateInfos getTemplateInfos(BusinessGroupShort group, List<RepositoryEntryShort> repoEntries) {
+		StringBuilder learningResources = new StringBuilder();
+		if(repoEntries != null && repoEntries.size() > 0) {
+			for (RepositoryEntryShort entry: repoEntries) {
+				String title = entry.getDisplayname();
+				String url = BusinessControlFactory.getInstance().getURLFromBusinessPathString("[RepositoryEntry:" + entry.getKey() + "]");
+				learningResources.append(title);
+				learningResources.append(" (");
+				learningResources.append(url);
+				learningResources.append(")\n");
+			}
+		}
+		
+		String courseList = null;
+		if(learningResources.length() > 0) {
+			courseList = learningResources.toString();
+		}
+
+		String groupNameWithUrl = null;
+		String groupDescription = null;
+		if(group != null) {
+			// get group name and description
+			StringBuilder sb = new StringBuilder();
+			sb.append(group.getName() == null ? "" : group.getName())
+			         .append(" (")
+			         .append(BusinessControlFactory.getInstance().getURLFromBusinessPathString("[BusinessGroup:" + group.getKey() + "]"))
+			         .append(")\n");
+			groupNameWithUrl = sb.toString();
+	
+			String description;
+			if(group instanceof BusinessGroup) {
+				description = ((BusinessGroup)group).getDescription(); 
+			} else {
+				description = CoreSpringFactory.getImpl(BusinessGroupDAO.class).loadDescription(group.getKey());
+			}
+			groupDescription = FilterFactory.getHtmlTagAndDescapingFilter().filter(description);
+		}
+		return new BGMailTemplateInfos(group.getName(), groupNameWithUrl, groupDescription, courseList);
+		
+	}
+	
+	public static final class BGMailTemplateInfos {
+		private final String groupName;
+		private final String groupNameWithUrl;
+		private final String groupDescription;
+		private final String courseList;
+		
+		public BGMailTemplateInfos(String groupName, String groupNameWithUrl, String groupDescription, String courseList) {
+			this.groupName = groupName;
+			this.groupNameWithUrl = groupNameWithUrl;
+			this.groupDescription = groupDescription;
+			this.courseList = courseList;
+		}
+		
+		public String getGroupName() {
+			if(groupName == null) return "";
+			return groupName;
+		}
+		
+		public String getGroupNameWithUrl() {
+			if(groupNameWithUrl == null) return "";
+			return groupNameWithUrl;
+		}
+		
+		public String getGroupDescription() {
+			if(groupDescription == null) return "";
+			return groupDescription;
+		}
+		
+		public String getCourseList() {
+			if(courseList == null) return "";
+			return courseList;
+		}
+	}
 }
diff --git a/src/main/java/org/olat/group/ui/_i18n/LocalStrings_de.properties b/src/main/java/org/olat/group/ui/_i18n/LocalStrings_de.properties
index 03b0809af6b..b01ee8cf883 100644
--- a/src/main/java/org/olat/group/ui/_i18n/LocalStrings_de.properties
+++ b/src/main/java/org/olat/group/ui/_i18n/LocalStrings_de.properties
@@ -102,6 +102,7 @@ notification.mail.added.self.body=*** Das ist eine automatisch generierte Nachri
 notification.mail.added.self.subject=Gruppe $groupname 
 notification.mail.added.subject=Gruppe $groupname 
 notification.mail.error=Die E-Mail konnte nicht verschickt werden. Bitte informieren Sie den Benutzer pers\u00F6nlich.
+notification.mail.no.ressource=Diese Gruppe wird in keiner Lernressource verwendet.
 notification.mail.removed.body=*** Das ist eine automatisch generierte Nachricht. Bitte antworten Sie nicht auf diese Nachricht *** \n\nSie wurden von {0} {1} ({2}) aus der Gruppe ausgetragen\: \n\nGruppenname\: $groupname\nBeschreibung\: $groupdescription\n\nBei Fragen kontaktieren Sie bitte {0} {1} ({2}). \n\nDiese Gruppe wurde in folgenden Lernressourcen verwendet\:\n\n$courselist
 notification.mail.removed.self.body=*** Das ist eine automatisch generierte Nachricht. Bitte antworten Sie nicht auf diese Nachricht *** \n\n Sie haben sich soeben aus der Gruppe ausgetragen\: \n\nGruppenname\: $groupname\nBeschreibung\: $groupdescription\n\nDiese Gruppe wurde in folgenden Lernressourcen verwendet\:\n\n$courselist
 notification.mail.removed.self.subject=Gruppe $groupname\: Sie wurden ausgetragen.
diff --git a/src/main/java/org/olat/group/ui/_i18n/LocalStrings_en.properties b/src/main/java/org/olat/group/ui/_i18n/LocalStrings_en.properties
index d9f0cfc90ce..7d73544f931 100644
--- a/src/main/java/org/olat/group/ui/_i18n/LocalStrings_en.properties
+++ b/src/main/java/org/olat/group/ui/_i18n/LocalStrings_en.properties
@@ -103,6 +103,7 @@ notification.mail.added.self.body=*** This is an automated message. Please do no
 notification.mail.added.self.subject=Group $groupname 
 notification.mail.added.subject=Group $groupname 
 notification.mail.error=E-Mail could not be sent. Please notify this user personally.
+notification.mail.no.ressource=This group is not used in any learning resource.
 notification.mail.removed.body=*** This is an automated message. Please do not reply *** \r\n\r\nYou were signed out of a group by {0} {1} ({2})\:  \r\n\r\nGroup name\: $groupname\r\nDescription\: $groupdescription\r\n\r\nFor questions, please contact  {0} {1} ({2}).\r\n\r\nThis group was used in the following learning resources\:\r\n\r\n$courselist
 notification.mail.removed.self.body=*** This is an automated message. Please do not reply *** \r\n\r\nYou have just signed out from the group\: \r\n\r\nGroup name\: $groupname\r\nDescription\: $groupdescription\r\n\r\nThis group was used in the following learning resources\:\r\n\r\n$courselist
 notification.mail.removed.self.subject=Group $groupname\: You have just been signed out.
-- 
GitLab