Search in sources :

Example 1 with FixedPkgLocalizationLookupServiceImpl

use of org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl 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 FixedPkgLocalizationLookupServiceImpl

use of org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl in project haikudepotserver by haiku.

the class HomeController method home.

/**
 * <p>This is the entry point for the home page.  It will look at the parameters supplied and will
 * establish what should be displayed.</p>
 */
@RequestMapping(method = RequestMethod.GET)
public ModelAndView home(HttpServletRequest httpServletRequest, @RequestParam(value = KEY_OFFSET, defaultValue = "0") Integer offset, @RequestParam(value = KEY_REPOSITORIESCODES, required = false) String repositoryCodes, @RequestParam(value = KEY_ARCHITECTURECODE, required = false) String architectureCode, @RequestParam(value = KEY_PKGCATEGORYCODE, required = false) String pkgCategoryCode, @RequestParam(value = KEY_SEARCHEXPRESSION, required = false) String searchExpression, @RequestParam(value = KEY_VIEWCRITERIATYPECODE, required = false) ViewCriteriaType viewCriteriaType) {
    ObjectContext context = serverRuntime.newContext();
    if (Strings.isNullOrEmpty(architectureCode)) {
        architectureCode = defaultArchitectureCode;
    }
    if (null == repositoryCodes) {
        repositoryCodes = Repository.CODE_DEFAULT;
    }
    // ------------------------------
    // FETCH THE DATA
    PkgSearchSpecification searchSpecification = new PkgSearchSpecification();
    searchSpecification.setOffset(offset);
    searchSpecification.setLimit(PAGESIZE);
    searchSpecification.setExpression(searchExpression);
    searchSpecification.setExpressionType(AbstractSearchSpecification.ExpressionType.CONTAINS);
    Repository repository = StringUtils.isBlank(repositoryCodes) ? null : Repository.getByCode(context, repositoryCodes);
    searchSpecification.setRepositories(null == repository ? Repository.getAllActive(context) : Collections.singletonList(repository));
    Architecture architecture = Architecture.getByCode(context, architectureCode);
    searchSpecification.setArchitectures(ImmutableList.of(architecture, Architecture.getByCode(context, Architecture.CODE_ANY)));
    Optional<PkgCategory> pkgCategoryOptional = Optional.empty();
    if (null != pkgCategoryCode) {
        pkgCategoryOptional = PkgCategory.getByCode(context, pkgCategoryCode);
    }
    NaturalLanguage naturalLanguage = NaturalLanguageWebHelper.deriveNaturalLanguage(context, httpServletRequest);
    searchSpecification.setNaturalLanguage(naturalLanguage);
    switch(null == viewCriteriaType ? ViewCriteriaType.FEATURED : viewCriteriaType) {
        case FEATURED:
            searchSpecification.setSortOrdering(PkgSearchSpecification.SortOrdering.PROMINENCE);
            break;
        case CATEGORIES:
            searchSpecification.setSortOrdering(PkgSearchSpecification.SortOrdering.NAME);
            searchSpecification.setPkgCategory(pkgCategoryOptional.orElseThrow(() -> new IllegalStateException("the pkg category code was unable to be found; " + pkgCategoryCode)));
            break;
        case ALL:
            searchSpecification.setSortOrdering(PkgSearchSpecification.SortOrdering.NAME);
            break;
        case MOSTVIEWED:
            searchSpecification.setSortOrdering(PkgSearchSpecification.SortOrdering.VERSIONVIEWCOUNTER);
            break;
        case MOSTRECENT:
            searchSpecification.setSortOrdering(PkgSearchSpecification.SortOrdering.VERSIONCREATETIMESTAMP);
            break;
        default:
            throw new IllegalStateException("unhandled view criteria type");
    }
    Long totalPkgVersions = pkgService.total(context, searchSpecification);
    if (searchSpecification.getOffset() > totalPkgVersions) {
        searchSpecification.setOffset(totalPkgVersions.intValue());
    }
    List<PkgVersion> pkgVersions = pkgService.search(context, searchSpecification);
    // ------------------------------
    // GENERATE OUTPUT
    HomeData data = new HomeData();
    data.setNaturalLanguage(naturalLanguage);
    final Set<String> excludedArchitectureCode = ImmutableSet.of(Architecture.CODE_ANY, Architecture.CODE_SOURCE);
    data.setAllArchitectures(Architecture.getAll(context).stream().filter(a -> !excludedArchitectureCode.contains(a.getCode())).collect(Collectors.toList()));
    data.setArchitecture(architecture);
    data.setRepository(repository);
    data.setAllRepositories(Repository.getAllActive(context));
    data.setAllPkgCategories(PkgCategory.getAll(context));
    data.setPkgCategory(pkgCategoryOptional.orElseGet(() -> PkgCategory.getAll(context).get(0)));
    data.setAllViewCriteriaTypes(ImmutableList.copyOf(ViewCriteriaType.values()));
    data.setViewCriteriaType(viewCriteriaType);
    data.setSearchExpression(searchExpression);
    data.setPkgVersions(pkgVersions);
    if (0 != totalPkgVersions.intValue()) {
        data.setPagination(new Pagination(totalPkgVersions.intValue(), offset, PAGESIZE));
    }
    httpServletRequest.setAttribute(MultipageConstants.KEY_PKGLOCALIZATIONLOOKUPSERVICE, new FixedPkgLocalizationLookupServiceImpl(context, pkgVersions, naturalLanguage));
    httpServletRequest.setAttribute(MultipageConstants.KEY_SERVERRUNTIME, serverRuntime);
    ModelAndView result = new ModelAndView("multipage/home");
    result.addObject("data", data);
    return result;
}
Also used : FixedPkgLocalizationLookupServiceImpl(org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl) ModelAndView(org.springframework.web.servlet.ModelAndView) Pagination(org.haiku.haikudepotserver.multipage.model.Pagination) ObjectContext(org.apache.cayenne.ObjectContext) PkgSearchSpecification(org.haiku.haikudepotserver.pkg.model.PkgSearchSpecification) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with FixedPkgLocalizationLookupServiceImpl

use of org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl in project haikudepotserver by haiku.

the class PkgDumpExportJobRunner method writePkgVersions.

private void writePkgVersions(JsonGenerator jsonGenerator, ObjectContext context, List<PkgVersion> pkgVersions, RepositorySource repositorySource, NaturalLanguage naturalLanguage) {
    final PkgLocalizationLookupService pkgLocalizationLookupService = new FixedPkgLocalizationLookupServiceImpl(context, pkgVersions, naturalLanguage);
    Map<String, List<PkgVersion>> pkgVersionsUnderCommonPkg = pkgVersions.stream().collect(Collectors.groupingBy((pv) -> pv.getPkg().getName()));
    pkgVersionsUnderCommonPkg.values().forEach((pvs) -> {
        try {
            objectMapper.writeValue(jsonGenerator, createDumpPkg(context, pvs, repositorySource, naturalLanguage, pkgLocalizationLookupService));
        } catch (IOException ioe) {
            throw new UncheckedIOException(ioe);
        }
    });
}
Also used : PkgService(org.haiku.haikudepotserver.pkg.model.PkgService) ObjectContext(org.apache.cayenne.ObjectContext) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DumpExportPkgScreenshot(org.haiku.haikudepotserver.pkg.model.dumpexport.DumpExportPkgScreenshot) Date(java.util.Date) FixedPkgLocalizationLookupServiceImpl(org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) ArchiveInfo(org.haiku.haikudepotserver.support.ArchiveInfo) Lists(com.google.common.collect.Lists) Map(java.util.Map) JobDataWithByteSink(org.haiku.haikudepotserver.job.model.JobDataWithByteSink) JobService(org.haiku.haikudepotserver.job.model.JobService) DumpExportPkg(org.haiku.haikudepotserver.pkg.model.dumpexport.DumpExportPkg) DumpExportPkgVersion(org.haiku.haikudepotserver.pkg.model.dumpexport.DumpExportPkgVersion) PkgDumpExportJobSpecification(org.haiku.haikudepotserver.pkg.model.PkgDumpExportJobSpecification) DateTimeHelper(org.haiku.haikudepotserver.support.DateTimeHelper) OutputStream(java.io.OutputStream) MediaType(com.google.common.net.MediaType) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) ResolvedPkgVersionLocalization(org.haiku.haikudepotserver.pkg.model.ResolvedPkgVersionLocalization) DataRow(org.apache.cayenne.DataRow) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AbstractJobRunner(org.haiku.haikudepotserver.job.AbstractJobRunner) RuntimeInformationService(org.haiku.haikudepotserver.support.RuntimeInformationService) IOException(java.io.IOException) PkgLocalizationLookupService(org.haiku.haikudepotserver.pkg.model.PkgLocalizationLookupService) Collectors(java.util.stream.Collectors) org.haiku.haikudepotserver.dataobjects(org.haiku.haikudepotserver.dataobjects) UncheckedIOException(java.io.UncheckedIOException) SQLTemplate(org.apache.cayenne.query.SQLTemplate) Component(org.springframework.stereotype.Component) List(java.util.List) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) DumpExportPkgCategory(org.haiku.haikudepotserver.pkg.model.dumpexport.DumpExportPkgCategory) Preconditions(com.google.common.base.Preconditions) ObjectSelect(org.apache.cayenne.query.ObjectSelect) GZIPOutputStream(java.util.zip.GZIPOutputStream) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) FixedPkgLocalizationLookupServiceImpl(org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl) PkgLocalizationLookupService(org.haiku.haikudepotserver.pkg.model.PkgLocalizationLookupService) List(java.util.List) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)3 FixedPkgLocalizationLookupServiceImpl (org.haiku.haikudepotserver.pkg.FixedPkgLocalizationLookupServiceImpl)3 Preconditions (com.google.common.base.Preconditions)2 IOException (java.io.IOException)2 Collectors (java.util.stream.Collectors)2 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)2 StringUtils (org.apache.commons.lang3.StringUtils)2 org.haiku.haikudepotserver.dataobjects (org.haiku.haikudepotserver.dataobjects)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Component (org.springframework.stereotype.Component)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)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 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1