use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.
the class NotificationsManagerImpl method markPublisherNews.
@Override
public void markPublisherNews(String publisherType, String data, Identity ignoreNewsFor, boolean sendEvents) {
// to make sure: ignore if no subscriptionContext
if (!StringHelper.containsNonWhitespace(publisherType) || !StringHelper.containsNonWhitespace(data))
return;
List<Publisher> publisherToUpdates = getPublishers(publisherType, data);
if (publisherToUpdates == null || publisherToUpdates.isEmpty()) {
return;
}
List<Publisher> updatedPublishers = new ArrayList<>(publisherToUpdates.size());
for (Publisher toUpdate : publisherToUpdates) {
toUpdate = getPublisherForUpdate(toUpdate);
toUpdate.setLatestNewsDate(new Date());
Publisher publisher = dbInstance.getCurrentEntityManager().merge(toUpdate);
// commit the select for update
dbInstance.commit();
updatedPublishers.add(publisher);
}
// user
if (ignoreNewsFor != null) {
for (Publisher publisher : updatedPublishers) {
markSubscriberRead(ignoreNewsFor, publisher);
}
}
if (sendEvents) {
// commit all things on the database
dbInstance.commit();
// channel-notify all interested listeners (e.g. the pnotificationsportletruncontroller)
// 1. find all subscribers which can be affected
List<Subscriber> subscribers = getValidSubscribersOf(publisherType, data);
Set<Long> subsKeys = new HashSet<Long>();
// 2. collect all keys of the affected subscribers
for (Subscriber subscriber : subscribers) {
subsKeys.add(subscriber.getKey());
}
// fire the event
MultiUserEvent mue = EventFactory.createAffectedEvent(subsKeys);
CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(mue, oresMyself);
}
}
use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.
the class NotificationsManagerImpl method delete.
/**
* delete publisher and subscribers
*
* @param scontext the subscriptioncontext
*/
@Override
public void delete(SubscriptionContext scontext) {
Publisher p = getPublisher(scontext);
// -> nothing to do
if (p == null)
return;
// first delete all subscribers
List<Subscriber> subscribers = getValidSubscribersOf(p);
for (Subscriber subscriber : subscribers) {
deleteSubscriber(subscriber);
}
// else:
dbInstance.deleteObject(p);
}
use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.
the class NotificationsManagerImpl method subscribe.
@Override
public void subscribe(List<Identity> identities, SubscriptionContext subscriptionContext, PublisherData publisherData) {
if (identities == null || identities.isEmpty())
return;
Publisher toUpdate = getPublisherForUpdate(subscriptionContext);
if (toUpdate == null) {
// create the publisher
findOrCreatePublisher(subscriptionContext, publisherData);
// lock the publisher
toUpdate = getPublisherForUpdate(subscriptionContext);
}
for (Identity identity : identities) {
Subscriber s = getSubscriber(identity, toUpdate);
if (s == null) {
// no subscriber -> create.
// s.latestReadDate >= p.latestNewsDate == no news for subscriber when no
// news after subscription time
doCreateAndPersistSubscriber(toUpdate, identity);
}
}
dbInstance.commit();
}
use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.
the class NotificationsManagerImpl method getSubscriptionInfos.
@Override
public List<SubscriptionInfo> getSubscriptionInfos(Identity identity, String publisherType) {
StringBuilder sb = new StringBuilder();
sb.append("select sub from notisub sub").append(" inner join fetch sub.publisher as pub").append(" where sub.identity=:identity and pub.type=:type and pub.state=:aState");
List<Subscriber> subscribers = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Subscriber.class).setParameter("aState", PUB_STATE_OK).setParameter("type", publisherType).setParameter("identity", identity).getResultList();
if (subscribers.isEmpty()) {
return Collections.emptyList();
}
Locale locale = new Locale(identity.getUser().getPreferences().getLanguage());
Date compareDate = getDefaultCompareDate();
List<SubscriptionInfo> sis = new ArrayList<SubscriptionInfo>();
for (Subscriber subscriber : subscribers) {
Publisher pub = subscriber.getPublisher();
NotificationsHandler notifHandler = getNotificationsHandler(pub);
// do not create subscription item when deleted
if (isPublisherValid(pub)) {
SubscriptionInfo subsInfo = notifHandler.createSubscriptionInfo(subscriber, locale, compareDate);
if (subsInfo.hasNews()) {
sis.add(subsInfo);
}
}
}
return sis;
}
use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.
the class NotificationsSubscribersTest method subscribe.
@Test
public void subscribe() throws IOException, URISyntaxException {
Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-1");
Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-2");
// deploy a course with forums
URL courseWithForumsUrl = MyForumsTest.class.getResource("myCourseWS.zip");
Assert.assertNotNull(courseWithForumsUrl);
File courseWithForums = new File(courseWithForumsUrl.toURI());
String softKey = UUID.randomUUID().toString().replace("-", "");
RepositoryEntry courseEntry = CourseFactory.deployCourseFromZIP(courseWithForums, softKey, 4);
Assert.assertNotNull(courseEntry);
// load the course and found the first forum
ICourse course = CourseFactory.loadCourse(courseEntry);
// find the forum
IdentityEnvironment ienv = new IdentityEnvironment(id1, new Roles(false, false, false, false, false, false, false));
ForumVisitor forumVisitor = new ForumVisitor(course);
new CourseTreeVisitor(course, ienv).visit(forumVisitor, new VisibleTreeFilter());
FOCourseNode courseNode = forumVisitor.firstNode;
Forum forum = forumVisitor.firstForum;
Assert.assertNotNull(courseNode);
Assert.assertNotNull(forum);
// put subscribers
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login("administrator", "openolat"));
PublisherVO subscribersVO = new PublisherVO();
// publisher data
subscribersVO.setType("Forum");
subscribersVO.setData(forum.getKey().toString());
subscribersVO.setBusinessPath("[RepositoryEntry:" + courseEntry.getKey() + "][CourseNode:" + courseNode.getIdent() + "]");
// context
subscribersVO.setResName("CourseModule");
subscribersVO.setResId(course.getResourceableId());
subscribersVO.setSubidentifier(courseNode.getIdent());
subscribersVO.getUsers().add(UserVOFactory.get(id1));
subscribersVO.getUsers().add(UserVOFactory.get(id2));
// create the subscribers
URI subscribersUri = UriBuilder.fromUri(getContextURI()).path("notifications").path("subscribers").build();
HttpPut putMethod = conn.createPut(subscribersUri, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(putMethod, subscribersVO);
HttpResponse response = conn.execute(putMethod);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
EntityUtils.consume(response.getEntity());
// get publisher
SubscriptionContext subsContext = new SubscriptionContext("CourseModule", course.getResourceableId(), courseNode.getIdent());
Publisher publisher = notificationsManager.getPublisher(subsContext);
Assert.assertNotNull(publisher);
// get subscribers
List<Subscriber> subscribers = notificationsManager.getSubscribers(publisher);
Assert.assertNotNull(subscribers);
Assert.assertEquals(2, subscribers.size());
conn.shutdown();
}
Aggregations