Example #1
0
  @Override
  JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {

    String pollValue = req.getParameter("poll");

    if (pollValue == null) {
      return MISSING_POLL;
    }

    Poll pollData;
    int numberOfOptions = 0;
    try {
      pollData = Poll.getPoll(Convert.parseUnsignedLong(pollValue));
      if (pollData != null) {
        numberOfOptions = pollData.getOptions().length;
      } else return INCORRECT_POLL;
    } catch (RuntimeException e) {
      return INCORRECT_POLL;
    }

    byte[] vote = new byte[numberOfOptions];
    try {
      for (int i = 0; i < numberOfOptions; i++) {
        String voteValue = req.getParameter("vote" + i);
        if (voteValue != null) {
          vote[i] = Byte.parseByte(voteValue);
        }
      }
    } catch (NumberFormatException e) {
      return INCORRECT_VOTE;
    }

    Account account = getAccount(req);
    if (account == null) {
      return UNKNOWN_ACCOUNT;
    }

    Attachment attachment = new Attachment.MessagingVoteCasting(pollData.getId(), vote);
    return createTransaction(req, account, attachment);
  }
Example #2
0
  @Override
  JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    long accountId = ParameterParser.getAccountId(req, "account", false);
    boolean includeFinished = "true".equalsIgnoreCase(req.getParameter("includeFinished"));
    int firstIndex = ParameterParser.getFirstIndex(req);
    int lastIndex = ParameterParser.getLastIndex(req);
    final int timestamp = ParameterParser.getTimestamp(req);

    JSONArray pollsJson = new JSONArray();
    DbIterator<Poll> polls = null;
    try {
      if (accountId == 0) {
        if (includeFinished) {
          polls = Poll.getAllPolls(firstIndex, lastIndex);
        } else {
          polls = Poll.getActivePolls(firstIndex, lastIndex);
        }
      } else {
        polls = Poll.getPollsByAccount(accountId, includeFinished, firstIndex, lastIndex);
      }

      while (polls.hasNext()) {
        Poll poll = polls.next();
        if (poll.getTimestamp() < timestamp) {
          break;
        }
        pollsJson.add(JSONData.poll(poll));
      }
    } finally {
      DbUtils.close(polls);
    }

    JSONObject response = new JSONObject();
    response.put("polls", pollsJson);
    return response;
  }