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>> 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; }
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; }); }
public <T extends WithId<T>> void update(T entity) { Optional<String> id = entity.getId(); if (!id.isPresent()) { throw new EntityNotFoundException("Setting the id on the entity is required for updates"); } String idVal = id.get(); Kind kind = entity.getKind(); T previous = this.<T, T>doWithDataAccessObject(kind.getModelClass(), d -> d.update(entity)); Map<String, T> cache = caches.getCache(kind.getModelName()); if (!cache.containsKey(idVal) && previous==null) { throw new EntityNotFoundException("Can not find " + kind + " with id " + idVal); } cache.put(idVal, entity); broadcast("updated", kind.getModelName(), idVal); //TODO 1. properly merge the data ? + add data validation in the REST Resource }
@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>> T create(final T entity) { Kind kind = entity.getKind(); Map<String, T> cache = caches.getCache(kind.getModelName()); Optional<String> id = entity.getId(); String idVal; final T entityToCreate; if (!id.isPresent()) { idVal = KeyGenerator.createKey(); entityToCreate = entity.withId(idVal); } else { idVal = id.get(); if (cache.containsKey(idVal)) { throw new EntityExistsException("There already exists a " + kind + " with id " + idVal); } entityToCreate = entity; } this.<T, T>doWithDataAccessObject(kind.getModelClass(), d -> d.create(entityToCreate)); cache.put(idVal, entityToCreate); broadcast("created", kind.getModelName(), idVal); return entityToCreate; }
private <T extends WithId<T>> void importModels(JsonDbDao<T> export, ArrayList<ChangeEvent> result) { for (T item : export.fetchAll().getItems()) { Kind kind = item.getKind(); String id = item.getId().get(); if (dataManager.fetch(export.getType(), id) == null) { dataManager.create(item); if( kind == Kind.Connection ) { connectionHandler.updateBulletinBoard(id); } result.add(ChangeEvent.of("created", kind.getModelName(), id)); } } }
private void importIntegrations(SecurityContext sec, JsonDbDao<Integration> export, ArrayList<ChangeEvent> result) { for (Integration integration : export.fetchAll().getItems()) { Integration.Builder builder = new Integration.Builder() .createFrom(integration) .isDeleted(false) .updatedAt(System.currentTimeMillis()); // Do we need to create it? String id = integration.getId().get(); Integration previous = dataManager.fetch(Integration.class, id); if (previous == null) { LOG.info("Creating integration: {}", integration.getName()); integrationHandler.create(sec, builder.build()); result.add(ChangeEvent.of("created", integration.getKind().getModelName(), id)); } else { LOG.info("Updating integration: {}", integration.getName()); integrationHandler.update(id, builder.version(previous.getVersion()+1).build()); result.add(ChangeEvent.of("updated", integration.getKind().getModelName(), id)); } integrationHandler.updateBulletinBoard(id); break; } }