use of it.vige.rubia.model.Topic in project rubia-forums by flashboss.
the class ForumsModuleImpl method removePost.
@Override
public void removePost(int postId, boolean isLastPost) throws ModuleException {
try {
Post post = em.find(Post.class, postId);
Topic topic = post.getTopic();
Forum forum = topic.getForum();
em.remove(post);
topic.setReplies(topic.getReplies() - 1);
forum.setPostCount(forum.getPostCount() - 1);
List<Post> posts = findPostsByTopicId(topic);
Post lastPost = posts.get(posts.size() - 1);
if (isLastPost) {
topic.setLastPostDate(lastPost.getCreateDate());
}
em.merge(topic);
em.merge(forum);
em.flush();
} catch (Exception e) {
String errorMessage = "Cannot delete post";
throw new ModuleException(errorMessage, e);
}
}
use of it.vige.rubia.model.Topic in project rubia-forums by flashboss.
the class ForumsModuleImpl method findLastPostsOfTopics.
@Override
public Map<Object, Object> findLastPostsOfTopics(Collection<Topic> topics) throws ModuleException {
try {
List<Object[]> lastPostDates = new ArrayList<Object[]>(topics.size());
List<Date> dates = new LinkedList<Date>();
for (Topic tmpTopic : topics) {
dates.add(tmpTopic.getLastPostDate());
lastPostDates.add(new Object[] { tmpTopic.getLastPostDate(), tmpTopic.getId() });
}
// if there are no createDates then we return an empty map
if (dates.size() == 0) {
return new HashMap<Object, Object>(0);
}
TypedQuery<Object[]> query = em.createNamedQuery("findLastPostsOfTopicsCreateDate", Object[].class);
query.setParameter("dates", dates);
List<Object[]> posts = query.getResultList();
Map<Object, Object> forumPostMap = new HashMap<Object, Object>(dates.size());
for (Object[] dateTopic : lastPostDates) {
int index = Collections.binarySearch(posts, dateTopic, new Comparator<Object>() {
public int compare(Object o1, Object o2) {
Object[] datePostPair1 = (Object[]) o1;
Object[] datePostPair2 = (Object[]) o2;
Date postDate1 = (Date) datePostPair1[0];
Date postDate2 = (Date) datePostPair2[0];
return postDate1.compareTo(postDate2);
}
});
if (index < 0) {
continue;
}
Object[] datePostPair = (Object[]) posts.get(index);
forumPostMap.put(dateTopic[1], datePostPair[1]);
}
return forumPostMap;
} catch (Exception e) {
log.error(e);
return null;
}
}
use of it.vige.rubia.model.Topic in project rubia-forums by flashboss.
the class ForumsModuleImpl method createTopic.
@Override
public Post createTopic(Forum forum, Message message, Date creationDate, Poster poster, Poll poll, Collection<Attachment> attachments, TopicType type) throws ModuleException {
try {
if (poster.getId() == null || em.find(Poster.class, poster.getId()) == null)
em.persist(poster);
em.merge(poster);
em.persist(poll);
for (PollOption pollOption : poll.getOptions()) em.persist(pollOption);
em.flush();
Post post = new Post();
post.setMessage(message);
post.setCreateDate(creationDate);
post.setPoster(poster);
if (attachments != null)
for (Attachment attachment : attachments) {
em.persist(attachment);
post.addAttachment(attachment);
}
Topic topic = new Topic();
topic.setSubject(message.getSubject());
topic.setForum(forum);
topic.setPoster(poster);
post.setTopic(topic);
topic.setLastPostDate(creationDate);
topic.setType(type);
topic.setStatus(TOPIC_UNLOCKED);
topic.setPoll(poll);
em.persist(topic);
em.persist(post);
em.flush();
forum.addTopicSize();
forum.addPostSize();
em.merge(forum);
post.setTopic(topic);
em.persist(post);
notificationEngine.scheduleForNotification(post.getId(), MODE_POST);
em.flush();
return post;
} catch (Exception e) {
String errorMessage = "Cannot create topic";
throw new ModuleException(errorMessage, e);
}
}
use of it.vige.rubia.model.Topic in project rubia-forums by flashboss.
the class ForumsModuleImpl method removeTopic.
@Override
public void removeTopic(int topicId) throws ModuleException {
try {
Topic topic = em.find(Topic.class, topicId);
Forum forum = topic.getForum();
if (forum != null) {
topic.setForum(null);
forum.setPostCount(forum.getPostCount() - topic.getReplies() - 1);
forum.setTopicCount(forum.getTopicCount() - 1);
em.merge(forum);
em.remove(em.merge(topic));
} else {
em.remove(topic);
}
em.flush();
} catch (Exception e) {
String errorMessage = "Cannot delete topic";
throw new ModuleException(errorMessage, e);
}
}
use of it.vige.rubia.model.Topic in project rubia-forums by flashboss.
the class EditPost method start.
// action processing
// methods-----------------------------------------------------------------------------------------------------
/**
* @return the navigation state of the application
*/
@SecureActionForum
@Interceptors(AuthorizationListener.class)
public String start() {
String navState = null;
try {
int postId = -1;
String p = getParameter(p_postId);
if (p != null && p.trim().length() > 0) {
postId = parseInt(p);
}
// grab the post information
if (postId != -1) {
// re-initialize this controller to edit the specified post
cleanup();
// get the post from the module
Post post = forumsModule.findPostById(postId);
Topic topic = post.getTopic();
// set the selected post's topic id
topicId = topic.getId().intValue();
this.postId = postId;
// set the subject of the post
subject = post.getMessage().getSubject();
// set the message of the post
message = post.getMessage().getText();
// set the topicType
topicType = topic.getType();
// setup poll related information
setupPoll(topic.getPoll());
attachments = forumsModule.findAttachments(post);
// setup the attachment related information
List<Post> posts = forumsModule.findPostsByTopicId(topic);
isFirstPost = false;
if (posts.get(0).getId().intValue() == post.getId().intValue()) {
isFirstPost = true;
}
}
navState = START_EDIT_POST;
} catch (Exception e) {
handleException(e);
}
return navState;
}
Aggregations