Search in sources :

Example 1 with GetQuery

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();
            }
        }
    }));
}
Also used : Task(org.apache.pivot.util.concurrent.Task) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) List(org.apache.pivot.collections.List) Map(org.apache.pivot.collections.Map)

Example 2 with GetQuery

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();
}
Also used : GetQuery(org.apache.pivot.web.GetQuery) Test(org.junit.Test)

Example 3 with GetQuery

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);
        }
    }));
}
Also used : Task(org.apache.pivot.util.concurrent.Task) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List)

Example 4 with GetQuery

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);
}
Also used : Task(org.apache.pivot.util.concurrent.Task) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) List(org.apache.pivot.collections.List) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 5 with GetQuery

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;
                }
            }
        }));
    }
}
Also used : Task(org.apache.pivot.util.concurrent.Task) Date(java.util.Date) GetQuery(org.apache.pivot.web.GetQuery) DateFormat(java.text.DateFormat) TaskListener(org.apache.pivot.util.concurrent.TaskListener) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) CSVSerializer(org.apache.pivot.serialization.CSVSerializer)

Aggregations

GetQuery (org.apache.pivot.web.GetQuery)8 Task (org.apache.pivot.util.concurrent.Task)6 TaskListener (org.apache.pivot.util.concurrent.TaskListener)6 List (org.apache.pivot.collections.List)5 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 ArrayList (org.apache.pivot.collections.ArrayList)2 Test (org.junit.Test)2 Desktop (java.awt.Desktop)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 DateFormat (java.text.DateFormat)1 Date (java.util.Date)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 Map (org.apache.pivot.collections.Map)1 JSONSerializer (org.apache.pivot.json.JSONSerializer)1 CSVSerializer (org.apache.pivot.serialization.CSVSerializer)1 DeleteQuery (org.apache.pivot.web.DeleteQuery)1