Skip to content

Commit

Permalink
Adjust predicate for get all shells.
Browse files Browse the repository at this point in the history
  • Loading branch information
tunacicek committed Aug 23, 2023
1 parent ff4c1bc commit 29f3c06
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public interface ShellRepository extends JpaRepository<Shell, UUID>, 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 " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,63 +48,42 @@ public class ShellSpecification<T> implements Specification<T> {

@Override
public Predicate toPredicate( Root<T> root, CriteriaQuery<?> cq, CriteriaBuilder criteriaBuilder ) {
return applyFilter( root,cq, criteriaBuilder );
return applyFilter( root, cq, criteriaBuilder );
}

private Predicate applyFilter( Root<T> root,CriteriaQuery<?> cq, CriteriaBuilder criteriaBuilder ) {
if(root.toString().contains( "Shell" )){
private Predicate applyFilter( Root<T> 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<T> root, CriteriaQuery<?> cq, CriteriaBuilder criteriaBuilder, Instant searchValue ){
// Query for noIdentifierSubQuery
Subquery<String> noIdentifierSubQuery = cq.subquery(String.class);
Root<ShellIdentifier> shellIdentifierRoot = noIdentifierSubQuery.from(ShellIdentifier.class);
noIdentifierSubQuery
.select(shellIdentifierRoot.get("shellId"))
.where(criteriaBuilder.equal(shellIdentifierRoot.get("shellId"), root));

// Query for identifierSubQuery
Subquery<UUID> identifierSubQuery = cq.subquery(UUID.class);
Subquery<UUID> identifierInnerSubQuery = cq.subquery(UUID.class);
Root<ShellIdentifier> shellIdentifierRoot1 = identifierSubQuery.from(ShellIdentifier.class);
Root<ShellIdentifierExternalSubjectReferenceKey> 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<T> root, CriteriaQuery<?> cq, CriteriaBuilder criteriaBuilder, Instant searchValue ) {
// Join Shell -> ShellIdentifier
Join<Shell,ShellIdentifier > shellIdentifierShellJoin = root.join( "identifiers" );
// join ShellIdentifier -> ShellIdentifierExternalSubjectReference -> ShellIdentifierExternalSubjectReferenceKey
Join<ShellIdentifierExternalSubjectReference,ShellIdentifierExternalSubjectReferenceKey> 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 )
);
}
}

0 comments on commit 29f3c06

Please sign in to comment.