use of org.apache.pivot.web.PostQuery in project pivot by apache.
the class ExpensesWindow method addExpense.
private void addExpense() {
expenseSheet.clear();
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> expense = new HashMap<>();
expenseSheet.store(expense);
// POST expense to server and then add to table
Expenses expensesApplicationLocal = getExpensesApplication();
PostQuery addExpenseQuery = new PostQuery(expensesApplicationLocal.getHostname(), expensesApplicationLocal.getPort(), "/pivot-tutorials/expenses", expensesApplicationLocal.isSecure());
addExpenseQuery.setValue(expense);
activityIndicatorBoxPane.setVisible(true);
activityIndicator.setActive(true);
addExpenseQuery.execute(new TaskAdapter<>(new TaskListener<URL>() {
@Override
public void taskExecuted(Task<URL> task) {
activityIndicatorBoxPane.setVisible(false);
activityIndicator.setActive(false);
URL location = task.getResult();
String file = location.getFile();
int id = Integer.parseInt(file.substring(file.lastIndexOf('/') + 1));
expense.put("id", id);
@SuppressWarnings("unchecked") List<Object> expenses = (List<Object>) expenseTableView.getTableData();
expenses.add(expense);
}
@Override
public void executeFailed(Task<URL> task) {
activityIndicatorBoxPane.setVisible(false);
activityIndicator.setActive(false);
Prompt.prompt(MessageType.ERROR, task.getFault().getMessage(), ExpensesWindow.this);
}
}));
}
}
});
}
use of org.apache.pivot.web.PostQuery 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);
}
Aggregations