Search in sources :

Example 26 with Subscriber

use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.

the class NotificationsManagerImpl method subscribe.

/**
 * @param identity
 * @param subscriptionContext
 * @param publisherData
 */
@Override
public void subscribe(Identity identity, SubscriptionContext subscriptionContext, PublisherData publisherData) {
    // need to sync as opt-in is sometimes implemented
    Publisher toUpdate = getPublisherForUpdate(subscriptionContext);
    if (toUpdate == null) {
        // create the publisher
        findOrCreatePublisher(subscriptionContext, publisherData);
        // lock the publisher
        toUpdate = getPublisherForUpdate(subscriptionContext);
    }
    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)

Example 27 with Subscriber

use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.

the class NotificationsManagerImpl method markPublisherNews.

/**
 * call this method to indicate that there is news for the given
 * subscriptionContext
 *
 * @param subscriptionContext
 * @param ignoreNewsFor
 */
@Override
public void markPublisherNews(final SubscriptionContext subscriptionContext, Identity ignoreNewsFor, boolean sendEvents) {
    // to make sure: ignore if no subscriptionContext
    if (subscriptionContext == null)
        return;
    Publisher toUpdate = getPublisherForUpdate(subscriptionContext);
    if (toUpdate == null) {
        return;
    }
    toUpdate.setLatestNewsDate(new Date());
    Publisher publisher = dbInstance.getCurrentEntityManager().merge(toUpdate);
    // commit the select for update
    dbInstance.commit();
    // user
    if (ignoreNewsFor != null) {
        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(publisher);
        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) Publisher(org.olat.core.commons.services.notifications.Publisher) MultiUserEvent(org.olat.core.util.event.MultiUserEvent) Date(java.util.Date) HashSet(java.util.HashSet)

Example 28 with Subscriber

use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.

the class NotificationsManagerImpl method getSubscriber.

/**
 * @param key
 * @return the subscriber with this key or null if not found
 */
@Override
public Subscriber getSubscriber(Long key) {
    StringBuilder q = new StringBuilder();
    q.append("select sub from notisub as sub").append(" inner join fetch sub.publisher ").append(" where sub.key=:aKey");
    List<Subscriber> res = dbInstance.getCurrentEntityManager().createQuery(q.toString(), Subscriber.class).setParameter("aKey", key.longValue()).getResultList();
    if (res.isEmpty())
        return null;
    if (res.size() > 1)
        throw new AssertException("more than one subscriber for key " + key);
    return res.get(0);
}
Also used : AssertException(org.olat.core.logging.AssertException) Subscriber(org.olat.core.commons.services.notifications.Subscriber)

Example 29 with Subscriber

use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.

the class NotificationsManagerImpl method unsubscribe.

/**
 * @param identity
 * @param subscriptionContext
 */
@Override
public void unsubscribe(Identity identity, SubscriptionContext subscriptionContext) {
    Publisher p = getPublisherForUpdate(subscriptionContext);
    if (p != null) {
        Subscriber s = getSubscriber(identity, p);
        if (s != null) {
            deleteSubscriber(s);
        } else {
            logWarn("could not unsubscribe " + identity.getName() + " from publisher:" + p.getResName() + "," + p.getResId() + "," + p.getSubidentifier(), null);
        }
    }
    dbInstance.commit();
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) Publisher(org.olat.core.commons.services.notifications.Publisher)

Example 30 with Subscriber

use of org.olat.core.commons.services.notifications.Subscriber in project openolat by klemens.

the class NotificationsManagerImpl method getSubscriber.

/**
 * @param identity
 * @param publisher
 * @return a Subscriber object belonging to the identity and listening to the
 *         given publisher
 */
@Override
public Subscriber getSubscriber(Identity identity, Publisher publisher) {
    List<Subscriber> res = dbInstance.getCurrentEntityManager().createNamedQuery("subscribersByPublisherAndIdentity", Subscriber.class).setParameter("publisherKey", publisher.getKey()).setParameter("identityKey", identity.getKey()).getResultList();
    if (res.size() == 0)
        return null;
    if (res.size() != 1)
        throw new AssertException("only one subscriber per person and publisher!!");
    Subscriber s = res.get(0);
    return s;
}
Also used : AssertException(org.olat.core.logging.AssertException) Subscriber(org.olat.core.commons.services.notifications.Subscriber)

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