use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.
the class RepositoryApiImpl method updateRepositorySourceMirror.
@Override
public UpdateRepositorySourceMirrorResult updateRepositorySourceMirror(UpdateRepositorySourceMirrorRequest request) {
Preconditions.checkArgument(null != request, "the request must be supplied");
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code), "the code for the mirror to update");
final ObjectContext context = serverRuntime.newContext();
RepositorySourceMirror repositorySourceMirror = RepositorySourceMirror.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(RepositorySourceMirror.class.getSimpleName(), request.code));
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySourceMirror.getRepositorySource().getRepository(), Permission.REPOSITORY_EDIT)) {
throw new AccessDeniedException("the repository [" + repositorySourceMirror.getRepositorySource().getRepository() + "] is unable to be edited");
}
for (UpdateRepositorySourceMirrorRequest.Filter filter : CollectionUtils.emptyIfNull(request.filter)) {
switch(filter) {
case ACTIVE:
if (repositorySourceMirror.getIsPrimary()) {
throw new ValidationException(new ValidationFailure(RepositorySourceMirror.ACTIVE.getName(), "confict"));
}
repositorySourceMirror.setActive(null != request.active && request.active);
break;
case BASE_URL:
if (StringUtils.isBlank(request.baseUrl)) {
throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "required"));
}
if (!repositorySourceMirror.getBaseUrl().equals(request.baseUrl)) {
if (tryGetRepositorySourceMirrorObjectIdForBaseUrl(repositorySourceMirror.getRepositorySource().getCode(), request.baseUrl).isPresent()) {
throw new ValidationException(new ValidationFailure(RepositorySourceMirror.BASE_URL.getName(), "unique"));
}
repositorySourceMirror.setBaseUrl(request.baseUrl);
}
break;
case COUNTRY:
Country country = Country.tryGetByCode(context, request.countryCode).orElseThrow(() -> new ObjectNotFoundException(Country.class.getSimpleName(), request.countryCode));
repositorySourceMirror.setCountry(country);
break;
case IS_PRIMARY:
boolean isPrimary = null != request.isPrimary && request.isPrimary;
if (isPrimary != repositorySourceMirror.getIsPrimary()) {
if (isPrimary) {
// in this case, the former primary should loose it's primary
// status so that it can be swapped to this one.
repositorySourceMirror.getRepositorySource().getPrimaryMirror().setIsPrimary(false);
repositorySourceMirror.setIsPrimary(true);
} else {
throw new ValidationException(new ValidationFailure(RepositorySourceMirror.IS_PRIMARY.getName(), "confict"));
}
}
break;
case DESCRIPTION:
repositorySourceMirror.setDescription(StringUtils.trimToNull(request.description));
break;
default:
throw new IllegalStateException("unknown change filter for mirror [" + filter + "]");
}
}
if (context.hasChanges()) {
repositorySourceMirror.getRepositorySource().getRepository().setModifyTimestamp();
}
context.commitChanges();
LOGGER.info("did update mirror [{}]", repositorySourceMirror.getCode());
return new UpdateRepositorySourceMirrorResult();
}
use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.
the class RepositoryApiImpl method removeRepositorySourceMirror.
@Override
public RemoveRepositorySourceMirrorResult removeRepositorySourceMirror(RemoveRepositorySourceMirrorRequest request) {
Preconditions.checkArgument(null != request, "the request is required");
Preconditions.checkArgument(StringUtils.isNotBlank(request.code), "the code is required on the request");
final ObjectContext context = serverRuntime.newContext();
RepositorySourceMirror repositorySourceMirror = RepositorySourceMirror.tryGetByCode(context, request.code).orElseThrow(() -> new ObjectNotFoundException(RepositorySourceMirror.class.getSimpleName(), request.code));
if (repositorySourceMirror.getIsPrimary()) {
throw new IllegalStateException("unable to remove the primary mirror");
}
repositorySourceMirror.getRepositorySource().getRepository().setModifyTimestamp();
context.deleteObject(repositorySourceMirror);
context.commitChanges();
LOGGER.info("did remote the repository source mirror [{}]", request.code);
return new RemoveRepositorySourceMirrorResult();
}
use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.
the class RepositoryApiImpl method triggerImportRepository.
// note; no integration test for this one.
@Override
public TriggerImportRepositoryResult triggerImportRepository(TriggerImportRepositoryRequest triggerImportRepositoryRequest) {
Preconditions.checkNotNull(triggerImportRepositoryRequest);
Preconditions.checkState(!Strings.isNullOrEmpty(triggerImportRepositoryRequest.repositoryCode));
Preconditions.checkArgument(null == triggerImportRepositoryRequest.repositorySourceCodes || !triggerImportRepositoryRequest.repositorySourceCodes.isEmpty(), "bad repository sources");
final ObjectContext context = serverRuntime.newContext();
Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, triggerImportRepositoryRequest.repositoryCode);
if (repositoryOptional.isEmpty()) {
throw new ObjectNotFoundException(Repository.class.getSimpleName(), triggerImportRepositoryRequest.repositoryCode);
}
Repository repository = repositoryOptional.get();
if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repository, Permission.REPOSITORY_IMPORT)) {
throw new AccessDeniedException("attempt to trigger repository import for [" + repository + "]");
}
Set<RepositorySource> repositorySources = null;
if (null != triggerImportRepositoryRequest.repositorySourceCodes) {
repositorySources = new HashSet<>();
for (String repositorySourceCode : triggerImportRepositoryRequest.repositorySourceCodes) {
repositorySources.add(repository.getRepositorySources().stream().filter(rs -> rs.getCode().equals(repositorySourceCode)).collect(SingleCollector.optional()).orElseThrow(() -> new ObjectNotFoundException(RepositorySource.class.getSimpleName(), repositorySourceCode)));
}
}
jobService.submit(new RepositoryHpkrIngressJobSpecification(repository.getCode(), null == repositorySources ? null : repositorySources.stream().map(RepositorySource::getCode).collect(Collectors.toSet())), JobSnapshot.COALESCE_STATUSES_QUEUED);
return new TriggerImportRepositoryResult();
}
use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.
the class RepositoryApiImpl method getRepositorySource.
@Override
public GetRepositorySourceResult getRepositorySource(GetRepositorySourceRequest request) {
Preconditions.checkArgument(null != request);
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code));
final ObjectContext context = serverRuntime.newContext();
Optional<RepositorySource> repositorySourceOptional = RepositorySource.tryGetByCode(context, request.code);
if (repositorySourceOptional.isEmpty()) {
throw new ObjectNotFoundException(RepositorySource.class.getSimpleName(), request.code);
}
RepositorySource repositorySource = repositorySourceOptional.get();
GetRepositorySourceResult result = new GetRepositorySourceResult();
result.active = repositorySource.getActive();
result.code = repositorySource.getCode();
result.repositoryCode = repositorySource.getRepository().getCode();
result.identifier = repositorySource.getIdentifier();
result.architectureCode = Optional.ofNullable(repositorySource.getArchitecture()).map(Architecture::getCode).orElse(null);
if (null != repositorySource.getLastImportTimestamp()) {
result.lastImportTimestamp = repositorySource.getLastImportTimestamp().getTime();
}
result.extraIdentifiers = repositorySource.getExtraIdentifiers();
result.repositorySourceMirrors = repositorySource.getRepositorySourceMirrors().stream().filter(m -> m.getActive() || BooleanUtils.isTrue(request.includeInactiveRepositorySourceMirrors)).sorted().map(rsm -> {
GetRepositorySourceResult.RepositorySourceMirror mirror = new GetRepositorySourceResult.RepositorySourceMirror();
mirror.active = rsm.getActive();
mirror.baseUrl = rsm.getBaseUrl();
mirror.countryCode = rsm.getCountry().getCode();
mirror.isPrimary = rsm.getIsPrimary();
mirror.code = rsm.getCode();
return mirror;
}).collect(Collectors.toList());
if (permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), repositorySource.getRepository(), Permission.REPOSITORY_EDIT)) {
result.forcedInternalBaseUrl = repositorySourceOptional.get().getForcedInternalBaseUrl();
}
return result;
}
use of org.haiku.haikudepotserver.api1.support.ObjectNotFoundException in project haikudepotserver by haiku.
the class UserRatingApiImpl method getUserRatingByUserAndPkgVersion.
@Override
public GetUserRatingByUserAndPkgVersionResult getUserRatingByUserAndPkgVersion(GetUserRatingByUserAndPkgVersionRequest request) {
Preconditions.checkNotNull(request);
Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgName), "the package name must be supplied");
Preconditions.checkState(!Strings.isNullOrEmpty(request.userNickname), "the user nickname must be supplied");
Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgVersionArchitectureCode), "the pkg version architecture code must be supplied");
Preconditions.checkState(!Strings.isNullOrEmpty(request.pkgVersionMajor), "the package version major code must be supplied");
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.repositoryCode), "the repository code should be supplied");
final ObjectContext context = serverRuntime.newContext();
Architecture architecture = getArchitecture(context, request.pkgVersionArchitectureCode);
User user = User.tryGetByNickname(context, request.userNickname).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), request.userNickname));
Pkg pkg = Pkg.tryGetByName(context, request.pkgName).orElseThrow(() -> new ObjectNotFoundException(Pkg.class.getSimpleName(), request.pkgName));
Repository repository = getRepository(context, request.repositoryCode);
VersionCoordinates versionCoordinates = new VersionCoordinates(request.pkgVersionMajor, request.pkgVersionMinor, request.pkgVersionMicro, request.pkgVersionPreRelease, request.pkgVersionRevision);
Optional<PkgVersion> pkgVersionOptional = PkgVersion.getForPkg(context, pkg, repository, architecture, versionCoordinates);
if (pkgVersionOptional.isEmpty() || !pkgVersionOptional.get().getActive()) {
throw new ObjectNotFoundException(PkgVersion.class.getSimpleName(), request.pkgName + "@" + versionCoordinates.toString());
}
Optional<UserRating> userRatingOptional = UserRating.getByUserAndPkgVersion(context, user, pkgVersionOptional.get());
if (userRatingOptional.isEmpty()) {
throw new ObjectNotFoundException(UserRating.class.getSimpleName(), "");
}
GetUserRatingByUserAndPkgVersionResult result = new GetUserRatingByUserAndPkgVersionResult();
fillAbstractGetUserRatingResult(userRatingOptional.get(), result);
return result;
}
Aggregations