/******************************************************************************
* Copyright © 2013-2016 The Nxt Core Developers. *
* *
* See the AUTHORS.txt, DEVELOPER-AGREEMENT.txt and LICENSE.txt files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Nxt software, including this file, may be copied, modified, propagated, *
* or distributed except according to the terms contained in the LICENSE.txt *
* file. *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
package nxt.http;
import nxt.Attachment;
import nxt.Nxt;
import nxt.NxtException;
import nxt.Order;
import nxt.Transaction;
import nxt.TransactionType;
import nxt.db.DbIterator;
import nxt.util.Filter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONStreamAware;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
public final class GetBidOrders extends APIServlet.APIRequestHandler {
static final GetBidOrders instance = new GetBidOrders();
private GetBidOrders() {
super(new APITag[] {APITag.AE}, "asset", "firstIndex", "lastIndex", "showExpectedCancellations");
}
@Override
JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
long assetId = ParameterParser.getUnsignedLong(req, "asset", true);
int firstIndex = ParameterParser.getFirstIndex(req);
int lastIndex = ParameterParser.getLastIndex(req);
boolean showExpectedCancellations = "true".equalsIgnoreCase(req.getParameter("showExpectedCancellations"));
long[] cancellations = null;
if (showExpectedCancellations) {
Filter<Transaction> filter = transaction -> transaction.getType() == TransactionType.ColoredCoins.BID_ORDER_CANCELLATION;
List<? extends Transaction> transactions = Nxt.getBlockchain().getExpectedTransactions(filter);
cancellations = new long[transactions.size()];
for (int i = 0; i < transactions.size(); i++) {
Attachment.ColoredCoinsOrderCancellation attachment = (Attachment.ColoredCoinsOrderCancellation) transactions.get(i).getAttachment();
cancellations[i] = attachment.getOrderId();
}
Arrays.sort(cancellations);
}
JSONArray orders = new JSONArray();
try (DbIterator<Order.Bid> bidOrders = Order.Bid.getSortedOrders(assetId, firstIndex, lastIndex)) {
while (bidOrders.hasNext()) {
Order.Bid order = bidOrders.next();
JSONObject orderJSON = JSONData.bidOrder(order);
if (showExpectedCancellations && Arrays.binarySearch(cancellations, order.getId()) >= 0) {
orderJSON.put("expectedCancellation", Boolean.TRUE);
}
orders.add(orderJSON);
}
}
JSONObject response = new JSONObject();
response.put("bidOrders", orders);
return response;
}
}