use of org.apache.pivot.serialization.CSVSerializer 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;
}
}
}));
}
}
use of org.apache.pivot.serialization.CSVSerializer in project pivot by apache.
the class ExpenseServlet method init.
@SuppressWarnings({ "resource", "unchecked" })
@Override
public void init() throws ServletException {
CSVSerializer expenseSerializer = new CSVSerializer(Expense.class);
expenseSerializer.setKeys("date", "type", "amount", "description");
// Load the initial expense data
InputStream inputStream = ExpenseServlet.class.getResourceAsStream("expenses.csv");
try {
expenses = (List<Expense>) expenseSerializer.readObject(inputStream);
} catch (IOException exception) {
throw new ServletException(exception);
} catch (SerializationException exception) {
throw new ServletException(exception);
}
// Index the initial expenses
for (Expense expense : expenses) {
int id = nextID++;
expense.setID(id);
expenseMap.put(id, expense);
}
}
use of org.apache.pivot.serialization.CSVSerializer in project pivot by apache.
the class CSVSerializerTest method testBasicWriteObject.
@SuppressWarnings("unchecked")
// or it will generate a warning during build with Java 7
@Test
public void testBasicWriteObject() throws IOException {
List<Object> items = new ArrayList<>();
items.add(new HashMap<>(new Dictionary.Pair<String, Object>("A", "a1"), new Dictionary.Pair<String, Object>("B", "b1"), new Dictionary.Pair<String, Object>("C", "c1")));
items.add(new HashMap<>(new Dictionary.Pair<String, Object>("A", "a2"), new Dictionary.Pair<String, Object>("B", "b2"), new Dictionary.Pair<String, Object>("C", "c2")));
StringWriter writer = new StringWriter();
CSVSerializer serializer = new CSVSerializer();
serializer.setKeys("A", "B", "C");
serializer.writeObject(items, writer);
assertEquals("a1,b1,c1\r\na2,b2,c2\r\n", writer.toString());
}
use of org.apache.pivot.serialization.CSVSerializer in project pivot by apache.
the class CSVSerializerTest method testQuotedNewlineReadObject.
@Test
public void testQuotedNewlineReadObject() throws IOException, SerializationException {
StringBuilder buf = new StringBuilder();
buf.append("a,\"b\nb \",c\r\n");
StringReader reader = new StringReader(buf.toString());
CSVSerializer serializer = new CSVSerializer();
serializer.setKeys("A", "B", "C");
List<?> result = serializer.readObject(reader);
@SuppressWarnings("unchecked") Dictionary<String, Object> row = (Dictionary<String, Object>) result.get(0);
assertEquals("a", row.get("A"));
assertEquals("b\nb", row.get("B"));
assertEquals("c", row.get("C"));
}
use of org.apache.pivot.serialization.CSVSerializer in project pivot by apache.
the class CSVSerializerTest method testQuotedNewlineWriteObject.
@SuppressWarnings("unchecked")
// or it will generate a warning during build with Java 7
@Test
public void testQuotedNewlineWriteObject() throws IOException {
List<Object> items = new ArrayList<>();
items.add(new HashMap<>(new Dictionary.Pair<String, Object>("A", "a"), new Dictionary.Pair<String, Object>("B", "\nb\n"), new Dictionary.Pair<String, Object>("C", "c")));
StringWriter writer = new StringWriter();
CSVSerializer serializer = new CSVSerializer();
serializer.setKeys("A", "B", "C");
serializer.writeObject(items, writer);
assertEquals("a,\"\nb\n\",c\r\n", writer.toString());
}
Aggregations