Search in sources :

Example 46 with Message

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

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 47 with Message

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

the class ForumWebService method getAttachment.

/**
 * Retrieves the attachment of the message
 * @response.representation.200.mediaType application/octet-stream
 * @response.representation.200.doc The portrait as image
 * @response.representation.404.doc The identity or the portrait not found
 * @param messageKey The identity key of the user being searched
 * @param filename The name of the attachment
 * @param request The REST request
 * @return The attachment
 */
@GET
@Path("posts/{messageKey}/attachments/{filename}")
@Produces({ "*/*", MediaType.APPLICATION_OCTET_STREAM })
public Response getAttachment(@PathParam("messageKey") Long messageKey, @PathParam("filename") String filename, @Context Request request) {
    // load message
    Message mess = fom.loadMessage(messageKey);
    if (mess == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!forum.equalsByPersistableKey(mess.getForum())) {
        return Response.serverError().status(Status.CONFLICT).build();
    }
    VFSContainer container = fom.getMessageContainer(mess.getForum().getKey(), mess.getKey());
    VFSItem item = container.resolve(filename);
    if (item instanceof LocalFileImpl) {
        // local file -> the length is given to the client which is good
        Date lastModified = new Date(item.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            File attachment = ((LocalFileImpl) item).getBasefile();
            String mimeType = WebappHelper.getMimeType(attachment.getName());
            if (mimeType == null)
                mimeType = "application/octet-stream";
            response = Response.ok(attachment).lastModified(lastModified).type(mimeType).cacheControl(cc);
        }
        return response.build();
    } else if (item instanceof VFSLeaf) {
        // stream -> the length is not given to the client which is not nice
        Date lastModified = new Date(item.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            StreamingOutput attachment = new VFSStreamingOutput((VFSLeaf) item);
            String mimeType = WebappHelper.getMimeType(item.getName());
            if (mimeType == null)
                mimeType = "application/octet-stream";
            response = Response.ok(attachment).lastModified(lastModified).type(mimeType).cacheControl(cc);
        }
        return response.build();
    }
    return Response.serverError().status(Status.NOT_FOUND).build();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Message(org.olat.modules.fo.Message) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) VFSStreamingOutput(org.olat.core.util.vfs.restapi.VFSStreamingOutput) VFSStreamingOutput(org.olat.core.util.vfs.restapi.VFSStreamingOutput) StreamingOutput(javax.ws.rs.core.StreamingOutput) Date(java.util.Date) Response(javax.ws.rs.core.Response) File(java.io.File) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 48 with Message

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

the class ForumWebService method newThreadToForum.

/**
 * Creates a new thread in the forum of the course node
 * @response.representation.200.qname {http://www.example.com}messageVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The root message of the thread
 * @response.representation.200.example {@link org.olat.modules.fo.restapi.Examples#SAMPLE_MESSAGEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The author, forum or message not found
 * @param title The title for the first post in the thread
 * @param body The body for the first post in the thread
 * @param authorKey The author user key (optional)
 * @param httpRequest The HTTP request
 * @return The new thread
 */
@PUT
@Path("threads")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response newThreadToForum(@QueryParam("title") String title, @QueryParam("body") String body, @QueryParam("authorKey") Long authorKey, @Context HttpServletRequest httpRequest) {
    Identity author = getMessageAuthor(authorKey, httpRequest);
    // creating the thread (a message without a parent message)
    Message newThread = fom.createMessage(forum, author, false);
    newThread.setTitle(title);
    newThread.setBody(body);
    // open a new thread
    fom.addTopMessage(newThread);
    MessageVO vo = new MessageVO(newThread);
    return Response.ok(vo).build();
}
Also used : Message(org.olat.modules.fo.Message) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 49 with Message

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

the class ThreadListController method doNewThread.

private void doNewThread(UserRequest ureq) {
    removeAsListenerAndDispose(newThreadCtrl);
    removeAsListenerAndDispose(cmc);
    // user has clicked on button 'open new thread'.
    Message m = forumManager.createMessage(forum, getIdentity(), guestOnly);
    newThreadCtrl = new MessageEditController(ureq, getWindowControl(), forum, foCallback, m, null, EditMode.newThread);
    listenTo(newThreadCtrl);
    String title = translate("msg.create");
    cmc = new CloseableModalController(getWindowControl(), "close", newThreadCtrl.getInitialComponent(), true, title);
    listenTo(newThreadCtrl);
    cmc.activate();
}
Also used : Message(org.olat.modules.fo.Message) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController)

Example 50 with Message

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

the class ForumManager method doDeleteForum.

/**
 * deletes all messages belonging to this forum and the forum entry itself
 *
 * @param forum
 */
private void doDeleteForum(final Forum forum) {
    final Long forumKey = forum.getKey();
    // delete read messsages
    String deleteReadMessages = "delete from foreadmessage as rmsg where rmsg.forum.key=:forumKey";
    dbInstance.getCurrentEntityManager().createQuery(deleteReadMessages).setParameter("forumKey", forumKey).executeUpdate();
    // delete messages
    String messagesToDelete = "select msg from fomessage as msg where msg.forum.key=:forumKey and msg.threadtop.key is null";
    List<Message> threadsToDelete = dbInstance.getCurrentEntityManager().createQuery(messagesToDelete, Message.class).setParameter("forumKey", forumKey).getResultList();
    for (Message threadToDelete : threadsToDelete) {
        deleteMessageTree(forumKey, threadToDelete);
        dbInstance.getCurrentEntityManager().remove(threadToDelete);
    }
    dbInstance.commit();
    // delete forum
    String deleteForum = "delete from forum as fo where fo.key=:forumKey";
    dbInstance.getCurrentEntityManager().createQuery(deleteForum).setParameter("forumKey", forumKey).executeUpdate();
    // delete all flags
    OLATResourceable ores = OresHelper.createOLATResourceableInstance(Forum.class, forum.getKey());
    markingService.getMarkManager().deleteMarks(ores);
}
Also used : Message(org.olat.modules.fo.Message) OLATResourceable(org.olat.core.id.OLATResourceable)

Aggregations

Message (org.olat.modules.fo.Message)108 Identity (org.olat.core.id.Identity)18 OLATResourceable (org.olat.core.id.OLATResourceable)16 ErrorEditMessage (org.olat.modules.fo.ui.events.ErrorEditMessage)16 Forum (org.olat.modules.fo.Forum)14 ForumManager (org.olat.modules.fo.manager.ForumManager)12 Date (java.util.Date)8 HttpResponse (org.apache.http.HttpResponse)8 Test (org.junit.Test)8 VFSContainer (org.olat.core.util.vfs.VFSContainer)8 ICourse (org.olat.course.ICourse)8 FOCourseNode (org.olat.course.nodes.FOCourseNode)8 RepositoryEntry (org.olat.repository.RepositoryEntry)8 File (java.io.File)6 ArrayList (java.util.ArrayList)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 CloseableModalController (org.olat.core.gui.control.generic.closablewrapper.CloseableModalController)6 VFSItem (org.olat.core.util.vfs.VFSItem)6 PublisherData (org.olat.core.commons.services.notifications.PublisherData)5