Search in sources :

Example 1 with UserRatingDerivationJobSpecification

use of org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification in project haikudepotserver by haiku.

the class UserApiImpl method updateUser.

@Override
public UpdateUserResult updateUser(UpdateUserRequest updateUserRequest) {
    Preconditions.checkNotNull(updateUserRequest);
    Preconditions.checkState(!Strings.isNullOrEmpty(updateUserRequest.nickname));
    Preconditions.checkNotNull(updateUserRequest.filter);
    final ObjectContext context = serverRuntime.newContext();
    boolean activeDidChange = false;
    User user = User.tryGetByNickname(context, updateUserRequest.nickname).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), User.NICKNAME.getName()));
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), user, Permission.USER_EDIT)) {
        throw new AccessDeniedException("cannot edit [" + user + "]");
    }
    for (UpdateUserRequest.Filter filter : updateUserRequest.filter) {
        switch(filter) {
            case NATURALLANGUAGE:
                if (Strings.isNullOrEmpty(updateUserRequest.naturalLanguageCode)) {
                    throw new IllegalStateException("the natural language code is required to update the natural language on a user");
                }
                user.setNaturalLanguage(getNaturalLanguage(context, updateUserRequest.naturalLanguageCode));
                LOGGER.info("will update the natural language on the user {} to {}", user.toString(), updateUserRequest.naturalLanguageCode);
                break;
            case EMAIL:
                user.setEmail(updateUserRequest.email);
                break;
            case ACTIVE:
                if (null == updateUserRequest.active) {
                    throw new IllegalStateException("the 'active' attribute is required to configure active on the user.");
                }
                activeDidChange = user.getActive() != updateUserRequest.active;
                user.setActive(updateUserRequest.active);
                break;
            default:
                throw new IllegalStateException("unknown filter in edit user; " + filter.name());
        }
    }
    if (context.hasChanges()) {
        context.commitChanges();
        LOGGER.info("did update the user {}", user.toString());
        if (activeDidChange) {
            List<String> pkgNames = userRatingService.pkgNamesEffectedByUserActiveStateChange(context, user);
            LOGGER.info("will update user rating derivation for {} packages owing to active state change on user {}", pkgNames.size(), user.toString());
            for (String pkgName : pkgNames) {
                jobService.submit(new UserRatingDerivationJobSpecification(pkgName), JobSnapshot.COALESCE_STATUSES_QUEUED);
            }
        }
    } else {
        LOGGER.info("no changes in updating the user {}", user.toString());
    }
    return new UpdateUserResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) ObjectContext(org.apache.cayenne.ObjectContext) UserRatingDerivationJobSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification)

Example 2 with UserRatingDerivationJobSpecification

use of org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification 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();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) ObjectContext(org.apache.cayenne.ObjectContext) UserRatingDerivationJobSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification)

Example 3 with UserRatingDerivationJobSpecification

use of org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification in project haikudepotserver by haiku.

the class UserRatingApiImpl method deriveAndStoreUserRatingsForAllPkgs.

@Override
public DeriveAndStoreUserRatingsForAllPkgsResult deriveAndStoreUserRatingsForAllPkgs(DeriveAndStoreUserRatingsForAllPkgsResult request) {
    Preconditions.checkNotNull(request);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.USERRATING_DERIVEANDSTOREFORPKG)) {
        throw new AccessDeniedException("unable to derive and store user ratings");
    }
    jobService.submit(new UserRatingDerivationJobSpecification(), JobSnapshot.COALESCE_STATUSES_QUEUED);
    LOGGER.info("did enqueue request to derive and store user ratings for all packages");
    return new DeriveAndStoreUserRatingsForAllPkgsResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) UserRatingDerivationJobSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification)

Example 4 with UserRatingDerivationJobSpecification

use of org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification in project haikudepotserver by haiku.

the class UserRatingServiceImpl method removeUserRatingAtomically.

@Override
public void removeUserRatingAtomically(String userRatingCode) {
    ObjectContext context = serverRuntime.newContext();
    UserRating userRating = UserRating.getByCode(context, userRatingCode);
    String pkgName = userRating.getPkgVersion().getPkg().getName();
    context.deleteObject(userRating);
    context.commitChanges();
    LOGGER.info("did delete user rating [{}]", userRatingCode);
    // This cannot be done via a listener because it is, after the deletion
    // no longer possible to find out the package that was attached to the
    // user rating.
    jobService.submit(new UserRatingDerivationJobSpecification(pkgName), JobSnapshot.COALESCE_STATUSES_QUEUED);
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) UserRatingDerivationJobSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification)

Aggregations

UserRatingDerivationJobSpecification (org.haiku.haikudepotserver.userrating.model.UserRatingDerivationJobSpecification)4 ObjectContext (org.apache.cayenne.ObjectContext)3 AccessDeniedException (org.springframework.security.access.AccessDeniedException)3 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)1 User (org.haiku.haikudepotserver.dataobjects.User)1