Search in sources :

Example 16 with PollOption

use of it.vige.rubia.model.PollOption 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 17 with PollOption

use of it.vige.rubia.model.PollOption in project rubia-forums by flashboss.

the class EditPost method execute.

/**
 * @return the navigation state of the application
 */
@SecureActionForum
@Interceptors(AuthorizationListener.class)
public String execute() {
    String navState = null;
    boolean success = false;
    try {
        // setup the business objects to be updated
        Post post = forumsModule.findPostById(postId);
        forumsModule.updateAttachments(attachments, post);
        // TODO: cleanup this forums update process............move this as
        // a private method
        // setup attachment information
        Topic topic = post.getTopic();
        // make sure this topic is not locked
        if (topic.getStatus() == TOPIC_LOCKED) {
            // should not allow posting a reply since the topic is locked
            throw new Exception(getBundleMessage(BUNDLE_NAME, TOPIC_LOCKED_ERR_KEY));
        }
        // setup the message/subject related data
        Message message = createMessage();
        message.setText(this.message);
        message.setSubject(subject);
        // update the message/subject/topicType data on the business objects
        post.setMessage(message);
        if (isFirstPost) {
            topic.setSubject(subject);
            topic.setType(topicType);
        }
        // miscellaneous post related update
        post.setEditCount(post.getEditCount() + 1);
        post.setEditDate(new Date());
        // TODO: cleanup this poll update process............move this as a
        // private method
        // setup poll information
        List<PollOption> localPollOptions = new LinkedList<PollOption>();
        for (String key : options.keySet()) {
            PollOption pollOption = createPollOption(topic.getPoll());
            pollOption.setQuestion(options.get(key));
            pollOption.setVotes(0);
            localPollOptions.add(pollOption);
        }
        // update poll information
        if (topic.getPoll() == null || topic.getPoll().getTitle() == null || topic.getPoll().getTitle().trim().length() == 0) {
            // no existing poll information found in the database
            if (localPollOptions.size() > 0 && question != null && question.trim().length() > 0) {
                // need to add a new poll to this topic
                Poll poll = createPoll();
                poll.setTitle(question);
                poll.setLength(activeDuration);
                poll.setOptions(localPollOptions);
                validatePoll(poll);
                forumsModule.addPollToTopic(topic, poll);
            }
        } else {
            // existing poll information is available in the database
            if (localPollOptions.size() > 0) {
                // this is a diff update..............................
                // setup the poll to be updated in the database
                Poll poll = createPoll();
                poll.setTitle(question);
                poll.setLength(activeDuration);
                poll.setVoted(topic.getPoll().getVoted());
                poll.setCreationDate(topic.getPoll().getCreationDate());
                for (PollOption newPollOption : localPollOptions) {
                    Iterator<PollOption> stored = topic.getPoll().getOptions().iterator();
                    while (stored.hasNext()) {
                        PollOption oldPollOption = (PollOption) stored.next();
                        if (oldPollOption != null && oldPollOption.getQuestion().equals(newPollOption.getQuestion())) {
                            newPollOption.setVotes(oldPollOption.getVotes());
                            break;
                        }
                    }
                }
                poll.setOptions(localPollOptions);
                forumsModule.addPollToTopic(topic, poll);
            } else {
                // remove the poll from the database...poll was removed
                // during this editPost process
                topic.setPoll(null);
            }
        }
        forumsModule.update(topic);
        forumsModule.update(post);
        // set the proper navigation state
        navState = SUCCESS;
        success = true;
    } catch (PollValidationException e) {
        // handle proper validation error with a proper message...not just a
        // generic message..
        // just use generic error page for the proof of concept
        // set the custom exception such that e.toString() results in the
        // proper message
        handleException(e);
    } catch (Exception e) {
        handleException(e);
    } finally {
        // cleanup if necessary
        if (success) {
            cleanup();
        }
    }
    return navState;
}
Also used : PortalUtil.createMessage(it.vige.rubia.PortalUtil.createMessage) JSFUtil.getBundleMessage(it.vige.rubia.ui.JSFUtil.getBundleMessage) Message(it.vige.rubia.model.Message) Post(it.vige.rubia.model.Post) JSFUtil.handleException(it.vige.rubia.ui.JSFUtil.handleException) Date(java.util.Date) LinkedList(java.util.LinkedList) PortalUtil.createPoll(it.vige.rubia.PortalUtil.createPoll) Poll(it.vige.rubia.model.Poll) PollOption(it.vige.rubia.model.PollOption) PortalUtil.createPollOption(it.vige.rubia.PortalUtil.createPollOption) Topic(it.vige.rubia.model.Topic) Interceptors(javax.interceptor.Interceptors) SecureActionForum(it.vige.rubia.auth.SecureActionForum)

Example 18 with PollOption

use of it.vige.rubia.model.PollOption in project rubia-forums by flashboss.

the class NewTopic method execute.

/**
 * Execute
 *
 * @return the navigation state of the application
 */
@SecureActionForum
@Interceptors(AuthorizationListener.class)
public String execute() {
    String navState = null;
    boolean success = false;
    try {
        // setup the message
        Message message = createMessage();
        message.setText(this.message);
        message.setSubject(subject);
        // setup the forum and the corresponding poster
        Forum forum = forumsModule.findForumById(forumId);
        Poster poster = getPoster(userModule, forumsModule);
        // setup the poll related information
        Poll poll = createPoll();
        if (question != null && question.trim().length() > 0) {
            poll.setTitle(question);
            poll.setLength(activeDuration);
            List<PollOption> pollOptions = new LinkedList<PollOption>();
            for (String option : options.keySet()) {
                PollOption pollOption = createPollOption(poll);
                pollOption.setQuestion((String) options.get(option));
                pollOption.setVotes(0);
                pollOptions.add(pollOption);
            }
            poll.setOptions(pollOptions);
            validatePoll(poll);
        }
        poster.incrementPostCount();
        // actually create the topic in this forum
        // use this method when poll and attachments are actually integrated
        // poll
        forumsModule.createTopic(// poll
        forum, // poll
        message, // poll
        new Date(), // poll
        poster, // poll
        poll, // attachments
        attachments, topicType);
        // setup the navigation state
        navState = SUCCESS;
        success = true;
    } catch (MessageValidationException e) {
        // handle proper validation error with a proper message...not just a
        // generic message..
        // just use generic error page for the proof of concept
        // set the custom exception such that e.toString() results in the
        // proper message
        handleException(e);
    } catch (PollValidationException e) {
        // handle proper validation error with a proper message...not just a
        // generic message..
        // just use generic error page for the proof of concept
        // set the custom exception such that e.toString() results in the
        // proper message
        handleException(e);
    } catch (Exception e) {
        handleException(e);
    } finally {
        // cleanup if necessary
        if (success) {
            cleanup();
        }
    }
    return navState;
}
Also used : PortalUtil.createMessage(it.vige.rubia.PortalUtil.createMessage) Message(it.vige.rubia.model.Message) LinkedList(java.util.LinkedList) Date(java.util.Date) JSFUtil.handleException(it.vige.rubia.ui.JSFUtil.handleException) Forum(it.vige.rubia.model.Forum) SecureActionForum(it.vige.rubia.auth.SecureActionForum) JSFUtil.getPoster(it.vige.rubia.ui.JSFUtil.getPoster) Poster(it.vige.rubia.model.Poster) PortalUtil.createPoll(it.vige.rubia.PortalUtil.createPoll) Poll(it.vige.rubia.model.Poll) PollOption(it.vige.rubia.model.PollOption) PortalUtil.createPollOption(it.vige.rubia.PortalUtil.createPollOption) Interceptors(javax.interceptor.Interceptors) SecureActionForum(it.vige.rubia.auth.SecureActionForum)

Example 19 with PollOption

use of it.vige.rubia.model.PollOption in project rubia-forums by flashboss.

the class ForumsModuleImpl method addPollToTopic.

@Override
public Poll addPollToTopic(Topic topic, Poll poll) throws ModuleException {
    try {
        TypedQuery<Poll> query = em.createNamedQuery("findPoll", Poll.class);
        query.setParameter("topicid", topic.getId());
        Poll oldpoll = null;
        try {
            oldpoll = query.getSingleResult();
        } catch (NoResultException ex) {
            oldpoll = null;
        }
        if (oldpoll != null) {
            em.remove(oldpoll);
        }
        em.persist(poll);
        topic.setPoll(poll);
        for (PollOption pollOption : poll.getOptions()) {
            pollOption.setPoll(poll);
            em.persist(pollOption);
        }
        update(topic);
        em.flush();
        return poll;
    } catch (Exception e) {
        String errorMessage = "Cannot add poll to topic";
        throw new ModuleException(errorMessage, e);
    }
}
Also used : Poll(it.vige.rubia.model.Poll) PollOption(it.vige.rubia.model.PollOption) NoResultException(javax.persistence.NoResultException) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException)

Example 20 with PollOption

use of it.vige.rubia.model.PollOption in project rubia-forums by flashboss.

the class OperationPollTest method verifyVote.

@Test
public void verifyVote() {
    Topic topic = new Topic(new Forum("Second Test Forum"), "Fourth Test Topic");
    goTo(driver, topic);
    Poll pollToUpdate = getPollOfCurrentTopic(driver);
    Poll updatedPoll = vote(driver, pollToUpdate, 0);
    assertEquals("Fourth Test Question", updatedPoll.getTitle());
    assertEquals(100, updatedPoll.getVotesSum());
    List<PollOption> options = updatedPoll.getOptions();
    assertEquals("Seventh Test Answer", options.get(0).getQuestion());
    assertEquals("Eight Test Answer", options.get(1).getQuestion());
    assertEquals(100, options.get(0).getVotes());
    assertEquals(0, options.get(1).getVotes());
    assertEquals(2, options.size());
    updatedPoll = vote(driver, pollToUpdate, 1);
    assertEquals("Fourth Test Question", updatedPoll.getTitle());
    assertEquals(100, updatedPoll.getVotesSum());
    options = updatedPoll.getOptions();
    assertEquals("Seventh Test Answer", options.get(0).getQuestion());
    assertEquals("Eight Test Answer", options.get(1).getQuestion());
    assertEquals(50, options.get(0).getVotes());
    assertEquals(50, options.get(1).getVotes());
    assertEquals(2, options.size());
    topic.setPoll(updatedPoll);
    String message = removePoll(driver, topic);
    assertTrue(message.equals(OK));
    String[] createdOptions = addPoll(driver, updatedPoll);
    assertEquals(2, createdOptions.length);
}
Also used : UpdatePoll.updatePoll(it.vige.rubia.selenium.forum.action.UpdatePoll.updatePoll) Poll(it.vige.rubia.model.Poll) RemovePoll.removePoll(it.vige.rubia.selenium.forum.action.RemovePoll.removePoll) CreatePoll.addPoll(it.vige.rubia.selenium.forum.action.CreatePoll.addPoll) PollOption(it.vige.rubia.model.PollOption) RemoveTopic.removeTopic(it.vige.rubia.selenium.forum.action.RemoveTopic.removeTopic) CreateTopic.createTopic(it.vige.rubia.selenium.forum.action.CreateTopic.createTopic) Topic(it.vige.rubia.model.Topic) VerifyPoll.getPollOfCurrentTopic(it.vige.rubia.selenium.forum.action.VerifyPoll.getPollOfCurrentTopic) Forum(it.vige.rubia.model.Forum) RemoveForum.removeForum(it.vige.rubia.selenium.adminpanel.action.RemoveForum.removeForum) CreateForum.createForum(it.vige.rubia.selenium.adminpanel.action.CreateForum.createForum) Test(org.junit.Test)

Aggregations

PollOption (it.vige.rubia.model.PollOption)33 Poll (it.vige.rubia.model.Poll)29 Topic (it.vige.rubia.model.Topic)24 Forum (it.vige.rubia.model.Forum)21 CreateTopic.createTopic (it.vige.rubia.selenium.forum.action.CreateTopic.createTopic)21 CreateForum.createForum (it.vige.rubia.selenium.adminpanel.action.CreateForum.createForum)20 Post (it.vige.rubia.model.Post)19 RemoveForum.removeForum (it.vige.rubia.selenium.adminpanel.action.RemoveForum.removeForum)19 Attachment (it.vige.rubia.model.Attachment)18 RemoveTopic.removeTopic (it.vige.rubia.selenium.forum.action.RemoveTopic.removeTopic)14 Category (it.vige.rubia.model.Category)12 CreateCategory.createCategory (it.vige.rubia.selenium.adminpanel.action.CreateCategory.createCategory)12 RemoveCategory.removeCategory (it.vige.rubia.selenium.adminpanel.action.RemoveCategory.removeCategory)12 BeforeClass (org.junit.BeforeClass)11 Test (org.junit.Test)9 CreatePost.createPost (it.vige.rubia.selenium.forum.action.CreatePost.createPost)7 CreatePoll.addPoll (it.vige.rubia.selenium.forum.action.CreatePoll.addPoll)6 RemovePoll.removePoll (it.vige.rubia.selenium.forum.action.RemovePoll.removePoll)6 UpdatePoll.updatePoll (it.vige.rubia.selenium.forum.action.UpdatePoll.updatePoll)6 VerifyPoll.getPollOfCurrentTopic (it.vige.rubia.selenium.forum.action.VerifyPoll.getPollOfCurrentTopic)6