1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.dao.hibernate;
24  
25  import com.liferay.portal.kernel.util.OrderByComparator;
26  import com.liferay.portal.kernel.util.Randomizer;
27  
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import org.hibernate.Query;
36  import org.hibernate.ScrollableResults;
37  import org.hibernate.dialect.Dialect;
38  
39  /**
40   * <a href="QueryUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class QueryUtil {
46  
47      public static final int ALL_POS = -1;
48  
49      public static Iterator<?> iterate(
50          Query query, Dialect dialect, int begin, int end) {
51  
52          return list(query, dialect, begin, end).iterator();
53      }
54  
55      public static List<?> list(
56          Query query, Dialect dialect, int begin, int end) {
57  
58          if ((begin == ALL_POS) && (end == ALL_POS)) {
59              return query.list();
60          }
61          else {
62              if (dialect.supportsLimit()) {
63                  query.setMaxResults(end - begin);
64                  query.setFirstResult(begin);
65  
66                  return query.list();
67              }
68              else {
69                  List<Object> list = new ArrayList<Object>();
70  
71                  ScrollableResults sr = query.scroll();
72  
73                  if (sr.first() && sr.scroll(begin)) {
74                      for (int i = begin; i < end; i++) {
75                          Object obj = sr.get(0);
76  
77                          list.add(obj);
78  
79                          if (!sr.next()) {
80                              break;
81                          }
82                      }
83                  }
84  
85                  return list;
86              }
87          }
88      }
89  
90      public static List<?> randomList(
91          Query query, Dialect dialect, int total, int num) {
92  
93          if ((total == 0) || (num == 0)) {
94              return new ArrayList<Object>();
95          }
96  
97          if (num >= total) {
98              return list(query, dialect, ALL_POS, ALL_POS);
99          }
100 
101         int[] scrollIds = Randomizer.getInstance().nextInt(total, num);
102 
103         List<Object> list = new ArrayList<Object>();
104 
105         ScrollableResults sr = query.scroll();
106 
107         for (int i = 0; i < scrollIds.length; i++) {
108             if (sr.scroll(scrollIds[i])) {
109                 Object obj = sr.get(0);
110 
111                 list.add(obj);
112 
113                 sr.first();
114             }
115         }
116 
117         return list;
118     }
119 
120     public static Comparable<?>[] getPrevAndNext(
121         Query query, int count, OrderByComparator obc,
122         Comparable<?> comparable) {
123 
124         int pos = count;
125         int boundary = 0;
126 
127         Comparable<?>[] array = new Comparable[3];
128 
129         ScrollableResults sr = query.scroll();
130 
131         if (sr.first()) {
132             while (true) {
133                 Object obj = sr.get(0);
134 
135                 if (obj == null) {
136                     if (_log.isWarnEnabled()) {
137                         _log.warn("Object is null");
138                     }
139 
140                     break;
141                 }
142 
143                 Comparable<?> curComparable = (Comparable<?>)obj;
144 
145                 int value = obc.compare(comparable, curComparable);
146 
147                 if (_log.isDebugEnabled()) {
148                     _log.debug("Comparison result is " + value);
149                 }
150 
151                 if (value == 0) {
152                     if (!comparable.equals(curComparable)) {
153                         break;
154                     }
155 
156                     array[1] = curComparable;
157 
158                     if (sr.previous()) {
159                         array[0] = (Comparable<?>)sr.get(0);
160                     }
161 
162                     sr.next();
163 
164                     if (sr.next()) {
165                         array[2] = (Comparable<?>)sr.get(0);
166                     }
167 
168                     break;
169                 }
170 
171                 if (pos == 1) {
172                     break;
173                 }
174 
175                 pos = (int)Math.ceil(pos / 2.0);
176 
177                 int scrollPos = pos;
178 
179                 if (value < 0) {
180                     scrollPos = scrollPos * -1;
181                 }
182 
183                 boundary += scrollPos;
184 
185                 if (boundary < 0) {
186                     scrollPos = scrollPos + (boundary * -1) + 1;
187 
188                     boundary = 0;
189                 }
190 
191                 if (boundary > count) {
192                     scrollPos = scrollPos - (boundary - count);
193 
194                     boundary = scrollPos;
195                 }
196 
197                 if (_log.isDebugEnabled()) {
198                     _log.debug("Scroll " + scrollPos);
199                 }
200 
201                 if (!sr.scroll(scrollPos)) {
202                     if (value < 0) {
203                         if (!sr.next()) {
204                             break;
205                         }
206                     }
207                     else {
208                         if (!sr.previous()) {
209                             break;
210                         }
211                     }
212                 }
213             }
214         }
215 
216         return array;
217     }
218 
219     private static Log _log = LogFactory.getLog(QueryUtil.class);
220 
221 }