Search in sources :

Example 1 with ObjectNotFoundException

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

the class PkgApiImpl method incrementViewCounter.

@Override
public IncrementViewCounterResult incrementViewCounter(IncrementViewCounterRequest request) {
    Preconditions.checkArgument(null != request, "the request object must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.name), "the package name must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.architectureCode), "the architecture must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositoryCode), "the repository code must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.major), "the version major must be supplied");
    ObjectContext context = serverRuntime.newContext();
    VersionCoordinates versionCoordinates = new VersionCoordinates(request.major, request.minor, request.micro, request.preRelease, request.revision);
    PkgVersion pkgVersion = PkgVersion.getForPkg(context, Pkg.getByName(context, request.name), Repository.getByCode(context, request.repositoryCode), Architecture.getByCode(context, request.architectureCode), versionCoordinates).orElseThrow(() -> new ObjectNotFoundException(PkgVersion.class.getSimpleName(), versionCoordinates));
    incrementCounter(pkgVersion);
    return new IncrementViewCounterResult();
}
Also used : ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) ObjectContext(org.apache.cayenne.ObjectContext)

Example 2 with ObjectNotFoundException

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

the class PkgJobApiImpl method queuePkgCategoryCoverageImportSpreadsheetJob.

@Override
public QueuePkgCategoryCoverageImportSpreadsheetJobResult queuePkgCategoryCoverageImportSpreadsheetJob(QueuePkgCategoryCoverageImportSpreadsheetJobRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.inputDataGuid), "the input data must be identified by guid");
    final ObjectContext context = serverRuntime.newContext();
    Optional<User> user = tryObtainAuthenticatedUser(context);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.BULK_PKGCATEGORYCOVERAGEIMPORTSPREADSHEET)) {
        throw new AccessDeniedException("attempt to import package categories, but was not authorized");
    }
    // now check that the data is present.
    jobService.tryGetData(request.inputDataGuid).orElseThrow(() -> new ObjectNotFoundException(JobData.class.getSimpleName(), request.inputDataGuid));
    // setup and go
    PkgCategoryCoverageImportSpreadsheetJobSpecification spec = new PkgCategoryCoverageImportSpreadsheetJobSpecification();
    spec.setOwnerUserNickname(user.map(_User::getNickname).orElse(null));
    spec.setInputDataGuid(request.inputDataGuid);
    return new QueuePkgCategoryCoverageImportSpreadsheetJobResult(jobService.submit(spec, JobSnapshot.COALESCE_STATUSES_NONE));
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) org.haiku.haikudepotserver.dataobjects.auto._User(org.haiku.haikudepotserver.dataobjects.auto._User) User(org.haiku.haikudepotserver.dataobjects.User) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) org.haiku.haikudepotserver.dataobjects.auto._User(org.haiku.haikudepotserver.dataobjects.auto._User) ObjectContext(org.apache.cayenne.ObjectContext)

Example 3 with ObjectNotFoundException

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

the class RepositoryApiImpl method createRepositorySourceMirror.

@Override
public CreateRepositorySourceMirrorResult createRepositorySourceMirror(CreateRepositorySourceMirrorRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositorySourceCode), "the code for the new repository source mirror");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.countryCode), "the country code should be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.baseUrl), "the base url should be supplied");
    final ObjectContext context = serverRuntime.newContext();
    Country country = Country.tryGetByCode(context, request.countryCode).orElseThrow(() -> new ObjectNotFoundException(Country.class.getSimpleName(), request.countryCode));
    RepositorySource repositorySource = getRepositorySourceOrThrow(context, request.repositorySourceCode);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySource.getRepository(), Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("the repository [" + repositorySource.getRepository() + "] is not able to be edited");
    }
    if (tryGetRepositorySourceMirrorObjectIdForBaseUrl(repositorySource.getCode(), request.baseUrl).isPresent()) {
        LOGGER.info("attempt to add a repository source mirror for a url [{}] that is " + " already in use", request.baseUrl);
        throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "unique"));
    }
    // if there is no other mirror then this should be the primary.
    RepositorySourceMirror mirror = context.newObject(RepositorySourceMirror.class);
    mirror.setIsPrimary(repositorySource.tryGetPrimaryMirror().isEmpty());
    mirror.setBaseUrl(request.baseUrl);
    mirror.setRepositorySource(repositorySource);
    mirror.setCountry(country);
    mirror.setDescription(StringUtils.trimToNull(request.description));
    mirror.setCode(UUID.randomUUID().toString());
    repositorySource.getRepository().setModifyTimestamp();
    context.commitChanges();
    LOGGER.info("did add mirror [{}] to repository source [{}]", country.getCode(), repositorySource.getCode());
    CreateRepositorySourceMirrorResult result = new CreateRepositorySourceMirrorResult();
    result.code = mirror.getCode();
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) Country(org.haiku.haikudepotserver.dataobjects.Country) ObjectContext(org.apache.cayenne.ObjectContext) CreateRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceMirrorResult) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Example 4 with ObjectNotFoundException

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

the class RepositoryApiImpl method getRepositorySourceMirror.

@Override
public GetRepositorySourceMirrorResult getRepositorySourceMirror(GetRepositorySourceMirrorRequest request) {
    Preconditions.checkArgument(null != request, "the request must be provided");
    Preconditions.checkArgument(StringUtils.isNotBlank(request.code), "a mirror code must be provided");
    final ObjectContext context = serverRuntime.newContext();
    RepositorySourceMirror repositorySourceMirror = RepositorySourceMirror.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(RepositorySourceMirror.class.getSimpleName(), request.code));
    GetRepositorySourceMirrorResult result = new GetRepositorySourceMirrorResult();
    result.active = repositorySourceMirror.getActive();
    result.baseUrl = repositorySourceMirror.getBaseUrl();
    result.code = repositorySourceMirror.getCode();
    result.countryCode = repositorySourceMirror.getCountry().getCode();
    result.createTimestamp = repositorySourceMirror.getCreateTimestamp().getTime();
    result.modifyTimestamp = repositorySourceMirror.getModifyTimestamp().getTime();
    result.description = repositorySourceMirror.getDescription();
    result.isPrimary = repositorySourceMirror.getIsPrimary();
    result.repositorySourceCode = repositorySourceMirror.getRepositorySource().getCode();
    return result;
}
Also used : GetRepositorySourceMirrorResult(org.haiku.haikudepotserver.api1.model.repository.GetRepositorySourceMirrorResult) org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.auto._RepositorySourceMirror) RepositorySourceMirror(org.haiku.haikudepotserver.dataobjects.RepositorySourceMirror) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) ObjectContext(org.apache.cayenne.ObjectContext)

Example 5 with ObjectNotFoundException

use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException 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;
}
Also used : VersionCoordinates(org.haiku.haikudepotserver.support.VersionCoordinates) UserRatingSearchSpecification(org.haiku.haikudepotserver.userrating.model.UserRatingSearchSpecification) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) org.haiku.haikudepotserver.dataobjects.auto._PkgVersion(org.haiku.haikudepotserver.dataobjects.auto._PkgVersion) ObjectContext(org.apache.cayenne.ObjectContext)

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