/* * (non-Javadoc) * * @see com.dtcc.awsticker.dao.IDaoTicker#getAllTickerRows() */ public List<TickerRow> getAllTickerRows() { // return LocalTickerTable.getAll(); List<TickerRow> rows = new ArrayList<TickerRow>(); ScanRequest scanRequest = new ScanRequest(tableName); ScanResult scanResult = db.scan(scanRequest); List<Map<String, AttributeValue>> items = scanResult.getItems(); if (items == null || items.isEmpty()) return rows; for (Map<String, AttributeValue> item : items) { Integer index = new Integer(0); try { index = Integer.decode(item.get("Index").getN()); } catch (NumberFormatException e) { log.warning("bad key " + e); } TickerRow tr = new TickerRow(); tr.setIndex(index); ItemToModel(item, tr); rows.add(tr); } return rows; }
/** transfers data from Item to TickerRow */ private void ItemToModel(Map<String, AttributeValue> item, TickerRow tr) { for (String attribute : item.keySet()) { if (attribute.equals("Name")) { tr.setName(item.get(attribute).getS()); } else if (attribute.equals("Price")) { tr.setPrice(item.get(attribute).getS()); } else if (attribute.equals("Number")) { tr.setNumber(item.get(attribute).getS()); } else if (attribute.equals("Time")) { tr.setDateTime(item.get(attribute).getS()); } } }
private Integer getNextIndex() { int nextIndex = 0; // TODO not efficient, but... List<TickerRow> list = getAllTickerRows(); // Get the maximum value. for (TickerRow tickerRow : list) { if (tickerRow.getIndex().intValue() > nextIndex) { nextIndex = tickerRow.getIndex().intValue(); } } // Add one for the next one. nextIndex += 1; return new Integer(nextIndex); }
/* * (non-Javadoc) * * @see com.dtcc.awsticker.dao.IDaoTicker#getTickerRow(java.lang.Integer) */ public TickerRow getTickerRow(Integer index) { // return LocalTickerTable.get(index); AttributeValue id = new AttributeValue().withN(index + ""); Key primaryKey = new Key().withHashKeyElement(id); GetItemRequest request = new GetItemRequest().withTableName(tableName).withKey(primaryKey); GetItemResult result = db.getItem(request); if (result.getItem() == null) return null; TickerRow tr = new TickerRow(); tr.setIndex(index); ItemToModel(result.getItem(), tr); return tr; }
/* * (non-Javadoc) * * @see com.dtcc.awsticker.dao.IDaoTicker#putTickerRow(java.lang.Integer, * com.dtcc.awsticker.domain.TickerRow) */ public void putTickerRow(Integer index, TickerRow tickerRow) { // LocalTickerTable.put(index, tickerRow); Map<String, AttributeValue> item = new HashMap<String, AttributeValue>(); item.put("Index", new AttributeValue().withN("" + tickerRow.getIndex())); // item.put("Date", new AttributeValue(tickerRow.getDate())); item.put("Name", new AttributeValue(tickerRow.getName())); item.put("Number", new AttributeValue(tickerRow.getNumber())); item.put("Price", new AttributeValue(tickerRow.getPrice())); item.put("Time", new AttributeValue(tickerRow.getDateTime())); item.put("CreatedTimeMs", new AttributeValue("" + System.currentTimeMillis())); PutItemRequest putItemRequest = new PutItemRequest(tableName, item); PutItemResult putItemResult = db.putItem(putItemRequest); }