Skip to content

Instantly share code, notes, and snippets.

@enesakar
Created March 8, 2012 08:01
Show Gist options
  • Save enesakar/1999548 to your computer and use it in GitHub Desktop.
Save enesakar/1999548 to your computer and use it in GitHub Desktop.
public void addContact(String phone, String name) {
List<ReplaceableAttribute> attrList = new ArrayList<ReplaceableAttribute>();
attrList.add(new ReplaceableAttribute("Phone", phone, true));
attrList.add(new ReplaceableAttribute("Name", name, true));
sdb.putAttributes(new PutAttributesRequest("Contact", name, attrList));
}
public Contact loadContact(String name) {
SelectRequest req = new SelectRequest("select * from Contact where itemName() = '" + name + "'");
SelectResult result = sdb.select(req);
if (!result.getItems().isEmpty()) {
Item item = result.getItems().get(0);
return formContact(item.getAttributes());
} else {
return null;
}
}
public Set<String> loadAllKeys() {
Set<String> res = new HashSet<String>();
SelectRequest req = new SelectRequest("select phone from Contact");
SelectResult result = sdb.select(req);
for(Item item : result.getItems()) {
res.add(item.getName());
}
return res;
}
public List<Contact> loadAllContacts(Collection<String> keys) {
List<Contact> contactList = new ArrayList<Contact>();
StringBuffer keystr = new StringBuffer();
String[] keyList = keys.toArray(new String[0]);
for (int i = 0; i < keyList.length; i++) {
if(i == 0) {
keystr.append("'"+ keyList[i] +"'");
}
else{
keystr.append(", '"+ keyList[i] +"'");
}
}
SelectRequest req = new SelectRequest("select * from Contact where itemName() in ("+ keystr +")");
SelectResult result = sdb.select(req);
for (Item item : result.getItems()) {
contactList.add(formContact(item.getAttributes()));
}
return contactList;
}
private Contact formContact(List<Attribute> attributes) {
Contact contact = new Contact();
for (Attribute attr : attributes) {
if (attr.getName().equals("Name"))
contact.setName(attr.getValue());
if (attr.getName().equals("Phone"))
contact.setPhone(attr.getValue());
}
return contact;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment