use of org.haiku.haikudepotserver.userrating.model.UserRatingSearchSpecification in project haikudepotserver by haiku.
the class UserRatingApiImpl method searchUserRatings.
@Override
public SearchUserRatingsResult searchUserRatings(SearchUserRatingsRequest request) {
Preconditions.checkNotNull(request);
Preconditions.checkNotNull(request.limit);
Preconditions.checkState(request.limit > 0);
final ObjectContext context = serverRuntime.newContext();
UserRatingSearchSpecification searchSpecification = new UserRatingSearchSpecification();
if (null != request.daysSinceCreated) {
searchSpecification.setDaysSinceCreated(request.daysSinceCreated.intValue());
}
Architecture architecture = null;
if (null != request.pkgVersionArchitectureCode) {
architecture = getArchitecture(context, request.pkgVersionArchitectureCode);
}
Optional<Pkg> pkgOptional = Optional.empty();
if (null != request.pkgName) {
pkgOptional = Pkg.tryGetByName(context, request.pkgName);
if (pkgOptional.isEmpty()) {
throw new ObjectNotFoundException(Pkg.class.getSimpleName(), request.pkgName);
}
}
Optional<Repository> repositoryOptional = Optional.empty();
if (!Strings.isNullOrEmpty(request.repositoryCode)) {
searchSpecification.setRepository(getRepository(context, request.repositoryCode));
}
if (null != request.pkgVersionMajor) {
if (repositoryOptional.isEmpty()) {
throw new IllegalStateException("the repository is required when a pkg version is specified");
}
if (pkgOptional.isEmpty()) {
throw new IllegalStateException("the pkg is required when a pkg version is specified");
}
if (null == architecture) {
throw new IllegalStateException("the architecture is required when a pkg version is specified");
}
PkgVersion pkgVersion = PkgVersion.getForPkg(context, pkgOptional.get(), repositoryOptional.get(), architecture, new VersionCoordinates(request.pkgVersionMajor, request.pkgVersionMinor, request.pkgVersionMicro, request.pkgVersionPreRelease, request.pkgVersionRevision)).filter(_PkgVersion::getActive).orElseThrow(() -> new ObjectNotFoundException(PkgVersion.class.getSimpleName(), ""));
searchSpecification.setPkgVersion(pkgVersion);
} else {
searchSpecification.setArchitecture(architecture);
if (pkgOptional.isPresent()) {
searchSpecification.setPkg(pkgOptional.get());
}
}
if (null != request.userNickname) {
Optional<User> userOptional = User.tryGetByNickname(context, request.userNickname);
if (userOptional.isEmpty()) {
throw new ObjectNotFoundException(User.class.getSimpleName(), request.userNickname);
}
searchSpecification.setUser(userOptional.get());
}
searchSpecification.setLimit(request.limit);
searchSpecification.setOffset(request.offset);
List<UserRating> foundUserRatings = userRatingService.search(context, searchSpecification);
final SearchUserRatingsResult result = new SearchUserRatingsResult();
result.total = userRatingService.total(context, searchSpecification);
result.items = foundUserRatings.stream().map(ur -> {
SearchUserRatingsResult.UserRating resultUserRating = new SearchUserRatingsResult.UserRating();
resultUserRating.active = ur.getActive();
resultUserRating.code = ur.getCode();
resultUserRating.comment = ur.getComment();
resultUserRating.createTimestamp = ur.getCreateTimestamp().getTime();
resultUserRating.modifyTimestamp = ur.getModifyTimestamp().getTime();
resultUserRating.userRatingStabilityCode = null != ur.getUserRatingStability() ? ur.getUserRatingStability().getCode() : null;
resultUserRating.naturalLanguageCode = ur.getNaturalLanguage().getCode();
resultUserRating.pkgVersion = createPkgVersion(ur.getPkgVersion());
resultUserRating.rating = ur.getRating();
resultUserRating.user = createUser(ur.getUser());
return resultUserRating;
}).collect(Collectors.toList());
return result;
}
use of org.haiku.haikudepotserver.userrating.model.UserRatingSearchSpecification in project haikudepotserver by haiku.
the class UserRatingSpreadsheetJobRunner method run.
@Override
public void run(JobService jobService, UserRatingSpreadsheetJobSpecification specification) throws IOException {
Preconditions.checkArgument(null != jobService);
Preconditions.checkArgument(null != specification);
final ObjectContext context = serverRuntime.newContext();
// 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, ',')) {
Optional<Pkg> paramPkgOptional = Optional.empty();
Optional<User> paramUserOptional = Optional.empty();
Optional<Repository> paramRepositoryOptional = Optional.empty();
if (!Strings.isNullOrEmpty(specification.getRepositoryCode())) {
paramRepositoryOptional = Repository.tryGetByCode(context, specification.getRepositoryCode());
if (!paramRepositoryOptional.isPresent()) {
throw new IllegalStateException("unable to find the repository; " + specification.getRepositoryCode());
}
}
if (!Strings.isNullOrEmpty(specification.getUserNickname())) {
paramUserOptional = User.tryGetByNickname(context, specification.getUserNickname());
if (!paramUserOptional.isPresent()) {
throw new IllegalStateException("unable to find the user; " + specification.getUserNickname());
}
}
if (!Strings.isNullOrEmpty(specification.getPkgName())) {
paramPkgOptional = Pkg.tryGetByName(context, specification.getPkgName());
if (!paramPkgOptional.isPresent()) {
throw new IllegalStateException("unable to find the package; " + specification.getPkgName());
}
}
writer.writeNext(new String[] { "pkg-name", "repository-code", "architecture-code", "version-coordinates", "user-nickname", "create-timestamp", "modify-timestamp", "rating", "stability-code", "natural-language-code", "comment", "code" });
// stream out the packages.
long startMs = System.currentTimeMillis();
LOGGER.info("will user rating spreadsheet report");
final DateTimeFormatter dateTimeFormatter = DateTimeHelper.createStandardDateTimeFormat();
UserRatingSearchSpecification spec = new UserRatingSearchSpecification();
spec.setPkg(paramPkgOptional.orElse(null));
spec.setUser(paramUserOptional.orElse(null));
spec.setRepository(paramRepositoryOptional.orElse(null));
// TODO; provide a prefetch tree into the user, pkgversion.
int count = userRatingService.each(context, spec, userRating -> {
writer.writeNext(new String[] { userRating.getPkgVersion().getPkg().getName(), userRating.getPkgVersion().getRepositorySource().getRepository().getCode(), userRating.getPkgVersion().getArchitecture().getCode(), userRating.getPkgVersion().toVersionCoordinates().toString(), userRating.getUser().getNickname(), dateTimeFormatter.format(Instant.ofEpochMilli(userRating.getCreateTimestamp().getTime())), dateTimeFormatter.format(Instant.ofEpochMilli(userRating.getModifyTimestamp().getTime())), null != userRating.getRating() ? userRating.getRating().toString() : "", null != userRating.getUserRatingStability() ? userRating.getUserRatingStability().getCode() : "", userRating.getNaturalLanguage().getCode(), userRating.getComment(), userRating.getCode() });
return true;
});
LOGGER.info("did produce user rating spreadsheet report for {} user ratings in {}ms", count, System.currentTimeMillis() - startMs);
}
}
Aggregations