KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > expressionbuilders > BasicExpressionBuilder


1 /* BasicExpressionBuilder.java */
2
3 package org.enhydra.shark.expressionbuilders;
4
5 import java.io.Serializable JavaDoc;
6 import java.util.*;
7
8 import org.enhydra.shark.api.common.ExpressionBuilder;
9
10 /**
11  * BasicExpressionBuilder implements core expression builder
12  * functionality.
13  *
14  * @author V.Puskas
15  * @version 0.21
16  */

17 public abstract class BasicExpressionBuilder implements Serializable JavaDoc {
18    protected List bshExpression;
19
20    protected List sqlExpression;
21
22    protected List propertiesUsed;
23
24    private String JavaDoc orderBy;
25
26    protected boolean sqlComplete;
27
28    protected int operator;
29
30    protected char delimiter = '\'';
31
32    protected boolean usingStandardVariableModel;
33
34    protected String JavaDoc objectid_column_name = " oid ";
35
36    public static final String JavaDoc PARAM_NAME_STRING_DELIMITER = "ExpressionBuilder.StringDelimiter";
37
38    public static final String JavaDoc PARAM_NAME_OBJECTID_COLUMN_NAME = "DatabaseManager.ObjectIdColumnName";
39
40    public static final String JavaDoc PARAM_NAME_VARIABLE_MODEL = "DODSEventAuditManager.useStandardVariableDataModel";
41
42    /**
43     * There are several parameters needed for expression building to
44     * work properly. Constructor parameter may contain:
45     * <tt>ExpressionBuilder.StringDelimiter</tt> and
46     * <tt>DatabaseManager.ObjectIdColumnName</tt>.
47     *
48     * @param p property object: probably
49     * <code>Shark.getInstance().getProperties()</code>, but
50     * we're nop picky :-) it could even be null in which case
51     * we'll use hardcoded defaults for configurable parameters
52     */

53    protected BasicExpressionBuilder(Properties p) {
54       this.bshExpression = new ArrayList();
55       this.sqlExpression = new ArrayList();
56       this.propertiesUsed = new ArrayList();
57       this.sqlComplete = true;
58       if (null != p) {
59          this.objectid_column_name = " "
60                                      + p.getProperty(PARAM_NAME_OBJECTID_COLUMN_NAME,
61                                                      "oid") + " ";
62          this.usingStandardVariableModel = Boolean.valueOf(p.getProperty(PARAM_NAME_VARIABLE_MODEL,
63                                                                          "true"))
64             .booleanValue();
65          this.delimiter = p.getProperty(PARAM_NAME_STRING_DELIMITER, "'")
66             .charAt(0);
67       }
68    }
69
70    /**
71     * Some database vendors are peculiar about string(CHAR) delimiters,
72     * thus need for its indirect usage.
73     *
74     * @return currently configured delimiter
75     */

76    protected char getDelimiter() {
77       return delimiter;
78    }
79
80    /**
81     * Every public method on expression builder class will end-up
82     * updating both native script and sql expression.
83     *
84     * @param sqlToo if method is certain that
85     * @return either blank ' ' or exclamation mark '!' chararter to
86     * insert in front of the next clause
87     */

88    protected char appendOperator(boolean sqlToo) {
89       char ret = ' ';
90       if (ExpressionBuilder.AND_OPERATOR == (ExpressionBuilder.AND_OPERATOR & this.operator)) {
91          this.bshExpression.add(" && ");
92          if (sqlToo) this.sqlExpression.add(" AND ");
93       } else if (ExpressionBuilder.OR_OPERATOR == (ExpressionBuilder.OR_OPERATOR & this.operator)) {
94          this.bshExpression.add("||");
95          if (sqlToo) this.sqlExpression.add(" OR ");
96       }
97       if (ExpressionBuilder.NOT_OPERATOR == (ExpressionBuilder.NOT_OPERATOR & this.operator)) {
98          ret = '!';
99       }
100       this.operator = 0;
101       return ret;
102    }
103
104    protected void addEquals(String JavaDoc java_name, String JavaDoc sql_name, String JavaDoc value) {
105       char _notPrecedes = appendOperator(true);
106       this.bshExpression.add(_notPrecedes
107                              + java_name + ".equals(\"" + value + "\")");
108       this.sqlExpression.add(sql_name
109                              + _notPrecedes + "= " + getDelimiter() + value
110                              + getDelimiter() + " ");
111       if (!this.propertiesUsed.contains(java_name)) {
112          this.propertiesUsed.add(java_name);
113       }
114    }
115
116    protected void addEquals(String JavaDoc java_name, String JavaDoc sql_name, long value) {
117       char _notPrecedes = appendOperator(true);
118       this.bshExpression.add(java_name
119                              + ".longValue()" + _notPrecedes + "== " + value);
120       this.sqlExpression.add(sql_name + _notPrecedes + "= " + value + " ");
121       if (!this.propertiesUsed.contains(java_name)) {
122          this.propertiesUsed.add(java_name);
123       }
124    }
125
126    protected void addGreaterThan(String JavaDoc java_name, String JavaDoc sql_name, long value) {
127       char _notPrecedes = appendOperator(true);
128       String JavaDoc operator = ' ' == _notPrecedes ? " > " : " <= ";
129       this.bshExpression.add(java_name + ".longValue()" + operator + value);
130       this.sqlExpression.add(sql_name + operator + value + " ");
131       if (!this.propertiesUsed.contains(java_name)) {
132          this.propertiesUsed.add(java_name);
133       }
134    }
135
136    protected void addLessThan(String JavaDoc java_name, String JavaDoc sql_name, long value) {
137       char _notPrecedes = appendOperator(true);
138       String JavaDoc operator = ' ' == _notPrecedes ? " < " : " >= ";
139       this.bshExpression.add(java_name + ".longValue()" + operator + value);
140       this.sqlExpression.add(sql_name + operator + value + " ");
141       if (!this.propertiesUsed.contains(java_name)) {
142          this.propertiesUsed.add(java_name);
143       }
144    }
145
146    protected void addGreaterThanWithSubQuery(String JavaDoc javaName,
147                                              String JavaDoc sqlName,
148                                              String JavaDoc sqlInPart,
149                                              long value,
150                                              String JavaDoc sqlEndPart) {
151       char _notPrecedes = appendOperator(true);
152       String JavaDoc operator = ' ' == _notPrecedes ? " > " : " <= ";
153       this.bshExpression.add(javaName + ".longValue()" + operator + value);
154       this.sqlExpression.add(sqlName
155                              + (' ' == _notPrecedes ? "" : "NOT ")
156                              + sqlInPart + operator + value + sqlEndPart);
157       if (!this.propertiesUsed.contains(javaName)) {
158          this.propertiesUsed.add(javaName);
159       }
160    }
161
162    protected void addLessThanWithSubQuery(String JavaDoc javaName,
163                                           String JavaDoc sqlName,
164                                           String JavaDoc sqlInPart,
165                                           long value,
166                                           String JavaDoc sqlEndPart) {
167       char _notPrecedes = appendOperator(true);
168       String JavaDoc operator = ' ' == _notPrecedes ? " < " : " >= ";
169       this.bshExpression.add(javaName + ".longValue()" + operator + value);
170       this.sqlExpression.add(sqlName
171                              + (' ' == _notPrecedes ? "" : "NOT ")
172                              + sqlInPart + operator + value + sqlEndPart);
173       if (!this.propertiesUsed.contains(javaName)) {
174          this.propertiesUsed.add(javaName);
175       }
176    }
177
178    protected void addEqualsWithSubQuery(String JavaDoc javaName,
179                                         String JavaDoc sqlName,
180                                         String JavaDoc sqlInPart,
181                                         long value,
182                                         String JavaDoc sqlEndPart) {
183       char _notPrecedes = appendOperator(true);
184       this.bshExpression.add(_notPrecedes
185                              + javaName + ".longValue() == " + value);
186       this.sqlExpression.add(sqlName
187                              + (' ' == _notPrecedes ? "" : "NOT ")
188                              + sqlInPart + value + sqlEndPart);
189       if (!this.propertiesUsed.contains(javaName)) {
190          this.propertiesUsed.add(javaName);
191       }
192    }
193
194    protected void addGreaterThanWithSubQuery(String JavaDoc javaName,
195                                              String JavaDoc sqlName,
196                                              String JavaDoc sqlInPart,
197                                              double value,
198                                              String JavaDoc sqlEndPart) {
199       char _notPrecedes = appendOperator(true);
200       String JavaDoc operator = ' ' == _notPrecedes ? " > " : " <= ";
201       this.bshExpression.add(javaName + ".doubleValue()" + operator + value);
202       this.sqlExpression.add(sqlName
203                              + (' ' == _notPrecedes ? "" : "NOT ")
204                              + sqlInPart + operator + value + sqlEndPart);
205       if (!this.propertiesUsed.contains(javaName)) {
206          this.propertiesUsed.add(javaName);
207       }
208    }
209
210    protected void addLessThanWithSubQuery(String JavaDoc javaName,
211                                           String JavaDoc sqlName,
212                                           String JavaDoc sqlInPart,
213                                           double value,
214                                           String JavaDoc sqlEndPart) {
215       char _notPrecedes = appendOperator(true);
216       String JavaDoc operator = ' ' == _notPrecedes ? " < " : " >= ";
217       this.bshExpression.add(javaName + ".doubleValue()" + operator + value);
218       this.sqlExpression.add(sqlName
219                              + (' ' == _notPrecedes ? "" : "NOT ")
220                              + sqlInPart + operator + value + sqlEndPart);
221       if (!this.propertiesUsed.contains(javaName)) {
222          this.propertiesUsed.add(javaName);
223       }
224    }
225
226    protected void addEqualsWithSubQuery(String JavaDoc javaName,
227                                         String JavaDoc sqlName,
228                                         String JavaDoc sqlInPart,
229                                         double value,
230                                         String JavaDoc sqlEndPart) {
231       char _notPrecedes = appendOperator(true);
232       this.bshExpression.add(_notPrecedes
233                              + javaName + ".doubleValue() == " + value);
234       this.sqlExpression.add(sqlName
235                              + (' ' == _notPrecedes ? "" : "NOT ")
236                              + sqlInPart + value + sqlEndPart);
237       if (!this.propertiesUsed.contains(javaName)) {
238          this.propertiesUsed.add(javaName);
239       }
240    }
241
242    protected void addEqualsWithSubQuery(String JavaDoc javaName,
243                                         String JavaDoc sqlName,
244                                         String JavaDoc sqlInPart,
245                                         String JavaDoc value,
246                                         String JavaDoc sqlEndPart) {
247       char _notPrecedes = appendOperator(true);
248       this.bshExpression.add(_notPrecedes
249                              + javaName + ".equals(\"" + value + "\")");
250       this.sqlExpression.add(sqlName
251                              + (' ' == _notPrecedes ? "" : "NOT ")
252                              + sqlInPart + getDelimiter() + value
253                              + getDelimiter() + sqlEndPart);
254       if (!this.propertiesUsed.contains(javaName)) {
255          this.propertiesUsed.add(javaName);
256       }
257    }
258
259    protected void addEqualsWithSubQueryTwice(String JavaDoc javaName,
260                                              String JavaDoc sqlName,
261                                              String JavaDoc sqlInPart,
262                                              String JavaDoc value,
263                                              String JavaDoc sqlMiddlePart,
264                                              String JavaDoc sqlEndPart) {
265       char _notPrecedes = appendOperator(true);
266       this.bshExpression.add(_notPrecedes
267                              + javaName + ".equals(\"" + value + "\")");
268       this.sqlExpression.add(sqlName
269                              + (' ' == _notPrecedes ? "" : "NOT ")
270                              + sqlInPart + getDelimiter() + value
271                              + getDelimiter() + sqlMiddlePart
272                              + getDelimiter() + value + getDelimiter()
273                              + sqlEndPart);
274       if (!this.propertiesUsed.contains(javaName)) {
275          this.propertiesUsed.add(javaName);
276       }
277    }
278
279    protected void addStartsWithSubQuery(String JavaDoc javaName,
280                                         String JavaDoc sqlName,
281                                         String JavaDoc sqlInPart,
282                                         String JavaDoc value,
283                                         String JavaDoc sqlEndPart) {
284       char _notPrecedes = appendOperator(true);
285       this.bshExpression.add(_notPrecedes
286                              + javaName + ".startsWith(\"" + value + "\")");
287       this.sqlExpression.add(sqlName
288                              + (' ' == _notPrecedes ? "" : "NOT ")
289                              + sqlInPart + getDelimiter() + value + "%"
290                              + getDelimiter() + sqlEndPart);
291       if (!this.propertiesUsed.contains(javaName)) {
292          this.propertiesUsed.add(javaName);
293       }
294    }
295
296    protected void addContainsWithSubQuery(String JavaDoc javaName,
297                                           String JavaDoc sqlName,
298                                           String JavaDoc sqlInPart,
299                                           String JavaDoc value,
300                                           String JavaDoc sqlEndPart) {
301       char _notPrecedes = appendOperator(true);
302       this.bshExpression.add(_notPrecedes
303                              + javaName + ".indexOf(\"" + value + "\") != -1");
304       this.sqlExpression.add(sqlName
305                              + (' ' == _notPrecedes ? "" : "NOT ")
306                              + sqlInPart + getDelimiter() + "%" + value + "%"
307                              + getDelimiter() + sqlEndPart);
308       if (!this.propertiesUsed.contains(javaName)) {
309          this.propertiesUsed.add(javaName);
310       }
311    }
312
313    protected void addContains(String JavaDoc javaName, String JavaDoc sqlName, String JavaDoc value) {
314       char _notPrecedes = appendOperator(true);
315       this.bshExpression.add(_notPrecedes
316                              + javaName + ".indexOf(\"" + value + "\") != -1");
317       this.sqlExpression.add(sqlName
318                              + (' ' == _notPrecedes ? "" : "NOT ") + " LIKE "
319                              + getDelimiter() + "%" + value + "%"
320                              + getDelimiter());
321       if (!this.propertiesUsed.contains(javaName)) {
322          this.propertiesUsed.add(javaName);
323       }
324    }
325
326    protected String JavaDoc parseSQLArguments() {
327       StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
328       for (Iterator i = this.sqlExpression.iterator(); i.hasNext();) {
329          Object JavaDoc o = i.next();
330          if (o instanceof ExpressionBuilder) {
331             ExpressionBuilder eb = (ExpressionBuilder) o;
332             sql.append("( " + eb.toSQL() + " )");
333          } else if (o instanceof String JavaDoc) {
334             sql.append(o);
335          }
336       }
337       if (null != orderBy) sql.append(orderBy);
338       return sql.toString();
339    }
340
341    protected String JavaDoc parseBshParameters() {
342       StringBuffer JavaDoc bsh = new StringBuffer JavaDoc();
343       for (Iterator i = this.bshExpression.iterator(); i.hasNext();) {
344          Object JavaDoc o = i.next();
345          if (o instanceof ExpressionBuilder) {
346             ExpressionBuilder eb = (ExpressionBuilder) o;
347             bsh.append("( " + eb.toScript() + " )");
348          } else if (o instanceof String JavaDoc) {
349             bsh.append(o);
350          }
351       }
352       return bsh.toString();
353    }
354
355    public boolean isComplete() {
356       return sqlComplete;
357    }
358
359    protected String JavaDoc whatsUsed() {
360       StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
361       for (Iterator iter = this.propertiesUsed.iterator(); iter.hasNext();) {
362          ret.append(iter.next()).append(" ");
363       }
364       return ret.toString();
365    }
366
367    public String JavaDoc toSQL() {
368       return parseSQLArguments();
369    }
370
371    public String JavaDoc toScript() {
372       return parseBshParameters();
373    }
374
375    public String JavaDoc toExpression() {
376       StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
377       if (!this.isComplete()) {
378          ret.append("/*FORCE*/\n");
379       }
380       if (0 < this.propertiesUsed.size()) {
381          ret.append("/*used ").append(whatsUsed()).append(" used*/\n");
382       }
383       ret.append(toScript());
384       if (0 < this.sqlExpression.size()) {
385          ret.append("\n/*sql ").append(toSQL()).append(" sql*/");
386       }
387       return ret.toString();
388    }
389
390    public void setOrderBy(String JavaDoc column, boolean isAscending) {
391       orderBy = " ORDER BY " + column + (isAscending ? " ASC" : " DESC");
392    }
393 }
Popular Tags