use of org.olat.modules.fo.Message in project openolat by klemens.
the class ForumManager method getForumThreads.
public List<ForumThread> getForumThreads(Forum forum, Identity identity) {
StringBuilder sb = new StringBuilder();
sb.append("select msg ").append(" , (select count(replies.key) from fomessage as replies").append(" where replies.threadtop.key=msg.key and replies.forum.key=:forumKey").append(" ) as numOfMessages").append(" , (select max(replies.lastModified) from fomessage as replies").append(" where replies.threadtop.key=msg.key and replies.forum.key=:forumKey").append(" ) as lastModified");
if (identity != null) {
sb.append(" , (select count(read.key) from foreadmessage as read, fomessage as posts").append(" where (posts.threadtop.key=msg.key or posts.key=msg.key) and read.message.key=posts.key and read.identity.key=:identityKey").append(" ) as numOfReadMessages").append(" ,(select count(mark.key) from ").append(MarkImpl.class.getName()).append(" as mark, fomessage as mposts ").append(" where mark.creator.key=:identityKey and mark.resId=:forumKey and (mposts.threadtop.key=msg.key or mposts.key=msg.key)").append(" and mposts.key=cast(mark.resSubPath as long) and mark.resName='Forum'").append(" ) as marks");
}
sb.append(" from fomessage as msg ").append(" left join fetch msg.creator as creator").append(" where msg.forum.key=:forumKey and msg.threadtop is null");
TypedQuery<Object[]> objectsQuery = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Object[].class).setParameter("forumKey", forum.getKey());
if (identity != null) {
objectsQuery.setParameter("identityKey", identity.getKey());
}
List<Object[]> objects = objectsQuery.getResultList();
List<ForumThread> threadList = new ArrayList<>(objects.size());
for (Object[] object : objects) {
Message msg = (Message) object[0];
Number numOfMessagesLong = (Number) object[1];
Date lastModified = (Date) object[2];
int numOfMessages = numOfMessagesLong == null ? 1 : numOfMessagesLong.intValue() + 1;
String creator = userManager.getUserDisplayName(msg.getCreator());
ForumThread thread = new ForumThread(msg, creator, lastModified, numOfMessages);
if (identity != null) {
Number readMessages = (Number) object[3];
int numOfReadMessages = readMessages == null ? 0 : readMessages.intValue();
thread.setNewMessages(numOfMessages - numOfReadMessages);
Number numOfMarkedMessagesLong = (Number) object[4];
int numOfMarkedMessages = numOfMarkedMessagesLong == null ? 0 : numOfMarkedMessagesLong.intValue();
thread.setMarkedMessages(numOfMarkedMessages);
}
threadList.add(thread);
}
return threadList;
}
use of org.olat.modules.fo.Message in project openolat by klemens.
the class ForumManager method createOrAppendThreadInAnotherForum.
/**
* Creates new thread in another forum or appends selected thread to another thread in another forum.
*
* @param msg the OLD parent message
* @param the destination forum
* @param topMsg the top message
* @return the message
*/
public Message createOrAppendThreadInAnotherForum(Message msg, Forum forum, Message topMsg) {
Message oldMessage = getMessageById(msg.getKey());
Message message = persistMessageInAnotherLocation(oldMessage, forum, topMsg, topMsg);
// reload message from database
message = getMessageById(message.getKey());
moveMessageContainer(oldMessage.getForum().getKey(), oldMessage.getKey(), message.getForum().getKey(), message.getKey());
if (hasChildren(oldMessage)) {
if (topMsg != null) {
// if added to an existing thread choose its top message
message = moveThreadToAnotherForumRecursively(oldMessage, message, message.getThreadtop());
} else {
// if a new thread is created in a forum the parent message is also the top message
message = moveThreadToAnotherForumRecursively(oldMessage, message, message);
}
}
// deletes all children of the old top message recursively
deleteMessageRecursion(oldMessage.getForum().getKey(), oldMessage);
return message;
}
use of org.olat.modules.fo.Message in project openolat by klemens.
the class ForumManager method getSubthread.
/**
* This is a recursive method. The subthreadList in an ordered list with all descendents of the input msg.
* @param msg
* @param threadList
* @param subthreadList
*/
private void getSubthread(Message msg, List<Message> threadList, List<Message> subthreadList) {
Iterator<Message> listIterator = threadList.iterator();
while (listIterator.hasNext()) {
Message currMessage = listIterator.next();
if (currMessage.getParent() != null && currMessage.getParent().getKey().equals(msg.getKey())) {
subthreadList.add(currMessage);
getSubthread(currMessage, threadList, subthreadList);
}
}
}
use of org.olat.modules.fo.Message in project openolat by klemens.
the class ForumManager method moveMessageToAnotherForum.
/**
* Move single message to another forum.
*
* @param msg the OLD parent message
* @param topMsg the NEW top message
* @return the message
*/
public Message moveMessageToAnotherForum(Message msg, Forum forum, Message topMsg) {
Message targetThread = null;
if (topMsg != null) {
targetThread = topMsg.getThreadtop();
if (targetThread == null) {
targetThread = topMsg;
}
targetThread = getMessageById(targetThread.getKey());
}
final Message oldParent = getMessageById(msg.getKey());
// one has to set a new parent for all children of the moved message
Message newParent = persistMessageInAnotherLocation(oldParent, forum, targetThread, topMsg);
moveMessageContainer(oldParent.getForum().getKey(), oldParent.getKey(), newParent.getForum().getKey(), newParent.getKey());
targetThread = targetThread == null ? newParent : targetThread;
if (hasChildren(oldParent)) {
moveThreadToAnotherForumRecursively(oldParent, newParent, targetThread);
}
deleteMessageRecursion(oldParent.getForum().getKey(), oldParent);
return newParent;
}
use of org.olat.modules.fo.Message in project openolat by klemens.
the class ForumManager method persistMessageInAnotherLocation.
/**
* Persist message in another location.
*/
private Message persistMessageInAnotherLocation(Message oldMessage, Forum forum, Message top, Message parent) {
// 1) take the new top messages forum to create a new child
Message message = createMessage(forum, oldMessage.getCreator(), oldMessage.isGuest());
((MessageImpl) message).setCreationDate(oldMessage.getCreationDate());
message.setLastModified(oldMessage.getLastModified());
message.setModifier(oldMessage.getModifier());
message.setTitle(oldMessage.getTitle());
message.setBody(oldMessage.getBody());
message.setPseudonym(oldMessage.getPseudonym());
// 2) set the thread top to the new top message
message.setThreadtop(top);
// 3) maintain the hierarchy, parent and top message can be equal
message.setParent(parent);
Status status = Status.getStatus(oldMessage.getStatusCode());
if (status != null) {
status.setMoved(true);
message.setStatusCode(Status.getStatusCode(status));
}
// 4) save the new massage in the new destination
message = saveMessage(message);
return message;
}
Aggregations