Search in sources :

Example 6 with CSVSerializer

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;
                }
            }
        }));
    }
}
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)

Example 7 with CSVSerializer

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);
    }
}
Also used : ServletException(javax.servlet.ServletException) SerializationException(org.apache.pivot.serialization.SerializationException) InputStream(java.io.InputStream) IOException(java.io.IOException) CSVSerializer(org.apache.pivot.serialization.CSVSerializer)

Example 8 with CSVSerializer

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());
}
Also used : StringWriter(java.io.StringWriter) ArrayList(org.apache.pivot.collections.ArrayList) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Example 9 with CSVSerializer

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"));
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) StringReader(java.io.StringReader) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Example 10 with CSVSerializer

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());
}
Also used : StringWriter(java.io.StringWriter) ArrayList(org.apache.pivot.collections.ArrayList) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Aggregations

CSVSerializer (org.apache.pivot.serialization.CSVSerializer)11 Test (org.junit.Test)9 StringReader (java.io.StringReader)5 ArrayList (org.apache.pivot.collections.ArrayList)5 StringWriter (java.io.StringWriter)4 Dictionary (org.apache.pivot.collections.Dictionary)4 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DateFormat (java.text.DateFormat)1 Date (java.util.Date)1 ServletException (javax.servlet.ServletException)1 List (org.apache.pivot.collections.List)1 CSVSerializerListener (org.apache.pivot.serialization.CSVSerializerListener)1 SerializationException (org.apache.pivot.serialization.SerializationException)1 Task (org.apache.pivot.util.concurrent.Task)1 TaskListener (org.apache.pivot.util.concurrent.TaskListener)1 GetQuery (org.apache.pivot.web.GetQuery)1