Skip to content

Commit

Permalink
implement extra identifier on a repository source
Browse files Browse the repository at this point in the history
  • Loading branch information
andponlin committed Jun 23, 2020
1 parent 1c3ef44 commit 293b7ff
Show file tree
Hide file tree
Showing 22 changed files with 298 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2018, Andrew Lindesay
* Copyright 2015-2020, Andrew Lindesay
* Distributed under the terms of the MIT License.
*/

Expand Down Expand Up @@ -41,6 +41,12 @@ public class GetRepositorySourceResult {

public Long lastImportTimestamp;

/**
* @since 2020-06-23
*/

public List<String> extraIdentifiers;

/**
* @since 2018-07-28
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class UpdateRepositorySourceRequest {

public enum Filter {
ACTIVE,
FORCED_INTERNAL_BASE_URL
FORCED_INTERNAL_BASE_URL,
EXTRA_IDENTIFIERS
}

/**
Expand All @@ -38,4 +39,10 @@ public enum Filter {

public List<Filter> filter;

/**
* @since 2020-06-23
*/

public List<String> extraIdentifiers;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019, Andrew Lindesay
* Copyright 2018-2020, Andrew Lindesay
* Distributed under the terms of the MIT License.
*/

Expand Down Expand Up @@ -161,6 +161,10 @@ public StandardTestData createStandardTestData() {
result.repositorySource.setRepository(result.repository);
result.repositorySource.setIdentifier("http://www.example.com/test/identifier/url");

RepositorySourceExtraIdentifier repositorySourceExtraIdentifier = context.newObject(RepositorySourceExtraIdentifier.class);
repositorySourceExtraIdentifier.setIdentifier("example:haiku:identifier");
repositorySourceExtraIdentifier.setRepositorySource(result.repositorySource);

RepositorySourceMirror primaryMirror = context.newObject(RepositorySourceMirror.class);
primaryMirror.setCountry(Country.getByCode(context, "NZ"));
primaryMirror.setIsPrimary(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018, Andrew Lindesay
* Copyright 2018-2020, Andrew Lindesay
* Distributed under the terms of the MIT License.
*/

Expand All @@ -25,6 +25,7 @@

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@ContextConfiguration(classes = TestConfig.class)
Expand Down Expand Up @@ -212,6 +213,7 @@ public void testGetRepositorySource() throws Exception {
Assertions.assertThat(result.repositoryCode).isEqualTo("testrepo");
Assertions.assertThat(result.identifier).isEqualTo("http://www.example.com/test/identifier/url");
Assertions.assertThat(result.repositorySourceMirrors.size()).isEqualTo(2);
Assertions.assertThat(result.extraIdentifiers).containsExactly("example:haiku:identifier");

GetRepositorySourceResult.RepositorySourceMirror mirror0 = result.repositorySourceMirrors.get(0);
GetRepositorySourceResult.RepositorySourceMirror mirror1 = result.repositorySourceMirrors.get(1);
Expand All @@ -231,7 +233,9 @@ public void testUpdateRepositorySource() throws Exception {
UpdateRepositorySourceRequest request = new UpdateRepositorySourceRequest();
request.code = "testreposrc_xyz";
request.active = false;
request.filter = Collections.singletonList(
request.extraIdentifiers = List.of("birds:of:a:feather");
request.filter = List.of(
UpdateRepositorySourceRequest.Filter.EXTRA_IDENTIFIERS,
UpdateRepositorySourceRequest.Filter.ACTIVE);

// ------------------------------------
Expand All @@ -244,6 +248,7 @@ public void testUpdateRepositorySource() throws Exception {
// this url was set before and is retained after the update.
Assertions.assertThat(repositorySourceAfter.getIdentifier()).isEqualTo("http://www.example.com/test/identifier/url");
Assertions.assertThat(repositorySourceAfter.getActive()).isFalse();
Assertions.assertThat(repositorySourceAfter.getExtraIdentifiers()).contains("birds:of:a:feather");
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018, Andrew Lindesay
* Copyright 2018-2020, Andrew Lindesay
* Distributed under the terms of the MIT License.
*/

Expand All @@ -13,6 +13,7 @@
import org.apache.cayenne.PersistentObject;
import org.apache.cayenne.configuration.server.ServerRuntime;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.SetUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.haiku.haikudepotserver.api1.model.repository.*;
Expand Down Expand Up @@ -96,11 +97,11 @@ public TriggerImportRepositoryResult triggerImportRepository(

Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, triggerImportRepositoryRequest.repositoryCode);

if(!repositoryOptional.isPresent()) {
if (!repositoryOptional.isPresent()) {
throw new ObjectNotFoundException(Repository.class.getSimpleName(), triggerImportRepositoryRequest.repositoryCode);
}

if(!authorizationService.check(
if (!authorizationService.check(
context,
tryObtainAuthenticatedUser(context).orElse(null),
repositoryOptional.get(),
Expand All @@ -110,11 +111,11 @@ public TriggerImportRepositoryResult triggerImportRepository(

Set<RepositorySource> repositorySources = null;

if(null!=triggerImportRepositoryRequest.repositorySourceCodes) {
if (null != triggerImportRepositoryRequest.repositorySourceCodes) {

repositorySources = new HashSet<>();

for(String repositorySourceCode : triggerImportRepositoryRequest.repositorySourceCodes) {
for (String repositorySourceCode : triggerImportRepositoryRequest.repositorySourceCodes) {
repositorySources.add(
repositoryOptional.get()
.getRepositorySources()
Expand Down Expand Up @@ -145,16 +146,16 @@ public SearchRepositoriesResult searchRepositories(SearchRepositoriesRequest req

final ObjectContext context = serverRuntime.newContext();

if(!authorizationService.check(
if (!authorizationService.check(
context,
tryObtainAuthenticatedUser(context).orElse(null),
null,
Permission.REPOSITORY_LIST)) {
throw new AuthorizationFailureException();
}

if(null!=request.includeInactive && request.includeInactive) {
if(!authorizationService.check(
if (null != request.includeInactive && request.includeInactive) {
if (!authorizationService.check(
context,
tryObtainAuthenticatedUser(context).orElse(null),
null,
Expand All @@ -166,13 +167,13 @@ public SearchRepositoriesResult searchRepositories(SearchRepositoriesRequest req
RepositorySearchSpecification specification = new RepositorySearchSpecification();
String exp = request.expression;

if(null!=exp) {
if (null != exp) {
exp = Strings.emptyToNull(exp.trim().toLowerCase());
}

specification.setExpression(exp);

if(null!=request.expressionType) {
if (null != request.expressionType) {
specification.setExpressionType(
PkgSearchSpecification.ExpressionType.valueOf(request.expressionType.name()));
}
Expand All @@ -186,7 +187,7 @@ public SearchRepositoriesResult searchRepositories(SearchRepositoriesRequest req
result.total = repositoryService.total(context, specification);
result.items = Collections.emptyList();

if(result.total > 0) {
if (result.total > 0) {
List<Repository> searchedRepositories = repositoryService.search(context, specification);

result.items = searchedRepositories.stream().map(sr -> {
Expand All @@ -210,11 +211,11 @@ public GetRepositoryResult getRepository(GetRepositoryRequest getRepositoryReque

Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, getRepositoryRequest.code);

if(!repositoryOptional.isPresent()) {
if (!repositoryOptional.isPresent()) {
throw new ObjectNotFoundException(Repository.class.getSimpleName(), getRepositoryRequest.code);
}

if(!authorizationService.check(
if (!authorizationService.check(
context,
tryObtainAuthenticatedUser(context).orElse(null),
repositoryOptional.get(),
Expand Down Expand Up @@ -270,22 +271,22 @@ public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRep
repository,
Permission.REPOSITORY_EDIT);

for(UpdateRepositoryRequest.Filter filter : updateRepositoryRequest.filter) {
switch(filter) {
for (UpdateRepositoryRequest.Filter filter : updateRepositoryRequest.filter) {
switch (filter) {

case ACTIVE:
if(null==updateRepositoryRequest.active) {
if (null==updateRepositoryRequest.active) {
throw new IllegalStateException("the active flag must be supplied");
}

if(repository.getActive() != updateRepositoryRequest.active) {
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()) {
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());
}
Expand All @@ -295,13 +296,13 @@ public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRep
break;

case NAME:
if(null==updateRepositoryRequest.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()) {
if (0 == name.length()) {
throw new ValidationException(new ValidationFailure(Repository.NAME.getName(), "invalid"));
}

Expand Down Expand Up @@ -329,7 +330,7 @@ public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRep
}
}

if(context.hasChanges()) {
if (context.hasChanges()) {
context.commitChanges();
}
else {
Expand All @@ -347,7 +348,7 @@ public CreateRepositoryResult createRepository(

final ObjectContext context = serverRuntime.newContext();

if(!authorizationService.check(
if (!authorizationService.check(
context,
tryObtainAuthenticatedUser(context).orElse(null),
null,
Expand All @@ -357,7 +358,7 @@ public CreateRepositoryResult createRepository(

// the code must be supplied.

if(Strings.isNullOrEmpty(createRepositoryRequest.code)) {
if (Strings.isNullOrEmpty(createRepositoryRequest.code)) {
throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "required"));
}

Expand All @@ -366,7 +367,7 @@ public CreateRepositoryResult createRepository(
{
Optional<Repository> repositoryOptional = Repository.tryGetByCode(context, createRepositoryRequest.code);

if(repositoryOptional.isPresent()) {
if (repositoryOptional.isPresent()) {
throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "unique"));
}
}
Expand All @@ -388,11 +389,10 @@ public GetRepositorySourceResult getRepositorySource(GetRepositorySourceRequest
Preconditions.checkArgument(!Strings.isNullOrEmpty(request.code));

final ObjectContext context = serverRuntime.newContext();
User authenticatedUser = tryObtainAuthenticatedUser(context).orElse(null);

Optional<RepositorySource> repositorySourceOptional = RepositorySource.tryGetByCode(context, request.code);

if(!repositorySourceOptional.isPresent()) {
if (!repositorySourceOptional.isPresent()) {
throw new ObjectNotFoundException(RepositorySource.class.getSimpleName(), request.code);
}

Expand All @@ -408,6 +408,8 @@ public GetRepositorySourceResult getRepositorySource(GetRepositorySourceRequest
result.lastImportTimestamp = repositorySource.getLastImportTimestamp().getTime();
}

result.extraIdentifiers = repositorySource.getExtraIdentifiers();

result.repositorySourceMirrors = repositorySource.getRepositorySourceMirrors()
.stream()
.filter(m -> m.getActive() || BooleanUtils.isTrue(request.includeInactiveRepositorySourceMirrors))
Expand All @@ -424,7 +426,7 @@ public GetRepositorySourceResult getRepositorySource(GetRepositorySourceRequest
})
.collect(Collectors.toList());

if(authorizationService.check(
if (authorizationService.check(
context,
tryObtainAuthenticatedUser(context).orElse(null),
repositorySource.getRepository(),
Expand All @@ -445,26 +447,26 @@ public UpdateRepositorySourceResult updateRepositorySource(UpdateRepositorySourc

Optional<RepositorySource> repositorySourceOptional = RepositorySource.tryGetByCode(context, request.code);

if(!repositorySourceOptional.isPresent()) {
if (!repositorySourceOptional.isPresent()) {
throw new ObjectNotFoundException(RepositorySource.class.getSimpleName(), request.code);
}

RepositorySource repositorySource = repositorySourceOptional.get();

if(!authorizationService.check(
if (!authorizationService.check(
context,
tryObtainAuthenticatedUser(context).orElse(null),
repositorySourceOptional.get().getRepository(),
Permission.REPOSITORY_EDIT)) {
throw new AuthorizationFailureException();
}

for(UpdateRepositorySourceRequest.Filter filter : request.filter) {
for (UpdateRepositorySourceRequest.Filter filter : request.filter) {

switch(filter) {
switch (filter) {

case ACTIVE:
if(null==request.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);
Expand All @@ -477,6 +479,25 @@ public UpdateRepositorySourceResult updateRepositorySource(UpdateRepositorySourc
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());

Expand Down
Loading

0 comments on commit 293b7ff

Please sign in to comment.