From 69e822b20f5bfea09f74e099fd86b6e73d78ff24 Mon Sep 17 00:00:00 2001 From: RHJ5FE Date: Mon, 9 Oct 2023 16:55:28 +0200 Subject: [PATCH] refactored the code --- .../registry/service/ShellService.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java index 1715b4f5..932fd817 100644 --- a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java +++ b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java @@ -123,8 +123,13 @@ private void checkForDuplicateIdShortWithInSubModels( Shell shell ) { .map( String::toLowerCase ) .toList(); - Optional.of( idShortList ).filter( idShorts -> idShortList.stream().distinct().count() == idShorts.size() ) // checking for duplicate idShort - .orElseThrow( () -> new DuplicateKeyException( DUPLICATE_SUBMODEL_EXCEPTION ) ); + boolean isDuplicateIdShortPresent = Optional.of( idShortList ).filter( idShorts -> idShortList.stream().distinct().count() != idShorts.size() ) + .isPresent(); + + if ( isDuplicateIdShortPresent ) { + throw new DuplicateKeyException( DUPLICATE_SUBMODEL_EXCEPTION ); + } + } public void mapShellCollection(Shell shell){ @@ -359,11 +364,9 @@ public Submodel save( String externalShellId, Submodel submodel, String external .anyMatch( idShort -> idShort.toLowerCase().equals( submodel.getIdShort().toLowerCase() ) ); // check whether the input sub-model.idShort exists in DB - Optional.of( isIdShortPresent ).filter( BooleanUtils::isFalse ) - .orElseThrow( () -> new DuplicateKeyException( - DUPLICATE_SUBMODEL_EXCEPTION ) );// Throw exception if sub-model.idShort exists in DB - - + if(isIdShortPresent){// Throw exception if sub-model.idShort exists in DB + throw new DuplicateKeyException(DUPLICATE_SUBMODEL_EXCEPTION); + } return saveSubmodel( submodel ); }