Search in sources :

Example 6 with ForumManager

use of org.olat.modules.fo.manager.ForumManager in project OpenOLAT by OpenOLAT.

the class ForumArchiveManager method convertToThreadTrees.

/**
 * If the forumCallback is null no filtering is executed,
 * else if a thread is hidden and the user doesn't have moderator rights the
 * hidden thread is not included into the archive.
 * @param forumId
 * @param metaInfo
 * @return all top message nodes together with their children in a list
 */
private List<MessageNode> convertToThreadTrees(Long forumId, ForumCallback forumCallback) {
    List<MessageNode> topNodeList = new ArrayList<>();
    ForumManager fm = ForumManager.getInstance();
    Forum f = fm.loadForum(forumId);
    List<Message> messages = fm.getMessagesByForum(f);
    for (Iterator<Message> iterTop = messages.iterator(); iterTop.hasNext(); ) {
        Message msg = iterTop.next();
        if (msg.getParent() == null) {
            iterTop.remove();
            MessageNode topNode = new MessageNode(msg);
            if (topNode.isHidden() && (forumCallback == null || (forumCallback != null && forumCallback.mayEditMessageAsModerator()))) {
                addChildren(messages, topNode);
                topNodeList.add(topNode);
            } else if (!topNode.isHidden()) {
                addChildren(messages, topNode);
                topNodeList.add(topNode);
            }
        }
    }
    Collections.sort(topNodeList, new MessageNodeComparator());
    return topNodeList;
}
Also used : Message(org.olat.modules.fo.Message) ForumManager(org.olat.modules.fo.manager.ForumManager) ArrayList(java.util.ArrayList) Forum(org.olat.modules.fo.Forum)

Example 7 with ForumManager

use of org.olat.modules.fo.manager.ForumManager in project OpenOLAT by OpenOLAT.

the class ForumTest method testUploadAttachmentWithFile64VO.

@Test
public void testUploadAttachmentWithFile64VO() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).queryParam("authorKey", id1.getKey()).queryParam("title", "New message with attachment ").queryParam("body", "A very interesting response in Thread-1 with an attachment").build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    MessageVO message = conn.parse(response, MessageVO.class);
    assertNotNull(message);
    // attachment
    InputStream portraitStream = CoursesElementsTest.class.getResourceAsStream("portrait.jpg");
    assertNotNull(portraitStream);
    // upload portrait
    URI attachUri = getForumUriBuilder().path("posts").path(message.getKey().toString()).path("attachments").build();
    HttpPut attachMethod = conn.createPut(attachUri, MediaType.APPLICATION_JSON, true);
    attachMethod.addHeader("Content-Type", MediaType.APPLICATION_JSON);
    byte[] portraitBytes = IOUtils.toByteArray(portraitStream);
    byte[] portrait64 = Base64.encodeBase64(portraitBytes, true);
    File64VO fileVo = new File64VO();
    fileVo.setFile(new String(portrait64));
    fileVo.setFilename("portrait64vo.jpg");
    conn.addJsonEntity(attachMethod, fileVo);
    HttpResponse attachCode = conn.execute(attachMethod);
    assertEquals(200, attachCode.getStatusLine().getStatusCode());
    // check if the file exists
    ForumManager fm = ForumManager.getInstance();
    VFSContainer container = fm.getMessageContainer(message.getForumKey(), message.getKey());
    VFSItem uploadedFile = container.resolve("portrait64vo.jpg");
    assertNotNull(uploadedFile);
    assertTrue(uploadedFile instanceof VFSLeaf);
    // check if the image is still an image
    VFSLeaf uploadedImage = (VFSLeaf) uploadedFile;
    InputStream uploadedStream = uploadedImage.getInputStream();
    BufferedImage image = ImageIO.read(uploadedStream);
    FileUtils.closeSafely(uploadedStream);
    assertNotNull(image);
    conn.shutdown();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputStream(java.io.InputStream) File64VO(org.olat.restapi.support.vo.File64VO) VFSContainer(org.olat.core.util.vfs.VFSContainer) HttpResponse(org.apache.http.HttpResponse) VFSItem(org.olat.core.util.vfs.VFSItem) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) BufferedImage(java.awt.image.BufferedImage) ForumManager(org.olat.modules.fo.manager.ForumManager) MessageVO(org.olat.modules.fo.restapi.MessageVO) Test(org.junit.Test)

Example 8 with ForumManager

use of org.olat.modules.fo.manager.ForumManager in project OpenOLAT by OpenOLAT.

the class ForumTest method testReplyWithTwoAttachments.

@Test
public void testReplyWithTwoAttachments() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    ReplyVO vo = new ReplyVO();
    vo.setTitle("Reply with attachment");
    vo.setBody("Reply with attachment body");
    File64VO[] files = new File64VO[2];
    // upload portrait
    InputStream portraitStream = CoursesElementsTest.class.getResourceAsStream("portrait.jpg");
    assertNotNull(portraitStream);
    byte[] portraitBytes = IOUtils.toByteArray(portraitStream);
    byte[] portrait64 = Base64.encodeBase64(portraitBytes, true);
    files[0] = new File64VO("portrait64.jpg", new String(portrait64));
    // upload single page
    InputStream indexStream = ForumTest.class.getResourceAsStream("singlepage.html");
    assertNotNull(indexStream);
    byte[] indexBytes = IOUtils.toByteArray(indexStream);
    byte[] index64 = Base64.encodeBase64(indexBytes, true);
    files[1] = new File64VO("singlepage64.html", new String(index64));
    vo.setAttachments(files);
    URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(method, vo);
    method.addHeader("Accept-Language", "en");
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    MessageVO message = conn.parse(response, MessageVO.class);
    assertNotNull(message);
    assertNotNull(message.getAttachments());
    assertEquals(2, message.getAttachments().length);
    for (FileVO attachment : message.getAttachments()) {
        String title = attachment.getTitle();
        assertNotNull(title);
        String href = attachment.getHref();
        URI attachmentUri = new URI(href);
        HttpGet getAttachment = conn.createGet(attachmentUri, "*/*", true);
        HttpResponse attachmentCode = conn.execute(getAttachment);
        assertEquals(200, attachmentCode.getStatusLine().getStatusCode());
        EntityUtils.consume(attachmentCode.getEntity());
    }
    // check if the file exists
    ForumManager fm = ForumManager.getInstance();
    VFSContainer container = fm.getMessageContainer(message.getForumKey(), message.getKey());
    VFSItem uploadedFile = container.resolve("portrait64.jpg");
    assertNotNull(uploadedFile);
    assertTrue(uploadedFile instanceof VFSLeaf);
    // check if the image is still an image
    VFSLeaf uploadedImage = (VFSLeaf) uploadedFile;
    InputStream uploadedStream = uploadedImage.getInputStream();
    BufferedImage image = ImageIO.read(uploadedStream);
    FileUtils.closeSafely(uploadedStream);
    assertNotNull(image);
    // check if the single page exists
    VFSItem uploadedPage = container.resolve("singlepage64.html");
    assertNotNull(uploadedPage);
    assertTrue(uploadedPage instanceof VFSLeaf);
    conn.shutdown();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputStream(java.io.InputStream) File64VO(org.olat.restapi.support.vo.File64VO) HttpGet(org.apache.http.client.methods.HttpGet) VFSContainer(org.olat.core.util.vfs.VFSContainer) HttpResponse(org.apache.http.HttpResponse) VFSItem(org.olat.core.util.vfs.VFSItem) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) BufferedImage(java.awt.image.BufferedImage) ForumManager(org.olat.modules.fo.manager.ForumManager) ReplyVO(org.olat.modules.fo.restapi.ReplyVO) FileVO(org.olat.restapi.support.vo.FileVO) MessageVO(org.olat.modules.fo.restapi.MessageVO) Test(org.junit.Test)

Example 9 with ForumManager

use of org.olat.modules.fo.manager.ForumManager in project OpenOLAT by OpenOLAT.

the class ForumNodeForumCallback method saveMultiForums.

private Forum saveMultiForums(final CourseEnvironment courseEnv) {
    final ForumManager fom = CoreSpringFactory.getImpl(ForumManager.class);
    final OLATResourceable courseNodeResourceable = OresHelper.createOLATResourceableInstance(FOCourseNode.class, new Long(getIdent()));
    return CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(courseNodeResourceable, new SyncerCallback<Forum>() {

        @Override
        public Forum execute() {
            List<Property> forumKeyProps = courseEnv.getCoursePropertyManager().findCourseNodeProperties(FOCourseNode.this, null, null, FORUM_KEY);
            Forum masterForum;
            if (forumKeyProps.size() == 1) {
                masterForum = loadForum(courseEnv, forumKeyProps.get(0));
            } else if (forumKeyProps.size() > 1) {
                Long masterForumKey = forumKeyProps.get(0).getLongValue();
                List<Long> forumsToMerge = new ArrayList<>();
                for (int i = 1; i < forumKeyProps.size(); i++) {
                    forumsToMerge.add(forumKeyProps.get(i).getLongValue());
                }
                fom.mergeForums(masterForumKey, forumsToMerge);
                masterForum = fom.loadForum(masterForumKey);
                for (int i = 1; i < forumKeyProps.size(); i++) {
                    courseEnv.getCoursePropertyManager().deleteProperty(forumKeyProps.get(i));
                }
            } else {
                masterForum = null;
            }
            return masterForum;
        }
    });
}
Also used : ForumManager(org.olat.modules.fo.manager.ForumManager) OLATResourceable(org.olat.core.id.OLATResourceable) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Forum(org.olat.modules.fo.Forum)

Example 10 with ForumManager

use of org.olat.modules.fo.manager.ForumManager in project OpenOLAT by OpenOLAT.

the class ForumNodeForumCallback method loadForum.

private Forum loadForum(CourseEnvironment courseEnv, Property prop) {
    final ForumManager fom = CoreSpringFactory.getImpl(ForumManager.class);
    Long forumKey = prop.getLongValue();
    Forum forum = fom.loadForum(forumKey);
    if (forum == null) {
        throw new OLATRuntimeException(FOCourseNode.class, "Tried to load forum with key " + forumKey.longValue() + " in course " + courseEnv.getCourseResourceableId() + " for node " + getIdent() + " as defined in course node property but forum manager could not load forum.", null);
    }
    return forum;
}
Also used : ForumManager(org.olat.modules.fo.manager.ForumManager) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) Forum(org.olat.modules.fo.Forum)

Aggregations

ForumManager (org.olat.modules.fo.manager.ForumManager)38 Forum (org.olat.modules.fo.Forum)20 Property (org.olat.properties.Property)14 URI (java.net.URI)12 HttpResponse (org.apache.http.HttpResponse)12 HttpPut (org.apache.http.client.methods.HttpPut)12 Test (org.junit.Test)12 VFSContainer (org.olat.core.util.vfs.VFSContainer)12 Message (org.olat.modules.fo.Message)12 MessageVO (org.olat.modules.fo.restapi.MessageVO)12 VFSItem (org.olat.core.util.vfs.VFSItem)10 BufferedImage (java.awt.image.BufferedImage)8 InputStream (java.io.InputStream)8 OLATResourceable (org.olat.core.id.OLATResourceable)8 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)8 NarrowedPropertyManager (org.olat.properties.NarrowedPropertyManager)8 AssertException (org.olat.core.logging.AssertException)6 CoursePropertyManager (org.olat.course.properties.CoursePropertyManager)6 File (java.io.File)4 ArrayList (java.util.ArrayList)4