/** * Gets the Item based on the item name field which is the same as id in SimpleJPA. * If the item doesn't exist then null is returned. * * @param db * @param domainName * @param itemName * @param consistentRead true to read consistently, false to use eventual consistency * @return * @throws AmazonClientException */ public static Item findItemById(AmazonSimpleDB db, String domainName, String itemName, boolean consistentRead) throws AmazonClientException { GetAttributesResult results = db.getAttributes(new GetAttributesRequest() .withConsistentRead(consistentRead) .withDomainName(domainName) .withItemName(itemName)); if(results.getAttributes().size() == 0) { return null; } Item item = new Item(itemName, results.getAttributes()); return item; } }
private void putAndDelete(String domainName, String oldAttributeName, String newAttributeName, List<Item> items) throws AmazonClientException { AmazonSimpleDB db = factory.getSimpleDb(); for (Item item : items) { GetAttributesResult getOldResults = db.getAttributes(new GetAttributesRequest().withDomainName(domainName).withConsistentRead(true).withItemName( item.getName()).withAttributeNames(oldAttributeName)); List<Attribute> oldAtts = getOldResults.getAttributes(); if (oldAtts.size() > 0) { Attribute oldAtt = oldAtts.get(0); List<ReplaceableAttribute> atts = new ArrayList<ReplaceableAttribute>(); atts.add(new ReplaceableAttribute(newAttributeName, oldAtt.getValue(), true)); db.putAttributes(new PutAttributesRequest().withDomainName(domainName).withItemName(item.getName()).withAttributes(atts)); db.deleteAttributes(new DeleteAttributesRequest().withDomainName(domainName).withItemName(item.getName()).withAttributes(oldAtts)); } } }