Search in sources :

Example 21 with ForumManager

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

the class GroupForumIndexer method doIndex.

/**
 * Index a forum in a group.
 * @param parentResourceContext
 * @param businessGroup
 * @param indexWriter
 * @throws IOException
 */
@Override
public void doIndex(SearchResourceContext parentResourceContext, Object businessObj, OlatFullIndexer indexWriter) throws IOException, InterruptedException {
    if (!(businessObj instanceof BusinessGroup))
        throw new AssertException("businessObj must be BusinessGroup");
    BusinessGroup businessGroup = (BusinessGroup) businessObj;
    NarrowedPropertyManager npm = NarrowedPropertyManager.getInstance(businessGroup);
    ForumManager fom = ForumManager.getInstance();
    Property forumKeyProperty = npm.findProperty(null, null, CollaborationTools.PROP_CAT_BG_COLLABTOOLS, CollaborationTools.KEY_FORUM);
    // Check if forum-property exist
    if (forumKeyProperty != null) {
        Long forumKey = forumKeyProperty.getLongValue();
        Forum forum = fom.loadForum(forumKey);
        SearchResourceContext forumSearchResourceContext = new SearchResourceContext(parentResourceContext);
        forumSearchResourceContext.setBusinessControlFor(BusinessGroupMainRunController.ORES_TOOLFORUM);
        forumSearchResourceContext.setDocumentType(TYPE);
        forumSearchResourceContext.setParentContextType(GroupDocument.TYPE);
        forumSearchResourceContext.setParentContextName(businessGroup.getName());
        if (forum == null) {
            // fxdiff: FXOLAT-104 warn about missing forums
            logError("found a forum-key " + forumKey + " for businessgroup " + businessGroup.getName() + " [" + businessGroup.getKey() + "] to index a forum that could not be " + "found by key! skip indexing, check if forum should still be enabled. context: " + forumSearchResourceContext.getResourceUrl(), null);
            return;
        }
        doIndexAllMessages(forumSearchResourceContext, forum, indexWriter);
    }
}
Also used : AssertException(org.olat.core.logging.AssertException) BusinessGroup(org.olat.group.BusinessGroup) ForumManager(org.olat.modules.fo.manager.ForumManager) SearchResourceContext(org.olat.search.service.SearchResourceContext) NarrowedPropertyManager(org.olat.properties.NarrowedPropertyManager) Property(org.olat.properties.Property) Forum(org.olat.modules.fo.Forum)

Example 22 with ForumManager

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

the class ForumCourseNodeWebService method addMessage.

/**
 * Internal helper method to add a message to a forum.
 * @param courseId
 * @param nodeId
 * @param parentMessageId can be null (will lead to new thread)
 * @param title
 * @param body
 * @param identityName
 * @param isSticky only necessary when adding new thread
 * @param request
 * @return
 */
private Response addMessage(Long courseId, String nodeId, Long parentMessageId, String title, String body, String identityName, Boolean isSticky, HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    BaseSecurity securityManager = BaseSecurityManager.getInstance();
    Identity identity;
    if (identityName != null) {
        identity = securityManager.findIdentityByName(identityName);
    } else {
        identity = RestSecurityHelper.getIdentity(request);
    }
    if (identity == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    // load forum
    ICourse course = CoursesWebService.loadCourse(courseId);
    if (course == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    } else if (!isAuthorEditor(course, request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    CourseNode courseNode = getParentNode(course, nodeId);
    if (courseNode == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    CoursePropertyManager cpm = course.getCourseEnvironment().getCoursePropertyManager();
    Property forumKeyProp = cpm.findCourseNodeProperty(courseNode, null, null, FOCourseNode.FORUM_KEY);
    Forum forum = null;
    ForumManager fom = ForumManager.getInstance();
    if (forumKeyProp != null) {
        // Forum does already exist, load forum with key from properties
        Long forumKey = forumKeyProp.getLongValue();
        forum = fom.loadForum(forumKey);
    }
    if (forum == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    MessageVO vo;
    if (parentMessageId == null || parentMessageId == 0L) {
        // creating the thread (a message without a parent message)
        Message newThread = fom.createMessage(forum, identity, false);
        if (isSticky != null && isSticky.booleanValue()) {
            // set sticky
            org.olat.modules.fo.Status status = new org.olat.modules.fo.Status();
            status.setSticky(true);
            newThread.setStatusCode(org.olat.modules.fo.Status.getStatusCode(status));
        }
        newThread.setTitle(title);
        newThread.setBody(body);
        // open a new thread
        fom.addTopMessage(newThread);
        vo = new MessageVO(newThread);
    } else {
        // adding response message (a message with a parent message)
        Message threadMessage = fom.loadMessage(parentMessageId);
        if (threadMessage == null) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
        // create new message
        Message message = fom.createMessage(forum, identity, false);
        message.setTitle(title);
        message.setBody(body);
        fom.replyToMessage(message, threadMessage);
        vo = new MessageVO(message);
    }
    return Response.ok(vo).build();
}
Also used : Status(javax.ws.rs.core.Response.Status) Message(org.olat.modules.fo.Message) ICourse(org.olat.course.ICourse) BaseSecurity(org.olat.basesecurity.BaseSecurity) Forum(org.olat.modules.fo.Forum) ForumManager(org.olat.modules.fo.manager.ForumManager) FOCourseNode(org.olat.course.nodes.FOCourseNode) CourseNode(org.olat.course.nodes.CourseNode) Identity(org.olat.core.id.Identity) Property(org.olat.properties.Property) CoursePropertyManager(org.olat.course.properties.CoursePropertyManager)

Example 23 with ForumManager

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

the class ForumTest method testNewThread.

@Test
public void testNewThread() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    URI uri = getForumUriBuilder().path("threads").queryParam("authorKey", id1.getKey()).queryParam("title", "New thread").queryParam("body", "A very interesting thread").build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    MessageVO thread = conn.parse(response, MessageVO.class);
    assertNotNull(thread);
    assertNotNull(thread.getKey());
    assertEquals(thread.getForumKey(), forum.getKey());
    assertEquals(thread.getAuthorKey(), id1.getKey());
    // really saved?
    boolean saved = false;
    ForumManager fm = ForumManager.getInstance();
    List<Message> allMessages = fm.getMessagesByForum(forum);
    for (Message message : allMessages) {
        if (message.getKey().equals(thread.getKey())) {
            saved = true;
        }
    }
    assertTrue(saved);
    conn.shutdown();
}
Also used : Message(org.olat.modules.fo.Message) ForumManager(org.olat.modules.fo.manager.ForumManager) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) MessageVO(org.olat.modules.fo.restapi.MessageVO) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 24 with ForumManager

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

the class ForumTest method testNewMessage.

@Test
public void testNewMessage() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).queryParam("authorKey", id1.getKey()).queryParam("title", "New message").queryParam("body", "A very interesting response in Thread-1").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);
    assertNotNull(message.getKey());
    assertEquals(message.getForumKey(), forum.getKey());
    assertEquals(message.getAuthorKey(), id1.getKey());
    assertEquals(message.getParentKey(), m1.getKey());
    // really saved?
    boolean saved = false;
    ForumManager fm = ForumManager.getInstance();
    List<Message> allMessages = fm.getMessagesByForum(forum);
    for (Message msg : allMessages) {
        if (msg.getKey().equals(message.getKey())) {
            saved = true;
        }
    }
    assertTrue(saved);
    conn.shutdown();
}
Also used : Message(org.olat.modules.fo.Message) ForumManager(org.olat.modules.fo.manager.ForumManager) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) MessageVO(org.olat.modules.fo.restapi.MessageVO) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 25 with ForumManager

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

the class ForumTest method testUpload64Attachment.

@Test
public void testUpload64Attachment() 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();
    byte[] portraitBytes = IOUtils.toByteArray(portraitStream);
    byte[] portrait64 = Base64.encodeBase64(portraitBytes, true);
    HttpPost attachMethod = conn.createPost(attachUri, MediaType.APPLICATION_JSON);
    attachMethod.addHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    conn.addEntity(attachMethod, new BasicNameValuePair("file", new String(portrait64)), new BasicNameValuePair("filename", "portrait64.jpg"));
    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("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);
    conn.shutdown();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputStream(java.io.InputStream) 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) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) MessageVO(org.olat.modules.fo.restapi.MessageVO) Test(org.junit.Test)

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