}
@SuppressWarnings("unchecked")
public HashMap<Object, HashMap<String, Object>> getAll(String clazz) {
// check memcache first
Cache cache;
HashMap<Object, HashMap<String, Object>> objects;
try {
cache = CacheManager.getInstance().getCacheFactory()
.createCache(Collections.emptyMap());
// Get the value from the cache.
objects = (HashMap<Object, HashMap<String, Object>>) cache
.get("__ALL__" + clazz);
if (objects != null) {
return objects;
}
} catch (CacheException e) {
}
// //////
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
objects = new HashMap<Object, HashMap<String, Object>>();
Query q = new Query(clazz);
PreparedQuery pq = datastore.prepare(q);
for (Entity result : pq.asIterable()) {
// iterate over each property
HashMap<String, Object> obj = new HashMap<String, Object>();
if (result.getKey().getName() != null) {
obj.put("name", result.getKey().getName());
} else {
obj.put("id", result.getKey().getId());
}
for (Entry<String, Object> entry : result.getProperties()
.entrySet()) {
if (entry.getValue() instanceof Text) {
obj.put(entry.getKey(),
((Text) entry.getValue()).getValue());
} else {
obj.put(entry.getKey(), entry.getValue());
}
}
// handle both string and numeric id's
Object key = result.getKey().getName() != null ? result
.getKey().getName() : result.getKey().getId();
objects.put(key, obj);
}
// if we had to go to the datastore, stash it in memcache for
// the next time
try {
cache = CacheManager.getInstance().getCacheFactory()
.createCache(Collections.emptyMap());
cache.put("__ALL__" + clazz, objects);
} catch (CacheException ce) {
}
return objects;
}