Ejemplo n.º 1
0
  protected PollsVote addPollsVote() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    PollsVote pollsVote = _persistence.create(pk);

    pollsVote.setUuid(ServiceTestUtil.randomString());

    pollsVote.setGroupId(ServiceTestUtil.nextLong());

    pollsVote.setCompanyId(ServiceTestUtil.nextLong());

    pollsVote.setUserId(ServiceTestUtil.nextLong());

    pollsVote.setUserName(ServiceTestUtil.randomString());

    pollsVote.setCreateDate(ServiceTestUtil.nextDate());

    pollsVote.setModifiedDate(ServiceTestUtil.nextDate());

    pollsVote.setQuestionId(ServiceTestUtil.nextLong());

    pollsVote.setChoiceId(ServiceTestUtil.nextLong());

    pollsVote.setVoteDate(ServiceTestUtil.nextDate());

    _persistence.update(pollsVote);

    return pollsVote;
  }
  public PollsVote addVote(
      long userId, long questionId, long choiceId, ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Choice

    Date now = new Date();

    PollsChoice choice = pollsChoicePersistence.findByPrimaryKey(choiceId);

    if (choice.getQuestionId() != questionId) {
      throw new NoSuchQuestionException();
    }

    // Question

    PollsQuestion question = pollsQuestionPersistence.findByPrimaryKey(questionId);

    if (question.isExpired(serviceContext, now)) {
      throw new QuestionExpiredException();
    }

    question.setLastVoteDate(serviceContext.getCreateDate(now));

    pollsQuestionPersistence.update(question, false);

    // Vote

    PollsVote vote = pollsVotePersistence.fetchByQ_U(questionId, userId);

    if (vote != null) {
      throw new DuplicateVoteException();
    } else {
      User user = userPersistence.findByPrimaryKey(userId);

      long voteId = counterLocalService.increment();

      vote = pollsVotePersistence.create(voteId);

      vote.setCompanyId(user.getCompanyId());
      vote.setUserId(user.getUserId());
      vote.setUserName(user.getFullName());
      vote.setCreateDate(serviceContext.getCreateDate(now));
      vote.setModifiedDate(serviceContext.getModifiedDate(now));
      vote.setQuestionId(questionId);
      vote.setChoiceId(choiceId);
      vote.setVoteDate(serviceContext.getCreateDate(now));

      pollsVotePersistence.update(vote, false);
    }

    return vote;
  }
Ejemplo n.º 3
0
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    PollsVote newPollsVote = _persistence.create(pk);

    newPollsVote.setUuid(ServiceTestUtil.randomString());

    newPollsVote.setGroupId(ServiceTestUtil.nextLong());

    newPollsVote.setCompanyId(ServiceTestUtil.nextLong());

    newPollsVote.setUserId(ServiceTestUtil.nextLong());

    newPollsVote.setUserName(ServiceTestUtil.randomString());

    newPollsVote.setCreateDate(ServiceTestUtil.nextDate());

    newPollsVote.setModifiedDate(ServiceTestUtil.nextDate());

    newPollsVote.setQuestionId(ServiceTestUtil.nextLong());

    newPollsVote.setChoiceId(ServiceTestUtil.nextLong());

    newPollsVote.setVoteDate(ServiceTestUtil.nextDate());

    _persistence.update(newPollsVote);

    PollsVote existingPollsVote = _persistence.findByPrimaryKey(newPollsVote.getPrimaryKey());

    Assert.assertEquals(existingPollsVote.getUuid(), newPollsVote.getUuid());
    Assert.assertEquals(existingPollsVote.getVoteId(), newPollsVote.getVoteId());
    Assert.assertEquals(existingPollsVote.getGroupId(), newPollsVote.getGroupId());
    Assert.assertEquals(existingPollsVote.getCompanyId(), newPollsVote.getCompanyId());
    Assert.assertEquals(existingPollsVote.getUserId(), newPollsVote.getUserId());
    Assert.assertEquals(existingPollsVote.getUserName(), newPollsVote.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingPollsVote.getCreateDate()),
        Time.getShortTimestamp(newPollsVote.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingPollsVote.getModifiedDate()),
        Time.getShortTimestamp(newPollsVote.getModifiedDate()));
    Assert.assertEquals(existingPollsVote.getQuestionId(), newPollsVote.getQuestionId());
    Assert.assertEquals(existingPollsVote.getChoiceId(), newPollsVote.getChoiceId());
    Assert.assertEquals(
        Time.getShortTimestamp(existingPollsVote.getVoteDate()),
        Time.getShortTimestamp(newPollsVote.getVoteDate()));
  }