99import com .beyt .jdq .dto .enums .CriteriaOperator ;
1010import com .beyt .jdq .exception .DynamicQueryIllegalArgumentException ;
1111import com .beyt .jdq .query .rule .specification .*;
12- import com .beyt .jdq .repository .DynamicSpecificationRepositoryImpl ;
13- import com .beyt .jdq .util .ApplicationContextUtil ;
12+ import com .beyt .jdq .repository .JpaDynamicQueryRepositoryImpl ;
1413import com .beyt .jdq .util .field .FieldUtil ;
1514import lombok .extern .slf4j .Slf4j ;
1615import org .apache .commons .collections4 .IterableUtils ;
@@ -65,94 +64,94 @@ public class DynamicQueryManager {
6564 }
6665
6766 public static <Entity > List <Entity > findAll (JpaSpecificationExecutor <Entity > repositoryExecutor ,
68- List <Criteria > searchCriteriaList ) {
67+ List <Criteria > searchCriteriaList , RepositoryContext context ) {
6968 List <Entity > result ;
7069 List <Criteria > specificationRules = getMapSpecificRules (specificationRuleMap , searchCriteriaList );
7170 DynamicSpecification <Entity > specification = null ;
7271
7372 if (!specificationRules .isEmpty ()) {
74- specification = new DynamicSpecification <>(specificationRules );
73+ specification = new DynamicSpecification <>(specificationRules , context );
7574 }
7675
7776 return repositoryExecutor .findAll (specification );
7877 }
7978
8079 public static <Entity > Page <Entity > findAll (JpaSpecificationExecutor <Entity > repositoryExecutor ,
81- List <Criteria > searchCriteriaList , Pageable pageable ) {
80+ List <Criteria > searchCriteriaList , Pageable pageable , RepositoryContext context ) {
8281 List <Criteria > specificationRules = getMapSpecificRules (specificationRuleMap , searchCriteriaList );
8382
8483 DynamicSpecification <Entity > specification = null ;
8584 if (!specificationRules .isEmpty ()) {
86- specification = new DynamicSpecification <>(specificationRules );
85+ specification = new DynamicSpecification <>(specificationRules , context );
8786 }
8887
8988 return repositoryExecutor .findAll (specification , pageable );
9089 }
9190
92- public static <Entity > Specification <Entity > getSpecification (List <Criteria > searchCriteriaList ) {
91+ public static <Entity > Specification <Entity > getSpecification (List <Criteria > searchCriteriaList , RepositoryContext context ) {
9392 List <Criteria > specificationRules = getMapSpecificRules (specificationRuleMap , searchCriteriaList );
9493
9594 DynamicSpecification <Entity > specification = null ;
9695 if (!specificationRules .isEmpty ()) {
97- specification = new DynamicSpecification <>(specificationRules );
96+ specification = new DynamicSpecification <>(specificationRules , context );
9897 }
9998
10099 return specification ;
101100 }
102101
103102 public static <Entity > long count (JpaSpecificationExecutor <Entity > repositoryExecutor ,
104- List <Criteria > searchCriteriaList ) {
103+ List <Criteria > searchCriteriaList , RepositoryContext context ) {
105104 Long result = 0l ;
106105 List <Criteria > specificationRules = getMapSpecificRules (specificationRuleMap , searchCriteriaList );
107106 DynamicSpecification <Entity > specification = null ;
108107 if (specificationRules == null || !specificationRules .isEmpty ()) {
109- specification = new DynamicSpecification <>(specificationRules );
108+ specification = new DynamicSpecification <>(specificationRules , context );
110109 }
111110 result = repositoryExecutor .count (specification );
112111
113112 return result ;
114113 }
115114
116- public static <Entity > List <Entity > getEntityListBySelectableFilterAsList (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery ) {
117- return (List <Entity >) DynamicQueryManager .getEntityListBySelectableFilter (repositoryExecutor , dynamicQuery , false );
115+ public static <Entity > List <Entity > getEntityListBySelectableFilterAsList (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , RepositoryContext context ) {
116+ return (List <Entity >) DynamicQueryManager .getEntityListBySelectableFilter (repositoryExecutor , dynamicQuery , false , context );
118117 }
119118
120- public static <Entity > Page <Entity > getEntityListBySelectableFilterAsPage (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery ) {
121- return (Page <Entity >) DynamicQueryManager .getEntityListBySelectableFilter (repositoryExecutor , dynamicQuery , true );
119+ public static <Entity > Page <Entity > getEntityListBySelectableFilterAsPage (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , RepositoryContext context ) {
120+ return (Page <Entity >) DynamicQueryManager .getEntityListBySelectableFilter (repositoryExecutor , dynamicQuery , true , context );
122121 }
123122
124- protected static <Entity > Iterable <Entity > getEntityListBySelectableFilter (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , boolean isPage ) {
123+ protected static <Entity > Iterable <Entity > getEntityListBySelectableFilter (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , boolean isPage , RepositoryContext context ) {
125124 Class <Entity > entityClass = getEntityClass (repositoryExecutor );
126- return getEntityListBySelectableFilterWithReturnType (repositoryExecutor , dynamicQuery , entityClass , isPage );
125+ return getEntityListBySelectableFilterWithReturnType (repositoryExecutor , dynamicQuery , entityClass , isPage , context );
127126 }
128127
129- public static <Entity > List <Tuple > getEntityListBySelectableFilterWithTupleAsList (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery ) {
130- return (List <Tuple >) DynamicQueryManager .getEntityListBySelectableFilterWithTuple (repositoryExecutor , dynamicQuery , false );
128+ public static <Entity > List <Tuple > getEntityListBySelectableFilterWithTupleAsList (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , RepositoryContext context ) {
129+ return (List <Tuple >) DynamicQueryManager .getEntityListBySelectableFilterWithTuple (repositoryExecutor , dynamicQuery , false , context );
131130 }
132131
133- public static <Entity > Page <Tuple > getEntityListBySelectableFilterWithTupleAsPage (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery ) {
134- return (Page <Tuple >) DynamicQueryManager .getEntityListBySelectableFilterWithTuple (repositoryExecutor , dynamicQuery , true );
132+ public static <Entity > Page <Tuple > getEntityListBySelectableFilterWithTupleAsPage (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , RepositoryContext context ) {
133+ return (Page <Tuple >) DynamicQueryManager .getEntityListBySelectableFilterWithTuple (repositoryExecutor , dynamicQuery , true , context );
135134 }
136135
137- protected static <Entity > Iterable <Tuple > getEntityListBySelectableFilterWithTuple (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , boolean isPage ) {
138- return getEntityListWithReturnClass (repositoryExecutor , dynamicQuery , Tuple .class , isPage );
136+ protected static <Entity > Iterable <Tuple > getEntityListBySelectableFilterWithTuple (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , boolean isPage , RepositoryContext context ) {
137+ return getEntityListWithReturnClass (repositoryExecutor , dynamicQuery , Tuple .class , isPage , context );
139138 }
140139
141- public static <Entity , ResultType > List <ResultType > getEntityListBySelectableFilterWithReturnTypeAsList (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , Class <ResultType > resultTypeClass ) {
142- return (List <ResultType >) DynamicQueryManager .getEntityListBySelectableFilterWithReturnType (repositoryExecutor , dynamicQuery , resultTypeClass , false );
140+ public static <Entity , ResultType > List <ResultType > getEntityListBySelectableFilterWithReturnTypeAsList (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , Class <ResultType > resultTypeClass , RepositoryContext context ) {
141+ return (List <ResultType >) DynamicQueryManager .getEntityListBySelectableFilterWithReturnType (repositoryExecutor , dynamicQuery , resultTypeClass , false , context );
143142 }
144143
145- public static <Entity , ResultType > Page <ResultType > getEntityListBySelectableFilterWithReturnTypeAsPage (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , Class <ResultType > resultTypeClass ) {
146- return (Page <ResultType >) DynamicQueryManager .getEntityListBySelectableFilterWithReturnType (repositoryExecutor , dynamicQuery , resultTypeClass , true );
144+ public static <Entity , ResultType > Page <ResultType > getEntityListBySelectableFilterWithReturnTypeAsPage (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , Class <ResultType > resultTypeClass , RepositoryContext context ) {
145+ return (Page <ResultType >) DynamicQueryManager .getEntityListBySelectableFilterWithReturnType (repositoryExecutor , dynamicQuery , resultTypeClass , true , context );
147146 }
148147
149- protected static <Entity , ResultType > Iterable <ResultType > getEntityListBySelectableFilterWithReturnType (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , Class <ResultType > resultTypeClass , boolean isPage ) {
148+ protected static <Entity , ResultType > Iterable <ResultType > getEntityListBySelectableFilterWithReturnType (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , Class <ResultType > resultTypeClass , boolean isPage , RepositoryContext context ) {
150149 Class <Entity > entityClass = getEntityClass (repositoryExecutor );
151150 if (resultTypeClass .equals (entityClass ) && CollectionUtils .isEmpty (dynamicQuery .getSelect ())) {
152- return getEntityListWithReturnClass (repositoryExecutor , dynamicQuery , resultTypeClass , isPage );
151+ return getEntityListWithReturnClass (repositoryExecutor , dynamicQuery , resultTypeClass , isPage , context );
153152 } else {
154153 extractIfJdqModel (dynamicQuery , resultTypeClass );
155- Iterable <Tuple > entityListBySelectableFilter = getEntityListWithReturnClass (repositoryExecutor , dynamicQuery , Tuple .class , isPage );
154+ Iterable <Tuple > entityListBySelectableFilter = getEntityListWithReturnClass (repositoryExecutor , dynamicQuery , Tuple .class , isPage , context );
156155
157156 if (!CollectionUtils .isEmpty (dynamicQuery .getSelect ())) {
158157 return convertResultToResultTypeList (dynamicQuery .getSelect (), resultTypeClass , entityListBySelectableFilter , isPage );
@@ -217,13 +216,13 @@ private static String prefixCreator(List<String> prefixList) {
217216 return collect ;
218217 }
219218
220- protected static <Entity , ResultType > Iterable <ResultType > getEntityListWithReturnClass (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , Class <ResultType > resultTypeClass , boolean isPage ) {
219+ protected static <Entity , ResultType > Iterable <ResultType > getEntityListWithReturnClass (JpaSpecificationExecutor <Entity > repositoryExecutor , DynamicQuery dynamicQuery , Class <ResultType > resultTypeClass , boolean isPage , RepositoryContext context ) {
221220 Class <Entity > entityClass = getEntityClass (repositoryExecutor );
222- EntityManager entityManager = ApplicationContextUtil .getEntityManager ();
221+ EntityManager entityManager = context .getEntityManager ();
223222 CriteriaBuilder builder = entityManager .getCriteriaBuilder ();
224223 CriteriaQuery <ResultType > query = builder .createQuery (resultTypeClass );
225224 Root <Entity > root = query .from (entityClass );
226- DynamicSpecification <Entity > specification = new DynamicSpecification <>(dynamicQuery .getWhere ());
225+ DynamicSpecification <Entity > specification = new DynamicSpecification <>(dynamicQuery .getWhere (), context );
227226 Pageable pageable = Pageable .unpaged ();
228227
229228 query .distinct (dynamicQuery .isDistinct ());
@@ -275,19 +274,19 @@ protected static <Entity, ResultType> Iterable<ResultType> getEntityListWithRetu
275274
276275 if (isPage ) {
277276 return PageableExecutionUtils .getPage (typedQuery .getResultList (), pageable ,
278- () -> executeCountQuery (getCountQuery (specification , entityClass )));
277+ () -> executeCountQuery (getCountQuery (specification , entityClass , context )));
279278 } else {
280279 return typedQuery .getResultList ();
281280 }
282281 }
283282
284283
285- protected static <S > TypedQuery <Long > getCountQuery (@ Nullable Specification <S > spec , Class <S > domainClass ) {
286- EntityManager entityManager = ApplicationContextUtil .getEntityManager ();
284+ protected static <S > TypedQuery <Long > getCountQuery (@ Nullable Specification <S > spec , Class <S > domainClass , RepositoryContext context ) {
285+ EntityManager entityManager = context .getEntityManager ();
287286 CriteriaBuilder builder = entityManager .getCriteriaBuilder ();
288287 CriteriaQuery <Long > query = builder .createQuery (Long .class );
289288
290- Root <S > root = applySpecificationToCriteria (spec , domainClass , query );
289+ Root <S > root = applySpecificationToCriteria (spec , domainClass , query , context );
291290
292291 if (query .isDistinct ()) {
293292 query .select (builder .countDistinct (root ));
@@ -302,11 +301,11 @@ protected static <S> TypedQuery<Long> getCountQuery(@Nullable Specification<S> s
302301 }
303302
304303 protected static <S , U > Root <U > applySpecificationToCriteria (@ Nullable Specification <U > spec , Class <U > domainClass ,
305- CriteriaQuery <S > query ) {
304+ CriteriaQuery <S > query , RepositoryContext context ) {
306305
307306 Assert .notNull (domainClass , "Domain class must not be null!" );
308307 Assert .notNull (query , "CriteriaQuery must not be null!" );
309- EntityManager entityManager = ApplicationContextUtil .getEntityManager ();
308+ EntityManager entityManager = context .getEntityManager ();
310309 Root <U > root = query .from (domainClass );
311310
312311 if (spec == null ) {
@@ -446,8 +445,8 @@ protected static <Entity> Class<Entity> getEntityClass(JpaSpecificationExecutor<
446445 Class <Entity > entityClass = (Class <Entity >) GenericTypeResolver .resolveTypeArgument (repositoryExecutor .getClass (), JpaSpecificationExecutor .class );
447446 if (Objects .nonNull (entityClass )) {
448447 return entityClass ;
449- } else if (repositoryExecutor instanceof DynamicSpecificationRepositoryImpl ) {
450- return ((DynamicSpecificationRepositoryImpl ) repositoryExecutor ).getDomainClass ();
448+ } else if (repositoryExecutor instanceof JpaDynamicQueryRepositoryImpl ) {
449+ return ((JpaDynamicQueryRepositoryImpl ) repositoryExecutor ).getDomainClass ();
451450 } else {
452451 throw new IllegalStateException ("Entity Class Type Detection Failed!" );
453452 }
0 commit comments