001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.dao.hibernate;
022:
023: import com.liferay.portal.kernel.util.OrderByComparator;
024: import com.liferay.portal.kernel.util.Randomizer;
025:
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import org.hibernate.Query;
034: import org.hibernate.ScrollableResults;
035: import org.hibernate.dialect.Dialect;
036:
037: /**
038: * <a href="QueryUtil.java.html"><b><i>View Source</i></b></a>
039: *
040: * @author Brian Wing Shun Chan
041: *
042: */
043: public class QueryUtil {
044:
045: public static final int ALL_POS = -1;
046:
047: public static Iterator iterate(Query query, Dialect dialect,
048: int begin, int end) {
049:
050: return list(query, dialect, begin, end).iterator();
051: }
052:
053: public static List list(Query query, Dialect dialect, int begin,
054: int end) {
055: if ((begin == ALL_POS) && (end == ALL_POS)) {
056: return query.list();
057: } else {
058: if (dialect.supportsLimit()) {
059: query.setMaxResults(end - begin);
060: query.setFirstResult(begin);
061:
062: return query.list();
063: } else {
064: List list = new ArrayList();
065:
066: ScrollableResults sr = query.scroll();
067:
068: if (sr.first() && sr.scroll(begin)) {
069: for (int i = begin; i < end; i++) {
070: Object obj = sr.get(0);
071:
072: list.add(obj);
073:
074: if (!sr.next()) {
075: break;
076: }
077: }
078: }
079:
080: return list;
081: }
082: }
083: }
084:
085: public static List randomList(Query query, Dialect dialect,
086: int total, int num) {
087:
088: if ((total == 0) || (num == 0)) {
089: return new ArrayList();
090: }
091:
092: if (num >= total) {
093: return list(query, dialect, ALL_POS, ALL_POS);
094: }
095:
096: int[] scrollIds = Randomizer.getInstance().nextInt(total, num);
097:
098: List list = new ArrayList();
099:
100: ScrollableResults sr = query.scroll();
101:
102: for (int i = 0; i < scrollIds.length; i++) {
103: if (sr.scroll(scrollIds[i])) {
104: Object obj = sr.get(0);
105:
106: list.add(obj);
107:
108: sr.first();
109: }
110: }
111:
112: return list;
113: }
114:
115: public static Comparable[] getPrevAndNext(Query query, int count,
116: OrderByComparator obc, Comparable comparable) {
117:
118: int pos = count;
119: int boundary = 0;
120:
121: Comparable[] array = new Comparable[3];
122:
123: ScrollableResults sr = query.scroll();
124:
125: if (sr.first()) {
126: while (true) {
127: Object obj = sr.get(0);
128:
129: if (obj == null) {
130: if (_log.isWarnEnabled()) {
131: _log.warn("Object is null");
132: }
133:
134: break;
135: }
136:
137: Comparable curComparable = (Comparable) obj;
138:
139: int value = obc.compare(comparable, curComparable);
140:
141: if (_log.isDebugEnabled()) {
142: _log.debug("Comparison result is " + value);
143: }
144:
145: if (value == 0) {
146: if (!comparable.equals(curComparable)) {
147: break;
148: }
149:
150: array[1] = curComparable;
151:
152: if (sr.previous()) {
153: array[0] = (Comparable) sr.get(0);
154: }
155:
156: sr.next();
157:
158: if (sr.next()) {
159: array[2] = (Comparable) sr.get(0);
160: }
161:
162: break;
163: }
164:
165: if (pos == 1) {
166: break;
167: }
168:
169: pos = (int) Math.ceil(pos / 2.0);
170:
171: int scrollPos = pos;
172:
173: if (value < 0) {
174: scrollPos = scrollPos * -1;
175: }
176:
177: boundary += scrollPos;
178:
179: if (boundary < 0) {
180: scrollPos = scrollPos + (boundary * -1) + 1;
181:
182: boundary = 0;
183: }
184:
185: if (boundary > count) {
186: scrollPos = scrollPos - (boundary - count);
187:
188: boundary = scrollPos;
189: }
190:
191: if (_log.isDebugEnabled()) {
192: _log.debug("Scroll " + scrollPos);
193: }
194:
195: if (!sr.scroll(scrollPos)) {
196: if (value < 0) {
197: if (!sr.next()) {
198: break;
199: }
200: } else {
201: if (!sr.previous()) {
202: break;
203: }
204: }
205: }
206: }
207: }
208:
209: return array;
210: }
211:
212: private static Log _log = LogFactory.getLog(QueryUtil.class);
213:
214: }
|