KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > BaseIteratorWrapper


1 package org.enhydra.shark;
2
3 import java.util.*;
4 import java.util.ListIterator JavaDoc;
5 import java.util.Map JavaDoc;
6 import org.enhydra.shark.api.*;
7 import org.enhydra.shark.api.client.wfbase.BaseException;
8 import org.enhydra.shark.api.client.wfbase.BaseIterator;
9 import org.enhydra.shark.api.client.wfbase.GrammarNotSupported;
10 import org.enhydra.shark.api.client.wfbase.InvalidQuery;
11 import org.enhydra.shark.api.client.wfbase.NameMismatch;
12 import org.enhydra.shark.api.common.SharkConstants;
13
14 /**
15  * Base class for all iterators.
16  *
17  * @author Sasa Bojanic
18  */

19 public abstract class BaseIteratorWrapper implements BaseIterator {
20
21    protected String JavaDoc userAuth;
22    protected List objectList;
23    protected ListIterator JavaDoc objectListIterator;
24
25    protected String JavaDoc queryExpression="";
26    protected Map JavaDoc namesInExpression;
27    protected String JavaDoc queryGrammar=SharkConstants.GRAMMAR_JAVA;
28    protected boolean eval;
29    protected String JavaDoc sqlWhere;
30    protected String JavaDoc usedContext;
31    protected boolean ignoreUsedContext;
32
33    protected BaseIteratorWrapper (String JavaDoc userAuth) throws BaseException {
34       this.userAuth=userAuth;
35    }
36
37    public String JavaDoc query_expression () throws BaseException {
38       return queryExpression;
39    }
40
41    public String JavaDoc query_expression (SharkTransaction t) throws BaseException {
42       return queryExpression;
43    }
44
45    public void set_query_expression(String JavaDoc query) throws BaseException,
46                                                  InvalidQuery {
47       set_query_expression(null, query);
48    }
49
50    public void set_query_expression (SharkTransaction t,String JavaDoc query) throws BaseException, InvalidQuery {
51       queryExpression = query;
52       if (queryExpression==null) queryExpression="";
53       clearObjectList();
54       eval = queryExpression.trim().length() > 0;
55       sqlWhere = null;
56       usedContext = "";
57       ignoreUsedContext = true;
58
59       if (eval) {
60          int sqlBeginIndex = queryExpression.indexOf("/*sql ") + 6;
61          int sqlEndIndex = queryExpression.indexOf(" sql*/");
62          if (5 < sqlBeginIndex) {
63             sqlWhere = queryExpression.substring(sqlBeginIndex, sqlEndIndex);
64             if (0 > queryExpression.indexOf("/*FORCE*/")) {
65                eval = false;
66             }
67          }
68          int usedBeginIndex = queryExpression.indexOf("/*used ");
69          if (usedBeginIndex==-1) return;
70          usedBeginIndex += 7;
71          int usedEndIndex = queryExpression.indexOf(" used*/");
72          if (6 < usedBeginIndex) {
73             usedContext = queryExpression.substring(usedBeginIndex,
74                                                     usedEndIndex);
75             ignoreUsedContext = false;
76          }
77       }
78    }
79
80    public Map JavaDoc names_in_expression () throws BaseException {
81       return namesInExpression;
82    }
83
84    public Map JavaDoc names_in_expression (SharkTransaction t) throws BaseException {
85       return namesInExpression;
86    }
87
88    public void set_names_in_expression (Map JavaDoc query) throws BaseException, NameMismatch {
89       namesInExpression=query;
90    }
91
92    public void set_names_in_expression (SharkTransaction t,Map JavaDoc query) throws BaseException, NameMismatch {
93       namesInExpression=query;
94    }
95
96
97    public String JavaDoc query_grammar () throws BaseException {
98       return queryGrammar;
99    }
100
101    public String JavaDoc query_grammar (SharkTransaction t) throws BaseException {
102       return queryGrammar;
103    }
104
105    public void set_query_grammar (String JavaDoc query_grammar) throws BaseException, GrammarNotSupported {
106       if (query_grammar!=null && (query_grammar.equals(SharkConstants.GRAMMAR_JAVA) ||
107                                      query_grammar.equals(SharkConstants.GRAMMAR_JAVA_SCRIPT) ||
108                                      query_grammar.equals(SharkConstants.GRAMMAR_PYTHON_SCRIPT))) {
109          this.queryGrammar=query_grammar;
110          clearObjectList();
111       } else {
112          String JavaDoc errMsg="Unsupported grammar "+query_grammar+". Supported grammars are:"+
113             SharkConstants.GRAMMAR_JAVA+", "+
114             SharkConstants.GRAMMAR_JAVA_SCRIPT+" and "+
115             SharkConstants.GRAMMAR_PYTHON_SCRIPT+".";
116          throw new GrammarNotSupported(errMsg);
117       }
118    }
119
120    public void set_query_grammar (SharkTransaction t,String JavaDoc query_grammar) throws BaseException, GrammarNotSupported {
121       if (query_grammar!=null && (query_grammar.equals(SharkConstants.GRAMMAR_JAVA) ||
122                                      query_grammar.equals(SharkConstants.GRAMMAR_JAVA_SCRIPT) ||
123                                      query_grammar.equals(SharkConstants.GRAMMAR_PYTHON_SCRIPT))) {
124          this.queryGrammar=query_grammar;
125          clearObjectList();
126       } else {
127          String JavaDoc errMsg="Unsupported grammar "+query_grammar+". Supported grammars are:"+
128             SharkConstants.GRAMMAR_JAVA+", "+
129             SharkConstants.GRAMMAR_JAVA_SCRIPT+" and "+
130             SharkConstants.GRAMMAR_PYTHON_SCRIPT+".";
131          throw new GrammarNotSupported(errMsg);
132       }
133    }
134
135    public int how_many () throws BaseException {
136       int ret=-1;
137       SharkTransaction t = null;
138       try {
139          t = SharkUtilities.createTransaction();
140          ret = how_many(t);
141          //SharkUtilities.commitTransaction(t);
142
} catch (RootException e) {
143          //SharkUtilities.rollbackTransaction(t);
144
SharkUtilities.emptyCaches(t);
145          if (e instanceof BaseException)
146             throw (BaseException)e;
147          else
148             throw new BaseException(e);
149       } finally {
150          SharkUtilities.releaseTransaction(t);
151       }
152       return ret;
153    }
154
155    public int how_many (SharkTransaction t) throws BaseException {
156       fillObjectList(t);
157       return objectList.size();
158    }
159
160    public void goto_start () throws BaseException {
161       SharkTransaction t = null;
162       try {
163          t = SharkUtilities.createTransaction();
164          goto_start(t);
165          //SharkUtilities.commitTransaction(t);
166
} catch (RootException e) {
167          //SharkUtilities.rollbackTransaction(t);
168
SharkUtilities.emptyCaches(t);
169          if (e instanceof BaseException)
170             throw (BaseException)e;
171          else
172             throw new BaseException(e);
173       } finally {
174          SharkUtilities.releaseTransaction(t);
175       }
176    }
177
178    public void goto_start (SharkTransaction t) throws BaseException {
179       fillObjectList(t);
180       while (objectListIterator.hasPrevious()) {
181          objectListIterator.previous();
182       }
183    }
184
185    public void goto_end () throws BaseException {
186       SharkTransaction t = null;
187       try {
188          t = SharkUtilities.createTransaction();
189          goto_end(t);
190          //SharkUtilities.commitTransaction(t);
191
} catch (RootException e) {
192          //SharkUtilities.rollbackTransaction(t);
193
SharkUtilities.emptyCaches(t);
194          if (e instanceof BaseException)
195             throw (BaseException)e;
196          else
197             throw new BaseException(e);
198       } finally {
199          SharkUtilities.releaseTransaction(t);
200       }
201    }
202
203    public void goto_end (SharkTransaction t) throws BaseException {
204       fillObjectList(t);
205       while (objectListIterator.hasNext()) {
206          objectListIterator.next();
207       }
208    }
209
210    protected void setObjectList (List lst) {
211       this.objectList=lst;
212       this.objectListIterator=objectList.listIterator();
213    }
214
215    protected void clearObjectList () {
216       this.objectList=null;
217       this.objectListIterator=null;
218    }
219
220    protected abstract void fillObjectList (SharkTransaction t) throws BaseException;
221
222    protected Object JavaDoc getNextObject (SharkTransaction t) throws BaseException {
223       fillObjectList(t);
224       Object JavaDoc ret=null;
225       if (objectListIterator.hasNext()) {
226          ret=objectListIterator.next();
227       }
228       if (ret==null) throw new BaseException("There is no next object!");
229       return ret;
230    }
231
232    protected Object JavaDoc getPreviousObject (SharkTransaction t) throws BaseException {
233       fillObjectList(t);
234       Object JavaDoc ret=null;
235       if (objectListIterator.hasPrevious()) {
236          ret=objectListIterator.previous();
237       }
238       if (ret==null) throw new BaseException("No previous object!");
239       return ret;
240    }
241
242    protected List getNextNSequence (SharkTransaction t,int max_number) throws BaseException {
243       fillObjectList(t);
244       List ret=new ArrayList();
245       if (max_number<=0) {
246          max_number=Integer.MAX_VALUE;
247       }
248       int i=0;
249       while (objectListIterator.hasNext() && i<max_number) {
250          Object JavaDoc next=objectListIterator.next();
251          ret.add(next);
252          i++;
253       }
254
255       return ret;
256    }
257
258    protected List getPreviousNSequence (SharkTransaction t,int max_number) throws BaseException {
259       fillObjectList(t);
260       List ret=new ArrayList();
261       if (max_number<=0) {
262          max_number=Integer.MAX_VALUE;
263       }
264       int i=0;
265       while (objectListIterator.hasPrevious() && i<max_number) {
266          Object JavaDoc prev=objectListIterator.previous();
267          ret.add(prev);
268          i++;
269       }
270
271       return ret;
272    }
273
274 }
275
Popular Tags