Search in sources :

Example 1 with PutQuery

use of org.apache.pivot.web.PutQuery in project pivot by apache.

the class ExpensesWindow method updateSelectedExpense.

private void updateSelectedExpense() {
    Object expense = expenseTableView.getSelectedRow();
    final int id = JSON.getInt(expense, "id");
    expenseSheet.load(expense);
    expenseSheet.open(this, new SheetCloseListener() {

        @Override
        public void sheetClosed(Sheet sheet) {
            if (sheet.getResult()) {
                // Get the expense data from the sheet
                final HashMap<String, Object> expenseLocal = new HashMap<>();
                expenseSheet.store(expenseLocal);
                // PUT expense to server and then update table
                Expenses expensesApplicationLocal = getExpensesApplication();
                PutQuery updateExpenseQuery = new PutQuery(expensesApplicationLocal.getHostname(), expensesApplicationLocal.getPort(), "/pivot-tutorials/expenses/" + JSON.get(expenseLocal, "id"), expensesApplicationLocal.isSecure());
                updateExpenseQuery.setValue(expenseLocal);
                activityIndicatorBoxPane.setVisible(true);
                activityIndicator.setActive(true);
                updateExpenseQuery.execute(new TaskAdapter<>(new TaskListener<Boolean>() {

                    @Override
                    public void taskExecuted(Task<Boolean> task) {
                        activityIndicatorBoxPane.setVisible(false);
                        activityIndicator.setActive(false);
                        // Find matching row and update
                        @SuppressWarnings("unchecked") List<Object> expenses = (List<Object>) expenseTableView.getTableData();
                        for (int i = 0, n = expenses.getLength(); i < n; i++) {
                            if (JSON.get(expenses.get(i), "id").equals(id)) {
                                expenses.update(i, expenseLocal);
                                break;
                            }
                        }
                    }

                    @Override
                    public void executeFailed(Task<Boolean> task) {
                        activityIndicatorBoxPane.setVisible(false);
                        activityIndicator.setActive(false);
                        Prompt.prompt(MessageType.ERROR, task.getFault().getMessage(), ExpensesWindow.this);
                    }
                }));
            }
        }
    });
}
Also used : HashMap(org.apache.pivot.collections.HashMap) SheetCloseListener(org.apache.pivot.wtk.SheetCloseListener) PutQuery(org.apache.pivot.web.PutQuery) TaskAdapter(org.apache.pivot.wtk.TaskAdapter) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) Sheet(org.apache.pivot.wtk.Sheet)

Example 2 with PutQuery

use of org.apache.pivot.web.PutQuery in project pivot by apache.

the class RESTDemoTest method testCRUD.

@Test
public void testCRUD() throws IOException, SerializationException, QueryException {
    JSONSerializer jsonSerializer = new JSONSerializer();
    Object contact = jsonSerializer.readObject(getClass().getResourceAsStream("contact.json"));
    // Create
    PostQuery postQuery = new PostQuery(hostname, port, "/pivot-demos/rest_demo", secure);
    postQuery.setValue(contact);
    URL location = postQuery.execute();
    assertNotNull(location);
    String path = location.getPath();
    // Read
    GetQuery getQuery = new GetQuery(hostname, port, path, secure);
    Object result = getQuery.execute();
    assertArrayEquals((Object[]) JSON.get(contact, "address.street"), (Object[]) JSON.get(result, "address.street"));
    assertEquals(contact, result);
    // Update
    JSON.put(contact, "name", "Joseph User");
    PutQuery putQuery = new PutQuery(hostname, port, path, secure);
    putQuery.setValue(contact);
    boolean created = putQuery.execute();
    assertFalse(created);
    assertEquals(contact, getQuery.execute());
    // Delete
    DeleteQuery deleteQuery = new DeleteQuery(hostname, port, path, secure);
    deleteQuery.execute();
    assertEquals(deleteQuery.getStatus(), Query.Status.NO_CONTENT);
}
Also used : GetQuery(org.apache.pivot.web.GetQuery) PostQuery(org.apache.pivot.web.PostQuery) DeleteQuery(org.apache.pivot.web.DeleteQuery) PutQuery(org.apache.pivot.web.PutQuery) URL(java.net.URL) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Aggregations

PutQuery (org.apache.pivot.web.PutQuery)2 URL (java.net.URL)1 ArrayList (org.apache.pivot.collections.ArrayList)1 HashMap (org.apache.pivot.collections.HashMap)1 List (org.apache.pivot.collections.List)1 JSONSerializer (org.apache.pivot.json.JSONSerializer)1 DeleteQuery (org.apache.pivot.web.DeleteQuery)1 GetQuery (org.apache.pivot.web.GetQuery)1 PostQuery (org.apache.pivot.web.PostQuery)1 Sheet (org.apache.pivot.wtk.Sheet)1 SheetCloseListener (org.apache.pivot.wtk.SheetCloseListener)1 TaskAdapter (org.apache.pivot.wtk.TaskAdapter)1 Test (org.junit.Test)1