Search in sources :

Example 1 with FeedSpecification

use of org.haiku.haikudepotserver.feed.model.FeedSpecification in project haikudepotserver by haiku.

the class CreatedUserRatingSyndEntrySupplier method generate.

@Override
public List<SyndEntry> generate(final FeedSpecification specification) {
    Preconditions.checkNotNull(specification);
    if (specification.getSupplierTypes().contains(FeedSpecification.SupplierType.CREATEDUSERRATING)) {
        if (null != specification.getPkgNames() && specification.getPkgNames().isEmpty()) {
            return Collections.emptyList();
        }
        ObjectSelect<UserRating> objectSelect = ObjectSelect.query(UserRating.class).where(UserRating.ACTIVE.isTrue()).and(UserRating.PKG_VERSION.dot(PkgVersion.ACTIVE).isTrue()).and(UserRating.PKG_VERSION.dot(PkgVersion.PKG).dot(Pkg.ACTIVE).isTrue()).statementFetchSize(specification.getLimit()).orderBy(UserRating.CREATE_TIMESTAMP.desc());
        if (null != specification.getPkgNames()) {
            objectSelect.and(UserRating.PKG_VERSION.dot(PkgVersion.PKG).dot(Pkg.NAME).in(specification.getPkgNames()));
        }
        ObjectContext context = serverRuntime.newContext();
        List<UserRating> userRatings = objectSelect.select(serverRuntime.newContext());
        NaturalLanguage naturalLanguage = deriveNaturalLanguage(context, specification);
        return userRatings.stream().map(ur -> {
            SyndEntry entry = new SyndEntryImpl();
            entry.setPublishedDate(ur.getCreateTimestamp());
            entry.setUpdatedDate(ur.getModifyTimestamp());
            entry.setAuthor(ur.getUser().getNickname());
            entry.setUri(URI_PREFIX + Hashing.sha1().hashUnencodedChars(String.format("%s_::_%s_::_%s_::_%s", this.getClass().getCanonicalName(), ur.getPkgVersion().getPkg().getName(), ur.getPkgVersion().toVersionCoordinates().toString(), ur.getUser().getNickname())).toString());
            entry.setLink(String.format("%s/#!/userrating/%s", baseUrl, ur.getCode()));
            ResolvedPkgVersionLocalization resolvedPkgVersionLocalization = pkgLocalizationService.resolvePkgVersionLocalization(context, ur.getPkgVersion(), null, naturalLanguage);
            entry.setTitle(messageSource.getMessage("feed.createdUserRating.atom.title", new Object[] { Optional.ofNullable(resolvedPkgVersionLocalization.getTitle()).orElse(ur.getPkgVersion().getPkg().getName()), ur.getUser().getNickname() }, new Locale(specification.getNaturalLanguageCode())));
            String contentString = ur.getComment();
            if (null != contentString && contentString.length() > CONTENT_LENGTH) {
                contentString = contentString.substring(0, CONTENT_LENGTH) + "...";
            }
            if (null != ur.getRating()) {
                contentString = buildRatingIndicator(ur.getRating()) + (Strings.isNullOrEmpty(contentString) ? "" : " -- " + contentString);
            }
            SyndContentImpl content = new SyndContentImpl();
            content.setType(MediaType.PLAIN_TEXT_UTF_8.type());
            content.setValue(contentString);
            entry.setDescription(content);
            return entry;
        }).collect(Collectors.toList());
    }
    return Collections.emptyList();
}
Also used : UserRating(org.haiku.haikudepotserver.dataobjects.UserRating) ObjectContext(org.apache.cayenne.ObjectContext) Hashing(com.google.common.hash.Hashing) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) StringUtils(org.apache.commons.lang3.StringUtils) FeedSpecification(org.haiku.haikudepotserver.feed.model.FeedSpecification) Value(org.springframework.beans.factory.annotation.Value) Strings(com.google.common.base.Strings) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion) Locale(java.util.Locale) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) MessageSource(org.springframework.context.MessageSource) MediaType(com.google.common.net.MediaType) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) ResolvedPkgVersionLocalization(org.haiku.haikudepotserver.pkg.model.ResolvedPkgVersionLocalization) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) NaturalLanguage(org.haiku.haikudepotserver.dataobjects.NaturalLanguage) UserRating(org.haiku.haikudepotserver.dataobjects.UserRating) SyndEntrySupplier(org.haiku.haikudepotserver.feed.model.SyndEntrySupplier) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) List(java.util.List) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ObjectSelect(org.apache.cayenne.query.ObjectSelect) PkgLocalizationService(org.haiku.haikudepotserver.pkg.model.PkgLocalizationService) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) Collections(java.util.Collections) Locale(java.util.Locale) ResolvedPkgVersionLocalization(org.haiku.haikudepotserver.pkg.model.ResolvedPkgVersionLocalization) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) ObjectContext(org.apache.cayenne.ObjectContext) NaturalLanguage(org.haiku.haikudepotserver.dataobjects.NaturalLanguage)

Example 2 with FeedSpecification

use of org.haiku.haikudepotserver.feed.model.FeedSpecification in project haikudepotserver by haiku.

the class MiscellaneousApiImpl method generateFeedUrl.

@Override
public GenerateFeedUrlResult generateFeedUrl(final GenerateFeedUrlRequest request) {
    Preconditions.checkNotNull(request);
    final ObjectContext context = serverRuntime.newContext();
    FeedSpecification specification = new FeedSpecification();
    specification.setFeedType(FeedSpecification.FeedType.ATOM);
    specification.setLimit(request.limit);
    if (null != request.supplierTypes) {
        specification.setSupplierTypes(request.supplierTypes.stream().map(st -> FeedSpecification.SupplierType.valueOf(st.name())).collect(Collectors.toList()));
    }
    if (null != request.naturalLanguageCode) {
        specification.setNaturalLanguageCode(getNaturalLanguage(context, request.naturalLanguageCode).getCode());
    }
    if (null != request.pkgNames) {
        List<String> checkedPkgNames = new ArrayList<>();
        for (String pkgName : request.pkgNames) {
            Optional<Pkg> pkgOptional = Pkg.tryGetByName(context, pkgName);
            if (pkgOptional.isEmpty()) {
                throw new ObjectNotFoundException(Pkg.class.getSimpleName(), pkgName);
            }
            checkedPkgNames.add(pkgOptional.get().getName());
        }
        specification.setPkgNames(checkedPkgNames);
    }
    GenerateFeedUrlResult result = new GenerateFeedUrlResult();
    result.url = feedService.generateUrl(specification);
    return result;
}
Also used : ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) FeedSpecification(org.haiku.haikudepotserver.feed.model.FeedSpecification) ObjectContext(org.apache.cayenne.ObjectContext)

Example 3 with FeedSpecification

use of org.haiku.haikudepotserver.feed.model.FeedSpecification in project haikudepotserver by haiku.

the class CreatedPkgVersionSyndEntrySupplier method generate.

@Override
public List<SyndEntry> generate(final FeedSpecification specification) {
    Preconditions.checkNotNull(specification);
    if (specification.getSupplierTypes().contains(FeedSpecification.SupplierType.CREATEDPKGVERSION)) {
        if (null != specification.getPkgNames() && specification.getPkgNames().isEmpty()) {
            return Collections.emptyList();
        }
        ObjectSelect<PkgVersion> objectSelect = ObjectSelect.query(PkgVersion.class).where(PkgVersion.ACTIVE.isTrue()).and(PkgVersion.PKG.dot(Pkg.ACTIVE).isTrue()).and(ExpressionFactory.or(PkgVersion.PKG.dot(Pkg.NAME).endsWith(PkgService.SUFFIX_PKG_DEBUGINFO), PkgVersion.PKG.dot(Pkg.NAME).endsWith(PkgService.SUFFIX_PKG_DEVELOPMENT), PkgVersion.PKG.dot(Pkg.NAME).endsWith(PkgService.SUFFIX_PKG_SOURCE)).notExp()).orderBy(PkgVersion.CREATE_TIMESTAMP.desc()).limit(specification.getLimit());
        if (null != specification.getPkgNames()) {
            objectSelect.and(PkgVersion.PKG.dot(Pkg.NAME).in(specification.getPkgNames()));
        }
        ObjectContext context = serverRuntime.newContext();
        NaturalLanguage naturalLanguage = deriveNaturalLanguage(context, specification);
        List<PkgVersion> pkgVersions = objectSelect.select(context);
        return pkgVersions.stream().map(pv -> {
            SyndEntry entry = new SyndEntryImpl();
            entry.setPublishedDate(pv.getCreateTimestamp());
            entry.setUpdatedDate(pv.getModifyTimestamp());
            entry.setUri(URI_PREFIX + Hashing.sha1().hashUnencodedChars(String.format("%s_::_%s_::_%s", this.getClass().getCanonicalName(), pv.getPkg().getName(), pv.toVersionCoordinates().toString())).toString());
            {
                UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl).pathSegment("#!", "pkg");
                pv.appendPathSegments(builder);
                entry.setLink(builder.build().toUriString());
            }
            ResolvedPkgVersionLocalization resolvedPkgVersionLocalization = pkgLocalizationService.resolvePkgVersionLocalization(context, pv, null, naturalLanguage);
            entry.setTitle(messageSource.getMessage("feed.createdPkgVersion.atom.title", new Object[] { Optional.ofNullable(resolvedPkgVersionLocalization.getTitle()).orElse(pv.getPkg().getName()), pv.toVersionCoordinates().toString() }, new Locale(naturalLanguage.getCode())));
            {
                SyndContent content = new SyndContentImpl();
                content.setType(MediaType.PLAIN_TEXT_UTF_8.type());
                content.setValue(resolvedPkgVersionLocalization.getSummary());
                entry.setDescription(content);
            }
            return entry;
        }).collect(Collectors.toList());
    }
    return Collections.emptyList();
}
Also used : PkgService(org.haiku.haikudepotserver.pkg.model.PkgService) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ObjectContext(org.apache.cayenne.ObjectContext) LoggerFactory(org.slf4j.LoggerFactory) Hashing(com.google.common.hash.Hashing) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) StringUtils(org.apache.commons.lang3.StringUtils) FeedSpecification(org.haiku.haikudepotserver.feed.model.FeedSpecification) Value(org.springframework.beans.factory.annotation.Value) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion) Locale(java.util.Locale) ExpressionFactory(org.apache.cayenne.exp.ExpressionFactory) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) MessageSource(org.springframework.context.MessageSource) MediaType(com.google.common.net.MediaType) Pkg(org.haiku.haikudepotserver.dataobjects.Pkg) Logger(org.slf4j.Logger) ResolvedPkgVersionLocalization(org.haiku.haikudepotserver.pkg.model.ResolvedPkgVersionLocalization) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) NaturalLanguage(org.haiku.haikudepotserver.dataobjects.NaturalLanguage) SyndContent(com.rometools.rome.feed.synd.SyndContent) SyndEntrySupplier(org.haiku.haikudepotserver.feed.model.SyndEntrySupplier) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) List(java.util.List) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ObjectSelect(org.apache.cayenne.query.ObjectSelect) PkgLocalizationService(org.haiku.haikudepotserver.pkg.model.PkgLocalizationService) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) Collections(java.util.Collections) Locale(java.util.Locale) ResolvedPkgVersionLocalization(org.haiku.haikudepotserver.pkg.model.ResolvedPkgVersionLocalization) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) NaturalLanguage(org.haiku.haikudepotserver.dataobjects.NaturalLanguage) SyndContent(com.rometools.rome.feed.synd.SyndContent) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) PkgVersion(org.haiku.haikudepotserver.dataobjects.PkgVersion) ObjectContext(org.apache.cayenne.ObjectContext)

Example 4 with FeedSpecification

use of org.haiku.haikudepotserver.feed.model.FeedSpecification in project haikudepotserver by haiku.

the class FeedServiceImpl method generateUrl.

/**
 * <p>Given a specification for a feed, this method will generate a URL that external users can query in order
 * to get that feed.</p>
 */
@Override
public String generateUrl(FeedSpecification specification) {
    Preconditions.checkNotNull(specification);
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl).path(PATH_ROOT + "/pkg.atom");
    if (null != specification.getNaturalLanguageCode()) {
        builder.queryParam(KEY_NATURALLANGUAGECODE, specification.getNaturalLanguageCode());
    }
    if (null != specification.getLimit()) {
        builder.queryParam(KEY_LIMIT, specification.getLimit().toString());
    }
    if (null != specification.getSupplierTypes()) {
        builder.queryParam(KEY_TYPES, String.join(",", specification.getSupplierTypes().stream().map(FeedSpecification.SupplierType::name).collect(Collectors.toList())));
    }
    if (null != specification.getPkgNames()) {
        // split on hyphens because hyphens are not allowed in package names
        builder.queryParam(KEY_PKGNAMES, String.join("-", specification.getPkgNames()));
    }
    return builder.build().toString();
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) FeedSpecification(org.haiku.haikudepotserver.feed.model.FeedSpecification)

Example 5 with FeedSpecification

use of org.haiku.haikudepotserver.feed.model.FeedSpecification in project haikudepotserver by haiku.

the class FeedController method generate.

@RequestMapping(value = "{pathBase:pkg\\.}{" + FeedServiceImpl.KEY_EXTENSION + ":atom|rss}", method = RequestMethod.GET)
public void generate(HttpServletResponse response, @PathVariable(value = FeedServiceImpl.KEY_EXTENSION) String extension, @RequestParam(value = FeedServiceImpl.KEY_NATURALLANGUAGECODE, required = false) String naturalLanguageCode, @RequestParam(value = FeedServiceImpl.KEY_PKGNAMES, required = false) String pkgNames, @RequestParam(value = FeedServiceImpl.KEY_LIMIT, required = false) Integer limit, @RequestParam(value = FeedServiceImpl.KEY_TYPES, required = false) String types) throws IOException, FeedException {
    Preconditions.checkNotNull(response);
    if (null == limit || limit > MAX_LIMIT) {
        limit = DEFAULT_LIMIT;
    }
    FeedSpecification.FeedType feedType = tryDeriveFeedTypeByExtension(extension).orElseThrow(() -> new IllegalStateException("unable to derive the feed type from [" + extension + "]"));
    FeedSpecification specification = new FeedSpecification();
    specification.setFeedType(feedType);
    specification.setLimit(limit > MAX_LIMIT ? MAX_LIMIT : limit);
    specification.setNaturalLanguageCode(!Strings.isNullOrEmpty(naturalLanguageCode) ? naturalLanguageCode : NaturalLanguage.CODE_ENGLISH);
    if (Strings.isNullOrEmpty(types)) {
        specification.setSupplierTypes(ImmutableList.copyOf(FeedSpecification.SupplierType.values()));
    } else {
        specification.setSupplierTypes(Splitter.on(',').trimResults().omitEmptyStrings().splitToList(types).stream().map(FeedSpecification.SupplierType::valueOf).collect(Collectors.toList()));
    }
    if (Strings.isNullOrEmpty(pkgNames)) {
        specification.setPkgNames(null);
    } else {
        // split on hyphens because hyphens are not allowed in package names
        specification.setPkgNames(Splitter.on('-').trimResults().omitEmptyStrings().splitToList(pkgNames));
    }
    SyndFeed feed = feedCache.getUnchecked(specification);
    response.setContentType(feedType.getContentType());
    Writer writer = response.getWriter();
    SyndFeedOutput syndFeedOutput = new SyndFeedOutput();
    syndFeedOutput.output(feed, writer);
    writer.close();
}
Also used : FeedSpecification(org.haiku.haikudepotserver.feed.model.FeedSpecification) SyndFeedOutput(com.rometools.rome.io.SyndFeedOutput) Writer(java.io.Writer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

FeedSpecification (org.haiku.haikudepotserver.feed.model.FeedSpecification)5 ObjectContext (org.apache.cayenne.ObjectContext)3 Preconditions (com.google.common.base.Preconditions)2 Hashing (com.google.common.hash.Hashing)2 MediaType (com.google.common.net.MediaType)2 SyndContentImpl (com.rometools.rome.feed.synd.SyndContentImpl)2 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)2 SyndEntryImpl (com.rometools.rome.feed.synd.SyndEntryImpl)2 Collections (java.util.Collections)2 List (java.util.List)2 Locale (java.util.Locale)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)2 ObjectSelect (org.apache.cayenne.query.ObjectSelect)2 StringUtils (org.apache.commons.lang3.StringUtils)2 NaturalLanguage (org.haiku.haikudepotserver.dataobjects.NaturalLanguage)2 Pkg (org.haiku.haikudepotserver.dataobjects.Pkg)2 PkgVersion (org.haiku.haikudepotserver.dataobjects.PkgVersion)2 SyndEntrySupplier (org.haiku.haikudepotserver.feed.model.SyndEntrySupplier)2