1717import org .springframework .data .domain .Page ;
1818import org .springframework .data .domain .PageRequest ;
1919import org .springframework .data .domain .Sort ;
20+ import org .springframework .data .jpa .domain .Specification ;
21+ import org .springframework .data .jpa .domain .Specifications ;
22+ import org .springframework .data .jpa .repository .JpaSpecificationExecutor ;
2023import org .springframework .data .repository .PagingAndSortingRepository ;
2124import org .springframework .transaction .annotation .Transactional ;
2225
@@ -43,7 +46,33 @@ public AbstractService( final Class< T > clazzToSet ){
4346
4447 @ Override
4548 public List < T > search ( final ImmutableTriple < String , ClientOperation , ? >... constraints ){
46- throw new UnsupportedOperationException ();
49+ final Specification < T > firstSpec = resolveConstraint ( constraints [0 ] );
50+ Specifications < T > specifications = Specifications .where ( firstSpec );
51+ for ( int i = 1 ; i < constraints .length ; i ++ ){
52+ specifications = specifications .and ( resolveConstraint ( constraints [i ] ) );
53+ }
54+ if ( firstSpec == null ){
55+ return Lists .newArrayList ();
56+ }
57+
58+ return getSpecificationExecutor ().findAll ( specifications );
59+ }
60+
61+ @ Override
62+ public Page < T > searchPaged ( final int page , final int size , final String sortBy , final ImmutableTriple < String , ClientOperation , ? >... constraints ){
63+ Sort sortInfo = null ;
64+ if ( sortBy != null ){
65+ sortInfo = new Sort ( sortBy );
66+ }
67+
68+ final Specification < T > firstSpec = resolveConstraint ( constraints [0 ] );
69+ Preconditions .checkState ( firstSpec != null );
70+ Specifications < T > specifications = Specifications .where ( firstSpec );
71+ for ( int i = 1 ; i < constraints .length ; i ++ ){
72+ specifications = specifications .and ( resolveConstraint ( constraints [i ] ) );
73+ }
74+
75+ return getSpecificationExecutor ().findAll ( specifications , new PageRequest ( page , size , sortInfo ) );
4776 }
4877
4978 // find
@@ -112,5 +141,11 @@ public void delete( final long id ){
112141 // template method
113142
114143 protected abstract PagingAndSortingRepository < T , Long > getDao ();
115-
144+ protected abstract JpaSpecificationExecutor < T > getSpecificationExecutor ();
145+
146+ @ SuppressWarnings ( "static-method" )
147+ public Specification < T > resolveConstraint ( final ImmutableTriple < String , ClientOperation , ? > constraint ){
148+ throw new UnsupportedOperationException ();
149+ }
150+
116151}
0 commit comments