use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project devspaces-images by redhat-developer.
the class UserDevfileDaoTest method shouldNotUpdateWorkspaceWhichDoesNotExist.
@Test
public void shouldNotUpdateWorkspaceWhichDoesNotExist() throws Exception {
// given
final UserDevfileImpl userDevfile = devfiles[0];
userDevfile.setId("non-existing-devfile");
// when
Optional<UserDevfile> result = userDevfileDaoDao.update(userDevfile);
// then
assertFalse(result.isPresent());
}
use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project devspaces-images by redhat-developer.
the class UserDevfileDaoTest method shouldBeAbleToGetAvailableToUserDevfilesWithFilterAndLimit.
@Test
public void shouldBeAbleToGetAvailableToUserDevfilesWithFilterAndLimit() throws ServerException, NotFoundException, ConflictException {
// given
final UserDevfileImpl update = devfiles[0];
update.setName("New345Name");
userDevfileDaoDao.update(update);
// when
final Page<UserDevfile> result = userDevfileDaoDao.getDevfiles(12, 0, ImmutableList.of(new Pair<>("name", "like:devfileName%")), Collections.emptyList());
// then
assertEquals(result.getItems().size(), 9);
}
use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project devspaces-images by redhat-developer.
the class UserDevfileManager method updateUserDevfile.
/**
* Updates an existing user devfile in accordance to the new configuration.
*
* <p>Note: Replace strategy is used for user devfile update, it means that existing devfile data
* will be replaced with given {@code update}.
*
* @param update user devfile update
* @return updated user devfile
* @throws NullPointerException when {@code update} is null
* @throws ConflictException when any conflict occurs.
* @throws NotFoundException when user devfile with given id not found
* @throws ServerException when any server error occurs
*/
public UserDevfile updateUserDevfile(UserDevfile update) throws ConflictException, NotFoundException, ServerException {
requireNonNull(update);
Optional<UserDevfile> result = userDevfileDao.update(update);
UserDevfile devfile = result.orElseThrow(() -> new NotFoundException(format("Devfile with id '%s' doesn't exist", update.getId())));
LOG.debug("UserDevfile '{}' with id '{}' update by user '{}'", devfile.getName(), devfile.getId(), EnvironmentContext.getCurrent().getSubject().getUserName());
eventService.publish(new DevfileUpdatedEvent(devfile));
return devfile;
}
use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project devspaces-images by redhat-developer.
the class JpaUserDevfileDao method doGetDevfiles.
@Transactional(rollbackOn = { ServerException.class })
protected Page<UserDevfile> doGetDevfiles(int maxItems, int skipCount, List<Pair<String, String>> filter, List<Pair<String, String>> order, Supplier<UserDevfileSearchQueryBuilder> queryBuilderSupplier) throws ServerException {
if (filter != null && !filter.isEmpty()) {
List<Pair<String, String>> invalidFilter = filter.stream().filter(p -> !VALID_SEARCH_FIELDS.contains(p.first.toLowerCase())).collect(toList());
if (!invalidFilter.isEmpty()) {
throw new IllegalArgumentException("Filtering allowed only by " + VALID_SEARCH_FIELDS + " but got: " + invalidFilter);
}
}
List<Pair<String, String>> effectiveOrder = DEFAULT_ORDER;
if (order != null && !order.isEmpty()) {
List<Pair<String, String>> invalidOrder = order.stream().filter(p -> !VALID_ORDER_FIELDS.contains(p.first.toLowerCase())).collect(toList());
if (!invalidOrder.isEmpty()) {
throw new IllegalArgumentException("Order allowed only by " + VALID_ORDER_FIELDS + "¬ but got: " + invalidOrder);
}
List<Pair<String, String>> invalidSortOrder = order.stream().filter(p -> !p.second.equalsIgnoreCase("asc") && !p.second.equalsIgnoreCase("desc")).collect(Collectors.toList());
if (!invalidSortOrder.isEmpty()) {
throw new IllegalArgumentException("Invalid sort order direction. Possible values are 'asc' or 'desc' but got: " + invalidSortOrder);
}
effectiveOrder = order;
}
try {
final long count = queryBuilderSupplier.get().withFilter(filter).buildCountQuery().getSingleResult();
if (count == 0) {
return new Page<>(emptyList(), skipCount, maxItems, count);
}
List<UserDevfileImpl> result = queryBuilderSupplier.get().withFilter(filter).withOrder(effectiveOrder).withMaxItems(maxItems).withSkipCount(skipCount).buildSelectItemsQuery().getResultList().stream().map(UserDevfileImpl::new).collect(toList());
return new Page<>(result, skipCount, maxItems, count);
} catch (RuntimeException x) {
throw new ServerException(x.getLocalizedMessage(), x);
}
}
use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project devspaces-images by redhat-developer.
the class DevfileService method getUserDevfiles.
@GET
@Path("search")
@Produces(APPLICATION_JSON)
@Operation(summary = "Get devfiles which user can read. This operation can be performed only by authorized user. " + "It is possible to add additional constraints for the desired devfiles by specifying\n" + "multiple query parameters that is representing fields of the devfile. All constrains\n" + "would be combined with \"And\" condition. Also, it is possible to specify 'like:' prefix\n" + "for the query parameters. In this case instead of an exact match would be used SQL pattern like search.\n" + "Examples id=sdfsdf5&devfile.meta.name=like:%dfdf&", responses = { @ApiResponse(responseCode = "200", description = "The devfiles successfully fetched", content = @Content(array = @ArraySchema(schema = @Schema(implementation = UserDevfileDto.class)))), @ApiResponse(responseCode = "500", description = "Internal server error occurred during devfiles fetching") })
public Response getUserDevfiles(@Parameter(description = "The number of the items to skip") @DefaultValue("0") @QueryParam("skipCount") Integer skipCount, @Parameter(description = "The limit of the items in the response, default is 30, maximum 60") @DefaultValue("30") @QueryParam("maxItems") Integer maxItems, @Parameter(description = "A list of fields and directions of sort. By default items would be sorted by id. Example id:asc,name:desc.") @QueryParam("order") String order) throws ServerException, BadRequestException {
final Set<String> skip = ImmutableSet.of("token", "skipCount", "maxItems", "order");
Map<String, Set<String>> queryParams = URLEncodedUtils.parse(uriInfo.getRequestUri());
final List<Pair<String, String>> query = queryParams.entrySet().stream().filter(param -> !param.getValue().isEmpty()).filter(param -> !skip.contains(param.getKey())).map(entry -> Pair.of(entry.getKey(), entry.getValue().iterator().next())).collect(toList());
List<Pair<String, String>> searchOrder = Collections.emptyList();
if (order != null && !order.isEmpty()) {
try {
searchOrder = Splitter.on(",").trimResults().omitEmptyStrings().withKeyValueSeparator(":").split(order).entrySet().stream().map(e -> Pair.of(e.getKey(), e.getValue())).collect(toList());
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid `order` query parameter format." + e.getMessage());
}
}
Page<? extends UserDevfile> userDevfilesPage = userDevfileManager.getUserDevfiles(maxItems, skipCount, query, searchOrder);
List<UserDevfileDto> list = userDevfilesPage.getItems().stream().map(DtoConverter::asDto).map(dto -> linksInjector.injectLinks(asDto(dto), getServiceContext())).collect(toList());
return Response.ok().entity(list).header("Link", createLinkHeader(userDevfilesPage)).build();
}
Aggregations