use of org.haiku.haikudepotserver.dataobjects.NaturalLanguage 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();
}
use of org.haiku.haikudepotserver.dataobjects.NaturalLanguage in project haikudepotserver by haiku.
the class MiscelaneousApiIT method testGetAllNaturalLanguages.
@Test
public void testGetAllNaturalLanguages() {
// ------------------------------------
GetAllNaturalLanguagesResult result = miscellaneousApi.getAllNaturalLanguages(new GetAllNaturalLanguagesRequest());
// ------------------------------------
ObjectContext objectContext = serverRuntime.newContext();
List<NaturalLanguage> naturalLanguages = NaturalLanguage.getAll(objectContext);
Assertions.assertThat(naturalLanguages.size()).isEqualTo(result.naturalLanguages.size());
for (int i = 0; i < naturalLanguages.size(); i++) {
NaturalLanguage naturalLanguage = naturalLanguages.get(i);
GetAllNaturalLanguagesResult.NaturalLanguage apiNaturalLanguage = result.naturalLanguages.get(i);
Assertions.assertThat(naturalLanguage.getName()).isEqualTo(apiNaturalLanguage.name);
Assertions.assertThat(naturalLanguage.getCode()).isEqualTo(apiNaturalLanguage.code);
}
}
use of org.haiku.haikudepotserver.dataobjects.NaturalLanguage in project haikudepotserver by haiku.
the class PkgLocalizationCoverageExportSpreadsheetJobRunner method run.
@Override
public void run(JobService jobService, PkgLocalizationCoverageExportSpreadsheetJobSpecification specification) throws IOException, JobRunnerException {
Preconditions.checkArgument(null != jobService);
Preconditions.checkArgument(null != specification);
final ObjectContext context = serverRuntime.newContext();
final List<NaturalLanguage> naturalLanguages = getNaturalLanguages(context);
if (naturalLanguages.isEmpty()) {
throw new RuntimeException("there appear to be no natural languages in the system");
}
// this will register the outbound data against the job.
JobDataWithByteSink jobDataWithByteSink = jobService.storeGeneratedData(specification.getGuid(), "download", MediaType.CSV_UTF_8.toString());
try (OutputStream outputStream = jobDataWithByteSink.getByteSink().openBufferedStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
CSVWriter writer = new CSVWriter(outputStreamWriter, ',')) {
final String[] cells = new String[1 + naturalLanguages.size()];
// headers
{
int c = 0;
cells[c++] = "pkg-name";
for (NaturalLanguage naturalLanguage : naturalLanguages) {
cells[c++] = naturalLanguage.getCode();
}
}
long startMs = System.currentTimeMillis();
writer.writeNext(cells);
// stream out the packages.
final long expectedTotal = pkgService.totalPkg(context, false);
final AtomicLong counter = new AtomicLong(0);
LOGGER.info("will produce package localization report for {} packages", expectedTotal);
long count = pkgService.eachPkg(context, // allow source only.
false, pkg -> {
PkgSupplement pkgSupplement = pkg.getPkgSupplement();
int c = 0;
cells[c++] = pkg.getName();
for (NaturalLanguage naturalLanguage : naturalLanguages) {
cells[c++] = pkgSupplement.getPkgLocalization(naturalLanguage).map(pl -> MARKER).orElse("");
}
writer.writeNext(cells);
jobService.setJobProgressPercent(specification.getGuid(), (int) ((100 * counter.incrementAndGet()) / expectedTotal));
// keep going!
return true;
});
LOGGER.info("did produce pkg localization coverage spreadsheet report for {} packages in {}ms", count, System.currentTimeMillis() - startMs);
}
}
use of org.haiku.haikudepotserver.dataobjects.NaturalLanguage 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();
}
use of org.haiku.haikudepotserver.dataobjects.NaturalLanguage in project haikudepotserver by haiku.
the class ReferenceDumpExportJobRunner method createDumpExportReference.
private DumpExportReference createDumpExportReference(ReferenceDumpExportJobSpecification specification) {
DumpExportReference dumpExportReference = new DumpExportReference();
ObjectContext context = serverRuntime.newContext();
NaturalLanguage naturalLanguage = NaturalLanguage.tryGetByCode(context, specification.getNaturalLanguageCode()).orElseGet(() -> NaturalLanguage.getEnglish(context));
dumpExportReference.setCountries(Country.getAll(context).stream().map(c -> {
DumpExportReferenceCountry dc = new DumpExportReferenceCountry();
dc.setCode(c.getCode());
dc.setName(c.getName());
return dc;
}).collect(Collectors.toList()));
dumpExportReference.setNaturalLanguages(NaturalLanguage.getAll(context).stream().map(nl -> {
DumpExportReferenceNaturalLanguage dnl = new DumpExportReferenceNaturalLanguage();
dnl.setIsPopular(nl.getIsPopular());
dnl.setCode(nl.getCode());
dnl.setName(messageSource.getMessage(nl.getTitleKey(), new Object[] {}, naturalLanguage.toLocale()));
return dnl;
}).collect(Collectors.toList()));
dumpExportReference.setPkgCategories(PkgCategory.getAll(context).stream().map(pc -> {
DumpExportReferencePkgCategory dpc = new DumpExportReferencePkgCategory();
dpc.setCode(pc.getCode());
dpc.setName(messageSource.getMessage(pc.getTitleKey(), new Object[] {}, naturalLanguage.toLocale()));
return dpc;
}).collect(Collectors.toList()));
dumpExportReference.setUserRatingStabilities(UserRatingStability.getAll(context).stream().map(urs -> {
DumpExportReferenceUserRatingStability derurs = new DumpExportReferenceUserRatingStability();
derurs.setOrdering(urs.getOrdering().longValue());
derurs.setCode(urs.getCode());
derurs.setName(messageSource.getMessage(urs.getTitleKey(), new Object[] {}, naturalLanguage.toLocale()));
return derurs;
}).collect(Collectors.toList()));
return dumpExportReference;
}
Aggregations