/** * Creates data transfer object, that represents info about the poll option. * * @param option the poll option */ public PollOptionDto(PollItem option) { this.id = option.getId(); this.votesCount = option.getVotesCount(); }
/** * Get count of unique poll items. * * @param pollItems the source list of poll items * @return count of unique poll items */ private int getUniqueItemsCount(List<PollItem> pollItems) { HashSet<String> uniquePollItemsNames = new HashSet<String>(); for (PollItem pollItem: pollItems) { String pollItemName = pollItem.getName(); uniquePollItemsNames.add(pollItemName); } return uniquePollItemsNames.size(); } }
/** * Add the list of poll options to this poll. * * @param options the list of poll options to this poll */ public void addPollOptions(List<PollItem> options) { pollItems.addAll(options); forEach(options).setPoll(this); }
/** * {@inheritDoc} */ @Override @PreAuthorize("hasPermission(#pollId, 'POLL', 'GeneralPermission.WRITE')") public Poll vote(Long pollId, List<Long> selectedOptionsIds) { Poll poll = getDao().get(pollId); if (poll.isActive()) { prohibitRevote(poll); for (PollItem option : poll.getPollItems()) { if (selectedOptionsIds.contains(option.getId())) { option.increaseVotesCount(); pollOptionDao.saveOrUpdate(option); } } } return poll; }
/** * Counts the total count of votes in the poll. * * @return the total count of votes in the poll */ public int getTotalVotesCount() { return sumFrom(pollItems, PollItem.class).getVotesCount(); }
/** * Prepare poll items list from string. Removes empty lines from. * * @param pollItems user input * @return processed poll items list */ private List<PollItem> parseItems(String pollItems) { List<PollItem> result = new ArrayList<>(); if (pollItems == null) { return result; } String[] items = StringUtils.split(pollItems, SEPARATOR); for (String item : items) { // If user entered empty lines these lines are ignoring from // validation. // Only meaningful lines are processed and user get processed output if (StringUtils.isNotBlank(item)) { item = item.trim(); PollItem pollItem = new PollItem(item); result.add(pollItem); } } return result; } }
public static Poll createDefaultVoting() { Topic topic = getDefaultTopic(); Poll poll = new Poll("New voting"); List<PollItem> pollItems = new ArrayList<>(); pollItems.add(new PollItem("item1")); pollItems.add(new PollItem("item2")); pollItems.add(new PollItem("item3")); poll.setPollItems(pollItems); topic.setPoll(poll); poll.setTopic(topic); persist(topic); return poll; }
/** * {@inheritDoc} */ @Override public void mergePollItems(Poll poll, List<PollItem> newItems) { List<PollItem> existing = poll.getPollItems(); ListIterator<PollItem> updated = newItems.listIterator(); while (updated.hasNext()) { PollItem item = updated.next(); for (PollItem old : existing) { if (item.getName().equals(old.getName())) { updated.set(old); } } } existing.clear(); existing.addAll(newItems); }
public static PollItem createDefaultVotingOption() { Poll voting = createDefaultVoting(); persist(voting); PollItem option = new PollItem("First voting option"); voting.addPollOptions(option); return option; }
/** * Validates the name of poll title. * * @param pollItem validated poll item * @return {@code true} if poll item name has correct length, * otherwise {@code false} */ private boolean isPollItemValid(PollItem pollItem) { String pollItemName = pollItem.getName(); Range range = new IntRange(minLength, maxLenght); int pollItemLength = pollItemName.length(); return range.containsInteger(pollItemLength); } }