public String getCollectionPath() { return "/"+Kind.from(getType()).getModelName()+"s"; }
public String getCollectionPath() { return "/"+Kind.from(getType()).getModelName()+"s"; }
public <T extends WithId<T>> T fetch(Class<T> model, String id) { Kind kind = Kind.from(model); Map<String, T> cache = caches.getCache(kind.getModelName()); T value = cache.get(id); if ( value == null) { value = doWithDataAccessObject(model, d -> d.fetch(id)); if (value != null) { cache.put(id, value); } } return value; }
public <T extends WithId<T>> void deleteAll(Class<T> model) { Kind kind = Kind.from(model); Map<String, WithId<T>> cache = caches.getCache(kind.getModelName()); cache.clear(); doWithDataAccessObject(model, d -> { d.deleteAll(); return null; }); }
@SafeVarargs @SuppressWarnings({"unchecked", "varargs"}) public final <T extends WithId<T>> ListResult<T> fetchAll(Class<T> model, Function<ListResult<T>, ListResult<T>>... operators) { ListResult<T> result; if (getDataAccessObject(model) != null) { return doWithDataAccessObject(model, d -> d.fetchAll(operators)); } else { Kind kind = Kind.from(model); Map<String, T> cache = caches.getCache(kind.getModelName()); result = ListResult.of(cache.values()); if (operators == null) { return result; } for (Function<ListResult<T>, ListResult<T>> operator : operators) { result = operator.apply(result); } return result; } }
public <T extends WithId<T>> boolean delete(Class<T> model, String id) { if (id == null || id.equals("")) { throw new EntityNotFoundException("Setting the id on the entity is required for updates"); } Kind kind = Kind.from(model); Map<String, WithId<T>> cache = caches.getCache(kind.getModelName()); // Remove it out of the cache WithId<T> entity = cache.remove(id); boolean deletedInCache = entity != null; // And out of the DAO boolean deletedFromDAO = Boolean.TRUE.equals(doWithDataAccessObject(model, d -> d.delete(id))); // Return true if the entity was found in any of the two. if ( deletedInCache || deletedFromDAO ) { broadcast("deleted", kind.getModelName(), id); return true; } return false; }