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);
}
Aggregations