Search in sources :

Example 1 with ExtensionQueryParam

use of org.eclipse.openvsx.adapter.ExtensionQueryParam in project openvsx by eclipse.

the class VSCodeAdapter method extensionQuery.

@PostMapping(path = "/vscode/gallery/extensionquery", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@CrossOrigin
public ExtensionQueryResult extensionQuery(@RequestBody ExtensionQueryParam param) {
    String queryString = null;
    String category = null;
    PageRequest pageRequest;
    String sortOrder;
    String sortBy;
    Set<String> extensionIds;
    Set<String> extensionNames;
    if (param.filters == null || param.filters.isEmpty()) {
        pageRequest = PageRequest.of(0, DEFAULT_PAGE_SIZE);
        sortBy = "relevance";
        sortOrder = "desc";
        extensionIds = Collections.emptySet();
        extensionNames = Collections.emptySet();
    } else {
        var filter = param.filters.get(0);
        extensionIds = new HashSet<>(filter.findCriteria(FILTER_EXTENSION_ID));
        extensionNames = new HashSet<>(filter.findCriteria(FILTER_EXTENSION_NAME));
        queryString = filter.findCriterion(FILTER_SEARCH_TEXT);
        if (queryString == null)
            queryString = filter.findCriterion(FILTER_TAG);
        category = filter.findCriterion(FILTER_CATEGORY);
        var pageSize = filter.pageSize > 0 ? filter.pageSize : DEFAULT_PAGE_SIZE;
        pageRequest = PageRequest.of(filter.pageNumber - 1, pageSize);
        sortOrder = getSortOrder(filter.sortOrder);
        sortBy = getSortBy(filter.sortBy);
    }
    Long totalCount = null;
    List<ExtensionDTO> extensions;
    if (!extensionIds.isEmpty()) {
        extensions = repositories.findAllActiveExtensionDTOsByPublicId(extensionIds);
    } else if (!extensionNames.isEmpty()) {
        extensions = extensionNames.stream().map(name -> name.split("\\.")).filter(split -> split.length == 2).map(split -> {
            var name = split[1];
            var namespaceName = split[0];
            return repositories.findActiveExtensionDTOByNameAndNamespaceName(name, namespaceName);
        }).filter(Objects::nonNull).collect(Collectors.toList());
    } else if (!search.isEnabled()) {
        extensions = Collections.emptyList();
    } else {
        try {
            var offset = pageRequest.getPageNumber() * pageRequest.getPageSize();
            var searchOptions = new SearchUtilService.Options(queryString, category, pageRequest.getPageSize(), offset, sortOrder, sortBy, false);
            var searchResult = search.search(searchOptions, pageRequest);
            totalCount = searchResult.getTotalHits();
            var ids = searchResult.getSearchHits().stream().map(hit -> hit.getContent().id).collect(Collectors.toList());
            var extensionsMap = repositories.findAllActiveExtensionDTOsById(ids).stream().collect(Collectors.toMap(e -> e.getId(), e -> e));
            // keep the same order as search results
            extensions = ids.stream().map(extensionsMap::get).filter(Objects::nonNull).collect(Collectors.toList());
        } catch (ErrorResultException exc) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, exc.getMessage(), exc);
        }
    }
    if (totalCount == null) {
        totalCount = (long) extensions.size();
    }
    var flags = param.flags;
    Map<ExtensionDTO, List<ExtensionVersionDTO>> extensionVersions;
    if (test(flags, FLAG_INCLUDE_LATEST_VERSION_ONLY)) {
        extensionVersions = extensions.stream().map(ExtensionDTO::getLatest).collect(Collectors.groupingBy(ExtensionVersionDTO::getExtension));
    } else if (test(flags, FLAG_INCLUDE_VERSIONS) || test(flags, FLAG_INCLUDE_VERSION_PROPERTIES)) {
        var idMap = extensions.stream().collect(Collectors.toMap(e -> e.getId(), e -> e));
        // similar to ExtensionVersion.SORT_COMPARATOR, difference is that it compares by extension id first
        var comparator = Comparator.<ExtensionVersionDTO, Long>comparing(ev -> ev.getExtension().getId()).thenComparing(ExtensionVersionDTO::getSemanticVersion).thenComparing(ExtensionVersionDTO::getTimestamp).reversed();
        extensionVersions = repositories.findAllActiveExtensionVersionDTOsByExtensionId(idMap.keySet()).stream().map(ev -> {
            ev.setExtension(idMap.get(ev.getExtensionId()));
            return ev;
        }).sorted(comparator).collect(Collectors.groupingBy(ExtensionVersionDTO::getExtension));
    } else {
        extensionVersions = Collections.emptyMap();
    }
    Map<ExtensionVersionDTO, List<FileResourceDTO>> resources;
    if (test(flags, FLAG_INCLUDE_FILES) && !extensionVersions.isEmpty()) {
        var types = List.of(MANIFEST, README, LICENSE, ICON, DOWNLOAD, CHANGELOG, WEB_RESOURCE);
        var idsMap = extensionVersions.values().stream().flatMap(Collection::stream).collect(Collectors.toMap(ev -> ev.getId(), ev -> ev));
        resources = repositories.findAllFileResourceDTOsByExtensionVersionIdAndType(idsMap.keySet(), types).stream().map(r -> {
            r.setExtensionVersion(idsMap.get(r.getExtensionVersionId()));
            return r;
        }).collect(Collectors.groupingBy(FileResourceDTO::getExtensionVersion));
    } else {
        resources = Collections.emptyMap();
    }
    Map<Long, Integer> activeReviewCounts;
    if (test(flags, FLAG_INCLUDE_STATISTICS) && !extensions.isEmpty()) {
        var ids = extensions.stream().map(ExtensionDTO::getId).collect(Collectors.toList());
        activeReviewCounts = repositories.findAllActiveReviewCountsByExtensionId(ids);
    } else {
        activeReviewCounts = Collections.emptyMap();
    }
    var extensionQueryResults = new ArrayList<ExtensionQueryResult.Extension>();
    for (var extension : extensions) {
        var queryExt = toQueryExtension(extension, activeReviewCounts, flags);
        queryExt.versions = extensionVersions.getOrDefault(extension, Collections.emptyList()).stream().map(extVer -> toQueryVersion(extVer, resources, flags)).collect(Collectors.toList());
        extensionQueryResults.add(queryExt);
    }
    return toQueryResult(extensionQueryResults, totalCount);
}
Also used : ExtensionVersion(org.eclipse.openvsx.entities.ExtensionVersion) java.util(java.util) ExtensionDTO(org.eclipse.openvsx.dto.ExtensionDTO) ExtensionFile(org.eclipse.openvsx.adapter.ExtensionQueryResult.ExtensionFile) ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayUtils(org.apache.commons.lang3.ArrayUtils) UrlUtil(org.eclipse.openvsx.util.UrlUtil) Criterion(org.eclipse.openvsx.adapter.ExtensionQueryParam.Criterion) CacheControl(org.springframework.http.CacheControl) ModelMap(org.springframework.ui.ModelMap) Value(org.springframework.beans.factory.annotation.Value) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) Statistic(org.eclipse.openvsx.adapter.ExtensionQueryResult.Statistic) FileResource(org.eclipse.openvsx.entities.FileResource) NotFoundException(org.eclipse.openvsx.util.NotFoundException) SearchUtilService(org.eclipse.openvsx.search.SearchUtilService) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExtensionVersionDTO(org.eclipse.openvsx.dto.ExtensionVersionDTO) MediaType(org.springframework.http.MediaType) PageRequest(org.springframework.data.domain.PageRequest) FileResourceDTO(org.eclipse.openvsx.dto.FileResourceDTO) GoogleCloudStorageService(org.eclipse.openvsx.storage.GoogleCloudStorageService) Collectors(java.util.stream.Collectors) ExtensionQueryParam(org.eclipse.openvsx.adapter.ExtensionQueryParam) TimeUnit(java.util.concurrent.TimeUnit) RepositoryService(org.eclipse.openvsx.repositories.RepositoryService) HttpStatus(org.springframework.http.HttpStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) FLAG_PREVIEW(org.eclipse.openvsx.adapter.ExtensionQueryResult.Extension.FLAG_PREVIEW) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Property(org.eclipse.openvsx.adapter.ExtensionQueryResult.Property) ResponseEntity(org.springframework.http.ResponseEntity) StorageUtilService(org.eclipse.openvsx.storage.StorageUtilService) ErrorResultException(org.eclipse.openvsx.util.ErrorResultException) ExtensionDTO(org.eclipse.openvsx.dto.ExtensionDTO) ExtensionVersionDTO(org.eclipse.openvsx.dto.ExtensionVersionDTO) FileResourceDTO(org.eclipse.openvsx.dto.FileResourceDTO) PageRequest(org.springframework.data.domain.PageRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException)

Aggregations

Lists (com.google.common.collect.Lists)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1 ExtensionQueryParam (org.eclipse.openvsx.adapter.ExtensionQueryParam)1 Criterion (org.eclipse.openvsx.adapter.ExtensionQueryParam.Criterion)1 FLAG_PREVIEW (org.eclipse.openvsx.adapter.ExtensionQueryResult.Extension.FLAG_PREVIEW)1 ExtensionFile (org.eclipse.openvsx.adapter.ExtensionQueryResult.ExtensionFile)1 Property (org.eclipse.openvsx.adapter.ExtensionQueryResult.Property)1 Statistic (org.eclipse.openvsx.adapter.ExtensionQueryResult.Statistic)1 ExtensionDTO (org.eclipse.openvsx.dto.ExtensionDTO)1 ExtensionVersionDTO (org.eclipse.openvsx.dto.ExtensionVersionDTO)1 FileResourceDTO (org.eclipse.openvsx.dto.FileResourceDTO)1 ExtensionVersion (org.eclipse.openvsx.entities.ExtensionVersion)1 FileResource (org.eclipse.openvsx.entities.FileResource)1 RepositoryService (org.eclipse.openvsx.repositories.RepositoryService)1 SearchUtilService (org.eclipse.openvsx.search.SearchUtilService)1 GoogleCloudStorageService (org.eclipse.openvsx.storage.GoogleCloudStorageService)1