use of org.eclipse.openvsx.json.QueryResultJson in project openvsx by eclipse.
the class LocalRegistryService method query.
@Override
public QueryResultJson query(QueryParamJson param) {
if (!Strings.isNullOrEmpty(param.extensionId)) {
var split = param.extensionId.split("\\.");
if (split.length != 2 || split[0].isEmpty() || split[1].isEmpty())
throw new ErrorResultException("The 'extensionId' parameter must have the format 'namespace.extension'.");
if (!Strings.isNullOrEmpty(param.namespaceName) && !param.namespaceName.equals(split[0]))
throw new ErrorResultException("Conflicting parameters 'extensionId' and 'namespaceName'");
if (!Strings.isNullOrEmpty(param.extensionName) && !param.extensionName.equals(split[1]))
throw new ErrorResultException("Conflicting parameters 'extensionId' and 'extensionName'");
param.namespaceName = split[0];
param.extensionName = split[1];
}
List<ExtensionVersionDTO> extensionVersions = new ArrayList<>();
// Add extension by UUID (public_id)
if (!Strings.isNullOrEmpty(param.extensionUuid)) {
extensionVersions.addAll(repositories.findActiveExtensionVersionDTOsByExtensionPublicId(param.extensionUuid));
}
// Add extensions by namespace UUID (public_id)
if (!Strings.isNullOrEmpty(param.namespaceUuid)) {
extensionVersions.addAll(repositories.findActiveExtensionVersionDTOsByNamespacePublicId(param.namespaceUuid));
}
// Add a specific version of an extension
if (!Strings.isNullOrEmpty(param.namespaceName) && !Strings.isNullOrEmpty(param.extensionName) && !Strings.isNullOrEmpty(param.extensionVersion) && !param.includeAllVersions) {
var extensionVersion = repositories.findActiveExtensionVersionDTOByVersion(param.extensionVersion, param.extensionName, param.namespaceName);
if (extensionVersion != null) {
extensionVersions.add(extensionVersion);
}
// Add extension by namespace and name
} else if (!Strings.isNullOrEmpty(param.namespaceName) && !Strings.isNullOrEmpty(param.extensionName)) {
extensionVersions.addAll(repositories.findActiveExtensionVersionDTOsByExtensionName(param.extensionName, param.namespaceName));
// Add extensions by namespace
} else if (!Strings.isNullOrEmpty(param.namespaceName)) {
extensionVersions.addAll(repositories.findActiveExtensionVersionDTOsByNamespaceName(param.namespaceName));
// Add extensions by name
} else if (!Strings.isNullOrEmpty(param.extensionName)) {
extensionVersions.addAll(repositories.findActiveExtensionVersionDTOsByExtensionName(param.extensionName));
}
extensionVersions = extensionVersions.stream().collect(Collectors.groupingBy(ExtensionVersionDTO::getId)).values().stream().map(l -> l.get(0)).filter(ev -> addToResult(ev, param)).collect(Collectors.toList());
if (Strings.isNullOrEmpty(param.extensionVersion) && !param.includeAllVersions) {
var latestIds = extensionVersions.stream().map(ExtensionVersionDTO::getExtension).map(ExtensionDTO::getLatestId).filter(Objects::nonNull).collect(Collectors.toList());
extensionVersions = extensionVersions.stream().filter(ev -> latestIds.contains(ev.getId())).collect(Collectors.toList());
}
var extensionIds = extensionVersions.stream().map(ExtensionVersionDTO::getExtensionId).collect(Collectors.toSet());
var reviewCounts = getReviewCounts(extensionIds);
var versionStrings = getVersionStrings(extensionIds);
var previewsByExtensionId = getPreviews(extensionIds);
var fileResourcesByExtensionVersionId = getFileResources(extensionVersions);
var membershipsByNamespaceId = getMemberships(extensionVersions);
var result = new QueryResultJson();
result.extensions = extensionVersions.stream().sorted(getExtensionVersionComparator()).map(ev -> {
var reviewCount = reviewCounts.getOrDefault(ev.getExtensionId(), 0);
var preview = previewsByExtensionId.get(ev.getExtensionId());
var versions = versionStrings.get(ev.getExtensionId());
var fileResources = fileResourcesByExtensionVersionId.getOrDefault(ev.getId(), Collections.emptyList());
return toExtensionVersionJson(ev, reviewCount, preview, versions, fileResources, membershipsByNamespaceId);
}).collect(Collectors.toList());
return result;
}
Aggregations