use of org.apache.pivot.web.GetQuery in project pivot by apache.
the class SearchDemo method executeQuery.
/**
* Executes a search.
*
* @param term The search term.
* @throws IllegalArgumentException If <tt>term</tt> is <tt>null</tt> or
* empty.
* @throws IllegalStateException If a query is already executing.
*/
public void executeQuery(String term) {
Utils.checkNullOrEmpty(term, "search term");
if (getQuery != null) {
throw new IllegalStateException("Query is already running!");
}
String country = Locale.getDefault().getCountry().toLowerCase();
getQuery = new GetQuery(QUERY_HOSTNAME, BASE_QUERY_PATH);
getQuery.getParameters().put("term", term);
getQuery.getParameters().put("country", country);
getQuery.getParameters().put("media", MEDIA);
getQuery.getParameters().put("limit", Integer.toString(LIMIT));
getQuery.getParameters().put("output", "json");
System.out.println(getQuery.getLocation());
statusLabel.setText("Searching...");
updateActivityState();
getQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {
@Override
public void taskExecuted(Task<Object> task) {
if (task == getQuery) {
@SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) task.getResult();
@SuppressWarnings("unchecked") List<Object> results = (List<Object>) result.get("results");
// Preserve any existing sort
@SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) resultsTableView.getTableData();
Comparator<Object> comparator = tableData.getComparator();
results.setComparator(comparator);
// Update the table data
resultsTableView.setTableData(results);
statusLabel.setText("Found " + results.getLength() + " matching items.");
getQuery = null;
updateActivityState();
if (results.getLength() > 0) {
resultsTableView.setSelectedIndex(0);
resultsTableView.requestFocus();
} else {
termTextInput.requestFocus();
}
}
}
@Override
public void executeFailed(Task<Object> task) {
if (task == getQuery) {
statusLabel.setText(task.getFault().getMessage());
getQuery = null;
updateActivityState();
termTextInput.requestFocus();
}
}
}));
}
use of org.apache.pivot.web.GetQuery in project pivot by apache.
the class RESTDemoTest method testException.
@Test(expected = QueryException.class)
public void testException() throws QueryException {
GetQuery getQuery = new GetQuery(hostname, port, "/pivot-demos/rest_demo/foo", secure);
getQuery.execute();
}
use of org.apache.pivot.web.GetQuery in project pivot by apache.
the class ExpensesWindow method refreshExpenseList.
private void refreshExpenseList() {
Expenses expensesApplicationLocal = getExpensesApplication();
GetQuery expenseListQuery = new GetQuery(expensesApplicationLocal.getHostname(), expensesApplicationLocal.getPort(), "/pivot-tutorials/expenses", expensesApplicationLocal.isSecure());
activityIndicatorBoxPane.setVisible(true);
activityIndicator.setActive(true);
expenseListQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {
@Override
public void taskExecuted(Task<Object> task) {
activityIndicatorBoxPane.setVisible(false);
activityIndicator.setActive(false);
List<?> expenseData = (List<?>) task.getResult();
expenseTableView.setTableData(expenseData);
}
@Override
public void executeFailed(Task<Object> task) {
activityIndicatorBoxPane.setVisible(false);
activityIndicator.setActive(false);
Prompt.prompt(MessageType.ERROR, task.getFault().getMessage(), ExpensesWindow.this);
}
}));
}
use of org.apache.pivot.web.GetQuery in project pivot by apache.
the class WebQueries method startup.
@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
window = (Window) bxmlSerializer.readObject(WebQueries.class, "web_queries.bxml");
listView = (ListView) bxmlSerializer.getNamespace().get("listView");
loadingLabel = (Label) bxmlSerializer.getNamespace().get("loadingLabel");
// Execute the query:
// http://pipes.yahoo.com/pipes/pipe.run?_id=43115761f2da5af5341ae2e56a93d646&_render=json
GetQuery getQuery = new GetQuery("pipes.yahoo.com", "/pipes/pipe.run");
getQuery.getParameters().put("_id", "43115761f2da5af5341ae2e56a93d646");
getQuery.getParameters().put("_render", "json");
getQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {
@Override
public void taskExecuted(Task<Object> task) {
List<?> items = (List<?>) JSON.get(task.getResult(), "value.items");
if (items.getLength() > 0) {
listView.setListData(items);
loadingLabel.setVisible(false);
} else {
loadingLabel.setText("No results.");
}
}
@Override
public void executeFailed(Task<Object> task) {
loadingLabel.setText(task.getFault().getMessage());
}
}));
window.open(display);
}
use of org.apache.pivot.web.GetQuery in project pivot by apache.
the class StockTrackerWindow method refreshTable.
private void refreshTable() {
// Abort any outstanding query
if (getQuery != null) {
synchronized (getQuery) {
if (getQuery.isPending()) {
getQuery.abort();
}
}
}
// Execute the query
if (symbols.getLength() > 0) {
getQuery = new GetQuery(SERVICE_HOSTNAME, SERVICE_PATH);
StringBuilder symbolsParameterBuilder = new StringBuilder();
for (int i = 0, n = symbols.getLength(); i < n; i++) {
if (i > 0) {
symbolsParameterBuilder.append(",");
}
symbolsParameterBuilder.append(symbols.get(i));
}
// Format:
// s - symbol
// n - company name
// l1 - most recent value
// o - opening value
// h - high value
// g - low value
// c1 - change percentage
// v - volume
String symbolsParameter = symbolsParameterBuilder.toString();
getQuery.getParameters().put("s", symbolsParameter);
getQuery.getParameters().put("f", "snl1ohgc1v");
CSVSerializer quoteSerializer = new CSVSerializer(StockQuote.class);
quoteSerializer.setKeys("symbol", "companyName", "value", "openingValue", "highValue", "lowValue", "change", "volume");
getQuery.setSerializer(quoteSerializer);
getQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {
@Override
public void taskExecuted(Task<Object> task) {
if (task == getQuery) {
@SuppressWarnings("unchecked") List<Object> quotes = (List<Object>) task.getResult();
// Preserve any existing sort and selection
Sequence<?> selectedStocks = stocksTableView.getSelectedRows();
@SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) stocksTableView.getTableData();
Comparator<Object> comparator = tableData.getComparator();
quotes.setComparator(comparator);
stocksTableView.setTableData(quotes);
if (selectedStocks.getLength() > 0) {
// Select current indexes of selected stocks
for (int i = 0, n = selectedStocks.getLength(); i < n; i++) {
Object selectedStock = selectedStocks.get(i);
int index = 0;
for (Object stock : stocksTableView.getTableData()) {
String symbol = JSON.get(stock, "symbol");
String selectedSymbol = JSON.get(selectedStock, "symbol");
if (symbol.equals(selectedSymbol)) {
stocksTableView.addSelectedIndex(index);
break;
}
index++;
}
}
} else {
if (quotes.getLength() > 0) {
stocksTableView.setSelectedIndex(0);
}
}
refreshDetail();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, Locale.getDefault());
lastUpdateLabel.setText(dateFormat.format(new Date()));
getQuery = null;
}
}
@Override
public void executeFailed(Task<Object> task) {
if (task == getQuery) {
System.err.println(task.getFault());
getQuery = null;
}
}
}));
}
}
Aggregations