1   /**
2    * Copyright (c) 2000-2007 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(Query query, Dialect dialect, int begin, int end) {
56          if ((begin == ALL_POS) && (end == ALL_POS)) {
57              return query.list();
58          }
59          else {
60              if (dialect.supportsLimit()) {
61                  query.setMaxResults(end - begin);
62                  query.setFirstResult(begin);
63  
64                  return query.list();
65              }
66              else {
67                  List list = new ArrayList();
68  
69                  ScrollableResults sr = query.scroll();
70  
71                  if (sr.first() && sr.scroll(begin)) {
72                      for (int i = begin; i < end; i++) {
73                          Object obj  = sr.get(0);
74  
75                          list.add(obj);
76  
77                          if (!sr.next()) {
78                              break;
79                          }
80                      }
81                  }
82  
83                  return list;
84              }
85          }
86      }
87  
88      public static List randomList(
89          Query query, Dialect dialect, int total, int num) {
90  
91          if ((total == 0) || (num == 0)) {
92              return new ArrayList();
93          }
94  
95          if (num >= total) {
96              return list(query, dialect, ALL_POS, ALL_POS);
97          }
98  
99          int[] scrollIds = Randomizer.getInstance().nextInt(total, num);
100 
101         List list = new ArrayList();
102 
103         ScrollableResults sr = query.scroll();
104 
105         for (int i = 0; i < scrollIds.length; i++) {
106             if (sr.scroll(scrollIds[i])) {
107                 Object obj = sr.get(0);
108 
109                 list.add(obj);
110 
111                 sr.first();
112             }
113         }
114 
115         return list;
116     }
117 
118     public static Comparable[] getPrevAndNext(
119         Query query, int count, OrderByComparator obc, Comparable comparable) {
120 
121         int pos = count;
122         int boundary = 0;
123 
124         Comparable[] array = new Comparable[3];
125 
126         ScrollableResults sr = query.scroll();
127 
128         if (sr.first()) {
129             while (true) {
130                 Object obj = sr.get(0);
131 
132                 if (obj == null) {
133                     if (_log.isWarnEnabled()) {
134                         _log.warn("Object is null");
135                     }
136 
137                     break;
138                 }
139 
140                 Comparable curComparable = (Comparable)obj;
141 
142                 int value = obc.compare(comparable, curComparable);
143 
144                 if (_log.isDebugEnabled()) {
145                     _log.debug("Comparison result is " + value);
146                 }
147 
148                 if (value == 0) {
149                     if (!comparable.equals(curComparable)) {
150                         break;
151                     }
152 
153                     array[1] = curComparable;
154 
155                     if (sr.previous()) {
156                         array[0] = (Comparable)sr.get(0);
157                     }
158 
159                     sr.next();
160 
161                     if (sr.next()) {
162                         array[2] = (Comparable)sr.get(0);
163                     }
164 
165                     break;
166                 }
167 
168                 if (pos == 1) {
169                     break;
170                 }
171 
172                 pos = (int)Math.ceil(pos / 2.0);
173 
174                 int scrollPos = pos;
175 
176                 if (value < 0) {
177                     scrollPos = scrollPos * -1;
178                 }
179 
180                 boundary += scrollPos;
181 
182                 if (boundary < 0) {
183                     scrollPos = scrollPos + (boundary * -1) + 1;
184 
185                     boundary = 0;
186                 }
187 
188                 if (boundary > count) {
189                     scrollPos = scrollPos - (boundary - count);
190 
191                     boundary = scrollPos;
192                 }
193 
194                 if (_log.isDebugEnabled()) {
195                     _log.debug("Scroll " + scrollPos);
196                 }
197 
198                 if (!sr.scroll(scrollPos)) {
199                     if (value < 0) {
200                         if (!sr.next()) {
201                             break;
202                         }
203                     }
204                     else {
205                         if (!sr.previous()) {
206                             break;
207                         }
208                     }
209                 }
210             }
211         }
212 
213         return array;
214     }
215 
216     private static Log _log = LogFactory.getLog(QueryUtil.class);
217 
218 }