@Override public Class<T> getType() { return model.getKind().getModelClass(); } };
@GET @Path("/snapshot-db") @Produces(MediaType.APPLICATION_JSON) public List<ModelData<?>> snapshotDB() { LOG.info("user {} is making snapshot", context.getRemoteUser()); ArrayList<ModelData<?>> result = new ArrayList<>(); for (DataAccessObject<?> dao : daos) { ListResult<? extends WithId<?>> l = dao.fetchAll(); for (WithId<?> entity : l.getItems()) { @SuppressWarnings({"unchecked", "rawtypes"}) ModelData<?> modelData = new ModelData(entity.getKind(), entity); result.add(modelData); } } return result; }
@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; }
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 }
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; }