Skip to content

Commit 293b7ff

Browse files
committed
implement extra identifier on a repository source
1 parent 1c3ef44 commit 293b7ff

File tree

22 files changed

+298
-53
lines changed

22 files changed

+298
-53
lines changed

haikudepotserver-api1/src/main/java/org/haiku/haikudepotserver/api1/model/repository/GetRepositorySourceResult.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2018, Andrew Lindesay
2+
* Copyright 2015-2020, Andrew Lindesay
33
* Distributed under the terms of the MIT License.
44
*/
55

@@ -41,6 +41,12 @@ public class GetRepositorySourceResult {
4141

4242
public Long lastImportTimestamp;
4343

44+
/**
45+
* @since 2020-06-23
46+
*/
47+
48+
public List<String> extraIdentifiers;
49+
4450
/**
4551
* @since 2018-07-28
4652
*/

haikudepotserver-api1/src/main/java/org/haiku/haikudepotserver/api1/model/repository/UpdateRepositorySourceRequest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public class UpdateRepositorySourceRequest {
1515

1616
public enum Filter {
1717
ACTIVE,
18-
FORCED_INTERNAL_BASE_URL
18+
FORCED_INTERNAL_BASE_URL,
19+
EXTRA_IDENTIFIERS
1920
}
2021

2122
/**
@@ -38,4 +39,10 @@ public enum Filter {
3839

3940
public List<Filter> filter;
4041

42+
/**
43+
* @since 2020-06-23
44+
*/
45+
46+
public List<String> extraIdentifiers;
47+
4148
}

haikudepotserver-core-test/src/main/java/org/haiku/haikudepotserver/IntegrationTestSupportService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019, Andrew Lindesay
2+
* Copyright 2018-2020, Andrew Lindesay
33
* Distributed under the terms of the MIT License.
44
*/
55

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

164+
RepositorySourceExtraIdentifier repositorySourceExtraIdentifier = context.newObject(RepositorySourceExtraIdentifier.class);
165+
repositorySourceExtraIdentifier.setIdentifier("example:haiku:identifier");
166+
repositorySourceExtraIdentifier.setRepositorySource(result.repositorySource);
167+
164168
RepositorySourceMirror primaryMirror = context.newObject(RepositorySourceMirror.class);
165169
primaryMirror.setCountry(Country.getByCode(context, "NZ"));
166170
primaryMirror.setIsPrimary(true);

haikudepotserver-core-test/src/test/java/org/haiku/haikudepotserver/api1/RepositoryApiIT.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018, Andrew Lindesay
2+
* Copyright 2018-2020, Andrew Lindesay
33
* Distributed under the terms of the MIT License.
44
*/
55

@@ -25,6 +25,7 @@
2525

2626
import javax.annotation.Resource;
2727
import java.util.Collections;
28+
import java.util.List;
2829
import java.util.Optional;
2930

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

216218
GetRepositorySourceResult.RepositorySourceMirror mirror0 = result.repositorySourceMirrors.get(0);
217219
GetRepositorySourceResult.RepositorySourceMirror mirror1 = result.repositorySourceMirrors.get(1);
@@ -231,7 +233,9 @@ public void testUpdateRepositorySource() throws Exception {
231233
UpdateRepositorySourceRequest request = new UpdateRepositorySourceRequest();
232234
request.code = "testreposrc_xyz";
233235
request.active = false;
234-
request.filter = Collections.singletonList(
236+
request.extraIdentifiers = List.of("birds:of:a:feather");
237+
request.filter = List.of(
238+
UpdateRepositorySourceRequest.Filter.EXTRA_IDENTIFIERS,
235239
UpdateRepositorySourceRequest.Filter.ACTIVE);
236240

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

249254
}

haikudepotserver-core/src/main/java/org/haiku/haikudepotserver/api1/RepositoryApiImpl.java

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018, Andrew Lindesay
2+
* Copyright 2018-2020, Andrew Lindesay
33
* Distributed under the terms of the MIT License.
44
*/
55

@@ -13,6 +13,7 @@
1313
import org.apache.cayenne.PersistentObject;
1414
import org.apache.cayenne.configuration.server.ServerRuntime;
1515
import org.apache.commons.collections4.CollectionUtils;
16+
import org.apache.commons.collections4.SetUtils;
1617
import org.apache.commons.lang3.BooleanUtils;
1718
import org.apache.commons.lang3.StringUtils;
1819
import org.haiku.haikudepotserver.api1.model.repository.*;
@@ -96,11 +97,11 @@ public TriggerImportRepositoryResult triggerImportRepository(
9697

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

99-
if(!repositoryOptional.isPresent()) {
100+
if (!repositoryOptional.isPresent()) {
100101
throw new ObjectNotFoundException(Repository.class.getSimpleName(), triggerImportRepositoryRequest.repositoryCode);
101102
}
102103

103-
if(!authorizationService.check(
104+
if (!authorizationService.check(
104105
context,
105106
tryObtainAuthenticatedUser(context).orElse(null),
106107
repositoryOptional.get(),
@@ -110,11 +111,11 @@ public TriggerImportRepositoryResult triggerImportRepository(
110111

111112
Set<RepositorySource> repositorySources = null;
112113

113-
if(null!=triggerImportRepositoryRequest.repositorySourceCodes) {
114+
if (null != triggerImportRepositoryRequest.repositorySourceCodes) {
114115

115116
repositorySources = new HashSet<>();
116117

117-
for(String repositorySourceCode : triggerImportRepositoryRequest.repositorySourceCodes) {
118+
for (String repositorySourceCode : triggerImportRepositoryRequest.repositorySourceCodes) {
118119
repositorySources.add(
119120
repositoryOptional.get()
120121
.getRepositorySources()
@@ -145,16 +146,16 @@ public SearchRepositoriesResult searchRepositories(SearchRepositoriesRequest req
145146

146147
final ObjectContext context = serverRuntime.newContext();
147148

148-
if(!authorizationService.check(
149+
if (!authorizationService.check(
149150
context,
150151
tryObtainAuthenticatedUser(context).orElse(null),
151152
null,
152153
Permission.REPOSITORY_LIST)) {
153154
throw new AuthorizationFailureException();
154155
}
155156

156-
if(null!=request.includeInactive && request.includeInactive) {
157-
if(!authorizationService.check(
157+
if (null != request.includeInactive && request.includeInactive) {
158+
if (!authorizationService.check(
158159
context,
159160
tryObtainAuthenticatedUser(context).orElse(null),
160161
null,
@@ -166,13 +167,13 @@ public SearchRepositoriesResult searchRepositories(SearchRepositoriesRequest req
166167
RepositorySearchSpecification specification = new RepositorySearchSpecification();
167168
String exp = request.expression;
168169

169-
if(null!=exp) {
170+
if (null != exp) {
170171
exp = Strings.emptyToNull(exp.trim().toLowerCase());
171172
}
172173

173174
specification.setExpression(exp);
174175

175-
if(null!=request.expressionType) {
176+
if (null != request.expressionType) {
176177
specification.setExpressionType(
177178
PkgSearchSpecification.ExpressionType.valueOf(request.expressionType.name()));
178179
}
@@ -186,7 +187,7 @@ public SearchRepositoriesResult searchRepositories(SearchRepositoriesRequest req
186187
result.total = repositoryService.total(context, specification);
187188
result.items = Collections.emptyList();
188189

189-
if(result.total > 0) {
190+
if (result.total > 0) {
190191
List<Repository> searchedRepositories = repositoryService.search(context, specification);
191192

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

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

213-
if(!repositoryOptional.isPresent()) {
214+
if (!repositoryOptional.isPresent()) {
214215
throw new ObjectNotFoundException(Repository.class.getSimpleName(), getRepositoryRequest.code);
215216
}
216217

217-
if(!authorizationService.check(
218+
if (!authorizationService.check(
218219
context,
219220
tryObtainAuthenticatedUser(context).orElse(null),
220221
repositoryOptional.get(),
@@ -270,22 +271,22 @@ public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRep
270271
repository,
271272
Permission.REPOSITORY_EDIT);
272273

273-
for(UpdateRepositoryRequest.Filter filter : updateRepositoryRequest.filter) {
274-
switch(filter) {
274+
for (UpdateRepositoryRequest.Filter filter : updateRepositoryRequest.filter) {
275+
switch (filter) {
275276

276277
case ACTIVE:
277-
if(null==updateRepositoryRequest.active) {
278+
if (null==updateRepositoryRequest.active) {
278279
throw new IllegalStateException("the active flag must be supplied");
279280
}
280281

281-
if(repository.getActive() != updateRepositoryRequest.active) {
282+
if (repository.getActive() != updateRepositoryRequest.active) {
282283
repository.setActive(updateRepositoryRequest.active);
283284
LOGGER.info("did set the active flag on repository {} to {}", updateRepositoryRequest.code, updateRepositoryRequest.active);
284285
}
285286

286-
if(!updateRepositoryRequest.active) {
287-
for(RepositorySource repositorySource : repository.getRepositorySources()) {
288-
if(repositorySource.getActive()) {
287+
if (!updateRepositoryRequest.active) {
288+
for (RepositorySource repositorySource : repository.getRepositorySources()) {
289+
if (repositorySource.getActive()) {
289290
repositorySource.setActive(false);
290291
LOGGER.info("did set the active flag on the repository source {} to false", repositorySource.getCode());
291292
}
@@ -295,13 +296,13 @@ public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRep
295296
break;
296297

297298
case NAME:
298-
if(null==updateRepositoryRequest.name) {
299+
if (null == updateRepositoryRequest.name) {
299300
throw new IllegalStateException("the name must be supplied to update the repository");
300301
}
301302

302303
String name = updateRepositoryRequest.name.trim();
303304

304-
if(0==name.length()) {
305+
if (0 == name.length()) {
305306
throw new ValidationException(new ValidationFailure(Repository.NAME.getName(), "invalid"));
306307
}
307308

@@ -329,7 +330,7 @@ public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRep
329330
}
330331
}
331332

332-
if(context.hasChanges()) {
333+
if (context.hasChanges()) {
333334
context.commitChanges();
334335
}
335336
else {
@@ -347,7 +348,7 @@ public CreateRepositoryResult createRepository(
347348

348349
final ObjectContext context = serverRuntime.newContext();
349350

350-
if(!authorizationService.check(
351+
if (!authorizationService.check(
351352
context,
352353
tryObtainAuthenticatedUser(context).orElse(null),
353354
null,
@@ -357,7 +358,7 @@ public CreateRepositoryResult createRepository(
357358

358359
// the code must be supplied.
359360

360-
if(Strings.isNullOrEmpty(createRepositoryRequest.code)) {
361+
if (Strings.isNullOrEmpty(createRepositoryRequest.code)) {
361362
throw new ValidationException(new ValidationFailure(Repository.CODE.getName(), "required"));
362363
}
363364

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

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

390391
final ObjectContext context = serverRuntime.newContext();
391-
User authenticatedUser = tryObtainAuthenticatedUser(context).orElse(null);
392392

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

395-
if(!repositorySourceOptional.isPresent()) {
395+
if (!repositorySourceOptional.isPresent()) {
396396
throw new ObjectNotFoundException(RepositorySource.class.getSimpleName(), request.code);
397397
}
398398

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

411+
result.extraIdentifiers = repositorySource.getExtraIdentifiers();
412+
411413
result.repositorySourceMirrors = repositorySource.getRepositorySourceMirrors()
412414
.stream()
413415
.filter(m -> m.getActive() || BooleanUtils.isTrue(request.includeInactiveRepositorySourceMirrors))
@@ -424,7 +426,7 @@ public GetRepositorySourceResult getRepositorySource(GetRepositorySourceRequest
424426
})
425427
.collect(Collectors.toList());
426428

427-
if(authorizationService.check(
429+
if (authorizationService.check(
428430
context,
429431
tryObtainAuthenticatedUser(context).orElse(null),
430432
repositorySource.getRepository(),
@@ -445,26 +447,26 @@ public UpdateRepositorySourceResult updateRepositorySource(UpdateRepositorySourc
445447

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

448-
if(!repositorySourceOptional.isPresent()) {
450+
if (!repositorySourceOptional.isPresent()) {
449451
throw new ObjectNotFoundException(RepositorySource.class.getSimpleName(), request.code);
450452
}
451453

452454
RepositorySource repositorySource = repositorySourceOptional.get();
453455

454-
if(!authorizationService.check(
456+
if (!authorizationService.check(
455457
context,
456458
tryObtainAuthenticatedUser(context).orElse(null),
457459
repositorySourceOptional.get().getRepository(),
458460
Permission.REPOSITORY_EDIT)) {
459461
throw new AuthorizationFailureException();
460462
}
461463

462-
for(UpdateRepositorySourceRequest.Filter filter : request.filter) {
464+
for (UpdateRepositorySourceRequest.Filter filter : request.filter) {
463465

464-
switch(filter) {
466+
switch (filter) {
465467

466468
case ACTIVE:
467-
if(null==request.active) {
469+
if (null == request.active) {
468470
throw new IllegalArgumentException("the active field must be provided if the request requires it to be updated");
469471
}
470472
repositorySource.setActive(request.active);
@@ -477,6 +479,25 @@ public UpdateRepositorySourceResult updateRepositorySource(UpdateRepositorySourc
477479
LOGGER.info("did set the repository source forced internal base url");
478480
break;
479481

482+
case EXTRA_IDENTIFIERS: {
483+
Set<String> existing = Set.copyOf(repositorySource.getExtraIdentifiers());
484+
Set<String> desired = Set.copyOf(CollectionUtils.emptyIfNull(request.extraIdentifiers));
485+
SetUtils.difference(existing, desired).stream()
486+
.map(repositorySource::tryGetRepositorySourceExtraIdentifierForIdentifier)
487+
.map(Optional::orElseThrow)
488+
.forEach(rsei -> {
489+
repositorySource.removeFromRepositorySourceExtraIdentifiers(rsei);
490+
context.deleteObject(rsei);
491+
});
492+
SetUtils.difference(desired, existing).forEach(i -> {
493+
RepositorySourceExtraIdentifier rsei = context.newObject(RepositorySourceExtraIdentifier.class);
494+
rsei.setRepositorySource(repositorySource);
495+
rsei.setIdentifier(i);
496+
repositorySource.addToRepositorySourceExtraIdentifiers(rsei);
497+
});
498+
break;
499+
}
500+
480501
default:
481502
throw new IllegalStateException("unhandled filter; " + filter.name());
482503

0 commit comments

Comments
 (0)