Search in sources :

Example 1 with SearchPkgsResult

use of org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsResult 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;
}
Also used : PkgVersionUrl(org.haiku.haikudepotserver.api1.model.pkg.PkgVersionUrl) ObjectContext(org.apache.cayenne.ObjectContext) java.util(java.util) Permission(org.haiku.haikudepotserver.security.model.Permission) org.haiku.haikudepotserver.support(org.haiku.haikudepotserver.support) PkgIcon(org.haiku.haikudepotserver.api1.model.pkg.PkgIcon) FixedPkgLocalizationLookupServiceImpl(org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl) LoggerFactory(org.slf4j.LoggerFactory) PkgLocalization(org.haiku.haikudepotserver.dataobjects.PkgLocalization) BadPkgIconException(org.haiku.haikudepotserver.api1.support.BadPkgIconException) StringUtils(org.apache.commons.lang3.StringUtils) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) ByteArrayInputStream(java.io.ByteArrayInputStream) PkgVersionLocalization(org.haiku.haikudepotserver.dataobjects.PkgVersionLocalization) org.haiku.haikudepotserver.pkg.model(org.haiku.haikudepotserver.pkg.model) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) org.haiku.haikudepotserver.api1.model.pkg(org.haiku.haikudepotserver.api1.model.pkg) Logger(org.slf4j.Logger) AutoJsonRpcServiceImpl(com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl) PermissionEvaluator(org.haiku.haikudepotserver.security.PermissionEvaluator) IOException(java.io.IOException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate(org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate) ComparisonChain(com.google.common.collect.ComparisonChain) Collectors(java.util.stream.Collectors) PkgVersionType(org.haiku.haikudepotserver.api1.model.PkgVersionType) PkgScreenshot(org.haiku.haikudepotserver.dataobjects.PkgScreenshot) org.haiku.haikudepotserver.dataobjects(org.haiku.haikudepotserver.dataobjects) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) Preconditions(com.google.common.base.Preconditions) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate(org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate) FixedPkgLocalizationLookupServiceImpl(org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) ObjectContext(org.apache.cayenne.ObjectContext) org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate(org.haiku.haikudepotserver.dataobjects.auto._PkgUserRatingAggregate)

Example 2 with SearchPkgsResult

use of org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsResult in project haikudepotserver by haiku.

the class PkgApiIT method searchPkgsTest_localizationDescriptionNotEnglish_hit.

/**
 * <p>This test will check that the search is able to find text in the content of the package
 * version localization where the localization is a specific language other than english.
 * This test will find something because it is looking for spanish and has some text from the
 * spanish localization for the package version.</p>
 */
@Test
public void searchPkgsTest_localizationDescriptionNotEnglish_hit() {
    integrationTestSupportService.createStandardTestData();
    SearchPkgsRequest request = new SearchPkgsRequest();
    request.architectureCodes = Collections.singletonList("x86_64");
    request.repositoryCodes = Collections.singletonList("testrepo");
    request.naturalLanguageCode = NaturalLanguage.CODE_SPANISH;
    request.expression = "feij";
    request.expressionType = SearchPkgsRequest.ExpressionType.CONTAINS;
    request.limit = 2;
    request.offset = 0;
    // ------------------------------------
    SearchPkgsResult result = pkgApi.searchPkgs(request);
    // ------------------------------------
    Assertions.assertThat(result.total).isEqualTo(1);
    Assertions.assertThat(result.items.size()).isEqualTo(1);
    Assertions.assertThat(result.items.get(0).name).isEqualTo("pkg1");
    Assertions.assertThat(result.items.get(0).versions.get(0).title).isEqualTo("Ping 1");
    Assertions.assertThat(result.items.get(0).versions.get(0).summary).isEqualTo("pkg1Version2SummarySpanish_feijoa");
}
Also used : SearchPkgsResult(org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsResult) SearchPkgsRequest(org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsRequest) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with SearchPkgsResult

use of org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsResult in project haikudepotserver by haiku.

the class PkgApiIT method searchPkgsTest_localizationDescriptionNotEnglishFallBackToEnglish_hit.

/**
 * <p>This test checks where the client is searching for a package in a specific language, but
 * there is no localization for that specific language.  In this case, </p>
 * @throws ObjectNotFoundException
 */
@Test
public void searchPkgsTest_localizationDescriptionNotEnglishFallBackToEnglish_hit() {
    integrationTestSupportService.createStandardTestData();
    SearchPkgsRequest request = new SearchPkgsRequest();
    request.architectureCodes = Collections.singletonList("x86_64");
    request.naturalLanguageCode = NaturalLanguage.CODE_FRENCH;
    request.repositoryCodes = Collections.singletonList("testrepo");
    request.expression = "persimon";
    request.expressionType = SearchPkgsRequest.ExpressionType.CONTAINS;
    request.limit = 2;
    request.offset = 0;
    // ------------------------------------
    SearchPkgsResult result = pkgApi.searchPkgs(request);
    // ------------------------------------
    Assertions.assertThat(result.total).isEqualTo(1);
    Assertions.assertThat(result.items.size()).isEqualTo(1);
    Assertions.assertThat(result.items.get(0).name).isEqualTo("pkg1");
    Assertions.assertThat(result.items.get(0).versions.get(0).summary).isEqualTo("pkg1Version2SummaryEnglish_persimon");
}
Also used : SearchPkgsResult(org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsResult) SearchPkgsRequest(org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsRequest) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with SearchPkgsResult

use of org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsResult in project haikudepotserver by haiku.

the class PkgApiIT method searchPkgsTest.

@Test
public void searchPkgsTest() {
    integrationTestSupportService.createStandardTestData();
    SearchPkgsRequest request = new SearchPkgsRequest();
    request.architectureCodes = ImmutableList.of("any", "x86_64");
    request.naturalLanguageCode = NaturalLanguage.CODE_ENGLISH;
    request.repositoryCodes = Collections.singletonList("testrepo");
    request.expression = "pk";
    request.expressionType = SearchPkgsRequest.ExpressionType.CONTAINS;
    request.limit = 2;
    request.offset = 0;
    // ------------------------------------
    SearchPkgsResult result = pkgApi.searchPkgs(request);
    // ------------------------------------
    // note includes the "any" package
    Assertions.assertThat(result.total).isEqualTo(4);
    Assertions.assertThat(result.items.size()).isEqualTo(2);
    Assertions.assertThat(result.items.get(0).name).isEqualTo("pkg1");
    Assertions.assertThat(result.items.get(1).name).isEqualTo("pkg2");
}
Also used : SearchPkgsResult(org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsResult) SearchPkgsRequest(org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsRequest) AbstractIntegrationTest(org.haiku.haikudepotserver.AbstractIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

AbstractIntegrationTest (org.haiku.haikudepotserver.AbstractIntegrationTest)3 SearchPkgsRequest (org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsRequest)3 SearchPkgsResult (org.haiku.haikudepotserver.api1.model.pkg.SearchPkgsResult)3 Test (org.junit.jupiter.api.Test)3 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 ComparisonChain (com.google.common.collect.ComparisonChain)1 ImmutableList (com.google.common.collect.ImmutableList)1 AutoJsonRpcServiceImpl (com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 ObjectContext (org.apache.cayenne.ObjectContext)1 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)1 StringUtils (org.apache.commons.lang3.StringUtils)1