Search in sources :

Example 1 with WithId

use of io.syndesis.common.model.WithId 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 2 with WithId

use of io.syndesis.common.model.WithId 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 3 with WithId

use of io.syndesis.common.model.WithId in project syndesis by syndesisio.

the class DataManager method delete.

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(EventBus.Action.DELETED, kind.getModelName(), id);
        return true;
    }
    return false;
}
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) WithId(io.syndesis.common.model.WithId) Kind(io.syndesis.common.model.Kind) EntityNotFoundException(javax.persistence.EntityNotFoundException)

Example 4 with WithId

use of io.syndesis.common.model.WithId in project syndesis by syndesisio.

the class UniquePropertyValidator method isValid.

@Override
public boolean isValid(final WithId<?> value, final ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }
    final PropertyAccessor bean = new BeanWrapperImpl(value);
    final String propertyValue = String.valueOf(bean.getPropertyValue(property));
    @SuppressWarnings({ "rawtypes", "unchecked" }) final Class<WithId> modelClass = (Class) value.getKind().modelClass;
    @SuppressWarnings("unchecked") final Set<String> ids = dataManager.fetchIdsByPropertyValue(modelClass, property, propertyValue);
    final boolean isUnique = ids.isEmpty() || value.getId().map(id -> ids.contains(id)).orElse(false);
    if (!isUnique) {
        if (ids.stream().allMatch(id -> consideredValidByException(modelClass, id))) {
            return true;
        }
        context.disableDefaultConstraintViolation();
        context.unwrap(HibernateConstraintValidatorContext.class).addExpressionVariable("nonUnique", propertyValue).buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()).addPropertyNode(property).addConstraintViolation();
    }
    return isUnique;
}
Also used : WithId(io.syndesis.common.model.WithId) PropertyAccessor(org.springframework.beans.PropertyAccessor) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl)

Aggregations

WithId (io.syndesis.common.model.WithId)4 Kind (io.syndesis.common.model.Kind)3 ChangeEvent (io.syndesis.common.model.ChangeEvent)2 ListResult (io.syndesis.common.model.ListResult)2 ModelData (io.syndesis.common.model.ModelData)2 WithIdVersioned (io.syndesis.common.model.WithIdVersioned)2 WithVersion (io.syndesis.common.model.WithVersion)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 SyndesisServerException (io.syndesis.common.util.SyndesisServerException)2 CacheManager (io.syndesis.common.util.cache.CacheManager)2 ReadApiClientData (io.syndesis.server.dao.init.ReadApiClientData)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2