2 * Copyright (C) 2005 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License.
22 * You must obey the GNU General Public License in all respects for all of the code used
23 * other than the above mentioned libraries. If you modify this file, you may extend this
24 * exception to your version of the file, but you are not obligated to do so.
25 * If you do not wish to do so, delete this exception statement from your version.
29 import java.util.List;
31 import mir.storage.Database;
32 import mir.storage.DatabaseFailure;
33 import mir.storage.DatabaseExc;
34 import mir.util.RewindableIterator;
35 import multex.Failure;
37 public class EntityBrowser implements RewindableIterator {
39 private Database database;
40 private String mainTablePrefix;
41 private List extraTables;
42 private String whereClause;
43 private String orderByClause;
44 private int batchSize;
45 private EntityList currentBatch;
50 private int batchPosition;
51 private int positionInBatch;
53 public EntityBrowser(Database aDatabase, String aMainTablePrefix, List someExtraTables,
54 String aWhereClause, String anOrderByClause,
55 int aBatchSize, int aLimit, int aSkip) throws DatabaseFailure {
58 mainTablePrefix=aMainTablePrefix;
59 extraTables=someExtraTables;
60 whereClause=aWhereClause;
61 orderByClause=anOrderByClause;
69 public EntityBrowser(Database aDatabase, String aWhereClause, String anOrderByClause,
70 int aBatchSize, int aLimit, int aSkip) throws DatabaseFailure {
71 this(aDatabase, "", null, aWhereClause, anOrderByClause, aBatchSize, aLimit, aSkip);
74 public EntityBrowser(Database aDatabase,
75 String aWhereClause, String anOrderByClause,
76 int aBatchSize) throws DatabaseFailure {
77 this(aDatabase, aWhereClause, anOrderByClause, aBatchSize, -1, 0);
80 public void readCurrentBatch(int aSkip) throws DatabaseExc, DatabaseFailure {
81 currentBatch = database.selectByWhereClause(mainTablePrefix, extraTables,
82 whereClause, orderByClause, aSkip, batchSize);
83 batchPosition = aSkip;
87 public void rewind() {
89 readCurrentBatch(skip);
92 throw new Failure("Error while rewinding", t);
96 public boolean hasNext() {
98 if (limit>-1 && batchPosition+positionInBatch>=skip+limit)
101 if (positionInBatch>=currentBatch.size() && currentBatch.hasNextBatch()) {
102 readCurrentBatch(batchPosition+positionInBatch);
105 return (positionInBatch<currentBatch.size());
107 catch (Throwable t) {
108 throw new RuntimeException(t.getMessage());
112 public Object next() {
115 Entity result = currentBatch.elementAt(positionInBatch);
116 positionInBatch=positionInBatch+1;
122 catch (Throwable t) {
123 throw new RuntimeException(t.getMessage());
127 public void remove() {
128 throw new UnsupportedOperationException();