Search in sources :

Example 31 with ForumManager

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

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 32 with ForumManager

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

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)

Example 33 with ForumManager

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

the class GroupMgmtTest method setUp.

/**
 * Set up a course with learn group and group area
 * @see org.olat.test.OlatJerseyTestCase#setUp()
 */
@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    conn = new RestConnection();
    // create a course with learn group
    owner1 = JunitTestHelper.createAndPersistIdentityAsUser("rest-one");
    owner2 = JunitTestHelper.createAndPersistIdentityAsUser("rest-two");
    owner3 = JunitTestHelper.createAndPersistIdentityAsUser("rest-three");
    part1 = JunitTestHelper.createAndPersistIdentityAsUser("rest-four");
    part2 = JunitTestHelper.createAndPersistIdentityAsUser("rest-five");
    part3 = JunitTestHelper.createAndPersistIdentityAsUser("rest-six");
    OLATResourceManager rm = OLATResourceManager.getInstance();
    // create course and persist as OLATResourceImpl
    OLATResourceable resourceable = OresHelper.createOLATResourceableInstance("junitcourse", System.currentTimeMillis());
    course = rm.findOrPersistResourceable(resourceable);
    RepositoryService rs = CoreSpringFactory.getImpl(RepositoryService.class);
    RepositoryEntry re = rs.create("administrator", "-", "rest-re", null, course);
    DBFactory.getInstance().commit();
    assertNotNull(re);
    // create learn group
    // 1) context one: learning groups
    RepositoryEntry c1 = JunitTestHelper.createAndPersistRepositoryEntry();
    // create groups without waiting list
    g1 = businessGroupService.createBusinessGroup(null, "rest-g1", null, 0, 10, false, false, c1);
    g2 = businessGroupService.createBusinessGroup(null, "rest-g2", null, 0, 10, false, false, c1);
    DBFactory.getInstance().commit();
    // permission to see owners and participants
    businessGroupService.updateDisplayMembers(g1, false, false, false, false, false, false, false);
    businessGroupService.updateDisplayMembers(g2, true, true, false, false, false, false, false);
    // members g1
    businessGroupRelationDao.addRole(owner1, g1, GroupRoles.coach.name());
    businessGroupRelationDao.addRole(owner2, g1, GroupRoles.coach.name());
    businessGroupRelationDao.addRole(part1, g1, GroupRoles.participant.name());
    businessGroupRelationDao.addRole(part2, g1, GroupRoles.participant.name());
    // members g2
    businessGroupRelationDao.addRole(owner1, g2, GroupRoles.coach.name());
    businessGroupRelationDao.addRole(part1, g2, GroupRoles.participant.name());
    // 2) context two: right groups
    RepositoryEntry c2 = JunitTestHelper.createAndPersistRepositoryEntry();
    // groups
    g3 = businessGroupService.createBusinessGroup(null, "rest-g3", null, -1, -1, false, false, c2);
    g4 = businessGroupService.createBusinessGroup(null, "rest-g4", null, -1, -1, false, false, c2);
    DBFactory.getInstance().commit();
    // members
    businessGroupRelationDao.addRole(owner1, g3, GroupRoles.participant.name());
    businessGroupRelationDao.addRole(owner2, g4, GroupRoles.participant.name());
    // 3) collaboration tools
    CollaborationTools collabTools1 = CollaborationToolsFactory.getInstance().getOrCreateCollaborationTools(g1);
    collabTools1.setToolEnabled(CollaborationTools.TOOL_FORUM, true);
    collabTools1.setToolEnabled(CollaborationTools.TOOL_WIKI, true);
    collabTools1.saveNews("<p>Hello world</p>");
    try {
        collabTools1.createForumController(null, null, true, false, null);
    } catch (Exception e) {
    // will fail but generate the forum key
    }
    CollaborationTools collabTools2 = CollaborationToolsFactory.getInstance().getOrCreateCollaborationTools(g2);
    collabTools2.setToolEnabled(CollaborationTools.TOOL_FORUM, true);
    // simulate user clicks
    DBFactory.getInstance().closeSession();
    // 4) fill forum for g1
    NarrowedPropertyManager npm = NarrowedPropertyManager.getInstance(g1);
    Property forumKeyProperty = npm.findProperty(null, null, CollaborationTools.PROP_CAT_BG_COLLABTOOLS, CollaborationTools.KEY_FORUM);
    ForumManager fm = ForumManager.getInstance();
    Forum forum = fm.loadForum(forumKeyProperty.getLongValue());
    m1 = fm.createMessage(forum, owner1, false);
    m1.setTitle("Thread-1");
    m1.setBody("Body of Thread-1");
    fm.addTopMessage(m1);
    m2 = fm.createMessage(forum, owner2, false);
    m2.setTitle("Thread-2");
    m2.setBody("Body of Thread-2");
    fm.addTopMessage(m2);
    DBFactory.getInstance().intermediateCommit();
    m3 = fm.createMessage(forum, owner3, false);
    m3.setTitle("Message-1.1");
    m3.setBody("Body of Message-1.1");
    fm.replyToMessage(m3, m1);
    m4 = fm.createMessage(forum, part1, false);
    m4.setTitle("Message-1.1.1");
    m4.setBody("Body of Message-1.1.1");
    fm.replyToMessage(m4, m3);
    m5 = fm.createMessage(forum, part2, false);
    m5.setTitle("Message-1.2");
    m5.setBody("Body of Message-1.2");
    fm.replyToMessage(m5, m1);
    DBFactory.getInstance().intermediateCommit();
}
Also used : OLATResourceable(org.olat.core.id.OLATResourceable) ForumManager(org.olat.modules.fo.manager.ForumManager) CollaborationTools(org.olat.collaboration.CollaborationTools) OLATResourceManager(org.olat.resource.OLATResourceManager) RepositoryEntry(org.olat.repository.RepositoryEntry) NarrowedPropertyManager(org.olat.properties.NarrowedPropertyManager) Property(org.olat.properties.Property) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) RepositoryService(org.olat.repository.RepositoryService) Forum(org.olat.modules.fo.Forum) Before(org.junit.Before)

Example 34 with ForumManager

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

the class NotificationsTest method createMessage.

private Message createMessage(Identity id, Forum fo) {
    ForumManager fm = ForumManager.getInstance();
    Message m1 = fm.createMessage(fo, id, false);
    m1.setTitle("Thread-1");
    m1.setBody("Body of Thread-1");
    fm.addTopMessage(m1);
    return m1;
}
Also used : Message(org.olat.modules.fo.Message) ForumManager(org.olat.modules.fo.manager.ForumManager)

Example 35 with ForumManager

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

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)

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