From c5a80d8f263672a181ba659c38e3a51067b5c708 Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Mon, 11 Feb 2019 11:58:13 +0100
Subject: [PATCH] OO-3873: use status of course to blocked resources
 notifications

Use the status of the course where the resources is linked to block the
notifications. Reverse the check if the notifications are allowed
between preview and life course environment
---
 .../CalendarNotificationHandler.java          |  8 ++--
 .../InfoMessageNotificationHandler.java       | 25 +++++------
 .../PersistingCourseGroupManager.java         |  6 ++-
 .../GTAMarkedNotificationsHandler.java        |  1 -
 .../nodes/gta/manager/GTANotifications.java   |  9 ++--
 .../gta/manager/GTANotificationsHandler.java  |  1 -
 .../nodes/pf/manager/PFNotifications.java     | 13 ++++--
 .../modules/fo/ForumNotificationsHandler.java | 16 +++----
 .../manager/FeedNotificationsHandler.java     | 42 ++++++++++---------
 9 files changed, 61 insertions(+), 60 deletions(-)

diff --git a/src/main/java/org/olat/commons/calendar/notification/CalendarNotificationHandler.java b/src/main/java/org/olat/commons/calendar/notification/CalendarNotificationHandler.java
index 4decf30ae34..c8ba7e8ec48 100644
--- a/src/main/java/org/olat/commons/calendar/notification/CalendarNotificationHandler.java
+++ b/src/main/java/org/olat/commons/calendar/notification/CalendarNotificationHandler.java
@@ -49,7 +49,6 @@ import org.olat.course.CourseModule;
 import org.olat.group.BusinessGroup;
 import org.olat.group.manager.BusinessGroupDAO;
 import org.olat.repository.RepositoryEntry;
-import org.olat.repository.RepositoryEntryStatusEnum;
 import org.olat.repository.RepositoryManager;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -100,10 +99,9 @@ public class CalendarNotificationHandler implements NotificationsHandler {
 				String calType = null;
 				String title = null;
 				if (type.equals(CalendarController.ACTION_CALENDAR_COURSE)) {
-					RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(OresHelper.createOLATResourceableInstance("CourseModule", id), false);
-					RepositoryEntryStatusEnum status = re.getEntryStatus();
-					if(status.decommissioned()) {
-						return NotificationsManager.getInstance().getNoSubscriptionInfo();
+					RepositoryEntry re = repositoryManager.lookupRepositoryEntry(OresHelper.createOLATResourceableInstance("CourseModule", id), false);
+					if(re == null || re.getEntryStatus().decommissioned()) {
+						return notificationsManager.getNoSubscriptionInfo();
 					}
 					String displayName = re.getDisplayname();
 					calType = CalendarManager.TYPE_COURSE;
diff --git a/src/main/java/org/olat/commons/info/notification/InfoMessageNotificationHandler.java b/src/main/java/org/olat/commons/info/notification/InfoMessageNotificationHandler.java
index f26d0797be8..91fb67d53bb 100644
--- a/src/main/java/org/olat/commons/info/notification/InfoMessageNotificationHandler.java
+++ b/src/main/java/org/olat/commons/info/notification/InfoMessageNotificationHandler.java
@@ -26,7 +26,6 @@ import java.util.Locale;
 
 import org.olat.commons.info.InfoMessage;
 import org.olat.commons.info.InfoMessageManager;
-import org.olat.core.CoreSpringFactory;
 import org.olat.core.commons.services.notifications.NotificationHelper;
 import org.olat.core.commons.services.notifications.NotificationsHandler;
 import org.olat.core.commons.services.notifications.NotificationsManager;
@@ -46,7 +45,6 @@ import org.olat.core.util.resource.OresHelper;
 import org.olat.group.BusinessGroup;
 import org.olat.group.BusinessGroupService;
 import org.olat.repository.RepositoryEntry;
-import org.olat.repository.RepositoryEntryStatusEnum;
 import org.olat.repository.RepositoryManager;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -67,8 +65,14 @@ public class InfoMessageNotificationHandler implements NotificationsHandler {
 
 	private static final String CSS_CLASS_ICON = "o_infomsg_icon";
 	
+	@Autowired
+	private RepositoryManager repositoryManager;
 	@Autowired
 	private InfoMessageManager infoMessageManager;
+	@Autowired
+	private NotificationsManager notificationsManager;
+	@Autowired
+	private BusinessGroupService businessGroupService;
 	
 	@Override
 	public SubscriptionInfo createSubscriptionInfo(Subscriber subscriber, Locale locale, Date compareDate) {
@@ -89,16 +93,13 @@ public class InfoMessageNotificationHandler implements NotificationsHandler {
 				String displayName;
 				String notificationtitle;
 				if ("BusinessGroup".equals(resName)) {
-					BusinessGroupService groupService = CoreSpringFactory.getImpl(BusinessGroupService.class);
-					BusinessGroup group = groupService.loadBusinessGroup(resId);
+					BusinessGroup group = businessGroupService.loadBusinessGroup(resId);
 					displayName = group.getName();
 					notificationtitle = "notification.title.group";
 				} else {
-					RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(OresHelper.createOLATResourceableInstance(resName, resId), false);
-					if(re== null || re.getEntryStatus() == RepositoryEntryStatusEnum.closed
-							|| re.getEntryStatus() == RepositoryEntryStatusEnum.trash
-							|| re.getEntryStatus() == RepositoryEntryStatusEnum.deleted) {
-						return NotificationsManager.getInstance().getNoSubscriptionInfo();
+					RepositoryEntry re = repositoryManager.lookupRepositoryEntry(OresHelper.createOLATResourceableInstance(resName, resId), false);
+					if(re== null || re.getEntryStatus().decommissioned()) {
+						return notificationsManager.getNoSubscriptionInfo();
 					}					
 					displayName = re.getDisplayname();	
 					notificationtitle = "notification.title";
@@ -122,10 +123,10 @@ public class InfoMessageNotificationHandler implements NotificationsHandler {
 				}
 			} catch (Exception e) {
 				log.error("Unexpected exception", e);
-				si = NotificationsManager.getInstance().getNoSubscriptionInfo();
+				si = notificationsManager.getNoSubscriptionInfo();
 			}
 		} else {
-			si = NotificationsManager.getInstance().getNoSubscriptionInfo();
+			si = notificationsManager.getNoSubscriptionInfo();
 		}
 		return si;
 	}
@@ -133,7 +134,7 @@ public class InfoMessageNotificationHandler implements NotificationsHandler {
 	@Override
 	public String createTitleInfo(Subscriber subscriber, Locale locale) {
 		Translator translator = Util.createPackageTranslator(this.getClass(), locale);
-		String displayName = RepositoryManager.getInstance().lookupDisplayNameByOLATResourceableId(subscriber.getPublisher().getResId());
+		String displayName = repositoryManager.lookupDisplayNameByOLATResourceableId(subscriber.getPublisher().getResId());
 		return translator.translate("notification.title", new String[]{displayName});
 	}
 	
diff --git a/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java b/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java
index 139a3ccb478..9c94f7b536f 100644
--- a/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java
+++ b/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java
@@ -128,7 +128,11 @@ public class PersistingCourseGroupManager implements CourseGroupManager {
 	
 	@Override
 	public boolean isNotificationsAllowed() {
-		return false;
+		RepositoryEntry re = getCourseEntry();
+		if(re == null || re.getEntryStatus() == null) {
+			return false;
+		}
+		return !re.getEntryStatus().decommissioned();
 	}
 
 	public void updateRepositoryEntry(RepositoryEntry entry) {
diff --git a/src/main/java/org/olat/course/nodes/gta/manager/GTAMarkedNotificationsHandler.java b/src/main/java/org/olat/course/nodes/gta/manager/GTAMarkedNotificationsHandler.java
index e2498f23b80..f597a2f966a 100644
--- a/src/main/java/org/olat/course/nodes/gta/manager/GTAMarkedNotificationsHandler.java
+++ b/src/main/java/org/olat/course/nodes/gta/manager/GTAMarkedNotificationsHandler.java
@@ -95,7 +95,6 @@ public class GTAMarkedNotificationsHandler implements NotificationsHandler  {
 			}
 		} catch (Exception e) {
 			log.error("Cannot create gtask notifications for subscriber: " + subscriber.getKey(), e);
-			//checkPublisher(p);
 			si = notificationsManager.getNoSubscriptionInfo();
 		}
 		return si;
diff --git a/src/main/java/org/olat/course/nodes/gta/manager/GTANotifications.java b/src/main/java/org/olat/course/nodes/gta/manager/GTANotifications.java
index 31aece7b311..7d28a559850 100644
--- a/src/main/java/org/olat/course/nodes/gta/manager/GTANotifications.java
+++ b/src/main/java/org/olat/course/nodes/gta/manager/GTANotifications.java
@@ -71,7 +71,6 @@ import org.olat.modules.assessment.Role;
 import org.olat.modules.assessment.manager.AssessmentEntryDAO;
 import org.olat.repository.RepositoryEntry;
 import org.olat.repository.RepositoryEntryRelationType;
-import org.olat.repository.RepositoryEntryStatusEnum;
 import org.olat.repository.RepositoryService;
 import org.olat.user.UserManager;
 
@@ -145,14 +144,12 @@ class GTANotifications {
 		if(subIdentifier.startsWith("Marked::")) {
 			subIdentifier = subIdentifier.substring("Marked::".length(), subIdentifier.length());
 		}
-		CourseNode node = course.getRunStructure().getNode(subIdentifier);
-		RepositoryEntry entry = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
-		if(entry == null || entry.getEntryStatus() == RepositoryEntryStatusEnum.closed
-				|| entry.getEntryStatus() == RepositoryEntryStatusEnum.trash
-				|| entry.getEntryStatus() == RepositoryEntryStatusEnum.deleted) {
+		if(!course.getCourseEnvironment().getCourseGroupManager().isNotificationsAllowed()) {
 			return Collections.emptyList();
 		}
 		
+		CourseNode node = course.getRunStructure().getNode(subIdentifier);
+		RepositoryEntry entry = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
 		if(node instanceof GTACourseNode) {
 			gtaNode = (GTACourseNode)node;
 			displayName = entry.getDisplayname();
diff --git a/src/main/java/org/olat/course/nodes/gta/manager/GTANotificationsHandler.java b/src/main/java/org/olat/course/nodes/gta/manager/GTANotificationsHandler.java
index e7a6a124833..6ecac9adfaa 100644
--- a/src/main/java/org/olat/course/nodes/gta/manager/GTANotificationsHandler.java
+++ b/src/main/java/org/olat/course/nodes/gta/manager/GTANotificationsHandler.java
@@ -95,7 +95,6 @@ public class GTANotificationsHandler implements NotificationsHandler  {
 			}
 		} catch (Exception e) {
 			log.error("Cannot create gtask notifications for subscriber: " + subscriber.getKey(), e);
-			//checkPublisher(p);
 			si = notificationsManager.getNoSubscriptionInfo();
 		}
 		return si;
diff --git a/src/main/java/org/olat/course/nodes/pf/manager/PFNotifications.java b/src/main/java/org/olat/course/nodes/pf/manager/PFNotifications.java
index 2e6d5f95102..52e8b1a6e1b 100644
--- a/src/main/java/org/olat/course/nodes/pf/manager/PFNotifications.java
+++ b/src/main/java/org/olat/course/nodes/pf/manager/PFNotifications.java
@@ -25,6 +25,7 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
@@ -83,19 +84,23 @@ public class PFNotifications {
 		translator = Util.createPackageTranslator(PFRunController.class, locale);
 	}
 	
-	public List<SubscriptionListItem> getItems() throws Exception {
+	public List<SubscriptionListItem> getItems() {
 		Publisher p = subscriber.getPublisher();
 		Identity identity = subscriber.getIdentity();
 		ICourse course = CourseFactory.loadCourse(p.getResId());
 		CourseEnvironment courseEnv = course.getCourseEnvironment();
-		CourseGroupManager groupManager = courseEnv.getCourseGroupManager();
+		if(!courseEnv.getCourseGroupManager().isNotificationsAllowed()) {
+			return Collections.emptyList();
+		}
+		
 		CourseNode node = course.getRunStructure().getNode(p.getSubidentifier());
-		RepositoryEntry entry = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
 		Date latestNews = p.getLatestNewsDate();
 
 		if (notificationsManager.isPublisherValid(p) && compareDate.before(latestNews)) {
+			RepositoryEntry entry = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
 			this.displayname = entry.getDisplayname();
-			
+
+			CourseGroupManager groupManager = courseEnv.getCourseGroupManager();
 			if (groupManager.isIdentityCourseCoach(identity) || groupManager.isIdentityCourseAdministrator(identity)) {
 				List<Identity> participants = pfManager.getParticipants(identity, courseEnv, groupManager.isIdentityCourseAdministrator(identity));
 
diff --git a/src/main/java/org/olat/modules/fo/ForumNotificationsHandler.java b/src/main/java/org/olat/modules/fo/ForumNotificationsHandler.java
index b08b9cfe6f1..5ebe911152f 100644
--- a/src/main/java/org/olat/modules/fo/ForumNotificationsHandler.java
+++ b/src/main/java/org/olat/modules/fo/ForumNotificationsHandler.java
@@ -42,7 +42,6 @@ import org.olat.core.commons.services.notifications.model.TitleItem;
 import org.olat.core.gui.translator.Translator;
 import org.olat.core.id.Identity;
 import org.olat.core.id.context.BusinessControlFactory;
-import org.olat.core.logging.LogDelegator;
 import org.olat.core.logging.OLog;
 import org.olat.core.logging.Tracing;
 import org.olat.core.util.StringHelper;
@@ -52,7 +51,6 @@ import org.olat.group.BusinessGroup;
 import org.olat.group.BusinessGroupService;
 import org.olat.modules.fo.manager.ForumManager;
 import org.olat.repository.RepositoryEntry;
-import org.olat.repository.RepositoryEntryStatusEnum;
 import org.olat.repository.RepositoryManager;
 
 /**
@@ -60,7 +58,7 @@ import org.olat.repository.RepositoryManager;
  * 
  * @author Felix Jost
  */
-public class ForumNotificationsHandler extends LogDelegator implements NotificationsHandler {
+public class ForumNotificationsHandler implements NotificationsHandler {
 	private static final OLog log = Tracing.createLoggerFor(ForumNotificationsHandler.class);
 
 	public ForumNotificationsHandler() {
@@ -85,16 +83,14 @@ public class ForumNotificationsHandler extends LogDelegator implements Notificat
 				try {
 					forumKey = Long.parseLong(p.getData());
 				} catch (NumberFormatException e) {
-					logError("Could not parse forum key!", e);
+					log.error("Could not parse forum key!", e);
 					NotificationsManager.getInstance().deactivate(p);
 					return NotificationsManager.getInstance().getNoSubscriptionInfo();
 				}
 				
 				if("CourseModule".equals(p.getResName())) {
 					RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(OresHelper.createOLATResourceableInstance(p.getResName(), p.getResId()), false);
-					if(re == null || re.getEntryStatus() == RepositoryEntryStatusEnum.closed
-							|| re.getEntryStatus() == RepositoryEntryStatusEnum.trash
-							|| re.getEntryStatus() == RepositoryEntryStatusEnum.deleted) {
+					if(re == null || re.getEntryStatus().decommissioned()) {
 						return NotificationsManager.getInstance().getNoSubscriptionInfo();
 					}
 				}
@@ -153,17 +149,17 @@ public class ForumNotificationsHandler extends LogDelegator implements Notificat
 			if("BusinessGroup".equals(p.getResName())) {
 				BusinessGroup bg = CoreSpringFactory.getImpl(BusinessGroupService.class).loadBusinessGroup(p.getResId());
 				if(bg == null) {
-					logInfo("deactivating publisher with key; " + p.getKey(), null);
+					log.info("deactivating publisher with key; " + p.getKey(), null);
 					NotificationsManager.getInstance().deactivate(p);
 				}
 			} else if ("CourseModule".equals(p.getResName())) {
 				if(!NotificationsUpgradeHelper.checkCourse(p)) {
-					logInfo("deactivating publisher with key; " + p.getKey(), null);
+					log.info("deactivating publisher with key; " + p.getKey(), null);
 					NotificationsManager.getInstance().deactivate(p);
 				}
 			}
 		} catch (Exception e) {
-			logError("", e);
+			log.error("", e);
 		}
 	}
 
diff --git a/src/main/java/org/olat/modules/webFeed/manager/FeedNotificationsHandler.java b/src/main/java/org/olat/modules/webFeed/manager/FeedNotificationsHandler.java
index f0900fbec40..19cf8e07f51 100644
--- a/src/main/java/org/olat/modules/webFeed/manager/FeedNotificationsHandler.java
+++ b/src/main/java/org/olat/modules/webFeed/manager/FeedNotificationsHandler.java
@@ -49,7 +49,6 @@ import org.olat.modules.webFeed.Feed;
 import org.olat.modules.webFeed.Item;
 import org.olat.modules.webFeed.ui.FeedMainController;
 import org.olat.repository.RepositoryEntry;
-import org.olat.repository.RepositoryEntryStatusEnum;
 import org.olat.repository.RepositoryManager;
 import org.olat.user.UserManager;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -85,29 +84,28 @@ public abstract class FeedNotificationsHandler implements NotificationsHandler {
 		try {
 		 	final Translator translator = Util.createPackageTranslator(FeedMainController.class, locale);
 		 	if (notificationsManager.isPublisherValid(p) && compareDate.before(latestNews)) {
+
+				RepositoryEntry re = repoManager.lookupRepositoryEntry(
+						OresHelper.createOLATResourceableInstance(p.getResName(), p.getResId()), false);
+				if (re.getEntryStatus().decommissioned()) {
+					return notificationsManager.getNoSubscriptionInfo();
+				}
+				
 				String title;
-				try {
-					RepositoryEntry re = repoManager.lookupRepositoryEntry(
-							OresHelper.createOLATResourceableInstance(p.getResName(), p.getResId()), false);
-					RepositoryEntryStatusEnum status = re.getEntryStatus();
-					if (status.decommissioned()) {
+				String displayName = re.getDisplayname();
+				if("CourseModule".equals(p.getResName())) {
+					ICourse course = CourseFactory.loadCourse(re);
+					CourseNode node = course.getRunStructure().getNode(p.getSubidentifier());
+					if(node == null) {
+						notificationsManager.deactivate(p);
 						return notificationsManager.getNoSubscriptionInfo();
 					}
-					String displayName = re.getDisplayname();
-					if("CourseModule".equals(p.getResName())) {
-						ICourse course = CourseFactory.loadCourse(re);
-						CourseNode node = course.getRunStructure().getNode(p.getSubidentifier());
-						if(node == null) {
-							notificationsManager.deactivate(p);
-							return notificationsManager.getNoSubscriptionInfo();
-						}
-						title = translator.translate(NOTIFICATIONS_HEADER_COURSE,  new String[]{displayName});
-					} else {
-						title = getHeader(translator, displayName);
+					if (!course.getCourseEnvironment().getCourseGroupManager().isNotificationsAllowed()) {
+						return notificationsManager.getNoSubscriptionInfo();
 					}
-				} catch (Exception e) {
-					log.error("Unknown Exception", e);
-					return notificationsManager.getNoSubscriptionInfo();
+					title = translator.translate(NOTIFICATIONS_HEADER_COURSE,  new String[]{displayName});
+				} else {
+					title = getHeader(translator, displayName);
 				}
 
 				OLATResourceable feedOres = OresHelper.createOLATResourceableInstance(p.getType(), new Long(p.getData()));
@@ -138,6 +136,10 @@ public abstract class FeedNotificationsHandler implements NotificationsHandler {
 		String urlToSend = BusinessControlFactory.getInstance()
 					.getURLFromBusinessPathString(businessPath);
 		String iconCssClass = item.extraCSSClass();
+		if(!StringHelper.containsNonWhitespace(iconCssClass)) {
+			iconCssClass = getCssClassIcon();
+		}
+		
 		Date publishDate = item.getPublishDate();
 		if(item.isPublished()) {
 			if(compareDate.before(publishDate)) {
-- 
GitLab