Search in sources :

Example 31 with Subscriber

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);
    }
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) ArrayList(java.util.ArrayList) Publisher(org.olat.core.commons.services.notifications.Publisher) MultiUserEvent(org.olat.core.util.event.MultiUserEvent) Date(java.util.Date) HashSet(java.util.HashSet)

Example 32 with Subscriber

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);
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) Publisher(org.olat.core.commons.services.notifications.Publisher)

Example 33 with Subscriber

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();
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) Publisher(org.olat.core.commons.services.notifications.Publisher) Identity(org.olat.core.id.Identity)

Example 34 with Subscriber

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;
}
Also used : Locale(java.util.Locale) Subscriber(org.olat.core.commons.services.notifications.Subscriber) NotificationsHandler(org.olat.core.commons.services.notifications.NotificationsHandler) ArrayList(java.util.ArrayList) SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo) NoSubscriptionInfo(org.olat.core.commons.services.notifications.model.NoSubscriptionInfo) Publisher(org.olat.core.commons.services.notifications.Publisher) Date(java.util.Date)

Example 35 with Subscriber

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();
}
Also used : VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) Roles(org.olat.core.id.Roles) FOCourseNode(org.olat.course.nodes.FOCourseNode) RepositoryEntry(org.olat.repository.RepositoryEntry) Publisher(org.olat.core.commons.services.notifications.Publisher) URI(java.net.URI) URL(java.net.URL) HttpPut(org.apache.http.client.methods.HttpPut) Forum(org.olat.modules.fo.Forum) Subscriber(org.olat.core.commons.services.notifications.Subscriber) PublisherVO(org.olat.core.commons.services.notifications.restapi.vo.PublisherVO) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Identity(org.olat.core.id.Identity) File(java.io.File) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) Test(org.junit.Test)

Aggregations

Subscriber (org.olat.core.commons.services.notifications.Subscriber)82 Publisher (org.olat.core.commons.services.notifications.Publisher)42 Identity (org.olat.core.id.Identity)30 NotificationsManager (org.olat.core.commons.services.notifications.NotificationsManager)26 ArrayList (java.util.ArrayList)24 SubscriptionContext (org.olat.core.commons.services.notifications.SubscriptionContext)22 HashSet (java.util.HashSet)18 Test (org.junit.Test)18 PublisherData (org.olat.core.commons.services.notifications.PublisherData)18 Date (java.util.Date)16 GET (javax.ws.rs.GET)16 Produces (javax.ws.rs.Produces)16 ICourse (org.olat.course.ICourse)16 CourseTreeVisitor (org.olat.course.run.userview.CourseTreeVisitor)16 VisibleTreeFilter (org.olat.course.run.userview.VisibleTreeFilter)16 SubscriptionInfo (org.olat.core.commons.services.notifications.SubscriptionInfo)10 Roles (org.olat.core.id.Roles)10 FOCourseNode (org.olat.course.nodes.FOCourseNode)10 List (java.util.List)8 Locale (java.util.Locale)8