Search in sources :

Example 1 with Visitor

use of org.olat.core.util.tree.Visitor in project OpenOLAT by OpenOLAT.

the class EditorMainController method doCreateAlternateBuildingBlock.

/**
 * The following operation are done:
 * <ul>
 * 	<li>create a new instance of the replacement type
 * 	<li>add the new element below the original element
 * 	<li>copy the element title, description and the generic configuration options
 * 	<li>copy the access, visibility and scoring rules (easy and expert)
 * 	<li>optionally copy some other configuration if this is possible at all
 * 	<li>move all child elements from the original to the replacement element
 * 	<li>mark the original element as deleted
 * </ul>
 *
 * @param chosenNode
 * @param selectAlternative
 */
private void doCreateAlternateBuildingBlock(UserRequest ureq, ICourse course, CourseNode chosenNode, String selectAlternative) {
    if (!StringHelper.containsNonWhitespace(selectAlternative))
        return;
    // create the alternative node
    CourseNodeConfiguration newConfig = CourseNodeFactory.getInstance().getCourseNodeConfiguration(selectAlternative);
    CourseNode newNode = newConfig.getInstance();
    // copy configurations
    chosenNode.copyConfigurationTo(newNode, course);
    // insert the node
    CourseEditorTreeNode cetn = (CourseEditorTreeNode) cetm.getNodeById(chosenNode.getIdent());
    CourseEditorTreeNode parentNode = (CourseEditorTreeNode) cetn.getParent();
    int position = cetn.getPosition() + 1;
    CourseEditorTreeNode newCetn = course.getEditorTreeModel().insertCourseNodeAt(newNode, parentNode.getCourseNode(), position);
    doInsert(ureq, newNode);
    // copy the children
    while (cetn.getChildCount() > 0) {
        CourseEditorTreeNode childNode = (CourseEditorTreeNode) cetn.getChildAt(0);
        newCetn.addChild(childNode);
    }
    // set all dirty
    TreeVisitor tv = new TreeVisitor(new Visitor() {

        public void visit(INode node) {
            ((CourseEditorTreeNode) node).setDirty(true);
        }
    }, newCetn, true);
    tv.visitAll();
    // mark as deleted
    doDelete(course, chosenNode.getIdent());
    // save
    CourseFactory.saveCourseEditorTreeModel(course.getResourceableId());
}
Also used : TreeVisitor(org.olat.core.util.tree.TreeVisitor) INode(org.olat.core.util.nodes.INode) TreeVisitor(org.olat.core.util.tree.TreeVisitor) Visitor(org.olat.core.util.tree.Visitor) CourseEditorTreeNode(org.olat.course.tree.CourseEditorTreeNode) CourseNodeConfiguration(org.olat.course.nodes.CourseNodeConfiguration) CourseNode(org.olat.course.nodes.CourseNode)

Example 2 with Visitor

use of org.olat.core.util.tree.Visitor in project OpenOLAT by OpenOLAT.

the class CourseEditorEnvImpl method checkFolderNodes.

private List<StatusDescription> checkFolderNodes(INode rootNode, ICourse course, List<StatusDescription> descriptions) {
    List<StatusDescription> descriptionsI = descriptions;
    Visitor visitor = new Visitor() {

        @Override
        public void visit(INode node) {
            CourseEditorTreeNode courseNode = (CourseEditorTreeNode) course.getEditorTreeModel().getNodeById(node.getIdent());
            if (!courseNode.isDeleted() && courseNode.getCourseNode() instanceof BCCourseNode) {
                BCCourseNode bcNode = (BCCourseNode) courseNode.getCourseNode();
                if (bcNode.isSharedFolder()) {
                    String translPackage = Util.getPackageName(BCCourseNodeEditController.class);
                    StatusDescription status = new StatusDescription(StatusDescription.ERROR, "warning.no.sharedfolder", "warning.no.sharedfolder", null, translPackage);
                    status.setDescriptionForUnit(bcNode.getIdent());
                    // set which pane is affected by error
                    status.setActivateableViewIdentifier(BCCourseNodeEditController.PANE_TAB_FOLDER);
                    descriptionsI.add(status);
                }
            }
        }
    };
    TreeVisitor v = new TreeVisitor(visitor, rootNode, false);
    v.visitAll();
    return descriptionsI;
}
Also used : TreeVisitor(org.olat.core.util.tree.TreeVisitor) INode(org.olat.core.util.nodes.INode) BCCourseNode(org.olat.course.nodes.BCCourseNode) TreeVisitor(org.olat.core.util.tree.TreeVisitor) Visitor(org.olat.core.util.tree.Visitor) CourseEditorTreeNode(org.olat.course.tree.CourseEditorTreeNode)

Example 3 with Visitor

use of org.olat.core.util.tree.Visitor in project OpenOLAT by OpenOLAT.

the class UserFoldersTest method myFolders.

/**
 * Test retrieve the folder which the user subscribe in a course.
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void myFolders() throws IOException, URISyntaxException {
    final Identity id = JunitTestHelper.createAndPersistIdentityAsUser("my-" + UUID.randomUUID().toString());
    dbInstance.commitAndCloseSession();
    // load my forums
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id.getName(), "A6B7C8"));
    // subscribed to nothing
    URI uri = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("folders").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    FolderVOes folders = conn.parse(body, FolderVOes.class);
    Assert.assertNotNull(folders);
    Assert.assertNotNull(folders.getFolders());
    Assert.assertEquals(0, folders.getFolders().length);
    // subscribe to the forum
    IdentityEnvironment ienv = new IdentityEnvironment(id, new Roles(false, false, false, false, false, false, false));
    new CourseTreeVisitor(myCourse, ienv).visit(new Visitor() {

        @Override
        public void visit(INode node) {
            if (node instanceof BCCourseNode) {
                BCCourseNode folderNode = (BCCourseNode) node;
                String relPath = BCCourseNode.getFoldernodePathRelToFolderBase(myCourse.getCourseEnvironment(), folderNode);
                String businessPath = "[RepositoryEntry:" + myCourseRe.getKey() + "][CourseNode:" + folderNode.getIdent() + "]";
                SubscriptionContext folderSubContext = new SubscriptionContext("CourseModule", myCourse.getResourceableId(), folderNode.getIdent());
                PublisherData folderPdata = new PublisherData("FolderModule", relPath, businessPath);
                NotificationsManager.getInstance().subscribe(id, folderSubContext, folderPdata);
            }
        }
    }, new VisibleTreeFilter());
    dbInstance.commitAndCloseSession();
    // retrieve my folders
    HttpGet method2 = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response2 = conn.execute(method2);
    assertEquals(200, response2.getStatusLine().getStatusCode());
    InputStream body2 = response2.getEntity().getContent();
    FolderVOes folders2 = conn.parse(body2, FolderVOes.class);
    Assert.assertNotNull(folders2);
    Assert.assertNotNull(folders2.getFolders());
    Assert.assertEquals(1, folders2.getFolders().length);
}
Also used : FolderVOes(org.olat.restapi.support.vo.FolderVOes) INode(org.olat.core.util.nodes.INode) Visitor(org.olat.core.util.tree.Visitor) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) InputStream(java.io.InputStream) VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) HttpGet(org.apache.http.client.methods.HttpGet) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) HttpResponse(org.apache.http.HttpResponse) Roles(org.olat.core.id.Roles) URI(java.net.URI) PublisherData(org.olat.core.commons.services.notifications.PublisherData) BCCourseNode(org.olat.course.nodes.BCCourseNode) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Identity(org.olat.core.id.Identity) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) Test(org.junit.Test)

Example 4 with Visitor

use of org.olat.core.util.tree.Visitor in project OpenOLAT by OpenOLAT.

the class ForumCourseNodeWebService method getForums.

/**
 * Retrieves metadata of the published course node
 * @response.representation.200.qname {http://www.example.com}forumVOes
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The course node metadatas
 * @response.representation.200.example {@link org.olat.modules.fo.restapi.Examples#SAMPLE_FORUMVOes}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The course or parentNode not found
 * @param courseId The course resourceable's id
 * @param httpRequest The HTTP request
 * @return The persisted structure element (fully populated)
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getForums(@PathParam("courseId") Long courseId, @Context HttpServletRequest httpRequest) {
    final ICourse course = CoursesWebService.loadCourse(courseId);
    if (course == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    } else if (!CourseWebService.isCourseAccessible(course, false, httpRequest)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    UserRequest ureq = getUserRequest(httpRequest);
    final Set<Long> subcribedForums = new HashSet<Long>();
    NotificationsManager man = NotificationsManager.getInstance();
    List<String> notiTypes = Collections.singletonList("Forum");
    List<Subscriber> subs = man.getSubscribers(ureq.getIdentity(), notiTypes);
    for (Subscriber sub : subs) {
        Long forumKey = Long.parseLong(sub.getPublisher().getData());
        subcribedForums.add(forumKey);
    }
    final List<ForumVO> forumVOs = new ArrayList<ForumVO>();
    new CourseTreeVisitor(course, ureq.getUserSession().getIdentityEnvironment()).visit(new Visitor() {

        @Override
        public void visit(INode node) {
            if (node instanceof FOCourseNode) {
                FOCourseNode forumNode = (FOCourseNode) node;
                ForumVO forum = createForumVO(course, forumNode, subcribedForums);
                forumVOs.add(forum);
            }
        }
    }, new VisibleTreeFilter());
    ForumVOes voes = new ForumVOes();
    voes.setForums(forumVOs.toArray(new ForumVO[forumVOs.size()]));
    voes.setTotalCount(forumVOs.size());
    return Response.ok(voes).build();
}
Also used : INode(org.olat.core.util.nodes.INode) Visitor(org.olat.core.util.tree.Visitor) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) ArrayList(java.util.ArrayList) ICourse(org.olat.course.ICourse) FOCourseNode(org.olat.course.nodes.FOCourseNode) Subscriber(org.olat.core.commons.services.notifications.Subscriber) NotificationsManager(org.olat.core.commons.services.notifications.NotificationsManager) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) HashSet(java.util.HashSet) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with Visitor

use of org.olat.core.util.tree.Visitor in project OpenOLAT by OpenOLAT.

the class MyForumsWebService method getForums.

/**
 * Retrieves a list of forums on a user base. All forums of groups
 * where the user is participant/tutor + all forums in course where
 * the user is a participant (owner, tutor or participant)
 * @response.representation.200.qname {http://www.example.com}forumVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The forums
 * @response.representation.200.example {@link org.olat.modules.fo.restapi.Examples#SAMPLE_FORUMVO}
 * @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 forums
 */
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getForums(@PathParam("identityKey") Long identityKey, @Context HttpServletRequest httpRequest) {
    Roles roles;
    Identity retrievedUser = getIdentity(httpRequest);
    if (retrievedUser == null) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    } else if (!identityKey.equals(retrievedUser.getKey())) {
        if (isAdmin(httpRequest)) {
            retrievedUser = BaseSecurityManager.getInstance().loadIdentityByKey(identityKey);
            roles = BaseSecurityManager.getInstance().getRoles(retrievedUser);
        } else {
            return Response.serverError().status(Status.UNAUTHORIZED).build();
        }
    } else {
        roles = getRoles(httpRequest);
    }
    Map<Long, Long> groupNotified = new HashMap<Long, Long>();
    Map<Long, Collection<Long>> courseNotified = new HashMap<Long, Collection<Long>>();
    final Set<Long> subscriptions = new HashSet<Long>();
    NotificationsManager man = NotificationsManager.getInstance();
    {
        // collect subscriptions
        List<String> notiTypes = Collections.singletonList("Forum");
        List<Subscriber> subs = man.getSubscribers(retrievedUser, notiTypes);
        for (Subscriber sub : subs) {
            String resName = sub.getPublisher().getResName();
            Long forumKey = Long.parseLong(sub.getPublisher().getData());
            subscriptions.add(forumKey);
            if ("BusinessGroup".equals(resName)) {
                Long groupKey = sub.getPublisher().getResId();
                groupNotified.put(groupKey, forumKey);
            } else if ("CourseModule".equals(resName)) {
                Long courseKey = sub.getPublisher().getResId();
                if (!courseNotified.containsKey(courseKey)) {
                    courseNotified.put(courseKey, new ArrayList<Long>());
                }
                courseNotified.get(courseKey).add(forumKey);
            }
        }
    }
    final List<ForumVO> forumVOs = new ArrayList<ForumVO>();
    final IdentityEnvironment ienv = new IdentityEnvironment(retrievedUser, roles);
    for (Map.Entry<Long, Collection<Long>> e : courseNotified.entrySet()) {
        final Long courseKey = e.getKey();
        final Collection<Long> forumKeys = e.getValue();
        final ICourse course = CourseFactory.loadCourse(courseKey);
        new CourseTreeVisitor(course, ienv).visit(new Visitor() {

            @Override
            public void visit(INode node) {
                if (node instanceof FOCourseNode) {
                    FOCourseNode forumNode = (FOCourseNode) node;
                    ForumVO forumVo = ForumCourseNodeWebService.createForumVO(course, forumNode, subscriptions);
                    if (forumKeys.contains(forumVo.getForumKey())) {
                        forumVOs.add(forumVo);
                    }
                }
            }
        }, 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 FOCourseNode) {
								FOCourseNode forumNode = (FOCourseNode)node;	
								ForumVO forumVo = ForumCourseNodeWebService.createForumVO(course, forumNode, subscriptions);
								forumVOs.add(forumVo);
							}
						}
					});
				} catch (Exception e) {
					log.error("", e);
				}
			}
		}*/
    // start found forums in groups
    BusinessGroupService bgs = CoreSpringFactory.getImpl(BusinessGroupService.class);
    SearchBusinessGroupParams params = new SearchBusinessGroupParams(retrievedUser, true, true);
    params.addTools(CollaborationTools.TOOL_FORUM);
    List<BusinessGroup> groups = bgs.findBusinessGroups(params, null, 0, -1);
    // list forum keys
    List<Long> groupIds = new ArrayList<Long>();
    Map<Long, BusinessGroup> groupsMap = new HashMap<Long, BusinessGroup>();
    for (BusinessGroup group : groups) {
        if (groupNotified.containsKey(group.getKey())) {
            ForumVO forumVo = new ForumVO();
            forumVo.setName(group.getName());
            forumVo.setGroupKey(group.getKey());
            forumVo.setForumKey(groupNotified.get(group.getKey()));
            forumVo.setSubscribed(true);
            forumVOs.add(forumVo);
            groupIds.remove(group.getKey());
        } else {
            groupIds.add(group.getKey());
            groupsMap.put(group.getKey(), group);
        }
    }
    PropertyManager pm = PropertyManager.getInstance();
    List<Property> forumProperties = pm.findProperties(OresHelper.calculateTypeName(BusinessGroup.class), groupIds, PROP_CAT_BG_COLLABTOOLS, KEY_FORUM);
    for (Property prop : forumProperties) {
        Long forumKey = prop.getLongValue();
        if (forumKey != null && groupsMap.containsKey(prop.getResourceTypeId())) {
            BusinessGroup group = groupsMap.get(prop.getResourceTypeId());
            ForumVO forumVo = new ForumVO();
            forumVo.setName(group.getName());
            forumVo.setGroupKey(group.getKey());
            forumVo.setForumKey(prop.getLongValue());
            forumVo.setSubscribed(false);
            forumVOs.add(forumVo);
        }
    }
    ForumVOes voes = new ForumVOes();
    voes.setForums(forumVOs.toArray(new ForumVO[forumVOs.size()]));
    voes.setTotalCount(forumVOs.size());
    return Response.ok(voes).build();
}
Also used : INode(org.olat.core.util.nodes.INode) 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) PropertyManager(org.olat.properties.PropertyManager) ArrayList(java.util.ArrayList) ICourse(org.olat.course.ICourse) FOCourseNode(org.olat.course.nodes.FOCourseNode) Subscriber(org.olat.core.commons.services.notifications.Subscriber) ArrayList(java.util.ArrayList) List(java.util.List) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Identity(org.olat.core.id.Identity) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) Property(org.olat.properties.Property) HashSet(java.util.HashSet) BusinessGroup(org.olat.group.BusinessGroup) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) RestSecurityHelper.getRoles(org.olat.restapi.security.RestSecurityHelper.getRoles) Roles(org.olat.core.id.Roles) SearchBusinessGroupParams(org.olat.group.model.SearchBusinessGroupParams) BusinessGroupService(org.olat.group.BusinessGroupService) NotificationsManager(org.olat.core.commons.services.notifications.NotificationsManager) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Visitor (org.olat.core.util.tree.Visitor)56 INode (org.olat.core.util.nodes.INode)44 TreeVisitor (org.olat.core.util.tree.TreeVisitor)42 ArrayList (java.util.ArrayList)24 ICourse (org.olat.course.ICourse)20 CourseNode (org.olat.course.nodes.CourseNode)20 Identity (org.olat.core.id.Identity)14 BCCourseNode (org.olat.course.nodes.BCCourseNode)14 CourseTreeVisitor (org.olat.course.run.userview.CourseTreeVisitor)14 VisibleTreeFilter (org.olat.course.run.userview.VisibleTreeFilter)14 CourseEditorTreeNode (org.olat.course.tree.CourseEditorTreeNode)14 IdentityEnvironment (org.olat.core.id.IdentityEnvironment)12 HashMap (java.util.HashMap)10 FOCourseNode (org.olat.course.nodes.FOCourseNode)10 File (java.io.File)8 List (java.util.List)8 GET (javax.ws.rs.GET)8 Produces (javax.ws.rs.Produces)8 NotificationsManager (org.olat.core.commons.services.notifications.NotificationsManager)8 Subscriber (org.olat.core.commons.services.notifications.Subscriber)8