diff --git a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java index 1db6d08f..acf88b23 100644 --- a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java @@ -39,7 +39,6 @@ public interface ShellRepository extends JpaRepository, JpaSpecific @Query( value = "select * from shell s " + "where s.id_external = :idExternal and (" + ":tenantId = :owningTenantId " + - "or not exists (select si.id from shell_identifier si where s.id = si.fk_shell_id) " + "or s.id in (" + "select si.fk_shell_id from shell_identifier si where exists (" + "select sider.ref_key_value from SHELL_IDENTIFIER_EXTERNAL_SUBJECT_REFERENCE_KEY sider " + diff --git a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/utils/ShellSpecification.java b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/utils/ShellSpecification.java index fb328e51..f9c80241 100644 --- a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/utils/ShellSpecification.java +++ b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/utils/ShellSpecification.java @@ -23,16 +23,17 @@ import java.util.List; import java.util.UUID; +import org.eclipse.tractusx.semantics.registry.model.Shell; import org.eclipse.tractusx.semantics.registry.model.ShellIdentifier; import org.eclipse.tractusx.semantics.registry.model.ShellIdentifierExternalSubjectReference; import org.eclipse.tractusx.semantics.registry.model.ShellIdentifierExternalSubjectReferenceKey; import org.springframework.data.jpa.domain.Specification; + import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; -import jakarta.persistence.criteria.ParameterExpression; +import jakarta.persistence.criteria.Join; import jakarta.persistence.criteria.Predicate; import jakarta.persistence.criteria.Root; -import jakarta.persistence.criteria.Subquery; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor @@ -47,63 +48,42 @@ public class ShellSpecification implements Specification { @Override public Predicate toPredicate( Root root, CriteriaQuery cq, CriteriaBuilder criteriaBuilder ) { - return applyFilter( root,cq, criteriaBuilder ); + return applyFilter( root, cq, criteriaBuilder ); } - private Predicate applyFilter( Root root,CriteriaQuery cq, CriteriaBuilder criteriaBuilder ) { - if(root.toString().contains( "Shell" )){ + private Predicate applyFilter( Root root, CriteriaQuery cq, CriteriaBuilder criteriaBuilder ) { + if ( root.toString().contains( "Shell" ) ) { Instant searchValue = shellCursor.getShellSearchCursor(); cq.orderBy( criteriaBuilder.asc( criteriaBuilder.coalesce( root.get( sortFieldName ), Instant.now() ) ) ); - if(owningTenantId.equals( tenantId )){ + if ( owningTenantId.equals( tenantId ) ) { return criteriaBuilder.greaterThan( root.get( sortFieldName ), searchValue ); } - return getAllShellsPredicate(root, cq, criteriaBuilder, searchValue); - }else{ + return getAllShellsPredicate( root, cq, criteriaBuilder, searchValue ); + } else { UUID searchValue = shellCursor.getSubmodelSearchCursor(); cq.orderBy( criteriaBuilder.asc( criteriaBuilder.coalesce( root.get( sortFieldName ), - UUID.fromString( "00000000-0000-0000-0000-000000000000" )) ) ); + UUID.fromString( "00000000-0000-0000-0000-000000000000" ) ) ) ); return criteriaBuilder.greaterThan( root.get( sortFieldName ), searchValue ); } } - private Predicate getAllShellsPredicate(Root root, CriteriaQuery cq, CriteriaBuilder criteriaBuilder, Instant searchValue ){ - // Query for noIdentifierSubQuery - Subquery noIdentifierSubQuery = cq.subquery(String.class); - Root shellIdentifierRoot = noIdentifierSubQuery.from(ShellIdentifier.class); - noIdentifierSubQuery - .select(shellIdentifierRoot.get("shellId")) - .where(criteriaBuilder.equal(shellIdentifierRoot.get("shellId"), root)); - - // Query for identifierSubQuery - Subquery identifierSubQuery = cq.subquery(UUID.class); - Subquery identifierInnerSubQuery = cq.subquery(UUID.class); - Root shellIdentifierRoot1 = identifierSubQuery.from(ShellIdentifier.class); - Root externalSubjectReferenceKeyRoot = identifierInnerSubQuery.from(ShellIdentifierExternalSubjectReferenceKey.class); - identifierInnerSubQuery - .select(externalSubjectReferenceKeyRoot.get("shellIdentifierExternalSubjectReference")) - .where( criteriaBuilder.and( - criteriaBuilder.or( - criteriaBuilder.equal(externalSubjectReferenceKeyRoot.get("value"),tenantId), - criteriaBuilder.and( - criteriaBuilder.equal(externalSubjectReferenceKeyRoot.get("value"),publicWildcardPrefix), - criteriaBuilder.in(shellIdentifierRoot1.get("key")).value(publicWildcardAllowedTypes) - ) - ), - criteriaBuilder.equal(externalSubjectReferenceKeyRoot.get("shellIdentifierExternalSubjectReference").get( "shellIdentifier" ),shellIdentifierRoot1) - ) - ); - - identifierSubQuery - .select(shellIdentifierRoot1.get("shellId").get("id")) - .where(criteriaBuilder.exists(identifierInnerSubQuery)); + private Predicate getAllShellsPredicate( Root root, CriteriaQuery cq, CriteriaBuilder criteriaBuilder, Instant searchValue ) { + // Join Shell -> ShellIdentifier + Join shellIdentifierShellJoin = root.join( "identifiers" ); + // join ShellIdentifier -> ShellIdentifierExternalSubjectReference -> ShellIdentifierExternalSubjectReferenceKey + Join referenceKeyJoin = shellIdentifierShellJoin.join( "externalSubjectId" ).join( "keys" ); return criteriaBuilder.and( criteriaBuilder.or( - criteriaBuilder.not(criteriaBuilder.exists(noIdentifierSubQuery)), - criteriaBuilder.in(root.get("id")).value(identifierSubQuery) + criteriaBuilder.equal( referenceKeyJoin.get( "value" ), tenantId ), + criteriaBuilder.and( + criteriaBuilder.equal( referenceKeyJoin.get( "value" ), publicWildcardPrefix ), + criteriaBuilder.in( shellIdentifierShellJoin.get( "key" ) ).value( publicWildcardAllowedTypes ) + ) ), - criteriaBuilder.greaterThan(root.get(sortFieldName), searchValue)); + criteriaBuilder.greaterThan( root.get( sortFieldName ), searchValue ) + ); } }