use of org.olat.core.commons.services.notifications.Subscriber in project OpenOLAT by OpenOLAT.
the class NotificationsSubscribersTest method unsubscribe.
@Test
public void unsubscribe() throws IOException, URISyntaxException {
Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-3");
Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-4");
Identity id3 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-5");
// 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);
// the 3 users subscribed to the forum
PublisherData publisherData = new PublisherData("Forum", forum.getKey().toString(), "[RepositoryEntry:" + courseEntry.getKey() + "][CourseNode:" + courseNode.getIdent() + "]");
SubscriptionContext subsContext = new SubscriptionContext("CourseModule", course.getResourceableId(), courseNode.getIdent());
notificationsManager.subscribe(id1, subsContext, publisherData);
notificationsManager.subscribe(id2, subsContext, publisherData);
notificationsManager.subscribe(id3, subsContext, publisherData);
dbInstance.commitAndCloseSession();
// get the subscriber
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login("administrator", "openolat"));
URI subscribersUri = UriBuilder.fromUri(getContextURI()).path("notifications").path("subscribers").path(subsContext.getResName()).path(subsContext.getResId().toString()).path(subsContext.getSubidentifier()).build();
HttpGet getMethod = conn.createGet(subscribersUri, MediaType.APPLICATION_JSON, true);
HttpResponse getResponse = conn.execute(getMethod);
Assert.assertEquals(200, getResponse.getStatusLine().getStatusCode());
List<SubscriberVO> subscriberVOes = parseGroupArray(getResponse.getEntity().getContent());
Assert.assertNotNull(subscriberVOes);
Assert.assertEquals(3, subscriberVOes.size());
SubscriberVO subscriberId2VO = null;
for (SubscriberVO subscriberVO : subscriberVOes) {
if (subscriberVO.getIdentityKey().equals(id2.getKey())) {
subscriberId2VO = subscriberVO;
}
}
// delete id2
URI deleteSubscriberUri = UriBuilder.fromUri(getContextURI()).path("notifications").path("subscribers").path(subscriberId2VO.getSubscriberKey().toString()).build();
HttpDelete deleteMethod = conn.createDelete(deleteSubscriberUri, MediaType.APPLICATION_JSON);
HttpResponse deleteResponse = conn.execute(deleteMethod);
Assert.assertEquals(200, deleteResponse.getStatusLine().getStatusCode());
// check
Publisher publisher = notificationsManager.getPublisher(subsContext);
List<Subscriber> survivingSubscribers = notificationsManager.getSubscribers(publisher);
Assert.assertNotNull(survivingSubscribers);
Assert.assertEquals(2, survivingSubscribers.size());
for (Subscriber subscriber : survivingSubscribers) {
Assert.assertNotEquals(id2, subscriber.getIdentity());
}
}
use of org.olat.core.commons.services.notifications.Subscriber in project OpenOLAT by OpenOLAT.
the class NotificationSubscriptionTableDataModel method getValueAt.
/**
* @see org.olat.core.gui.components.table.DefaultTableDataModel#getValueAt(int,
* int)
*/
@Override
public Object getValueAt(int row, int col) {
Subscriber sub = getObject(row);
Publisher pub = sub.getPublisher();
switch(col) {
case 0:
return sub.getKey();
case 1:
String innerType = pub.getType();
return NewControllerFactory.translateResourceableTypeName(innerType, getLocale());
case 2:
String containerType = pub.getResName();
return NewControllerFactory.translateResourceableTypeName(containerType, getLocale());
case 3:
NotificationsHandler handler = NotificationsManager.getInstance().getNotificationsHandler(pub);
if (handler == null) {
return "";
}
String title = handler.createTitleInfo(sub, getLocale());
if (title == null) {
return "";
}
return title;
case 4:
return sub.getCreationDate();
case 5:
return sub.getLatestEmailed();
default:
return "ERROR";
}
}
use of org.olat.core.commons.services.notifications.Subscriber in project OpenOLAT by OpenOLAT.
the class NotificationNewsController method event.
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
* org.olat.core.gui.components.Component,
* org.olat.core.gui.control.Event)
*/
@Override
protected void event(UserRequest ureq, Component source, Event event) {
if (source == emailLink) {
// send email to user with the currently visible date
NotificationsManager man = NotificationsManager.getInstance();
List<SubscriptionItem> infoList = new ArrayList<>();
List<Subscriber> subsList = new ArrayList<>();
for (Subscriber subscriber : subsInfoMap.keySet()) {
subsList.add(subscriber);
SubscriptionItem item = man.createSubscriptionItem(subscriber, getLocale(), SubscriptionInfo.MIME_HTML, SubscriptionInfo.MIME_HTML, compareDate);
if (item != null) {
infoList.add(item);
}
}
if (man.sendMailToUserAndUpdateSubscriber(subscriberIdentity, infoList, getTranslator(), subsList)) {
showInfo("email.ok");
} else {
showError("email.nok");
}
}
}
use of org.olat.core.commons.services.notifications.Subscriber in project OpenOLAT by OpenOLAT.
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);
}
use of org.olat.core.commons.services.notifications.Subscriber in project OpenOLAT by OpenOLAT.
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();
}
Aggregations