Search in sources :

Example 1 with Kind

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));
        }
    }
}
Also used : GET(javax.ws.rs.GET) POST(javax.ws.rs.POST) Kind(io.syndesis.common.model.Kind)

Example 2 with Kind

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;
}
Also used : ModelData(io.syndesis.common.model.ModelData) KeyGenerator(io.syndesis.common.util.KeyGenerator) ResourcePatternUtils(org.springframework.core.io.support.ResourcePatternUtils) Kind(io.syndesis.common.model.Kind) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Value(org.springframework.beans.factory.annotation.Value) Connection(io.syndesis.common.model.connection.Connection) Service(org.springframework.stereotype.Service) Map(java.util.Map) EntityNotFoundException(javax.persistence.EntityNotFoundException) Resource(org.springframework.core.io.Resource) StreamUtils(org.springframework.util.StreamUtils) Logger(org.slf4j.Logger) ResourceLoader(org.springframework.core.io.ResourceLoader) EntityExistsException(javax.persistence.EntityExistsException) WithId(io.syndesis.common.model.WithId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Connector(io.syndesis.common.model.connection.Connector) IOException(java.io.IOException) ListResult(io.syndesis.common.model.ListResult) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) PersistenceException(javax.persistence.PersistenceException) ReadApiClientData(io.syndesis.server.dao.init.ReadApiClientData) SyndesisServerException(io.syndesis.common.util.SyndesisServerException) WithVersion(io.syndesis.common.model.WithVersion) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) WithIdVersioned(io.syndesis.common.model.WithIdVersioned) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) EventBus(io.syndesis.common.util.EventBus) CacheManager(io.syndesis.common.util.cache.CacheManager) ChangeEvent(io.syndesis.common.model.ChangeEvent) Json(io.syndesis.common.util.Json) InputStream(java.io.InputStream) Kind(io.syndesis.common.model.Kind)

Example 3 with Kind

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;
    });
}
Also used : WithId(io.syndesis.common.model.WithId) Kind(io.syndesis.common.model.Kind)

Example 4 with Kind

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

Example 5 with Kind

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;
    }
}
Also used : ListResult(io.syndesis.common.model.ListResult) Kind(io.syndesis.common.model.Kind)

Aggregations

Kind (io.syndesis.common.model.Kind)11 EntityNotFoundException (javax.persistence.EntityNotFoundException)5 EntityExistsException (javax.persistence.EntityExistsException)4 ListResult (io.syndesis.common.model.ListResult)3 WithId (io.syndesis.common.model.WithId)3 WithIdVersioned (io.syndesis.common.model.WithIdVersioned)3 WithVersion (io.syndesis.common.model.WithVersion)3 SyndesisServerException (io.syndesis.common.util.SyndesisServerException)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 PersistenceException (javax.persistence.PersistenceException)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 ChangeEvent (io.syndesis.common.model.ChangeEvent)2 ModelData (io.syndesis.common.model.ModelData)2 Connection (io.syndesis.common.model.connection.Connection)2 Connector (io.syndesis.common.model.connection.Connector)2 EventBus (io.syndesis.common.util.EventBus)2 Json (io.syndesis.common.util.Json)2 KeyGenerator (io.syndesis.common.util.KeyGenerator)2