@@ -344,23 +344,13 @@ public T lockOneRandomRow(final SearchCriteria<T> sc, final boolean exclusive) {
344344
345345 @ DB ()
346346 protected List <T > search (SearchCriteria <T > sc , final Filter filter , final Boolean lock , final boolean cache ) {
347- if (_removed != null ) {
348- if (sc == null ) {
349- sc = createSearchCriteria ();
350- }
351- sc .addAnd (_removed .second ().field .getName (), SearchCriteria .Op .NULL );
352- }
347+ sc = checkAndSetRemovedNull (sc );
353348 return searchIncludingRemoved (sc , filter , lock , cache );
354349 }
355350
356351 @ DB ()
357352 protected List <T > search (SearchCriteria <T > sc , final Filter filter , final Boolean lock , final boolean cache , final boolean enableQueryCache ) {
358- if (_removed != null ) {
359- if (sc == null ) {
360- sc = createSearchCriteria ();
361- }
362- sc .addAnd (_removed .second ().field .getName (), SearchCriteria .Op .NULL );
363- }
353+ sc = checkAndSetRemovedNull (sc );
364354 return searchIncludingRemoved (sc , filter , lock , cache , enableQueryCache );
365355 }
366356
@@ -519,7 +509,6 @@ public <M> List<M> customSearch(SearchCriteria<M> sc, final Filter filter) {
519509 if (_removed != null ) {
520510 sc .addAnd (_removed .second ().field .getName (), SearchCriteria .Op .NULL );
521511 }
522-
523512 return customSearchIncludingRemoved (sc , filter );
524513 }
525514
@@ -911,26 +900,20 @@ protected T findOneIncludingRemovedBy(final SearchCriteria<T> sc) {
911900
912901 @ Override
913902 @ DB ()
914- public T findOneBy (final SearchCriteria <T > sc ) {
915- if (_removed != null ) {
916- sc .addAnd (_removed .second ().field .getName (), SearchCriteria .Op .NULL );
917- }
903+ public T findOneBy (SearchCriteria <T > sc ) {
904+ sc = checkAndSetRemovedNull (sc );
918905 return findOneIncludingRemovedBy (sc );
919906 }
920907
921908 @ DB ()
922- protected List <T > listBy (final SearchCriteria <T > sc , final Filter filter ) {
923- if (_removed != null ) {
924- sc .addAnd (_removed .second ().field .getName (), SearchCriteria .Op .NULL );
925- }
909+ protected List <T > listBy (SearchCriteria <T > sc , final Filter filter ) {
910+ sc = checkAndSetRemovedNull (sc );
926911 return listIncludingRemovedBy (sc , filter );
927912 }
928913
929914 @ DB ()
930- protected List <T > listBy (final SearchCriteria <T > sc , final Filter filter , final boolean enableQueryCache ) {
931- if (_removed != null ) {
932- sc .addAnd (_removed .second ().field .getName (), SearchCriteria .Op .NULL );
933- }
915+ protected List <T > listBy (SearchCriteria <T > sc , final Filter filter , final boolean enableQueryCache ) {
916+ sc = checkAndSetRemovedNull (sc );
934917 return listIncludingRemovedBy (sc , filter , enableQueryCache );
935918 }
936919
@@ -1329,7 +1312,7 @@ public List<T> search(final SearchCriteria<T> sc, final Filter filter) {
13291312 @ DB ()
13301313 public Pair <List <T >, Integer > searchAndCount (final SearchCriteria <T > sc , final Filter filter ) {
13311314 List <T > objects = search (sc , filter , null , false );
1332- Integer count = getCount (sc );
1315+ Integer count = getCount (sc , false );
13331316 // Count cannot be less than the result set but can be higher due to pagination, see CLOUDSTACK-10320
13341317 if (count < objects .size ()) {
13351318 count = objects .size ();
@@ -1341,7 +1324,7 @@ public Pair<List<T>, Integer> searchAndCount(final SearchCriteria<T> sc, final F
13411324 @ DB ()
13421325 public Pair <List <T >, Integer > searchAndDistinctCount (final SearchCriteria <T > sc , final Filter filter ) {
13431326 List <T > objects = search (sc , filter , null , false );
1344- Integer count = getDistinctCount (sc );
1327+ Integer count = getDistinctCount (sc , false );
13451328 // Count cannot be 0 if there is at least a result in the list, see CLOUDSTACK-10320
13461329 if (count == 0 && !objects .isEmpty ()) {
13471330 // Cannot assume if it's more than one since the count is distinct vs search
@@ -1354,7 +1337,7 @@ public Pair<List<T>, Integer> searchAndDistinctCount(final SearchCriteria<T> sc,
13541337 @ DB ()
13551338 public Pair <List <T >, Integer > searchAndDistinctCount (final SearchCriteria <T > sc , final Filter filter , final String [] distinctColumns ) {
13561339 List <T > objects = search (sc , filter , null , false );
1357- Integer count = getDistinctCount (sc , distinctColumns );
1340+ Integer count = getDistinctCount (sc , distinctColumns , false );
13581341 // Count cannot be 0 if there is at least a result in the list, see CLOUDSTACK-10320
13591342 if (count == 0 && !objects .isEmpty ()) {
13601343 // Cannot assume if it's more than one since the count is distinct vs search
@@ -1935,6 +1918,23 @@ public SearchCriteria<T> createSearchCriteria() {
19351918 return builder .create ();
19361919 }
19371920
1921+ private SearchCriteria <T > checkAndSetRemovedNull (SearchCriteria <T > sc ) {
1922+ if (_removed != null ) {
1923+ if (sc == null ) {
1924+ sc = createSearchCriteria ();
1925+ }
1926+ sc .addAnd (_removed .second ().field .getName (), SearchCriteria .Op .NULL );
1927+ }
1928+ return sc ;
1929+ }
1930+
1931+ public Integer getDistinctCount (SearchCriteria <T > sc , boolean removed ) {
1932+ if (!removed ) {
1933+ sc = checkAndSetRemovedNull (sc );
1934+ }
1935+ return getDistinctCount (sc );
1936+ }
1937+
19381938 public Integer getDistinctCount (SearchCriteria <T > sc ) {
19391939 String clause = sc != null ? sc .getWhereClause () : null ;
19401940 if (clause != null && clause .length () == 0 ) {
@@ -1993,6 +1993,13 @@ public Integer getDistinctCount(SearchCriteria<T> sc) {
19931993 }
19941994 }
19951995
1996+ public Integer getDistinctCount (SearchCriteria <T > sc , String [] distinctColumns , boolean removed ) {
1997+ if (!removed ) {
1998+ sc = checkAndSetRemovedNull (sc );
1999+ }
2000+ return getDistinctCount (sc , distinctColumns );
2001+ }
2002+
19962003 public Integer getDistinctCount (SearchCriteria <T > sc , String [] distinctColumns ) {
19972004 String clause = sc != null ? sc .getWhereClause () : null ;
19982005 if (Strings .isNullOrEmpty (clause )) {
@@ -2041,9 +2048,13 @@ public Integer getDistinctCount(SearchCriteria<T> sc, String[] distinctColumns)
20412048
20422049 public Integer countAll () {
20432050 SearchCriteria <T > sc = null ;
2044- if (_removed != null ) {
2045- sc = createSearchCriteria ();
2046- sc .addAnd (_removed .second ().field .getName (), SearchCriteria .Op .NULL );
2051+ sc = checkAndSetRemovedNull (sc );
2052+ return getCount (sc );
2053+ }
2054+
2055+ public Integer getCount (SearchCriteria <T > sc , boolean removed ) {
2056+ if (!removed ) {
2057+ sc = checkAndSetRemovedNull (sc );
20472058 }
20482059 return getCount (sc );
20492060 }
0 commit comments