scripts/mir-setup/README: update with link to new doc on wiki
[mir.git] / source / mir / entity / EntityBrowser.java
1 /*
2  * Copyright (C) 2005 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
26  */
27 package mir.entity;
28
29 import java.util.List;
30
31 import mir.storage.Database;
32 import mir.storage.DatabaseFailure;
33 import mir.storage.DatabaseExc;
34 import mir.util.RewindableIterator;
35 import multex.Failure;
36
37 public class EntityBrowser implements RewindableIterator {
38
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;
46
47   private int skip;
48   private int limit;
49
50   private int batchPosition;
51   private int positionInBatch;
52
53   public EntityBrowser(Database aDatabase, String aMainTablePrefix, List someExtraTables,
54      String aWhereClause, String anOrderByClause,
55      int aBatchSize, int aLimit, int aSkip) throws DatabaseFailure {
56
57     database=aDatabase;
58     mainTablePrefix=aMainTablePrefix;
59     extraTables=someExtraTables;
60     whereClause=aWhereClause;
61     orderByClause=anOrderByClause;
62     batchSize=aBatchSize;
63     skip=aSkip;
64     limit=aLimit;
65
66     rewind();
67   }
68
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);
72   }
73
74   public EntityBrowser(Database aDatabase,
75           String aWhereClause, String anOrderByClause,
76           int aBatchSize) throws DatabaseFailure {
77     this(aDatabase, aWhereClause, anOrderByClause, aBatchSize, -1, 0);
78   }
79
80   public void readCurrentBatch(int aSkip) throws DatabaseExc, DatabaseFailure {
81     currentBatch = database.selectByWhereClause(mainTablePrefix, extraTables,
82         whereClause, orderByClause, aSkip, batchSize);
83     batchPosition = aSkip;
84     positionInBatch = 0;
85   }
86
87   public void rewind() {
88     try {
89       readCurrentBatch(skip);
90     }
91     catch (Throwable t) {
92       throw new Failure("Error while rewinding", t);
93     }
94   }
95
96   public boolean hasNext() {
97     try {
98       if (limit>-1 && batchPosition+positionInBatch>=skip+limit)
99         return false;
100
101       if (positionInBatch>=currentBatch.size() && currentBatch.hasNextBatch()) {
102         readCurrentBatch(batchPosition+positionInBatch);
103       }
104
105       return (positionInBatch<currentBatch.size());
106     }
107     catch (Throwable t) {
108       throw new RuntimeException(t.getMessage());
109     }
110   }
111
112   public Object next() {
113     try {
114       if (hasNext()) {
115         Entity result = currentBatch.elementAt(positionInBatch);
116         positionInBatch=positionInBatch+1;
117
118         return result;
119       }
120                         return null;
121     }
122     catch (Throwable t) {
123       throw new RuntimeException(t.getMessage());
124     }
125   }
126
127   public void remove() {
128     throw new UnsupportedOperationException();
129   }
130 }