@Override public boolean delete(WithId<T> entity) { return this.delete(entity.getId().get()); }
@Override public boolean delete(WithId<T> entity) { return this.delete(entity.getId().get()); }
@Override public T update(T entity) { try { T previousValue = this.fetch(entity.getId().get()); // Only update if the entity existed. if( previousValue !=null ) { String dbPath = getCollectionPath()+"/:"+entity.getId().get(); byte[] json = Json.mapper().writeValueAsBytes(entity); jsondb.set(dbPath, json); } return previousValue; } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException|IOException e) { throw SyndesisServerException.launderThrowable(e); } }
@Override public T update(T entity) { try { T previousValue = this.fetch(entity.getId().get()); // Only update if the entity existed. if( previousValue !=null ) { String dbPath = getCollectionPath()+"/:"+entity.getId().get(); byte[] json = Json.writer().writeValueAsBytes(entity); jsondb.set(dbPath, json); } return previousValue; } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException|IOException e) { throw SyndesisServerException.launderThrowable(e); } }
@Override public T create(T entity) { try { String dbPath = getCollectionPath()+"/:"+entity.getId().get(); // Only create if it did not exist. if( jsondb.exists(dbPath) ) { return null; } byte[] json = Json.mapper().writeValueAsBytes(entity); jsondb.set(dbPath, json); return entity; } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException|IOException e) { throw SyndesisServerException.launderThrowable(e); } }
public <T extends WithId<T>> void store(T entity, Class<T> modelClass) { try { Optional<String> id = entity.getId(); if (!id.isPresent()) { LOGGER.warn("Cannot load entity since it's missing an id: {}", entity); } else { WithId<?> prev = null; try { prev = this.fetch(modelClass, id.get()); } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException e) { // Lets try to wipe out the previous record in case // we are running into something like a schema change. this.delete(modelClass, id.get()); } if (prev == null) { create(entity); } else { update(entity); } } } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception e) { LOGGER.warn("Cannot load entity: ", e); throw SyndesisServerException.launderThrowable(e); } }
@Override public void set(T entity) { try { String dbPath = getCollectionPath()+"/:"+entity.getId().get(); byte[] json = Json.writer().writeValueAsBytes(entity); jsondb.set(dbPath, json); } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException|IOException e) { throw SyndesisServerException.launderThrowable(e); } }
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"); } String idVal = id.get(); Kind kind = entity.getKind(); T previous = this.<T, T>doWithDataAccessObject(kind.getModelClass(), d -> d.update(entity)); 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, entity); broadcast("updated", kind.getModelName(), idVal); //TODO 1. properly merge the data ? + add data validation in the REST Resource }
@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; }
@Override public T create(T entity) { try { String dbPath = getCollectionPath()+"/:"+entity.getId().get(); // Only create if it did not exist. if( jsondb.exists(dbPath) ) { return null; } byte[] json = Json.writer().writeValueAsBytes(entity); jsondb.set(dbPath, json); return entity; } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") RuntimeException|IOException e) { throw SyndesisServerException.launderThrowable(e); } }
private <T extends WithId<T>> void importModels(JsonDbDao<T> export, ArrayList<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); if( kind == Kind.Connection ) { connectionHandler.updateBulletinBoard(id); } result.add(ChangeEvent.of("created", kind.getModelName(), id)); } } }
public <T extends WithId<T>> T create(final T entity) { Kind kind = entity.getKind(); Map<String, T> cache = caches.getCache(kind.getModelName()); Optional<String> id = entity.getId(); String idVal; final T entityToCreate; if (!id.isPresent()) { idVal = KeyGenerator.createKey(); entityToCreate = entity.withId(idVal); } else { idVal = id.get(); if (cache.containsKey(idVal)) { throw new EntityExistsException("There already exists a " + kind + " with id " + idVal); } entityToCreate = entity; } this.<T, T>doWithDataAccessObject(kind.getModelClass(), d -> d.create(entityToCreate)); cache.put(idVal, entityToCreate); broadcast("created", kind.getModelName(), idVal); return entityToCreate; }