use of org.olat.core.commons.services.notifications.Publisher in project OpenOLAT by OpenOLAT.
the class NotificationsManagerTest method testSubscribe.
@Test
public void testSubscribe() {
Identity id = JunitTestHelper.createAndPersistIdentityAsUser("subs-" + UUID.randomUUID().toString());
// create a publisher
String identifier = UUID.randomUUID().toString().replace("-", "");
SubscriptionContext context = new SubscriptionContext("All", new Long(123), identifier);
PublisherData publisherData = new PublisherData("testAllPublishers", "e.g. forumdata=keyofforum", null);
Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
dbInstance.commitAndCloseSession();
Assert.assertNotNull(publisher);
// subscribe
notificationManager.subscribe(id, context, publisherData);
dbInstance.commitAndCloseSession();
// check
boolean subscribed = notificationManager.isSubscribed(id, context);
Assert.assertTrue(subscribed);
dbInstance.commitAndCloseSession();
// double check
Subscriber subscriber = notificationManager.getSubscriber(id, publisher);
Assert.assertNotNull(subscriber);
Assert.assertEquals(publisher, subscriber.getPublisher());
dbInstance.commitAndCloseSession();
// triple check
Subscriber reloadedSubscriber = notificationManager.getSubscriber(subscriber.getKey());
Assert.assertNotNull(reloadedSubscriber);
Assert.assertEquals(subscriber, reloadedSubscriber);
}
use of org.olat.core.commons.services.notifications.Publisher in project OpenOLAT by OpenOLAT.
the class NotificationsManagerTest method testMarkSubscriberRead.
@Test
public void testMarkSubscriberRead() {
Identity id = JunitTestHelper.createAndPersistIdentityAsUser("subs-" + UUID.randomUUID().toString());
// create a publisher
String identifier = UUID.randomUUID().toString().replace("-", "");
SubscriptionContext context = new SubscriptionContext("All", new Long(123), identifier);
PublisherData publisherData = new PublisherData("testAllPublishers", "e.g. forumdata=keyofforum", null);
Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
dbInstance.commit();
Assert.assertNotNull(publisher);
// subscribe
notificationManager.subscribe(id, context, publisherData);
dbInstance.commit();
// load the subscriber
Subscriber subscriber = notificationManager.getSubscriber(id, publisher);
Assert.assertNotNull(subscriber);
dbInstance.commitAndCloseSession();
sleep(2000);
notificationManager.markSubscriberRead(id, context);
// check the last modification date
Subscriber reloadedSubscriber = notificationManager.getSubscriber(subscriber.getKey());
Assert.assertNotNull(reloadedSubscriber);
Assert.assertEquals(subscriber, reloadedSubscriber);
Assert.assertTrue(subscriber.getLastModified().before(reloadedSubscriber.getLastModified()));
}
use of org.olat.core.commons.services.notifications.Publisher in project OpenOLAT by OpenOLAT.
the class NotificationsManagerTest method testGetSubscriberIdentities.
@Test
public void testGetSubscriberIdentities() {
Identity id1 = JunitTestHelper.createAndPersistIdentityAsUser("valid1b-" + UUID.randomUUID().toString());
Identity id2 = JunitTestHelper.createAndPersistIdentityAsUser("valid1b-" + UUID.randomUUID().toString());
// create a publisher
String identifier = UUID.randomUUID().toString().replace("-", "");
SubscriptionContext context = new SubscriptionContext("Subscribers", new Long(123), identifier);
PublisherData publisherData = new PublisherData("testGetSubscriberIdentities", "e.g. forumdata=keyofforum", null);
Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
dbInstance.commitAndCloseSession();
// add subscribers
notificationManager.subscribe(id1, context, publisherData);
notificationManager.subscribe(id2, context, publisherData);
dbInstance.commitAndCloseSession();
// get identities
List<Identity> identities = notificationManager.getSubscriberIdentities(publisher);
Assert.assertNotNull(identities);
Assert.assertEquals(2, identities.size());
Assert.assertTrue(identities.contains(id1));
Assert.assertTrue(identities.contains(id2));
}
use of org.olat.core.commons.services.notifications.Publisher in project OpenOLAT by OpenOLAT.
the class NotificationsManagerTest method testCreateUpdatePublisher.
@Test
public void testCreateUpdatePublisher() {
String identifier = UUID.randomUUID().toString().replace("-", "");
SubscriptionContext context = new SubscriptionContext("PS2", new Long(124), identifier);
PublisherData publisherData = new PublisherData("testPublisherSubscriber", "e.g. forumdata=keyofforum", null);
Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
dbInstance.commitAndCloseSession();
// check values
Assert.assertNotNull(publisher);
Assert.assertNotNull(publisher.getKey());
Assert.assertNotNull(publisher.getCreationDate());
Assert.assertNotNull(publisher.getLatestNewsDate());
Assert.assertEquals("PS2", publisher.getResName());
Assert.assertEquals(new Long(124), publisher.getResId());
sleep(2000);
// update the publisher
notificationManager.markPublisherNews(context, null, false);
// check if exists and last news date is updated
Publisher reloadedPublisher = notificationManager.getPublisher(context);
Assert.assertNotNull(reloadedPublisher);
Assert.assertEquals(publisher, reloadedPublisher);
Assert.assertTrue(publisher.getLatestNewsDate().before(reloadedPublisher.getLatestNewsDate()));
}
use of org.olat.core.commons.services.notifications.Publisher in project openolat by klemens.
the class CalendarNotificationHandler method createSubscriptionInfo.
@Override
public SubscriptionInfo createSubscriptionInfo(Subscriber subscriber, Locale locale, Date compareDate) {
SubscriptionInfo si = null;
Publisher p = subscriber.getPublisher();
Date latestNews = p.getLatestNewsDate();
// can't be loaded when already deleted
if (notificationsManager.isPublisherValid(p) && compareDate.before(latestNews)) {
Long id = p.getResId();
String type = p.getSubidentifier();
try {
Translator translator = Util.createPackageTranslator(CalendarModule.class, locale);
String calType = null;
String title = null;
if (type.equals(CalendarController.ACTION_CALENDAR_COURSE)) {
RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(OresHelper.createOLATResourceableInstance("CourseModule", id), false);
if (re.getRepositoryEntryStatus().isClosed() || re.getRepositoryEntryStatus().isUnpublished()) {
return NotificationsManager.getInstance().getNoSubscriptionInfo();
}
String displayName = re.getDisplayname();
calType = CalendarManager.TYPE_COURSE;
title = translator.translate("cal.notifications.header.course", new String[] { displayName });
} else if (type.equals(CalendarController.ACTION_CALENDAR_GROUP)) {
BusinessGroup group = businessGroupDao.load(id);
calType = CalendarManager.TYPE_GROUP;
if (group == null) {
return notificationsManager.getNoSubscriptionInfo();
}
title = translator.translate("cal.notifications.header.group", new String[] { group.getName() });
}
if (calType != null) {
Formatter form = Formatter.getInstance(locale);
si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(title, CSS_CLASS_CALENDAR_ICON), null);
String bPath;
if (StringHelper.containsNonWhitespace(p.getBusinessPath())) {
bPath = p.getBusinessPath();
} else if ("CalendarManager.course".equals(p.getResName())) {
try {
OLATResourceable ores = OresHelper.createOLATResourceableInstance(CourseModule.getCourseTypeName(), p.getResId());
RepositoryEntry re = repositoryManager.lookupRepositoryEntry(ores, true);
// Fallback
bPath = "[RepositoryEntry:" + re.getKey() + "]";
} catch (Exception e) {
log.error("Error processing calendar notifications of publisher:" + p.getKey(), e);
return notificationsManager.getNoSubscriptionInfo();
}
} else {
// cannot make link without business path
return notificationsManager.getNoSubscriptionInfo();
}
Kalendar cal = calendarManager.getCalendar(calType, id.toString());
Collection<KalendarEvent> calEvents = cal.getEvents();
for (KalendarEvent kalendarEvent : calEvents) {
if (showEvent(compareDate, kalendarEvent)) {
log.debug("found a KalendarEvent: " + kalendarEvent.getSubject() + " with time: " + kalendarEvent.getBegin() + " modified before: " + compareDate.toString(), null);
// found a modified event in this calendar
Date modDate = null;
if (kalendarEvent.getLastModified() > 0) {
modDate = new Date(kalendarEvent.getLastModified());
} else if (kalendarEvent.getCreated() > 0) {
modDate = new Date(kalendarEvent.getCreated());
} else if (kalendarEvent.getBegin() != null) {
modDate = kalendarEvent.getBegin();
}
String subject = kalendarEvent.getSubject();
String author = kalendarEvent.getCreatedBy();
if (author == null)
author = "";
String location = "";
if (StringHelper.containsNonWhitespace(kalendarEvent.getLocation())) {
location = kalendarEvent.getLocation() == null ? "" : translator.translate("cal.notifications.location", new String[] { kalendarEvent.getLocation() });
}
String dateStr;
if (kalendarEvent.isAllDayEvent()) {
dateStr = form.formatDate(kalendarEvent.getBegin());
} else {
dateStr = form.formatDate(kalendarEvent.getBegin()) + " - " + form.formatDate(kalendarEvent.getEnd());
}
String desc = translator.translate("cal.notifications.entry", new String[] { subject, dateStr, location, author });
String businessPath = bPath + "[path=" + kalendarEvent.getID() + ":0]";
String urlToSend = BusinessControlFactory.getInstance().getURLFromBusinessPathString(businessPath);
SubscriptionListItem subListItem = new SubscriptionListItem(desc, urlToSend, businessPath, modDate, CSS_CLASS_CALENDAR_ICON);
si.addSubscriptionListItem(subListItem);
}
}
}
} catch (Exception e) {
log.error("Unexpected exception", e);
checkPublisher(p);
si = notificationsManager.getNoSubscriptionInfo();
}
} else {
si = notificationsManager.getNoSubscriptionInfo();
}
return si;
}
Aggregations