Search in sources :

Example 1 with Poll

use of org.xel.Poll in project elastic-core-maven by OrdinaryDude.

the class GetPolls method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    long accountId = ParameterParser.getAccountId(req, "account", false);
    boolean includeFinished = "true".equalsIgnoreCase(req.getParameter("includeFinished"));
    boolean finishedOnly = "true".equalsIgnoreCase(req.getParameter("finishedOnly"));
    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 (finishedOnly) {
                polls = Poll.getPollsFinishingAtOrBefore(Nxt.getBlockchain().getHeight(), firstIndex, lastIndex);
            } else if (includeFinished) {
                polls = Poll.getAllPolls(firstIndex, lastIndex);
            } else {
                polls = Poll.getActivePolls(firstIndex, lastIndex);
            }
        } else {
            polls = Poll.getPollsByAccount(accountId, includeFinished, finishedOnly, 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;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) Poll(org.xel.Poll)

Example 2 with Poll

use of org.xel.Poll in project elastic-core-maven by OrdinaryDude.

the class GetPollResult method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    Poll poll = ParameterParser.getPoll(req);
    List<Poll.OptionResult> pollResults;
    VoteWeighting voteWeighting;
    if (Convert.emptyToNull(req.getParameter("votingModel")) == null) {
        pollResults = poll.getResults();
        voteWeighting = poll.getVoteWeighting();
    } else {
        byte votingModel = ParameterParser.getByte(req, "votingModel", (byte) 0, (byte) 3, true);
        long holdingId = ParameterParser.getLong(req, "holding", Long.MIN_VALUE, Long.MAX_VALUE, false);
        long minBalance = ParameterParser.getLong(req, "minBalance", 0, Long.MAX_VALUE, false);
        byte minBalanceModel = ParameterParser.getByte(req, "minBalanceModel", (byte) 0, (byte) 3, false);
        voteWeighting = new VoteWeighting(votingModel, holdingId, minBalance, minBalanceModel);
        voteWeighting.validate();
        pollResults = poll.getResults(voteWeighting);
    }
    if (pollResults == null) {
        return POLL_RESULTS_NOT_AVAILABLE;
    }
    return JSONData.pollResults(poll, pollResults, voteWeighting);
}
Also used : Poll(org.xel.Poll) VoteWeighting(org.xel.VoteWeighting)

Example 3 with Poll

use of org.xel.Poll in project elastic-core-maven by OrdinaryDude.

the class GetPollVotes method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    int firstIndex = ParameterParser.getFirstIndex(req);
    int lastIndex = ParameterParser.getLastIndex(req);
    boolean includeWeights = "true".equalsIgnoreCase(req.getParameter("includeWeights"));
    Poll poll = ParameterParser.getPoll(req);
    int countHeight;
    JSONData.VoteWeighter weighter = null;
    if (includeWeights && (countHeight = Math.min(poll.getFinishHeight(), Nxt.getBlockchain().getHeight())) >= Nxt.getBlockchainProcessor().getMinRollbackHeight()) {
        VoteWeighting voteWeighting = poll.getVoteWeighting();
        VoteWeighting.VotingModel votingModel = voteWeighting.getVotingModel();
        weighter = voterId -> votingModel.calcWeight(voteWeighting, voterId, countHeight);
    }
    JSONArray votesJson = new JSONArray();
    try (DbIterator<Vote> votes = Vote.getVotes(poll.getId(), firstIndex, lastIndex)) {
        for (Vote vote : votes) {
            votesJson.add(JSONData.vote(vote, weighter));
        }
    }
    JSONObject response = new JSONObject();
    response.put("votes", votesJson);
    return response;
}
Also used : Vote(org.xel.Vote) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) Poll(org.xel.Poll) VoteWeighting(org.xel.VoteWeighting)

Example 4 with Poll

use of org.xel.Poll in project elastic-core-maven by OrdinaryDude.

the class CastVote method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    Poll poll = ParameterParser.getPoll(req);
    if (poll.isFinished()) {
        return POLL_FINISHED;
    }
    int numberOfOptions = poll.getOptions().length;
    byte[] vote = new byte[numberOfOptions];
    try {
        for (int i = 0; i < numberOfOptions; i++) {
            String voteValue = Convert.emptyToNull(req.getParameter("vote" + (i < 10 ? "0" + i : i)));
            if (voteValue != null) {
                vote[i] = Byte.parseByte(voteValue);
                if (vote[i] != Constants.NO_VOTE_VALUE && (vote[i] < poll.getMinRangeValue() || vote[i] > poll.getMaxRangeValue())) {
                    return INCORRECT_VOTE;
                }
            } else {
                vote[i] = Constants.NO_VOTE_VALUE;
            }
        }
    } catch (NumberFormatException e) {
        return INCORRECT_VOTE;
    }
    Account account = ParameterParser.getSenderAccount(req);
    Attachment attachment = new Attachment.MessagingVoteCasting(poll.getId(), vote);
    return createTransaction(req, account, attachment);
}
Also used : Account(org.xel.Account) Poll(org.xel.Poll) Attachment(org.xel.Attachment)

Example 5 with Poll

use of org.xel.Poll in project elastic-core-maven by OrdinaryDude.

the class GetPollVote method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    Poll poll = ParameterParser.getPoll(req);
    long accountId = ParameterParser.getAccountId(req, true);
    boolean includeWeights = "true".equalsIgnoreCase(req.getParameter("includeWeights"));
    Vote vote = Vote.getVote(poll.getId(), accountId);
    if (vote != null) {
        int countHeight;
        JSONData.VoteWeighter weighter = null;
        if (includeWeights && (countHeight = Math.min(poll.getFinishHeight(), Nxt.getBlockchain().getHeight())) >= Nxt.getBlockchainProcessor().getMinRollbackHeight()) {
            VoteWeighting voteWeighting = poll.getVoteWeighting();
            VoteWeighting.VotingModel votingModel = voteWeighting.getVotingModel();
            weighter = voterId -> votingModel.calcWeight(voteWeighting, voterId, countHeight);
        }
        return JSONData.vote(vote, weighter);
    }
    return JSON.emptyJSON;
}
Also used : Vote(org.xel.Vote) Poll(org.xel.Poll) VoteWeighting(org.xel.VoteWeighting)

Aggregations

Poll (org.xel.Poll)6 JSONArray (org.json.simple.JSONArray)3 JSONObject (org.json.simple.JSONObject)3 VoteWeighting (org.xel.VoteWeighting)3 Vote (org.xel.Vote)2 Account (org.xel.Account)1 Attachment (org.xel.Attachment)1