1
1
/*
2
- * Copyright 2018, Andrew Lindesay
2
+ * Copyright 2018-2020 , Andrew Lindesay
3
3
* Distributed under the terms of the MIT License.
4
4
*/
5
5
13
13
import org .apache .cayenne .PersistentObject ;
14
14
import org .apache .cayenne .configuration .server .ServerRuntime ;
15
15
import org .apache .commons .collections4 .CollectionUtils ;
16
+ import org .apache .commons .collections4 .SetUtils ;
16
17
import org .apache .commons .lang3 .BooleanUtils ;
17
18
import org .apache .commons .lang3 .StringUtils ;
18
19
import org .haiku .haikudepotserver .api1 .model .repository .*;
@@ -96,11 +97,11 @@ public TriggerImportRepositoryResult triggerImportRepository(
96
97
97
98
Optional <Repository > repositoryOptional = Repository .tryGetByCode (context , triggerImportRepositoryRequest .repositoryCode );
98
99
99
- if (!repositoryOptional .isPresent ()) {
100
+ if (!repositoryOptional .isPresent ()) {
100
101
throw new ObjectNotFoundException (Repository .class .getSimpleName (), triggerImportRepositoryRequest .repositoryCode );
101
102
}
102
103
103
- if (!authorizationService .check (
104
+ if (!authorizationService .check (
104
105
context ,
105
106
tryObtainAuthenticatedUser (context ).orElse (null ),
106
107
repositoryOptional .get (),
@@ -110,11 +111,11 @@ public TriggerImportRepositoryResult triggerImportRepository(
110
111
111
112
Set <RepositorySource > repositorySources = null ;
112
113
113
- if (null != triggerImportRepositoryRequest .repositorySourceCodes ) {
114
+ if (null != triggerImportRepositoryRequest .repositorySourceCodes ) {
114
115
115
116
repositorySources = new HashSet <>();
116
117
117
- for (String repositorySourceCode : triggerImportRepositoryRequest .repositorySourceCodes ) {
118
+ for (String repositorySourceCode : triggerImportRepositoryRequest .repositorySourceCodes ) {
118
119
repositorySources .add (
119
120
repositoryOptional .get ()
120
121
.getRepositorySources ()
@@ -145,16 +146,16 @@ public SearchRepositoriesResult searchRepositories(SearchRepositoriesRequest req
145
146
146
147
final ObjectContext context = serverRuntime .newContext ();
147
148
148
- if (!authorizationService .check (
149
+ if (!authorizationService .check (
149
150
context ,
150
151
tryObtainAuthenticatedUser (context ).orElse (null ),
151
152
null ,
152
153
Permission .REPOSITORY_LIST )) {
153
154
throw new AuthorizationFailureException ();
154
155
}
155
156
156
- if (null != request .includeInactive && request .includeInactive ) {
157
- if (!authorizationService .check (
157
+ if (null != request .includeInactive && request .includeInactive ) {
158
+ if (!authorizationService .check (
158
159
context ,
159
160
tryObtainAuthenticatedUser (context ).orElse (null ),
160
161
null ,
@@ -166,13 +167,13 @@ public SearchRepositoriesResult searchRepositories(SearchRepositoriesRequest req
166
167
RepositorySearchSpecification specification = new RepositorySearchSpecification ();
167
168
String exp = request .expression ;
168
169
169
- if (null != exp ) {
170
+ if (null != exp ) {
170
171
exp = Strings .emptyToNull (exp .trim ().toLowerCase ());
171
172
}
172
173
173
174
specification .setExpression (exp );
174
175
175
- if (null != request .expressionType ) {
176
+ if (null != request .expressionType ) {
176
177
specification .setExpressionType (
177
178
PkgSearchSpecification .ExpressionType .valueOf (request .expressionType .name ()));
178
179
}
@@ -186,7 +187,7 @@ public SearchRepositoriesResult searchRepositories(SearchRepositoriesRequest req
186
187
result .total = repositoryService .total (context , specification );
187
188
result .items = Collections .emptyList ();
188
189
189
- if (result .total > 0 ) {
190
+ if (result .total > 0 ) {
190
191
List <Repository > searchedRepositories = repositoryService .search (context , specification );
191
192
192
193
result .items = searchedRepositories .stream ().map (sr -> {
@@ -210,11 +211,11 @@ public GetRepositoryResult getRepository(GetRepositoryRequest getRepositoryReque
210
211
211
212
Optional <Repository > repositoryOptional = Repository .tryGetByCode (context , getRepositoryRequest .code );
212
213
213
- if (!repositoryOptional .isPresent ()) {
214
+ if (!repositoryOptional .isPresent ()) {
214
215
throw new ObjectNotFoundException (Repository .class .getSimpleName (), getRepositoryRequest .code );
215
216
}
216
217
217
- if (!authorizationService .check (
218
+ if (!authorizationService .check (
218
219
context ,
219
220
tryObtainAuthenticatedUser (context ).orElse (null ),
220
221
repositoryOptional .get (),
@@ -270,22 +271,22 @@ public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRep
270
271
repository ,
271
272
Permission .REPOSITORY_EDIT );
272
273
273
- for (UpdateRepositoryRequest .Filter filter : updateRepositoryRequest .filter ) {
274
- switch (filter ) {
274
+ for (UpdateRepositoryRequest .Filter filter : updateRepositoryRequest .filter ) {
275
+ switch (filter ) {
275
276
276
277
case ACTIVE :
277
- if (null ==updateRepositoryRequest .active ) {
278
+ if (null ==updateRepositoryRequest .active ) {
278
279
throw new IllegalStateException ("the active flag must be supplied" );
279
280
}
280
281
281
- if (repository .getActive () != updateRepositoryRequest .active ) {
282
+ if (repository .getActive () != updateRepositoryRequest .active ) {
282
283
repository .setActive (updateRepositoryRequest .active );
283
284
LOGGER .info ("did set the active flag on repository {} to {}" , updateRepositoryRequest .code , updateRepositoryRequest .active );
284
285
}
285
286
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 ()) {
289
290
repositorySource .setActive (false );
290
291
LOGGER .info ("did set the active flag on the repository source {} to false" , repositorySource .getCode ());
291
292
}
@@ -295,13 +296,13 @@ public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRep
295
296
break ;
296
297
297
298
case NAME :
298
- if (null == updateRepositoryRequest .name ) {
299
+ if (null == updateRepositoryRequest .name ) {
299
300
throw new IllegalStateException ("the name must be supplied to update the repository" );
300
301
}
301
302
302
303
String name = updateRepositoryRequest .name .trim ();
303
304
304
- if ( 0 == name .length ()) {
305
+ if ( 0 == name .length ()) {
305
306
throw new ValidationException (new ValidationFailure (Repository .NAME .getName (), "invalid" ));
306
307
}
307
308
@@ -329,7 +330,7 @@ public UpdateRepositoryResult updateRepository(UpdateRepositoryRequest updateRep
329
330
}
330
331
}
331
332
332
- if (context .hasChanges ()) {
333
+ if (context .hasChanges ()) {
333
334
context .commitChanges ();
334
335
}
335
336
else {
@@ -347,7 +348,7 @@ public CreateRepositoryResult createRepository(
347
348
348
349
final ObjectContext context = serverRuntime .newContext ();
349
350
350
- if (!authorizationService .check (
351
+ if (!authorizationService .check (
351
352
context ,
352
353
tryObtainAuthenticatedUser (context ).orElse (null ),
353
354
null ,
@@ -357,7 +358,7 @@ public CreateRepositoryResult createRepository(
357
358
358
359
// the code must be supplied.
359
360
360
- if (Strings .isNullOrEmpty (createRepositoryRequest .code )) {
361
+ if (Strings .isNullOrEmpty (createRepositoryRequest .code )) {
361
362
throw new ValidationException (new ValidationFailure (Repository .CODE .getName (), "required" ));
362
363
}
363
364
@@ -366,7 +367,7 @@ public CreateRepositoryResult createRepository(
366
367
{
367
368
Optional <Repository > repositoryOptional = Repository .tryGetByCode (context , createRepositoryRequest .code );
368
369
369
- if (repositoryOptional .isPresent ()) {
370
+ if (repositoryOptional .isPresent ()) {
370
371
throw new ValidationException (new ValidationFailure (Repository .CODE .getName (), "unique" ));
371
372
}
372
373
}
@@ -388,11 +389,10 @@ public GetRepositorySourceResult getRepositorySource(GetRepositorySourceRequest
388
389
Preconditions .checkArgument (!Strings .isNullOrEmpty (request .code ));
389
390
390
391
final ObjectContext context = serverRuntime .newContext ();
391
- User authenticatedUser = tryObtainAuthenticatedUser (context ).orElse (null );
392
392
393
393
Optional <RepositorySource > repositorySourceOptional = RepositorySource .tryGetByCode (context , request .code );
394
394
395
- if (!repositorySourceOptional .isPresent ()) {
395
+ if (!repositorySourceOptional .isPresent ()) {
396
396
throw new ObjectNotFoundException (RepositorySource .class .getSimpleName (), request .code );
397
397
}
398
398
@@ -408,6 +408,8 @@ public GetRepositorySourceResult getRepositorySource(GetRepositorySourceRequest
408
408
result .lastImportTimestamp = repositorySource .getLastImportTimestamp ().getTime ();
409
409
}
410
410
411
+ result .extraIdentifiers = repositorySource .getExtraIdentifiers ();
412
+
411
413
result .repositorySourceMirrors = repositorySource .getRepositorySourceMirrors ()
412
414
.stream ()
413
415
.filter (m -> m .getActive () || BooleanUtils .isTrue (request .includeInactiveRepositorySourceMirrors ))
@@ -424,7 +426,7 @@ public GetRepositorySourceResult getRepositorySource(GetRepositorySourceRequest
424
426
})
425
427
.collect (Collectors .toList ());
426
428
427
- if (authorizationService .check (
429
+ if (authorizationService .check (
428
430
context ,
429
431
tryObtainAuthenticatedUser (context ).orElse (null ),
430
432
repositorySource .getRepository (),
@@ -445,26 +447,26 @@ public UpdateRepositorySourceResult updateRepositorySource(UpdateRepositorySourc
445
447
446
448
Optional <RepositorySource > repositorySourceOptional = RepositorySource .tryGetByCode (context , request .code );
447
449
448
- if (!repositorySourceOptional .isPresent ()) {
450
+ if (!repositorySourceOptional .isPresent ()) {
449
451
throw new ObjectNotFoundException (RepositorySource .class .getSimpleName (), request .code );
450
452
}
451
453
452
454
RepositorySource repositorySource = repositorySourceOptional .get ();
453
455
454
- if (!authorizationService .check (
456
+ if (!authorizationService .check (
455
457
context ,
456
458
tryObtainAuthenticatedUser (context ).orElse (null ),
457
459
repositorySourceOptional .get ().getRepository (),
458
460
Permission .REPOSITORY_EDIT )) {
459
461
throw new AuthorizationFailureException ();
460
462
}
461
463
462
- for (UpdateRepositorySourceRequest .Filter filter : request .filter ) {
464
+ for (UpdateRepositorySourceRequest .Filter filter : request .filter ) {
463
465
464
- switch (filter ) {
466
+ switch (filter ) {
465
467
466
468
case ACTIVE :
467
- if (null == request .active ) {
469
+ if (null == request .active ) {
468
470
throw new IllegalArgumentException ("the active field must be provided if the request requires it to be updated" );
469
471
}
470
472
repositorySource .setActive (request .active );
@@ -477,6 +479,25 @@ public UpdateRepositorySourceResult updateRepositorySource(UpdateRepositorySourc
477
479
LOGGER .info ("did set the repository source forced internal base url" );
478
480
break ;
479
481
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
+
480
501
default :
481
502
throw new IllegalStateException ("unhandled filter; " + filter .name ());
482
503
0 commit comments