Permalink
Browse files

Consolidate code for URL creation into private methods

  • Loading branch information...
1 parent 161d4b0 commit 918e5f2626ebf762acabdfd0b1524a02647bc86c @scotchi scotchi committed Oct 9, 2012
Showing with 33 additions and 21 deletions.
  1. +33 −21 Java/src/com/directededge/Item.java
@@ -477,7 +477,7 @@ public void clearProperty(String name)
HashMap<String, Object> options = new HashMap<String, Object>();
options.put("maxResults", maxResults);
options.put("excludeLinked", false);
- return readList(document(Arrays.asList(id, "related"), options), "related");
+ return readList(document(resource("related"), options), "related");
}
/**
@@ -494,7 +494,7 @@ public void clearProperty(String name)
public List<String> getRelated(Set<String> tags, Map<String, Object> options)
{
options.put("tags", StringUtils.join(tags, ','));
- return readList(document(Arrays.asList(id, "related"), options), "related");
+ return readList(document(resource("related"), options), "related");
}
/**
@@ -542,7 +542,7 @@ public void clearProperty(String name)
options.put("tags", StringUtils.join(tags, ','));
options.put("maxResults", maxResults);
options.put("excludeLinked", true);
- return readList(document(Arrays.asList(id, "recommended"), options), "recommended");
+ return readList(document(resource("recommended"), options), "recommended");
}
/**
@@ -559,7 +559,7 @@ public void clearProperty(String name)
public List<String> getRecommended(Set<String> tags, Map<String, Object> options)
{
options.put("tags", StringUtils.join(tags, ','));
- return readList(document(Arrays.asList(id, "recommended"), options), "recommended");
+ return readList(document(resource("recommended"), options), "recommended");
}
/**
@@ -571,21 +571,17 @@ public void save()
{
if(isCached)
{
- database.put(Arrays.asList("items", id), toXML(tags, links, properties, true));
+ database.put(resource(), toXML(Updater.Method.Replace, true));
}
else
{
- HashMap<String, Object> options = new HashMap<String, Object>();
- options.put("updateMethod", "add");
+ database.post(resource(), toXML(Updater.Method.Add, true),
+ options(Updater.Method.Add));
- database.post(Arrays.asList("items", id), toXML(tags, links, properties, true), options);
-
- if(!linksToRemove.isEmpty() ||
- !tagsToRemove.isEmpty() ||
- !propertiesToRemove.isEmpty())
+ if(subtractionNeeded())
{
- database.post(Arrays.asList("items", id),
- toXML(Updater.Method.Subtract, true), options);
+ database.post(resource(), toXML(Updater.Method.Subtract, true),
+ options(Updater.Method.Subtract));
}
}
}
@@ -598,7 +594,7 @@ public void save()
public void destroy() throws ResourceException
{
- database.delete(Arrays.asList("items", id));
+ database.delete(resource());
}
/**
@@ -640,6 +636,27 @@ public String toXML(Updater.Method method, boolean includeDocument)
return toXML(tagsToRemove, linkMap, propertyMap, includeDocument);
}
+ private List<String> resource(String... args)
+ {
+ ArrayList<String> list = new ArrayList<String>(Arrays.asList("items", id));
+ list.addAll(Arrays.asList(args));
+ return list;
+ }
+
+ private Map<String, Object> options(Updater.Method method)
+ {
+ HashMap<String, Object> options = new HashMap<String, Object>();
+ options.put("updateMethod", method.name().toLowerCase());
+ return options;
+ }
+
+ private boolean subtractionNeeded()
+ {
+ return (!linksToRemove.isEmpty() ||
+ !tagsToRemove.isEmpty() ||
+ !propertiesToRemove.isEmpty());
+ }
+
private String toXML(Set<String> tags, Map<String, Map<String, Integer>> links,
Map<String, String> properties, boolean includeDocument)
{
@@ -708,7 +725,7 @@ private void read()
return;
}
- Document doc = document(Arrays.asList(id), new HashMap<String, Object>());
+ Document doc = document(resource(), new HashMap<String, Object>());
NodeList nodes = doc.getElementsByTagName("link");
for(int i = 0; i < nodes.getLength(); i++)
@@ -765,11 +782,6 @@ private void read()
private Document document(List<String> resources, Map<String, Object> options)
{
- List<String> withItemPrefix = new ArrayList<String>();
- withItemPrefix.add("items");
- withItemPrefix.addAll(resources);
- resources = withItemPrefix;
-
try
{
DocumentBuilder builder =

0 comments on commit 918e5f2

Please sign in to comment.