Search in sources :

Example 26 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.

the class UserRatingApiImpl method updateUserRating.

@Override
public UpdateUserRatingResult updateUserRating(UpdateUserRatingRequest request) {
    Preconditions.checkNotNull(request);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.code));
    Preconditions.checkNotNull(request.filter);
    final ObjectContext context = serverRuntime.newContext();
    UserRating userRating = UserRating.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(UserRating.class.getSimpleName(), request.code));
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), userRating, Permission.USERRATING_EDIT)) {
        throw new AccessDeniedException("unable to edit the userrating");
    }
    for (UpdateUserRatingRequest.Filter filter : request.filter) {
        switch(filter) {
            case ACTIVE:
                if (null == request.active) {
                    throw new IllegalStateException("the active flag must be supplied to configure this field");
                }
                userRating.setActive(request.active);
                break;
            case COMMENT:
                if (null != request.comment) {
                    userRating.setComment(Strings.emptyToNull(request.comment.trim()));
                } else {
                    userRating.setComment(null);
                }
                break;
            case NATURALLANGUAGE:
                NaturalLanguage naturalLanguage = getNaturalLanguage(context, request.naturalLanguageCode);
                userRating.setNaturalLanguage(naturalLanguage);
                break;
            case RATING:
                userRating.setRating(request.rating);
                break;
            case USERRATINGSTABILITY:
                if (null == request.userRatingStabilityCode) {
                    userRating.setUserRatingStability(null);
                } else {
                    userRating.setUserRatingStability(UserRatingStability.getByCode(context, request.userRatingStabilityCode).orElseThrow(() -> new ObjectNotFoundException(UserRatingStability.class.getSimpleName(), request.userRatingStabilityCode)));
                }
                break;
            default:
                throw new IllegalStateException("the filter; " + filter.name() + " is not handled");
        }
    }
    LOGGER.info("did update user rating for user {} on package {}", userRating.getUser().toString(), userRating.getPkgVersion().getPkg().toString());
    context.commitChanges();
    return new UpdateUserRatingResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) ObjectContext(org.apache.cayenne.ObjectContext)

Example 27 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.

the class UserRatingApiImpl method getUserRating.

@Override
public GetUserRatingResult getUserRating(GetUserRatingRequest request) {
    Preconditions.checkNotNull(request);
    Preconditions.checkState(!Strings.isNullOrEmpty(request.code));
    final ObjectContext context = serverRuntime.newContext();
    UserRating userRating = UserRating.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(UserRating.class.getSimpleName(), request.code));
    GetUserRatingResult result = new GetUserRatingResult();
    fillAbstractGetUserRatingResult(userRating, result);
    return result;
}
Also used : ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) ObjectContext(org.apache.cayenne.ObjectContext)

Example 28 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.

the class UserRatingApiImpl method removeUserRating.

@Override
public RemoveUserRatingResult removeUserRating(RemoveUserRatingRequest request) {
    Preconditions.checkNotNull(request);
    Preconditions.checkState(StringUtils.isNotBlank(request.code));
    final ObjectContext context = serverRuntime.newContext();
    UserRating userRating = UserRating.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(UserRating.class.getSimpleName(), request.code));
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), userRating, Permission.USERRATING_REMOVE)) {
        throw new AccessDeniedException("unable to delete the userrating");
    }
    userRatingService.removeUserRatingAtomically(userRating.getCode());
    return new RemoveUserRatingResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) ObjectContext(org.apache.cayenne.ObjectContext)

Example 29 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.

the class AbstractApiImpl method getNaturalLanguage.

protected NaturalLanguage getNaturalLanguage(ObjectContext context, String naturalLanguageCode) {
    Preconditions.checkNotNull(context);
    Preconditions.checkState(!Strings.isNullOrEmpty(naturalLanguageCode));
    return NaturalLanguage.tryGetByCode(context, naturalLanguageCode).orElseThrow(() -> new ObjectNotFoundException(NaturalLanguage.class.getSimpleName(), naturalLanguageCode));
}
Also used : ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)

Example 30 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.

the class AbstractApiImpl method getArchitecture.

protected Architecture getArchitecture(ObjectContext context, String architectureCode) {
    Preconditions.checkNotNull(context);
    Preconditions.checkState(!Strings.isNullOrEmpty(architectureCode), "an architecture code is required to get the architecture");
    return Architecture.tryGetByCode(context, architectureCode).orElseThrow(() -> new ObjectNotFoundException(Architecture.class.getSimpleName(), architectureCode));
}
Also used : ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)

Aggregations

ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)32 ObjectContext (org.apache.cayenne.ObjectContext)28 AccessDeniedException (org.springframework.security.access.AccessDeniedException)17 org.haiku.haikudepotserver.dataobjects.auto._PkgVersion (org.haiku.haikudepotserver.dataobjects.auto._PkgVersion)7 RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror)6 org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror (org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror)6 User (org.haiku.haikudepotserver.dataobjects.User)5 Preconditions (com.google.common.base.Preconditions)3 Strings (com.google.common.base.Strings)3 AutoJsonRpcServiceImpl (com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl)3 Collectors (java.util.stream.Collectors)3 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)3 StringUtils (org.apache.commons.lang3.StringUtils)3 ValidationException (org.haiku.haikudepotserver.api1.support.ValidationException)3 ValidationFailure (org.haiku.haikudepotserver.api1.support.ValidationFailure)3 Country (org.haiku.haikudepotserver.dataobjects.Country)3 PkgScreenshot (org.haiku.haikudepotserver.dataobjects.PkgScreenshot)3 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)3 org.haiku.haikudepotserver.dataobjects.auto._User (org.haiku.haikudepotserver.dataobjects.auto._User)3 Logger (org.slf4j.Logger)3