Search in sources :

Example 1 with CreateRepositorySourceResult

use of org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceResult in project haikudepotserver by haiku.

the class RepositoryApiImpl method createRepositorySource.

@Override
public CreateRepositorySourceResult createRepositorySource(CreateRepositorySourceRequest request) {
    Preconditions.checkArgument(null != request, "the request must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code), "the code for the new repository source must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositoryCode), "the repository for the new repository source must be identified");
    final ObjectContext context = serverRuntime.newContext();
    Repository repository = getRepositoryOrThrow(context, request.repositoryCode);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("unable to edit the repository [" + repository + "]");
    }
    Optional<RepositorySource> existingRepositorySourceOptional = RepositorySource.tryGetByCode(context, request.code);
    if (existingRepositorySourceOptional.isPresent()) {
        throw new ValidationException(new ValidationFailure(RepositorySource.CODE.getName(), "unique"));
    }
    RepositorySource repositorySource = context.newObject(RepositorySource.class);
    repositorySource.setRepository(repository);
    repositorySource.setCode(request.code);
    repository.setModifyTimestamp();
    context.commitChanges();
    LOGGER.info("did create a new repository source '{}' on the repository '{}'", repositorySource, repository);
    return new CreateRepositorySourceResult();
}
Also used : Repository(org.haiku.haikudepotserver.dataobjects.Repository) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) CreateRepositorySourceResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceResult) ObjectContext(org.apache.cayenne.ObjectContext) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)1 CreateRepositorySourceResult (org.haiku.haikudepotserver.api1.model.repository.CreateRepositorySourceResult)1 ValidationException (org.haiku.haikudepotserver.api1.support.ValidationException)1 ValidationFailure (org.haiku.haikudepotserver.api1.support.ValidationFailure)1 Repository (org.haiku.haikudepotserver.dataobjects.Repository)1 RepositorySource (org.haiku.haikudepotserver.dataobjects.RepositorySource)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1