use of io.syndesis.common.model.Kind in project syndesis by syndesisio.
the class IntegrationSupportHandler method importModels.
private <T extends WithId<T>> void importModels(JsonDbDao<T> export, List<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);
result.add(ChangeEvent.of("created", kind.getModelName(), id));
}
}
}
use of io.syndesis.common.model.Kind in project syndesis by syndesisio.
the class DataManager method fetch.
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;
}
use of io.syndesis.common.model.Kind in project syndesis by syndesisio.
the class DataManager method deleteAll.
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;
});
}
use of io.syndesis.common.model.Kind in project syndesis by syndesisio.
the class DataManager method update.
@SuppressWarnings("unchecked")
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");
}
validateNoDuplicateName(entity, id.get());
String idVal = id.get();
Kind kind = entity.getKind();
final T oldEntity = this.<T>fetch(kind.getModelClass(), idVal);
final T newEntity;
if (oldEntity != null && entity instanceof WithIdVersioned && entity instanceof WithVersion.AutoUpdatable) {
WithIdVersioned<T> prev = WithIdVersioned.class.cast(oldEntity);
int revision = prev.getVersion();
newEntity = (T) WithIdVersioned.class.cast(entity).withVersion(revision + 1);
} else {
newEntity = entity;
}
T previous = this.<T, T>doWithDataAccessObject(kind.getModelClass(), d -> d.update(newEntity));
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, newEntity);
broadcast(EventBus.Action.UPDATED, kind.getModelName(), idVal);
// TODO 1. properly merge the data ? + add data validation in the REST Resource
}
use of io.syndesis.common.model.Kind in project syndesis by syndesisio.
the class DataManager method fetchAll.
@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;
}
}
Aggregations