use of org.haiku.haikudepotserver.dataobjects.RepositorySourceExtraIdentifier in project haikudepotserver by haiku.
the class RepositoryApiImpl method updateRepositorySource.
@Override
public UpdateRepositorySourceResult updateRepositorySource(UpdateRepositorySourceRequest request) {
Preconditions.checkArgument(null != request);
Preconditions.checkArgument(StringUtils.isNotBlank(request.code), "a code is required to identify the repository source to update");
Preconditions.checkArgument(null != request.filter, "filters must be provided to specify what aspects of the repository source should be updated");
final ObjectContext context = serverRuntime.newContext();
RepositorySource repositorySource = getRepositorySourceOrThrow(context, request.code);
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySource.getRepository(), Permission.REPOSITORY_EDIT)) {
throw new AccessDeniedException("cannot edit the repository [" + repositorySource.getRepository() + "]");
}
for (UpdateRepositorySourceRequest.Filter filter : request.filter) {
switch(filter) {
case ACTIVE:
if (null == request.active) {
throw new IllegalArgumentException("the active field must be provided if the request requires it to be updated");
}
repositorySource.setActive(request.active);
LOGGER.info("did set the repository source {} active to {}", repositorySource, request.active);
break;
case FORCED_INTERNAL_BASE_URL:
repositorySource.setForcedInternalBaseUrl(StringUtils.trimToNull(request.forcedInternalBaseUrl));
LOGGER.info("did set the repository source forced internal base url");
break;
case EXTRA_IDENTIFIERS:
{
Set<String> existing = Set.copyOf(repositorySource.getExtraIdentifiers());
Set<String> desired = Set.copyOf(CollectionUtils.emptyIfNull(request.extraIdentifiers));
SetUtils.difference(existing, desired).stream().map(repositorySource::tryGetRepositorySourceExtraIdentifierForIdentifier).map(Optional::orElseThrow).forEach(rsei -> {
repositorySource.removeFromRepositorySourceExtraIdentifiers(rsei);
context.deleteObject(rsei);
});
SetUtils.difference(desired, existing).forEach(i -> {
RepositorySourceExtraIdentifier rsei = context.newObject(RepositorySourceExtraIdentifier.class);
rsei.setRepositorySource(repositorySource);
rsei.setIdentifier(i);
repositorySource.addToRepositorySourceExtraIdentifiers(rsei);
});
break;
}
default:
throw new IllegalStateException("unhandled filter; " + filter.name());
}
}
if (context.hasChanges()) {
repositorySource.getRepository().setModifyTimestamp();
context.commitChanges();
} else {
LOGGER.info("update repository source {} with no changes made", repositorySource);
}
return new UpdateRepositorySourceResult();
}
Aggregations