Search in sources :

Example 1 with CreateRepositoryResult

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

the class RepositoryApiImpl method createRepository.

@Override
public CreateRepositoryResult createRepository(CreateRepositoryRequest createRepositoryRequest) {
    Preconditions.checkNotNull(createRepositoryRequest);
    final ObjectContext context = serverRuntime.newContext();
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.REPOSITORY_ADD)) {
        throw new AccessDeniedException("unable to add a repository");
    }
    if (Strings.isNullOrEmpty(createRepositoryRequest.code)) {
        throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "required"));
    }
    // check to see if there is an existing repository with the same code; non-unique.
    {
        Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, createRepositoryRequest.code);
        if (repositoryOptional.isPresent()) {
            throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "unique"));
        }
    }
    Repository repository = context.newObject(Repository.class);
    repository.setCode(createRepositoryRequest.code);
    repository.setName(createRepositoryRequest.name);
    repository.setInformationUrl(createRepositoryRequest.informationUrl);
    context.commitChanges();
    return new CreateRepositoryResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) Repository(org.haiku.haikudepotserver.dataobjects.Repository) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) Optional(java.util.Optional) ObjectContext(org.apache.cayenne.ObjectContext) CreateRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryResult) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Aggregations

Optional (java.util.Optional)1 ObjectContext (org.apache.cayenne.ObjectContext)1 CreateRepositoryResult (org.haiku.haikudepotserver.api1.model.repository.CreateRepositoryResult)1 ValidationException (org.haiku.haikudepotserver.api1.support.ValidationException)1 ValidationFailure (org.haiku.haikudepotserver.api1.support.ValidationFailure)1 Repository (org.haiku.haikudepotserver.dataobjects.Repository)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1