Search in sources :

Example 51 with Topic

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);
    }
}
Also used : Post(it.vige.rubia.model.Post) Topic(it.vige.rubia.model.Topic) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException) Forum(it.vige.rubia.model.Forum)

Example 52 with Topic

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;
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date) LinkedList(java.util.LinkedList) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException) Topic(it.vige.rubia.model.Topic)

Example 53 with Topic

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);
    }
}
Also used : Post(it.vige.rubia.model.Post) PollOption(it.vige.rubia.model.PollOption) Attachment(it.vige.rubia.model.Attachment) Topic(it.vige.rubia.model.Topic) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException)

Example 54 with Topic

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);
    }
}
Also used : Topic(it.vige.rubia.model.Topic) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException) Forum(it.vige.rubia.model.Forum)

Example 55 with Topic

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;
}
Also used : Post(it.vige.rubia.model.Post) Topic(it.vige.rubia.model.Topic) JSFUtil.handleException(it.vige.rubia.ui.JSFUtil.handleException) Interceptors(javax.interceptor.Interceptors) SecureActionForum(it.vige.rubia.auth.SecureActionForum)

Aggregations

Topic (it.vige.rubia.model.Topic)112 CreateTopic.createTopic (it.vige.rubia.selenium.forum.action.CreateTopic.createTopic)64 Forum (it.vige.rubia.model.Forum)63 Post (it.vige.rubia.model.Post)62 CreateForum.createForum (it.vige.rubia.selenium.adminpanel.action.CreateForum.createForum)49 RemoveTopic.removeTopic (it.vige.rubia.selenium.forum.action.RemoveTopic.removeTopic)48 RemoveForum.removeForum (it.vige.rubia.selenium.adminpanel.action.RemoveForum.removeForum)39 Category (it.vige.rubia.model.Category)36 Test (org.junit.Test)36 CreateCategory.createCategory (it.vige.rubia.selenium.adminpanel.action.CreateCategory.createCategory)30 RemoveCategory.removeCategory (it.vige.rubia.selenium.adminpanel.action.RemoveCategory.removeCategory)30 Poll (it.vige.rubia.model.Poll)25 PollOption (it.vige.rubia.model.PollOption)24 Attachment (it.vige.rubia.model.Attachment)23 Poster (it.vige.rubia.model.Poster)17 CreatePost.createPost (it.vige.rubia.selenium.forum.action.CreatePost.createPost)16 WebElement (org.openqa.selenium.WebElement)16 SecureActionForum (it.vige.rubia.auth.SecureActionForum)14 Date (java.util.Date)14 Interceptors (javax.interceptor.Interceptors)13