Search in sources :

Example 1 with WithVersion

use of io.syndesis.common.model.WithVersion 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
}
Also used : WithVersion(io.syndesis.common.model.WithVersion) Kind(io.syndesis.common.model.Kind) EntityNotFoundException(javax.persistence.EntityNotFoundException) WithIdVersioned(io.syndesis.common.model.WithIdVersioned)

Aggregations

Kind (io.syndesis.common.model.Kind)1 WithIdVersioned (io.syndesis.common.model.WithIdVersioned)1 WithVersion (io.syndesis.common.model.WithVersion)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1