use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.
the class UserRatingApiImpl method deriveAndStoreUserRatingForPkg.
@Override
public DeriveAndStoreUserRatingForPkgResult deriveAndStoreUserRatingForPkg(DeriveAndStoreUserRatingForPkgRequest request) {
Preconditions.checkNotNull(request);
Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgName));
final ObjectContext context = serverRuntime.newContext();
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.USERRATING_DERIVEANDSTOREFORPKG)) {
throw new AccessDeniedException("unable to derive and store user ratings");
}
Pkg.tryGetByName(context, request.pkgName).orElseThrow(() -> new ObjectNotFoundException(Pkg.class.getSimpleName(), request.pkgName));
jobService.submit(new UserRatingDerivationJobSpecification(request.pkgName), JobSnapshot.COALESCE_STATUSES_QUEUED);
return new DeriveAndStoreUserRatingForPkgResult();
}
use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.
the class UserRatingApiImpl method createUserRating.
@Override
public CreateUserRatingResult createUserRating(CreateUserRatingRequest request) {
Preconditions.checkNotNull(request);
Preconditions.checkState(!Strings.isNullOrEmpty(request.naturalLanguageCode));
Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgName));
Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgVersionArchitectureCode));
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositoryCode), "the repository code should be supplied");
Preconditions.checkArgument(null == request.rating || request.rating >= UserRating.MIN_USER_RATING, "the user rating " + request.rating + " is less than the minimum allowed of " + UserRating.MIN_USER_RATING);
Preconditions.checkArgument(null == request.rating || request.rating <= UserRating.MAX_USER_RATING, "the user rating " + request.rating + " is greater than the maximum allowed of " + UserRating.MIN_USER_RATING);
if (null != request.comment) {
request.comment = Strings.emptyToNull(request.comment.trim());
}
if (Strings.isNullOrEmpty(request.comment) && null == request.userRatingStabilityCode && null == request.rating) {
throw new IllegalStateException("it is not possible to create a user rating with no meaningful rating data");
}
final ObjectContext context = serverRuntime.newContext();
Optional<Pkg> pkgOptional = Pkg.tryGetByName(context, request.pkgName);
if (pkgOptional.isEmpty()) {
throw new ObjectNotFoundException(Pkg.class.getSimpleName(), request.pkgName);
}
Architecture architecture = getArchitecture(context, request.pkgVersionArchitectureCode);
NaturalLanguage naturalLanguage = getNaturalLanguage(context, request.naturalLanguageCode);
Repository repository = getRepository(context, request.repositoryCode);
User user = User.tryGetByNickname(context, request.userNickname).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), request.userNickname));
Optional<UserRatingStability> userRatingStabilityOptional = Optional.empty();
if (null != request.userRatingStabilityCode) {
userRatingStabilityOptional = UserRatingStability.getByCode(context, request.userRatingStabilityCode);
if (userRatingStabilityOptional.isEmpty()) {
throw new ObjectNotFoundException(UserRatingStability.class.getSimpleName(), request.userRatingStabilityCode);
}
}
// check authorization
Optional<User> authenticatedUserOptional = tryObtainAuthenticatedUser(context);
if (authenticatedUserOptional.isEmpty()) {
throw new AccessDeniedException("only authenticated users are able to add user ratings");
}
if (!authenticatedUserOptional.get().getNickname().equals(user.getNickname())) {
throw new AccessDeniedException("it is not allowed to add a user rating for another user");
}
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), pkgOptional.get(), Permission.PKG_CREATEUSERRATING)) {
throw new AccessDeniedException("unable to create user ratings for [" + pkgOptional.get() + "]");
}
// check the package version
Optional<PkgVersion> pkgVersionOptional;
switch(request.pkgVersionType) {
case LATEST:
pkgVersionOptional = pkgService.getLatestPkgVersionForPkg(context, pkgOptional.get(), repository, Collections.singletonList(architecture));
break;
case SPECIFIC:
pkgVersionOptional = PkgVersion.getForPkg(context, pkgOptional.get(), repository, architecture, new VersionCoordinates(request.pkgVersionMajor, request.pkgVersionMinor, request.pkgVersionMicro, request.pkgVersionPreRelease, request.pkgVersionRevision));
break;
default:
throw new IllegalStateException("unsupported pkg version type; " + request.pkgVersionType.name());
}
if (pkgVersionOptional.isEmpty()) {
throw new ObjectNotFoundException(PkgVersion.class.getSimpleName(), pkgOptional.get().getName() + "_" + request.pkgVersionType.name());
}
if (!pkgVersionOptional.get().getIsLatest()) {
throw new IllegalStateException("it is not possible to add a user rating to a version other than the latest version.");
}
List<UserRating> legacyUserRatings = UserRating.findByUserAndPkg(context, user, pkgOptional.get());
for (UserRating legacyUserRating : legacyUserRatings) {
if (legacyUserRating.getPkgVersion().equals(pkgVersionOptional.get())) {
throw new IllegalStateException("an existing user rating '" + legacyUserRating.getCode() + "' already exists for this package version; it is not possible to add another one");
}
}
// now create the new user rating.
UserRating userRating = context.newObject(UserRating.class);
userRating.setCode(UUID.randomUUID().toString());
userRating.setUserRatingStability(userRatingStabilityOptional.orElse(null));
userRating.setUser(user);
userRating.setComment(Strings.emptyToNull(Strings.nullToEmpty(request.comment).trim()));
userRating.setPkgVersion(pkgVersionOptional.get());
userRating.setNaturalLanguage(naturalLanguage);
userRating.setRating(request.rating);
context.commitChanges();
LOGGER.info("did create user rating for user {} on package {}", user.toString(), pkgOptional.get().toString());
return new CreateUserRatingResult(userRating.getCode());
}
use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.
the class PkgApiIT method testGetPkg_notFound.
@Test
public void testGetPkg_notFound() {
integrationTestSupportService.createStandardTestData();
GetPkgRequest request = new GetPkgRequest();
request.architectureCode = "x86_64";
request.name = "pkg9";
request.versionType = PkgVersionType.LATEST;
request.naturalLanguageCode = NaturalLanguage.CODE_GERMAN;
request.repositoryCode = "testrepo";
try {
// ------------------------------------
pkgApi.getPkg(request);
// ------------------------------------
org.junit.jupiter.api.Assertions.fail("expected an instance of " + ObjectNotFoundException.class.getSimpleName() + " to be thrown, but was not");
} catch (ObjectNotFoundException onfe) {
Assertions.assertThat(onfe.getEntityName()).isEqualTo(Pkg.class.getSimpleName());
Assertions.assertThat(onfe.getIdentifier()).isEqualTo("pkg9");
} catch (Throwable th) {
org.junit.jupiter.api.Assertions.fail("expected an instance of " + ObjectNotFoundException.class.getSimpleName() + " to be thrown, but " + th.getClass().getSimpleName() + " was instead");
}
}
use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.
the class PkgApiIT method searchPkgsTest_localizationDescriptionNotEnglishFallBackToEnglish_hit.
/**
* <p>This test checks where the client is searching for a package in a specific language, but
* there is no localization for that specific language. In this case, </p>
* @throws ObjectNotFoundException
*/
@Test
public void searchPkgsTest_localizationDescriptionNotEnglishFallBackToEnglish_hit() {
integrationTestSupportService.createStandardTestData();
SearchPkgsRequest request = new SearchPkgsRequest();
request.architectureCodes = Collections.singletonList("x86_64");
request.naturalLanguageCode = NaturalLanguage.CODE_FRENCH;
request.repositoryCodes = Collections.singletonList("testrepo");
request.expression = "persimon";
request.expressionType = SearchPkgsRequest.ExpressionType.CONTAINS;
request.limit = 2;
request.offset = 0;
// ------------------------------------
SearchPkgsResult result = pkgApi.searchPkgs(request);
// ------------------------------------
Assertions.assertThat(result.total).isEqualTo(1);
Assertions.assertThat(result.items.size()).isEqualTo(1);
Assertions.assertThat(result.items.get(0).name).isEqualTo("pkg1");
Assertions.assertThat(result.items.get(0).versions.get(0).summary).isEqualTo("pkg1Version2SummaryEnglish_persimon");
}
use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.
the class AbstractApiImpl method getRepository.
/**
* <p>Obtains and returns the repository based on the supplied code. It will throw a runtime exception if the code
* is not supplied or if no repository was able to be found for the code supplied.</p>
*/
protected Repository getRepository(ObjectContext context, String repositoryCode) {
Preconditions.checkNotNull(context);
Preconditions.checkArgument(!Strings.isNullOrEmpty(repositoryCode), "a repository code is required to search for the repository");
return Repository.tryGetByCode(context, repositoryCode).orElseThrow(() -> new ObjectNotFoundException(Repository.class.getSimpleName(), repositoryCode));
}
Aggregations