@Override public Class<T> getType() { return model.getKind().getModelClass(); } };
@DELETE @Consumes("application/json") @Path(value = "/{id}") default void delete(@NotNull @PathParam("id") @ApiParam(required = true) String id) { Class<T> modelClass = resourceKind().getModelClass(); getDataManager().delete(modelClass, id); }
@GET @Produces(MediaType.APPLICATION_JSON) @Path(value = "/{id}") default T get(@NotNull @PathParam("id") @ApiParam(required = true) String id) { Class<T> modelClass = resourceKind().getModelClass(); T result = getDataManager().fetch(modelClass, id); if( result == null ) { throw new EntityNotFoundException(); } return result; }
public <T extends WithId<T>> void store(ModelData<T> modelData) { try { final Kind kind = modelData.getKind(); final T entity = modelData.getData(); LOGGER.debug("{}:{}", kind, modelData.getDataAsJson()); store(entity, kind.getModelClass()); } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception e) { LOGGER.warn("Cannot load entity from file: ", 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 }
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; }
@PATCH @Path(value = "/{id}") @Consumes(MediaType.APPLICATION_JSON) default void patch(@NotNull @PathParam("id") @ApiParam(required = true) String id, @NotNull JsonNode patchJson) throws IOException { Class<T> modelClass = resourceKind().getModelClass(); final T existing = getDataManager().fetch(modelClass, id); if( existing == null ) { throw new EntityNotFoundException(); } JsonNode document = Json.reader().readTree(Json.writer().writeValueAsString(existing)); // Attempt to apply the patch... final JsonMergePatch patch; try { patch = JsonMergePatch.fromJson(patchJson); document = patch.apply(document); } catch (JsonPatchException e) { throw new WebApplicationException(e, Response.Status.BAD_REQUEST); } // Convert the Json back to an entity. T obj = Json.reader().forType(modelClass).readValue(Json.writer().writeValueAsBytes(document)); if (this instanceof Validating) { final Validator validator = ((Validating<?>) this).getValidator(); final Set<ConstraintViolation<T>> violations = validator.validate(obj, AllValidations.class); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } getDataManager().update(obj); }
@Override public ListResult<Integration> list(UriInfo uriInfo) { Class<Integration> clazz = resourceKind().getModelClass(); return getDataManager().fetchAll(Integration.class, new DeletedFilter(), new ReflectiveSorter<>(clazz, new SortOptionsFromQueryParams(uriInfo)), new PaginationFilter<>(new PaginationOptionsFromQueryParams(uriInfo)) ); }
@GET @Produces(MediaType.APPLICATION_JSON) @ApiImplicitParams({ @ApiImplicitParam( name = "sort", value = "Sort the result list according to the given field value", paramType = "query", dataType = "string"), @ApiImplicitParam( name = "direction", value = "Sorting direction when a 'sort' field is provided. Can be 'asc' " + "(ascending) or 'desc' (descending)", paramType = "query", dataType = "string"), @ApiImplicitParam( name = "page", value = "Page number to return", paramType = "query", dataType = "integer", defaultValue = "1"), @ApiImplicitParam( name = "per_page", value = "Number of records per page", paramType = "query", dataType = "integer", defaultValue = "20"), @ApiImplicitParam( name = "query", value = "The search query to filter results on", paramType = "query", dataType = "string"), }) default ListResult<T> list(@Context UriInfo uriInfo) { Class<T> clazz = resourceKind().getModelClass(); return getDataManager().fetchAll( clazz, new ReflectiveFilterer<>(clazz, new FilterOptionsFromQueryParams(uriInfo).getFilters()), new ReflectiveSorter<>(clazz, new SortOptionsFromQueryParams(uriInfo)), new PaginationFilter<>(new PaginationOptionsFromQueryParams(uriInfo)) ); }