11/*
2- * Copyright 2018, Andrew Lindesay
2+ * Copyright 2018-2020 , Andrew Lindesay
33 * Distributed under the terms of the MIT License.
44 */
55
1313import org .apache .cayenne .PersistentObject ;
1414import org .apache .cayenne .configuration .server .ServerRuntime ;
1515import org .apache .commons .collections4 .CollectionUtils ;
16+ import org .apache .commons .collections4 .SetUtils ;
1617import org .apache .commons .lang3 .BooleanUtils ;
1718import org .apache .commons .lang3 .StringUtils ;
1819import 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