use of org.olat.core.commons.services.notifications.Publisher 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.Publisher in project openolat by klemens.
the class RepositoryEntryMembershipProcessorTest method testRemoveParticipant.
@Test
public void testRemoveParticipant() {
RepositoryEntry re = JunitTestHelper.createAndPersistRepositoryEntry();
// create a group with members
Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("remp-proc-1");
Identity member = JunitTestHelper.createAndPersistIdentityAsRndUser("remp-proc-2");
Identity participant = JunitTestHelper.createAndPersistIdentityAsRndUser("mbr-proc-3");
repositoryEntryRelationDao.addRole(owner, re, GroupRoles.owner.name());
repositoryEntryRelationDao.addRole(member, re, GroupRoles.coach.name());
repositoryEntryRelationDao.addRole(member, re, GroupRoles.participant.name());
repositoryEntryRelationDao.addRole(participant, re, GroupRoles.participant.name());
// create a publisher
SubscriptionContext context = new SubscriptionContext(re.getOlatResource(), "");
PublisherData publisherData = new PublisherData("testGroupPublishers", "e.g. something", null);
Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
Assert.assertNotNull(publisher);
dbInstance.commitAndCloseSession();
// subscribe
notificationManager.subscribe(owner, context, publisherData);
notificationManager.subscribe(member, context, publisherData);
notificationManager.subscribe(participant, context, publisherData);
dbInstance.commitAndCloseSession();
// remove member and participant as participant of the repo entry
List<Identity> removeIdentities = new ArrayList<>(2);
removeIdentities.add(member);
removeIdentities.add(participant);
MailPackage mailing = new MailPackage(false);
repositoryManager.removeParticipants(owner, removeIdentities, re, mailing, false);
// wait for the remove of subscription
waitForCondition(new CheckUnsubscription(participant, context, dbInstance, notificationManager), 5000);
sleep(1000);
// check that subscription of id1 was deleted but not the ones of id2 and coach
boolean subscribedPart = notificationManager.isSubscribed(participant, context);
Assert.assertFalse(subscribedPart);
boolean subscribedMember = notificationManager.isSubscribed(member, context);
Assert.assertTrue(subscribedMember);
boolean subscribedOwner = notificationManager.isSubscribed(owner, context);
Assert.assertTrue(subscribedOwner);
}
use of org.olat.core.commons.services.notifications.Publisher in project openolat by klemens.
the class RepositoryEntryMembershipProcessorTest method testRemoveCoach_withBusinessGroups.
@Test
public void testRemoveCoach_withBusinessGroups() {
RepositoryEntry re = JunitTestHelper.createAndPersistRepositoryEntry();
// create a group with members
Identity owner = JunitTestHelper.createAndPersistIdentityAsRndUser("remp-proc-1");
Identity member = JunitTestHelper.createAndPersistIdentityAsRndUser("remp-proc-2");
Identity coach = JunitTestHelper.createAndPersistIdentityAsRndUser("mbr-proc-3");
repositoryEntryRelationDao.addRole(owner, re, GroupRoles.owner.name());
repositoryEntryRelationDao.addRole(member, re, GroupRoles.coach.name());
repositoryEntryRelationDao.addRole(coach, re, GroupRoles.coach.name());
BusinessGroup businessGroup = businessGroupDao.createAndPersist(coach, "mbr-proc-1", "mbr-proc-desc", -1, -1, false, false, false, false, false);
businessGroupRelationDao.addRelationToResource(businessGroup, re);
// create a publisher
SubscriptionContext context = new SubscriptionContext(re.getOlatResource(), "");
PublisherData publisherData = new PublisherData("testGroupPublishers", "e.g. something", null);
Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
Assert.assertNotNull(publisher);
dbInstance.commitAndCloseSession();
// subscribe
notificationManager.subscribe(owner, context, publisherData);
notificationManager.subscribe(member, context, publisherData);
notificationManager.subscribe(coach, context, publisherData);
dbInstance.commitAndCloseSession();
// remove member and coach as coach of the repo entry
List<Identity> removeIdentities = new ArrayList<>(2);
removeIdentities.add(member);
removeIdentities.add(coach);
repositoryManager.removeTutors(owner, removeIdentities, re, new MailPackage(false));
// wait for the remove of subscription
waitForCondition(new CheckUnsubscription(member, context, dbInstance, notificationManager), 5000);
sleep(1000);
// check that subscription of id1 was deleted but not the ones of id2 and coach
boolean subscribedMember = notificationManager.isSubscribed(member, context);
Assert.assertFalse(subscribedMember);
boolean subscribedCoach = notificationManager.isSubscribed(coach, context);
Assert.assertTrue(subscribedCoach);
boolean subscribedOwner = notificationManager.isSubscribed(owner, context);
Assert.assertTrue(subscribedOwner);
}
use of org.olat.core.commons.services.notifications.Publisher 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();
}
use of org.olat.core.commons.services.notifications.Publisher in project openolat by klemens.
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());
}
}
Aggregations