Search in sources :

Example 1 with PollBuilder

use of org.xel.Attachment.MessagingPollCreation.PollBuilder in project elastic-core-maven by OrdinaryDude.

the class CreatePoll method processRequest.

@Override
protected JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
    String nameValue = Convert.emptyToNull(req.getParameter("name"));
    String descriptionValue = req.getParameter("description");
    if (nameValue == null || nameValue.trim().isEmpty()) {
        return MISSING_NAME;
    } else if (descriptionValue == null) {
        return MISSING_DESCRIPTION;
    }
    if (nameValue.length() > Constants.MAX_POLL_NAME_LENGTH) {
        return INCORRECT_POLL_NAME_LENGTH;
    }
    if (descriptionValue.length() > Constants.MAX_POLL_DESCRIPTION_LENGTH) {
        return INCORRECT_POLL_DESCRIPTION_LENGTH;
    }
    List<String> options = new ArrayList<>();
    while (options.size() < Constants.MAX_POLL_OPTION_COUNT) {
        int i = options.size();
        String optionValue = Convert.emptyToNull(req.getParameter("option" + (i < 10 ? "0" + i : i)));
        if (optionValue == null) {
            break;
        }
        if (optionValue.length() > Constants.MAX_POLL_OPTION_LENGTH || (optionValue = optionValue.trim()).isEmpty()) {
            return INCORRECT_POLL_OPTION_LENGTH;
        }
        options.add(optionValue);
    }
    byte optionsSize = (byte) options.size();
    if (options.size() == 0) {
        return INCORRECT_ZEROOPTIONS;
    }
    int currentHeight = Nxt.getBlockchain().getHeight();
    int finishHeight = ParameterParser.getInt(req, "finishHeight", currentHeight + 2, currentHeight + Constants.MAX_POLL_DURATION + 1, true);
    byte votingModel = ParameterParser.getByte(req, "votingModel", (byte) 0, (byte) 3, true);
    byte minNumberOfOptions = ParameterParser.getByte(req, "minNumberOfOptions", (byte) 1, optionsSize, true);
    byte maxNumberOfOptions = ParameterParser.getByte(req, "maxNumberOfOptions", minNumberOfOptions, optionsSize, true);
    byte minRangeValue = ParameterParser.getByte(req, "minRangeValue", Constants.MIN_VOTE_VALUE, Constants.MAX_VOTE_VALUE, true);
    byte maxRangeValue = ParameterParser.getByte(req, "maxRangeValue", minRangeValue, Constants.MAX_VOTE_VALUE, true);
    PollBuilder builder = new PollBuilder(nameValue.trim(), descriptionValue.trim(), options.toArray(new String[options.size()]), finishHeight, votingModel, minNumberOfOptions, maxNumberOfOptions, minRangeValue, maxRangeValue);
    long minBalance = ParameterParser.getLong(req, "minBalance", 0, Long.MAX_VALUE, false);
    if (minBalance != 0) {
        byte minBalanceModel = ParameterParser.getByte(req, "minBalanceModel", (byte) 0, (byte) 1, true);
        builder.minBalance(minBalanceModel, minBalance);
    }
    long holdingId = ParameterParser.getUnsignedLong(req, "holding", false);
    if (holdingId != 0) {
        builder.holdingId(holdingId);
    }
    Account account = ParameterParser.getSenderAccount(req);
    Attachment attachment = builder.build();
    return createTransaction(req, account, attachment);
}
Also used : Account(org.xel.Account) PollBuilder(org.xel.Attachment.MessagingPollCreation.PollBuilder) ArrayList(java.util.ArrayList) Attachment(org.xel.Attachment)

Aggregations

ArrayList (java.util.ArrayList)1 Account (org.xel.Account)1 Attachment (org.xel.Attachment)1 PollBuilder (org.xel.Attachment.MessagingPollCreation.PollBuilder)1