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;
}
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;
});
}
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;
}
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;
}
Aggregations