Search in sources :

Example 1 with UpdateRepositoryResult

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

the class RepositoryApiImpl method updateRepository.

@Override
public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRepositoryRequest) {
    Preconditions.checkNotNull(updateRepositoryRequest);
    final ObjectContext context = serverRuntime.newContext();
    Repository repository = getRepositoryOrThrow(context, updateRepositoryRequest.code);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_EDIT)) {
        throw new AccessDeniedException("unable to edit the repository [" + repository + "]");
    }
    for (UpdateRepositoryRequest.Filter filter : updateRepositoryRequest.filter) {
        switch(filter) {
            case ACTIVE:
                if (null == updateRepositoryRequest.active) {
                    throw new IllegalStateException("the active flag must be supplied");
                }
                if (repository.getActive() != updateRepositoryRequest.active) {
                    repository.setActive(updateRepositoryRequest.active);
                    LOGGER.info("did set the active flag on repository {} to {}", updateRepositoryRequest.code, updateRepositoryRequest.active);
                }
                if (!updateRepositoryRequest.active) {
                    for (RepositorySource repositorySource : repository.getRepositorySources()) {
                        if (repositorySource.getActive()) {
                            repositorySource.setActive(false);
                            LOGGER.info("did set the active flag on the repository source {} to false", repositorySource.getCode());
                        }
                    }
                }
                break;
            case NAME:
                if (null == updateRepositoryRequest.name) {
                    throw new IllegalStateException("the name must be supplied to update the repository");
                }
                String name = updateRepositoryRequest.name.trim();
                if (0 == name.length()) {
                    throw new ValidationException(new ValidationFailure(Repository.NAME.getName(), "invalid"));
                }
                repository.setName(name);
                break;
            case INFORMATIONURL:
                repository.setInformationUrl(updateRepositoryRequest.informationUrl);
                LOGGER.info("did set the information url on repository {} to {}", updateRepositoryRequest.code, updateRepositoryRequest.informationUrl);
                break;
            case PASSWORD:
                if (StringUtils.isBlank(updateRepositoryRequest.passwordClear)) {
                    repository.setPasswordSalt(null);
                    repository.setPasswordHash(null);
                    LOGGER.info("cleared the password for repository [{}]", repository);
                } else {
                    repositoryService.setPassword(repository, updateRepositoryRequest.passwordClear);
                    LOGGER.info("did update the repository [{}] password", repository);
                }
                break;
            default:
                throw new IllegalStateException("unhandled filter for updating a repository");
        }
    }
    if (context.hasChanges()) {
        context.commitChanges();
    } else {
        LOGGER.info("update repository {} with no changes made", updateRepositoryRequest.code);
    }
    return new UpdateRepositoryResult();
}
Also used : UpdateRepositoryRequest(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest) Repository(org.haiku.haikudepotserver.dataobjects.Repository) AccessDeniedException(org.springframework.security.access.AccessDeniedException) UpdateRepositoryResult(org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryResult) ValidationException(org.haiku.haikudepotserver.api1.support.ValidationException) RepositorySource(org.haiku.haikudepotserver.dataobjects.RepositorySource) ObjectContext(org.apache.cayenne.ObjectContext) ValidationFailure(org.haiku.haikudepotserver.api1.support.ValidationFailure)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)1 UpdateRepositoryRequest (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryRequest)1 UpdateRepositoryResult (org.haiku.haikudepotserver.api1.model.repository.UpdateRepositoryResult)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