Search in sources :

Example 66 with Subscriber

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

the class UserFoldersWebService method getFolders.

/**
 * Retrieves a list of folders on a user base. All folders of groups
 * where the user is participant/tutor + all folders in course where
 * the user is a participant (owner, tutor or participant)
 * @response.representation.200.qname {http://www.example.com}folderVOes
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The folders
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_FOLDERVOes}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @param identityKey The key of the user (IdentityImpl)
 * @param httpRequest The HTTP request
 * @return The folders
 */
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getFolders(@Context HttpServletRequest httpRequest) {
    Roles roles;
    Identity ureqIdentity = getIdentity(httpRequest);
    if (ureqIdentity == null) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    } else if (!identity.getKey().equals(ureqIdentity.getKey())) {
        if (isAdmin(httpRequest)) {
            ureqIdentity = identity;
            roles = BaseSecurityManager.getInstance().getRoles(identity);
        } else {
            return Response.serverError().status(Status.UNAUTHORIZED).build();
        }
    } else {
        roles = getRoles(httpRequest);
    }
    final Map<Long, Long> groupNotified = new HashMap<Long, Long>();
    final Map<Long, Collection<String>> courseNotified = new HashMap<Long, Collection<String>>();
    NotificationsManager man = NotificationsManager.getInstance();
    {
        // collect subscriptions
        List<String> notiTypes = Collections.singletonList("FolderModule");
        List<Subscriber> subs = man.getSubscribers(ureqIdentity, notiTypes);
        for (Subscriber sub : subs) {
            String resName = sub.getPublisher().getResName();
            if ("BusinessGroup".equals(resName)) {
                Long groupKey = sub.getPublisher().getResId();
                groupNotified.put(groupKey, sub.getPublisher().getResId());
            } else if ("CourseModule".equals(resName)) {
                Long courseKey = sub.getPublisher().getResId();
                if (!courseNotified.containsKey(courseKey)) {
                    courseNotified.put(courseKey, new ArrayList<String>());
                }
                courseNotified.get(courseKey).add(sub.getPublisher().getSubidentifier());
            }
        }
    }
    final List<FolderVO> folderVOs = new ArrayList<FolderVO>();
    final IdentityEnvironment ienv = new IdentityEnvironment(ureqIdentity, roles);
    for (Map.Entry<Long, Collection<String>> e : courseNotified.entrySet()) {
        final Long courseKey = e.getKey();
        final Collection<String> nodeKeys = e.getValue();
        final ICourse course = CourseFactory.loadCourse(courseKey);
        new CourseTreeVisitor(course, ienv).visit(new Visitor() {

            @Override
            public void visit(INode node) {
                if (node instanceof BCCourseNode) {
                    BCCourseNode bcNode = (BCCourseNode) node;
                    if (nodeKeys.contains(bcNode.getIdent())) {
                        FolderVO folder = BCWebService.createFolderVO(ienv, course, bcNode, courseNotified.get(course.getResourceableId()));
                        folderVOs.add(folder);
                    }
                }
            }
        }, new VisibleTreeFilter());
    }
    /*
		RepositoryManager rm = RepositoryManager.getInstance();
		ACService acManager = CoreSpringFactory.getImpl(ACService.class);
		SearchRepositoryEntryParameters repoParams = new SearchRepositoryEntryParameters(retrievedUser, roles, "CourseModule");
		repoParams.setOnlyExplicitMember(true);
		List<RepositoryEntry> entries = rm.genericANDQueryWithRolesRestriction(repoParams, 0, -1, true);
		for(RepositoryEntry entry:entries) {
			AccessResult result = acManager.isAccessible(entry, retrievedUser, false);
			if(result.isAccessible()) {
				try {
					final ICourse course = CourseFactory.loadCourse(entry.getOlatResource());
					final IdentityEnvironment ienv = new IdentityEnvironment(retrievedUser, roles);
					
					new CourseTreeVisitor(course,  ienv).visit(new Visitor() {
						@Override
						public void visit(INode node) {
							if(node instanceof BCCourseNode) {
								BCCourseNode bcNode = (BCCourseNode)node;
								FolderVO folder = BCWebService.createFolderVO(ienv, course, bcNode, courseNotified.get(course.getResourceableId()));
								folderVOs.add(folder);
							}
						}
					});
				} catch (Exception e) {
					log.error("", e);
				}
			}
		}*/
    // start found forums in groups
    BusinessGroupService bgs = CoreSpringFactory.getImpl(BusinessGroupService.class);
    SearchBusinessGroupParams params = new SearchBusinessGroupParams(ureqIdentity, true, true);
    params.addTools(CollaborationTools.TOOL_FOLDER);
    List<BusinessGroup> groups = bgs.findBusinessGroups(params, null, 0, -1);
    for (BusinessGroup group : groups) {
        CollaborationTools tools = CollaborationToolsFactory.getInstance().getOrCreateCollaborationTools(group);
        VFSContainer container = tools.getSecuredFolder(group, null, ureqIdentity, false);
        FolderVO folderVo = new FolderVO();
        folderVo.setName(group.getName());
        folderVo.setGroupKey(group.getKey());
        folderVo.setSubscribed(groupNotified.containsKey(group.getKey()));
        folderVo.setRead(container.getLocalSecurityCallback().canRead());
        folderVo.setList(container.getLocalSecurityCallback().canList());
        folderVo.setWrite(container.getLocalSecurityCallback().canWrite());
        folderVo.setDelete(container.getLocalSecurityCallback().canDelete());
        folderVOs.add(folderVo);
    }
    FolderVOes voes = new FolderVOes();
    voes.setFolders(folderVOs.toArray(new FolderVO[folderVOs.size()]));
    voes.setTotalCount(folderVOs.size());
    return Response.ok(voes).build();
}
Also used : INode(org.olat.core.util.nodes.INode) FolderVOes(org.olat.restapi.support.vo.FolderVOes) Visitor(org.olat.core.util.tree.Visitor) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) HashMap(java.util.HashMap) VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) ArrayList(java.util.ArrayList) ICourse(org.olat.course.ICourse) Subscriber(org.olat.core.commons.services.notifications.Subscriber) List(java.util.List) ArrayList(java.util.ArrayList) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) FolderVO(org.olat.restapi.support.vo.FolderVO) BusinessGroup(org.olat.group.BusinessGroup) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) VFSContainer(org.olat.core.util.vfs.VFSContainer) Roles(org.olat.core.id.Roles) RestSecurityHelper.getRoles(org.olat.restapi.security.RestSecurityHelper.getRoles) SearchBusinessGroupParams(org.olat.group.model.SearchBusinessGroupParams) BCCourseNode(org.olat.course.nodes.BCCourseNode) BusinessGroupService(org.olat.group.BusinessGroupService) NotificationsManager(org.olat.core.commons.services.notifications.NotificationsManager) CollaborationTools(org.olat.collaboration.CollaborationTools) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 67 with Subscriber

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

the class NotificationsManagerTest method testMarkSubscriberRead.

@Test
public void testMarkSubscriberRead() {
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("subs-" + UUID.randomUUID().toString());
    // create a publisher
    String identifier = UUID.randomUUID().toString().replace("-", "");
    SubscriptionContext context = new SubscriptionContext("All", new Long(123), identifier);
    PublisherData publisherData = new PublisherData("testAllPublishers", "e.g. forumdata=keyofforum", null);
    Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
    dbInstance.commit();
    Assert.assertNotNull(publisher);
    // subscribe
    notificationManager.subscribe(id, context, publisherData);
    dbInstance.commit();
    // load the subscriber
    Subscriber subscriber = notificationManager.getSubscriber(id, publisher);
    Assert.assertNotNull(subscriber);
    dbInstance.commitAndCloseSession();
    sleep(2000);
    notificationManager.markSubscriberRead(id, context);
    // check the last modification date
    Subscriber reloadedSubscriber = notificationManager.getSubscriber(subscriber.getKey());
    Assert.assertNotNull(reloadedSubscriber);
    Assert.assertEquals(subscriber, reloadedSubscriber);
    Assert.assertTrue(subscriber.getLastModified().before(reloadedSubscriber.getLastModified()));
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Publisher(org.olat.core.commons.services.notifications.Publisher) Identity(org.olat.core.id.Identity) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Test(org.junit.Test)

Example 68 with Subscriber

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

the class NotificationsManagerTest method testUnsubscribe_v1.

@Test
public void testUnsubscribe_v1() {
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("unsubs-" + UUID.randomUUID().toString());
    // create a publisher
    String identifier = UUID.randomUUID().toString().replace("-", "");
    SubscriptionContext context = new SubscriptionContext("All", new Long(123), identifier);
    PublisherData publisherData = new PublisherData("testUnsubscribe", "e.g. forumdata=keyofforum", null);
    Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(publisher);
    // subscribe
    notificationManager.subscribe(id, context, publisherData);
    dbInstance.commitAndCloseSession();
    // check
    Subscriber subscriber = notificationManager.getSubscriber(id, publisher);
    Assert.assertNotNull(subscriber);
    // unsubscribe
    notificationManager.unsubscribe(subscriber);
    dbInstance.commitAndCloseSession();
    // check
    boolean subscribed = notificationManager.isSubscribed(id, context);
    Assert.assertFalse(subscribed);
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Publisher(org.olat.core.commons.services.notifications.Publisher) Identity(org.olat.core.id.Identity) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Test(org.junit.Test)

Example 69 with Subscriber

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

the class NotificationsManagerTest method testGetSubscribersByTypes.

@Test
public void testGetSubscribersByTypes() {
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("type1-" + UUID.randomUUID().toString());
    // create a first publisher
    String identifier = UUID.randomUUID().toString().replace("-", "");
    SubscriptionContext context1 = new SubscriptionContext("Subscribers", new Long(123), identifier);
    PublisherData publisherData1 = new PublisherData("testGetSubscribersByType1", "e.g. forumdata=keyofforum", null);
    Publisher publisher1 = notificationManager.getOrCreatePublisher(context1, publisherData1);
    dbInstance.commitAndCloseSession();
    String identifier2 = UUID.randomUUID().toString().replace("-", "");
    SubscriptionContext context2 = new SubscriptionContext("Subscribers", new Long(123), identifier2);
    PublisherData publisherData2 = new PublisherData("testGetSubscribersByType2", "e.g. forumdata=keyofforum", null);
    Publisher publisher2 = notificationManager.getOrCreatePublisher(context2, publisherData2);
    dbInstance.commitAndCloseSession();
    // add subscribers
    notificationManager.subscribe(id, context1, publisherData1);
    notificationManager.subscribe(id, context2, publisherData2);
    dbInstance.commitAndCloseSession();
    // get subscribers without types
    List<Subscriber> emptySubscribers = notificationManager.getSubscribers(id, null);
    Assert.assertNotNull(emptySubscribers);
    Assert.assertEquals(2, emptySubscribers.size());
    // get subscribers with 1 type
    List<String> types = Collections.singletonList(publisher1.getType());
    List<Subscriber> typedSubscribers = notificationManager.getSubscribers(id, types);
    Assert.assertNotNull(typedSubscribers);
    Assert.assertEquals(1, typedSubscribers.size());
    // get subscribers with 2 types
    List<String> allTypes = new ArrayList<String>(2);
    allTypes.add(publisher1.getType());
    allTypes.add(publisher2.getType());
    List<Subscriber> allSubscribers = notificationManager.getSubscribers(id, allTypes);
    Assert.assertNotNull(allSubscribers);
    Assert.assertEquals(2, allSubscribers.size());
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) ArrayList(java.util.ArrayList) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Publisher(org.olat.core.commons.services.notifications.Publisher) Identity(org.olat.core.id.Identity) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Test(org.junit.Test)

Example 70 with Subscriber

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

the class NotificationSubscriptionController method updateSubscriptionsDataModel.

/**
 * Update the table model
 *
 * @param ureq
 */
void updateSubscriptionsDataModel() {
    // Load subscriptions from DB. Don't use the ureq.getIdentity() but the
    // subscriberIdentity instead to make this controller also be usable in the
    // admin environment (admins might change notifications for a user)
    NotificationsManager man = NotificationsManager.getInstance();
    List<Subscriber> subs = man.getSubscribers(subscriberIdentity);
    for (Iterator<Subscriber> subIt = subs.iterator(); subIt.hasNext(); ) {
        Subscriber sub = subIt.next();
        if (!man.isPublisherValid(sub.getPublisher())) {
            subIt.remove();
        }
    }
    subscriptionsTableModel.setObjects(subs);
    // Tell table about model change or set table model if not already set
    if (subscriptionsTableCtr.getTableDataModel() == null) {
        subscriptionsTableCtr.setTableDataModel(subscriptionsTableModel);
    } else {
        subscriptionsTableCtr.modelChanged(true);
    }
}
Also used : Subscriber(org.olat.core.commons.services.notifications.Subscriber) NotificationsManager(org.olat.core.commons.services.notifications.NotificationsManager)

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