Search in sources :

Example 21 with Visitor

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

the class CourseEditorEnvImpl method validateCourse.

/**
 * @see org.olat.course.editor.CourseEditorEnv#validateCourse()
 */
@Override
public void validateCourse() {
    /*
		 * collect all condition error messages and soft references collect all
		 * configuration errors.
		 */
    String currentNodeWas = currentCourseNodeId;
    // reset all
    softRefs = new HashMap<String, List<ConditionExpression>>();
    Visitor v = new CollectConditionExpressionsVisitor();
    (new TreeVisitor(v, cetm.getRootNode(), true)).visitAll();
    // refresh,create status descriptions of the course
    statusDescs = new HashMap<String, List<StatusDescription>>();
    v = new CollectStatusDescriptionVisitor(this);
    (new TreeVisitor(v, cetm.getRootNode(), true)).visitAll();
    // 
    currentCourseNodeId = currentNodeWas;
}
Also used : TreeVisitor(org.olat.core.util.tree.TreeVisitor) TreeVisitor(org.olat.core.util.tree.TreeVisitor) Visitor(org.olat.core.util.tree.Visitor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 22 with Visitor

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

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 23 with Visitor

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

the class QTIEditorMainController method createChangeMessage.

/**
 * helper method to create the change log message
 *
 * @return
 */
private String createChangeMessage() {
    // FIXME:pb:break down into smaller pieces
    final StringBuilder result = new StringBuilder();
    if (isRestrictedEdit()) {
        Visitor v = new Visitor() {

            /*
				 * a history key is built as follows
				 * sectionkey+"/"+itemkey+"/"+questionkey+"/"+responsekey
				 */
            String sectionKey = null;

            Map<String, String> itemMap = new HashMap<>();

            public void visit(INode node) {
                if (node instanceof AssessmentNode) {
                    AssessmentNode an = (AssessmentNode) node;
                    String key = "null/null/null/null";
                    if (history.containsKey(key)) {
                        // some assessment top level data changed
                        Memento mem = history.get(key);
                        result.append("---+ Changes in test " + formatVariable(startedWithTitle) + ":");
                        result.append(an.createChangeMessage(mem));
                    }
                } else if (node instanceof SectionNode) {
                    SectionNode sn = (SectionNode) node;
                    String tmpKey = ((Section) sn.getUnderlyingQTIObject()).getIdent();
                    String key = tmpKey + "/null/null/null";
                    if (history.containsKey(key)) {
                        // some section only data changed
                        Memento mem = history.get(key);
                        result.append("\n---++ Section " + formatVariable(sn.getAltText()) + " changes:");
                        result.append(sn.createChangeMessage(mem));
                    }
                } else if (node instanceof ItemNode) {
                    ItemNode in = (ItemNode) node;
                    SectionNode sn = (SectionNode) in.getParent();
                    String parentSectkey = ((Section) ((SectionNode) in.getParent()).getUnderlyingQTIObject()).getIdent();
                    Item item = (Item) in.getUnderlyingQTIObject();
                    Question question = item.getQuestion();
                    String itemKey = item.getIdent();
                    String prefixKey = "null/" + itemKey;
                    String questionIdent = question != null ? question.getQuestion().getId() : "null";
                    String key = prefixKey + "/" + questionIdent + "/null";
                    StringBuilder changeMessage = new StringBuilder();
                    boolean hasChanges = false;
                    if (!itemMap.containsKey(itemKey)) {
                        Memento questMem = null;
                        Memento respMem = null;
                        if (history.containsKey(key)) {
                            // question changed!
                            questMem = history.get(key);
                            hasChanges = true;
                        }
                        // if(!hasChanges){
                        // check if a response changed
                        // new prefix for responses
                        prefixKey += "/null/";
                        // list contains org.olat.ims.qti.editor.beecom.objects.Response
                        List<Response> responses = question != null ? question.getResponses() : null;
                        if (responses != null && responses.size() > 0) {
                            // check for changes in each response
                            for (Iterator<Response> iter = responses.iterator(); iter.hasNext(); ) {
                                Response resp = iter.next();
                                if (history.containsKey(prefixKey + resp.getIdent())) {
                                    // this response changed!
                                    Memento tmpMem = history.get(prefixKey + resp.getIdent());
                                    if (respMem != null) {
                                        respMem = respMem.getTimestamp() > tmpMem.getTimestamp() ? tmpMem : respMem;
                                    } else {
                                        hasChanges = true;
                                        respMem = tmpMem;
                                    }
                                }
                            }
                        }
                        // output message
                        if (hasChanges) {
                            Memento mem = null;
                            if (questMem != null && respMem != null) {
                                // use the earlier memento
                                mem = questMem.getTimestamp() > respMem.getTimestamp() ? respMem : questMem;
                            } else if (questMem != null) {
                                mem = questMem;
                            } else if (respMem != null) {
                                mem = respMem;
                            }
                            changeMessage.append(in.createChangeMessage(mem));
                            itemMap.put(itemKey, itemKey);
                            if (!parentSectkey.equals(sectionKey)) {
                                // either this item belongs to a new section or no section
                                // is active
                                result.append("\n---++ Section " + formatVariable(sn.getAltText()) + " changes:");
                                result.append("\n").append(changeMessage);
                                sectionKey = parentSectkey;
                            } else {
                                result.append("\n").append(changeMessage);
                            }
                        }
                    }
                }
            }

            private String formatVariable(String var) {
                if (StringHelper.containsNonWhitespace(var)) {
                    return var;
                }
                return "[no entry]";
            }
        };
        TreeVisitor tv = new TreeVisitor(v, menuTreeModel.getRootNode(), false);
        tv.visitAll();
    }
    /*
		 * 
		 */
    return result.toString();
}
Also used : INode(org.olat.core.util.nodes.INode) TreeVisitor(org.olat.core.util.tree.TreeVisitor) Visitor(org.olat.core.util.tree.Visitor) AssessmentNode(org.olat.ims.qti.editor.tree.AssessmentNode) SectionNode(org.olat.ims.qti.editor.tree.SectionNode) Section(org.olat.ims.qti.editor.beecom.objects.Section) Response(org.olat.ims.qti.editor.beecom.objects.Response) TreeVisitor(org.olat.core.util.tree.TreeVisitor) Memento(org.olat.core.util.memento.Memento) Item(org.olat.ims.qti.editor.beecom.objects.Item) ItemNode(org.olat.ims.qti.editor.tree.ItemNode) ChoiceQuestion(org.olat.ims.qti.editor.beecom.objects.ChoiceQuestion) Question(org.olat.ims.qti.editor.beecom.objects.Question) Map(java.util.Map) HashMap(java.util.HashMap)

Example 24 with Visitor

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

the class CoursesInfosWebService method collect.

private CourseInfoVO collect(final Identity identity, final Roles roles, final RepositoryEntry entry, final Set<Long> forumNotified, final Map<Long, Set<String>> courseNotified) {
    CourseInfoVO info = new CourseInfoVO();
    info.setRepoEntryKey(entry.getKey());
    info.setSoftKey(entry.getSoftkey());
    info.setDisplayName(entry.getDisplayname());
    ACService acManager = CoreSpringFactory.getImpl(ACService.class);
    AccessResult result = acManager.isAccessible(entry, identity, false);
    if (result.isAccessible()) {
        try {
            final ICourse course = CourseFactory.loadCourse(entry);
            final List<FolderVO> folders = new ArrayList<FolderVO>();
            final List<ForumVO> forums = new ArrayList<ForumVO>();
            final IdentityEnvironment ienv = new IdentityEnvironment(identity, roles);
            new CourseTreeVisitor(course, ienv).visit(new Visitor() {

                @Override
                public void visit(INode node) {
                    if (node instanceof BCCourseNode) {
                        BCCourseNode bcNode = (BCCourseNode) node;
                        folders.add(BCWebService.createFolderVO(ienv, course, bcNode, courseNotified.get(course.getResourceableId())));
                    } else if (node instanceof FOCourseNode) {
                        FOCourseNode forumNode = (FOCourseNode) node;
                        forums.add(ForumCourseNodeWebService.createForumVO(course, forumNode, forumNotified));
                    }
                }
            }, new VisibleTreeFilter());
            info.setKey(course.getResourceableId());
            info.setTitle(course.getCourseTitle());
            info.setFolders(folders.toArray(new FolderVO[folders.size()]));
            info.setForums(forums.toArray(new ForumVO[forums.size()]));
        } catch (Exception e) {
            log.error("", e);
        }
    }
    return info;
}
Also used : ForumVO(org.olat.modules.fo.restapi.ForumVO) INode(org.olat.core.util.nodes.INode) Visitor(org.olat.core.util.tree.Visitor) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) FolderVO(org.olat.restapi.support.vo.FolderVO) 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) BCCourseNode(org.olat.course.nodes.BCCourseNode) ACService(org.olat.resource.accesscontrol.ACService) AccessResult(org.olat.resource.accesscontrol.AccessResult) CourseInfoVO(org.olat.restapi.support.vo.CourseInfoVO) IdentityEnvironment(org.olat.core.id.IdentityEnvironment)

Example 25 with Visitor

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

the class MyForumsTest method myForums.

/**
 * Test retrieve the forum which the user subscribe in a course.
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void myForums() throws IOException, URISyntaxException {
    URL courseWithForumsUrl = MyForumsTest.class.getResource("myCourseWS.zip");
    Assert.assertNotNull(courseWithForumsUrl);
    File courseWithForums = new File(courseWithForumsUrl.toURI());
    String softKey = UUID.randomUUID().toString().replace("_", "");
    RepositoryEntry myCourseRe = CourseFactory.deployCourseFromZIP(courseWithForums, softKey, 4);
    Assert.assertNotNull(myCourseRe);
    ICourse myCourse = CourseFactory.loadCourse(myCourseRe);
    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("forums").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();
    ForumVOes forums = conn.parse(body, ForumVOes.class);
    Assert.assertNotNull(forums);
    Assert.assertNotNull(forums.getForums());
    Assert.assertEquals(0, forums.getForums().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 FOCourseNode) {
                FOCourseNode forumNode = (FOCourseNode) node;
                Forum forum = forumNode.loadOrCreateForum(myCourse.getCourseEnvironment());
                String businessPath = "[RepositoryEntry:" + myCourseRe.getKey() + "][CourseNode:" + forumNode.getIdent() + "]";
                SubscriptionContext forumSubContext = new SubscriptionContext("CourseModule", myCourse.getResourceableId(), forumNode.getIdent());
                PublisherData forumPdata = new PublisherData(OresHelper.calculateTypeName(Forum.class), forum.getKey().toString(), businessPath);
                NotificationsManager.getInstance().subscribe(id, forumSubContext, forumPdata);
            }
        }
    }, new VisibleTreeFilter());
    dbInstance.commitAndCloseSession();
    // retrieve my forums
    HttpGet method2 = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response2 = conn.execute(method2);
    assertEquals(200, response2.getStatusLine().getStatusCode());
    InputStream body2 = response2.getEntity().getContent();
    ForumVOes forums2 = conn.parse(body2, ForumVOes.class);
    Assert.assertNotNull(forums2);
    Assert.assertNotNull(forums2.getForums());
    Assert.assertEquals(1, forums2.getForums().length);
}
Also used : 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) ForumVOes(org.olat.modules.fo.restapi.ForumVOes) 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) ICourse(org.olat.course.ICourse) Roles(org.olat.core.id.Roles) FOCourseNode(org.olat.course.nodes.FOCourseNode) RepositoryEntry(org.olat.repository.RepositoryEntry) URI(java.net.URI) PublisherData(org.olat.core.commons.services.notifications.PublisherData) URL(java.net.URL) Forum(org.olat.modules.fo.Forum) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Identity(org.olat.core.id.Identity) File(java.io.File) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) Test(org.junit.Test)

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