Search in sources :

Example 1 with RepositorySearchSpecification

use of org.haiku.haikudepotserver.repository.model.RepositorySearchSpecification in project haikudepotserver by haiku.

the class RepositoryApiImpl method searchRepositories.

@Override
public SearchRepositoriesResult searchRepositories(SearchRepositoriesRequest request) {
    Preconditions.checkNotNull(request);
    final ObjectContext context = serverRuntime.newContext();
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.REPOSITORY_LIST)) {
        throw new AccessDeniedException("unable to view lists of repositories");
    }
    if (null != request.includeInactive && request.includeInactive) {
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.REPOSITORY_LIST_INACTIVE)) {
            throw new AccessDeniedException("unable to view lists of inactive repositories");
        }
    }
    RepositorySearchSpecification specification = new RepositorySearchSpecification();
    String exp = request.expression;
    if (null != exp) {
        exp = Strings.emptyToNull(exp.trim().toLowerCase());
    }
    specification.setExpression(exp);
    if (null != request.expressionType) {
        specification.setExpressionType(PkgSearchSpecification.ExpressionType.valueOf(request.expressionType.name()));
    }
    specification.setLimit(request.limit);
    specification.setOffset(request.offset);
    specification.setIncludeInactive(null != request.includeInactive && request.includeInactive);
    SearchRepositoriesResult result = new SearchRepositoriesResult();
    result.total = repositoryService.total(context, specification);
    result.items = Collections.emptyList();
    if (result.total > 0) {
        List<Repository> searchedRepositories = repositoryService.search(context, specification);
        result.items = searchedRepositories.stream().map(sr -> {
            SearchRepositoriesResult.Repository resultRepository = new SearchRepositoriesResult.Repository();
            resultRepository.active = sr.getActive();
            resultRepository.name = sr.getName();
            resultRepository.code = sr.getCode();
            return resultRepository;
        }).collect(Collectors.toList());
    }
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) RepositorySearchSpecification(org.haiku.haikudepotserver.repository.model.RepositorySearchSpecification) Repository(org.haiku.haikudepotserver.dataobjects.Repository) ObjectContext(org.apache.cayenne.ObjectContext) SearchRepositoriesResult(org.haiku.haikudepotserver.api1.model.repository.SearchRepositoriesResult)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)1 SearchRepositoriesResult (org.haiku.haikudepotserver.api1.model.repository.SearchRepositoriesResult)1 Repository (org.haiku.haikudepotserver.dataobjects.Repository)1 RepositorySearchSpecification (org.haiku.haikudepotserver.repository.model.RepositorySearchSpecification)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1