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();
}
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();
}
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();
}
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();
}
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);
}
Aggregations