use of org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate in project haikudepotserver by haiku.
the class PkgApiImpl method searchPkgs.
@Override
public SearchPkgsResult searchPkgs(final SearchPkgsRequest request) {
Preconditions.checkNotNull(request);
Preconditions.checkState(null != request.architectureCodes && !request.architectureCodes.isEmpty(), "architecture codes must be supplied and at least one is required");
Preconditions.checkState(null != request.repositoryCodes && !request.repositoryCodes.isEmpty(), "repository codes must be supplied and at least one is required");
Preconditions.checkState(!Strings.isNullOrEmpty(request.naturalLanguageCode));
Preconditions.checkNotNull(request.limit);
Preconditions.checkState(request.limit > 0);
if (null == request.sortOrdering) {
request.sortOrdering = SearchPkgsRequest.SortOrdering.NAME;
}
final ObjectContext context = serverRuntime.newContext();
final NaturalLanguage naturalLanguage = getNaturalLanguage(context, request.naturalLanguageCode);
PkgSearchSpecification specification = new PkgSearchSpecification();
String exp = request.expression;
if (null != exp) {
exp = Strings.emptyToNull(exp.trim().toLowerCase());
}
specification.setExpression(exp);
if (null != request.pkgCategoryCode) {
specification.setPkgCategory(PkgCategory.getByCode(context, request.pkgCategoryCode).get());
}
if (null != request.expressionType) {
specification.setExpressionType(PkgSearchSpecification.ExpressionType.valueOf(request.expressionType.name()));
}
specification.setNaturalLanguage(getNaturalLanguage(context, request.naturalLanguageCode));
specification.setDaysSinceLatestVersion(request.daysSinceLatestVersion);
specification.setSortOrdering(PkgSearchSpecification.SortOrdering.valueOf(request.sortOrdering.name()));
specification.setArchitectures(transformCodesToArchitectures(context, request.architectureCodes));
specification.setRepositories(transformCodesToRepositories(context, request.repositoryCodes));
specification.setLimit(request.limit);
specification.setOffset(request.offset);
SearchPkgsResult result = new SearchPkgsResult();
// if there are more than we asked for then there must be more available.
result.total = pkgService.total(context, specification);
result.items = Collections.emptyList();
if (result.total > 0) {
List<PkgVersion> searchedPkgVersions = pkgService.search(context, specification);
// if there is a pattern then it is not possible to use the fixed lookup (which
// is faster).
final PkgLocalizationLookupService localPkgLocalizationLookupService = null != specification.getExpressionAsPattern() ? pkgLocalizationService : new FixedPkgLocalizationLookupServiceImpl(context, searchedPkgVersions, naturalLanguage);
result.items = searchedPkgVersions.stream().map(spv -> {
Optional<PkgUserRatingAggregate> pkgUserRatingAggregateOptional = PkgUserRatingAggregate.getByPkgAndRepository(context, spv.getPkg(), spv.getRepositorySource().getRepository());
SearchPkgsResult.Pkg resultPkg = new SearchPkgsResult.Pkg();
resultPkg.name = spv.getPkg().getName();
resultPkg.modifyTimestamp = spv.getPkg().getModifyTimestamp().getTime();
resultPkg.derivedRating = pkgUserRatingAggregateOptional.map(_PkgUserRatingAggregate::getDerivedRating).orElse(null);
resultPkg.hasAnyPkgIcons = !PkgIconImage.findForPkg(context, spv.getPkg()).isEmpty();
ResolvedPkgVersionLocalization resolvedPkgVersionLocalization = localPkgLocalizationLookupService.resolvePkgVersionLocalization(context, spv, specification.getExpressionAsPattern(), naturalLanguage);
SearchPkgsResult.PkgVersion resultVersion = new SearchPkgsResult.PkgVersion();
resultVersion.major = spv.getMajor();
resultVersion.minor = spv.getMinor();
resultVersion.micro = spv.getMicro();
resultVersion.preRelease = spv.getPreRelease();
resultVersion.revision = spv.getRevision();
resultVersion.createTimestamp = spv.getCreateTimestamp().getTime();
resultVersion.viewCounter = spv.getViewCounter();
resultVersion.architectureCode = spv.getArchitecture().getCode();
resultVersion.payloadLength = spv.getPayloadLength();
resultVersion.title = resolvedPkgVersionLocalization.getTitle();
resultVersion.summary = resolvedPkgVersionLocalization.getSummary();
resultVersion.repositorySourceCode = spv.getRepositorySource().getCode();
resultVersion.repositoryCode = spv.getRepositorySource().getRepository().getCode();
if (null != request.expressionType && StringUtils.isNotBlank(request.expression) && Stream.of(resolvedPkgVersionLocalization.getTitle(), resolvedPkgVersionLocalization.getSummary()).noneMatch(s -> StringUtils.containsIgnoreCase(StringUtils.trimToEmpty(s), StringUtils.trimToEmpty(request.expression)))) {
resultVersion.descriptionSnippet = StringHelper.tryCreateTextSnippetAroundFoundText(resolvedPkgVersionLocalization.getDescription(), request.expression, SNIPPET_LENGTH).orElse(null);
}
resultPkg.versions = Collections.singletonList(resultVersion);
return resultPkg;
}).collect(Collectors.toList());
}
LOGGER.info("search for pkgs found {} results", result.items.size());
return result;
}
Aggregations