use of org.haiku.haikudepotserver.dataobjects.auto._PkgVersion 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.dataobjects.auto._PkgVersion in project haikudepotserver by haiku.
the class PkgApiImpl method getPkg.
@Override
public GetPkgResult getPkg(GetPkgRequest request) {
Preconditions.checkNotNull(request);
Preconditions.checkState(!Strings.isNullOrEmpty(request.name), "request pkg name is required");
Preconditions.checkNotNull(request.versionType);
Preconditions.checkState(!Strings.isNullOrEmpty(request.naturalLanguageCode));
Preconditions.checkArgument(EnumSet.of(PkgVersionType.NONE, PkgVersionType.ALL).contains(request.versionType) || !Strings.isNullOrEmpty(request.repositoryCode), "the repository code should be supplied of the version request is not ALL or NONE");
final ObjectContext context = serverRuntime.newContext();
Optional<Architecture> architectureOptional = Optional.empty();
if (!Strings.isNullOrEmpty(request.architectureCode)) {
architectureOptional = Architecture.tryGetByCode(context, request.architectureCode);
}
Pkg pkg = getPkg(context, request.name);
Repository repository = null;
if (!Strings.isNullOrEmpty(request.repositoryCode)) {
repository = getRepository(context, request.repositoryCode);
}
final NaturalLanguage naturalLanguage = getNaturalLanguage(context, request.naturalLanguageCode);
GetPkgResult result = new GetPkgResult();
result.name = pkg.getName();
result.modifyTimestamp = pkg.getModifyTimestamp().getTime();
result.vanityLinkUrl = pkgService.createVanityLinkUrl(pkg);
result.hasChangelog = pkg.getPkgSupplement().getPkgChangelog().isPresent();
result.pkgCategoryCodes = pkg.getPkgSupplement().getPkgPkgCategories().stream().map(ppc -> ppc.getPkgCategory().getCode()).collect(Collectors.toList());
if (null != repository) {
Optional<PkgUserRatingAggregate> userRatingAggregate = pkg.getPkgUserRatingAggregate(repository);
if (userRatingAggregate.isPresent()) {
result.derivedRating = userRatingAggregate.get().getDerivedRating();
result.derivedRatingSampleSize = userRatingAggregate.get().getDerivedRatingSampleSize();
}
}
if (null != repository) {
result.prominenceOrdering = pkg.getPkgProminence(repository).map(pp -> pp.getProminence().getOrdering()).orElse(null);
}
switch(request.versionType) {
case ALL:
{
List<PkgVersion> allVersions;
if (null == repository) {
// active only
allVersions = PkgVersion.getForPkg(context, pkg, false);
} else {
// active only
allVersions = PkgVersion.getForPkg(context, pkg, repository, false);
}
if (architectureOptional.isPresent()) {
final Architecture a = architectureOptional.get();
allVersions = allVersions.stream().filter(v -> v.getArchitecture().equals(a)).collect(Collectors.toList());
}
// now sort those.
final VersionCoordinatesComparator vcc = new VersionCoordinatesComparator();
Collections.sort(allVersions, (pv1, pv2) -> ComparisonChain.start().compare(pv1.getArchitecture().getCode(), pv2.getArchitecture().getCode()).compare(pv1.toVersionCoordinates(), pv2.toVersionCoordinates(), vcc).result());
result.versions = allVersions.stream().map(v -> createGetPkgResultPkgVersion(context, v, naturalLanguage)).collect(Collectors.toList());
}
break;
case SPECIFIC:
{
if (architectureOptional.isEmpty()) {
throw new IllegalStateException("the specified architecture was not able to be found; " + request.architectureCode);
}
VersionCoordinates coordinates = new VersionCoordinates(request.major, request.minor, request.micro, request.preRelease, request.revision);
PkgVersion pkgVersion = PkgVersion.getForPkg(context, pkg, repository, architectureOptional.get(), coordinates).filter(_PkgVersion::getActive).orElseThrow(() -> new ObjectNotFoundException(PkgVersion.class.getSimpleName(), ""));
if (null != request.incrementViewCounter && request.incrementViewCounter) {
incrementCounter(pkgVersion);
}
result.versions = Collections.singletonList(createGetPkgResultPkgVersion(context, pkgVersion, naturalLanguage));
}
break;
case LATEST:
{
if (architectureOptional.isEmpty()) {
throw new IllegalStateException("the specified architecture was not able to be found; " + request.architectureCode);
}
PkgVersion pkgVersion = pkgService.getLatestPkgVersionForPkg(context, pkg, repository, ImmutableList.of(architectureOptional.get(), Architecture.tryGetByCode(context, Architecture.CODE_ANY).get(), Architecture.tryGetByCode(context, Architecture.CODE_SOURCE).get())).orElseThrow(() -> new ObjectNotFoundException(PkgVersion.class.getSimpleName(), request.name));
if (null != request.incrementViewCounter && request.incrementViewCounter) {
incrementCounter(pkgVersion);
}
result.versions = Collections.singletonList(createGetPkgResultPkgVersion(context, pkgVersion, naturalLanguage));
}
break;
case // no version is actually required.
NONE:
break;
default:
throw new IllegalStateException("unhandled version type in request");
}
return result;
}
Aggregations